qtmobility/examples/samplephonebook/filterpage.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    44 #include <QtGui>
    44 #include <QtGui>
    45 
    45 
    46 FilterPage::FilterPage(QWidget* parent)
    46 FilterPage::FilterPage(QWidget* parent)
    47         : QWidget(parent)
    47         : QWidget(parent)
    48 {
    48 {
    49     m_valueCriteriaEdit = new QLineEdit(this);
    49     m_nameEdit = new QLineEdit(this);
    50     m_fieldCriteriaCombo = new QComboBox(this);
    50     m_phoneEdit = new QLineEdit(this);
    51     m_criteriaTypeCombo = new QComboBox(this);
    51     m_emailEdit = new QLineEdit(this);
    52     m_joinMethodCombo = new QComboBox(this);
    52     m_addressEdit = new QLineEdit(this);
    53     m_cumulativeExpressionLabel = new QLabel("Match All Contacts", this);
    53     QLabel *nameLabel = new QLabel(tr("Name"), this);
    54     m_cumulativeExpressionLabel->setWordWrap(true);
    54     QLabel *phoneLabel = new QLabel(tr("Phone"), this);
    55     m_cumulativeExpressionLabel->setFocusPolicy(Qt::StrongFocus);
    55     QLabel *emailLabel = new QLabel(tr("Email"), this);
    56     
    56     QLabel *addressLabel = new QLabel(tr("Address"), this);
    57 #ifdef Q_OS_SYMBIAN
    57 
    58     // In symbian use softkeys instead of normal buttons
       
    59     m_addBtn = new QAction("Add", this);
       
    60     m_addBtn->setSoftKeyRole(QAction::PositiveSoftKey);
       
    61     addAction(m_addBtn);
       
    62     connect(m_addBtn, SIGNAL(triggered(bool)), this, SLOT(addClicked()));    
       
    63     m_filterBtn = new QAction("Filter", this);
       
    64     m_filterBtn->setSoftKeyRole(QAction::NegativeSoftKey);
       
    65     addAction(m_filterBtn);
       
    66     connect(m_filterBtn, SIGNAL(triggered(bool)), this, SLOT(filterClicked()));
       
    67 #else
       
    68     m_addBtn = new QPushButton("Add", this);
       
    69     connect(m_addBtn, SIGNAL(clicked()), this, SLOT(addClicked()));
       
    70     m_filterBtn = new QPushButton("Filter", this);
       
    71     connect(m_filterBtn, SIGNAL(clicked()), this, SLOT(filterClicked()));
       
    72 #endif
       
    73     m_clearBtn = new QPushButton("Clear", this);
       
    74     connect(m_clearBtn, SIGNAL(clicked()), this, SLOT(clearClicked()));
       
    75 
       
    76     QStringList filterableFields;
       
    77     filterableFields.append("Name");
       
    78     filterableFields.append("Phone Number");
       
    79     filterableFields.append("Email");
       
    80     m_fieldCriteriaCombo->addItems(filterableFields);
       
    81 
       
    82     m_criteriaTypeCombo->addItem("Equals", QContactFilter::MatchExactly);
       
    83     m_criteriaTypeCombo->addItem("Contains", QContactFilter::MatchContains);
       
    84     m_criteriaTypeCombo->addItem("Starts with", QContactFilter::MatchStartsWith);
       
    85     m_criteriaTypeCombo->addItem("Ends with", QContactFilter::MatchEndsWith);
       
    86 
       
    87     QStringList joinTypes;
       
    88     joinTypes.append("AND");
       
    89     joinTypes.append("OR");
       
    90     m_joinMethodCombo->addItems(joinTypes);
       
    91     
       
    92     QFormLayout *formLayout = new QFormLayout;
    58     QFormLayout *formLayout = new QFormLayout;
    93     formLayout->addRow(new QLabel("Search String:", this));
    59     if (QApplication::desktop()->availableGeometry().width() < 360) {
    94     formLayout->addRow(m_valueCriteriaEdit);
    60         // Narrow screen: put label on separate line to textbox
    95     formLayout->addRow(new QLabel("Search Field:", this));
    61         formLayout->addRow(nameLabel);
    96     formLayout->addRow(m_fieldCriteriaCombo);
    62         formLayout->addRow(m_nameEdit);
    97     formLayout->addRow(new QLabel("Criteria Type:", this));
    63         formLayout->addRow(phoneLabel);
    98     formLayout->addRow(m_criteriaTypeCombo);
    64         formLayout->addRow(m_phoneEdit);
    99     formLayout->addRow(new QLabel("Join Method:", this));
    65         formLayout->addRow(emailLabel);
   100     formLayout->addRow(m_joinMethodCombo);
    66         formLayout->addRow(m_emailEdit);
   101     QFrame* separatorFrame = new QFrame(this);
    67         formLayout->addRow(addressLabel);
   102     separatorFrame->setFrameShape(QFrame::HLine);
    68         formLayout->addRow(m_addressEdit);
   103     separatorFrame->setFrameShadow(QFrame::Plain);
    69     } else {
   104     separatorFrame->setLineWidth(2);
    70         // Wide screen: put label on same line as textbox
   105     formLayout->addRow(separatorFrame);
    71         formLayout->addRow(nameLabel, m_nameEdit);
   106     formLayout->addRow(new QLabel("Filter Expression:", this));
    72         formLayout->addRow(phoneLabel, m_phoneEdit);
   107     formLayout->addRow(m_cumulativeExpressionLabel);
    73         formLayout->addRow(emailLabel, m_emailEdit);
   108 #ifdef Q_OS_SYMBIAN
    74         formLayout->addRow(addressLabel, m_addressEdit);
   109     formLayout->addRow(m_clearBtn);
    75     }
   110 #endif    
    76 
   111     
       
   112     QVBoxLayout *pageLayout = new QVBoxLayout;
    77     QVBoxLayout *pageLayout = new QVBoxLayout;
   113     
    78 
   114     QScrollArea *formScrollArea = new QScrollArea(this);
    79     QScrollArea *formScrollArea = new QScrollArea(this);
   115     formScrollArea->setWidgetResizable(true);
    80     formScrollArea->setWidgetResizable(true);
   116     QWidget *formContainer = new QWidget(formScrollArea);
    81     QWidget *formContainer = new QWidget(formScrollArea);
   117     formContainer->setLayout(formLayout);
    82     formContainer->setLayout(formLayout);
   118     formScrollArea->setWidget(formContainer);
    83     formScrollArea->setWidget(formContainer);
   119     pageLayout->addWidget(formScrollArea);
    84     pageLayout->addWidget(formScrollArea);
   120     
    85 
   121 #ifndef Q_OS_SYMBIAN
    86 #ifdef Q_OS_SYMBIAN
       
    87     m_filterBtn = new QAction(tr("Filter"), this);
       
    88     m_filterBtn->setSoftKeyRole(QAction::PositiveSoftKey);
       
    89     addAction(m_filterBtn);
       
    90     connect(m_filterBtn, SIGNAL(triggered(bool)), this, SLOT(filterClicked()));
       
    91     m_cancelBtn = new QAction(tr("Cancel"), this);
       
    92     m_cancelBtn->setSoftKeyRole(QAction::NegativeSoftKey);
       
    93     addAction(m_cancelBtn);
       
    94     connect(m_cancelBtn, SIGNAL(triggered(bool)), this, SLOT(cancelClicked()));
       
    95 #else
       
    96     m_filterBtn = new QPushButton(tr("&Filter"), this);
       
    97     m_filterBtn->setDefault(true);
       
    98     connect(m_filterBtn, SIGNAL(clicked()), this, SLOT(filterClicked()));
       
    99     m_cancelBtn = new QPushButton(tr("&Cancel"), this);
       
   100     connect(m_cancelBtn, SIGNAL(clicked()), this, SLOT(cancelClicked()));
       
   101 
   122     QHBoxLayout *btnLayout = new QHBoxLayout;
   102     QHBoxLayout *btnLayout = new QHBoxLayout;
   123     btnLayout->addWidget(m_addBtn);
       
   124     btnLayout->addWidget(m_clearBtn);
       
   125     btnLayout->addWidget(m_filterBtn);
   103     btnLayout->addWidget(m_filterBtn);
       
   104     btnLayout->addWidget(m_cancelBtn);
   126     pageLayout->addLayout(btnLayout);
   105     pageLayout->addLayout(btnLayout);
   127 #endif
   106 #endif
   128 
   107 
   129     setLayout(pageLayout);
   108     setLayout(pageLayout);
   130 }
   109 }
   131 
   110 
   132 FilterPage::~FilterPage()
   111 FilterPage::~FilterPage()
   133 {
   112 {
   134 }
   113 }
   135 
   114 
   136 void FilterPage::addClicked()
   115 void FilterPage::clearFilter()
   137 {
   116 {
   138     QContactDetailFilter fil;
   117     m_name.clear();
   139     QString defName;
   118     m_phone.clear();
   140     QString fieldName;
   119     m_email.clear();
   141     QString exprName;
   120     m_address.clear();
   142     QString exprMatch;
   121     m_nameEdit->clear();
   143     QString exprJoin;
   122     m_phoneEdit->clear();
   144     switch (m_fieldCriteriaCombo->currentIndex()) {
   123     m_emailEdit->clear();
   145         case 0:
   124     m_addressEdit->clear();
   146         {
   125     m_currentFilter = QContactIntersectionFilter();
   147             // name
   126     emit showListPage(m_currentFilter);
   148             defName = QString(QLatin1String(QContactDisplayLabel::DefinitionName));
   127 }
   149             fieldName = QString(QLatin1String(QContactDisplayLabel::FieldLabel));
   128 
   150 
   129 void FilterPage::filterClicked()
   151             exprName = "Name";
   130 {
       
   131     m_name = m_nameEdit->text();
       
   132     m_phone = m_phoneEdit->text();
       
   133     m_email = m_emailEdit->text();
       
   134     m_address = m_addressEdit->text();
       
   135     // The intersection filter ensures that non-empty field value must be found in the contact.
       
   136     m_currentFilter = QContactIntersectionFilter();
       
   137     if (!m_nameEdit->text().isEmpty()) {
       
   138         // Search all fields of the name by building a union filter
       
   139         QContactUnionFilter nameFilter;
       
   140         QStringList nameFields;
       
   141         nameFields << QContactName::FieldCustomLabel;
       
   142         nameFields << QContactName::FieldFirstName;
       
   143         nameFields << QContactName::FieldLastName;
       
   144         nameFields << QContactName::FieldMiddleName;
       
   145         nameFields << QContactName::FieldPrefix;
       
   146         nameFields << QContactName::FieldSuffix;
       
   147         foreach (const QString& fieldName, nameFields) {
       
   148             QContactDetailFilter subFilter;
       
   149             subFilter.setDetailDefinitionName(QContactName::DefinitionName, fieldName);
       
   150             subFilter.setValue(m_nameEdit->text());
       
   151             subFilter.setMatchFlags(QContactFilter::MatchContains);
       
   152             nameFilter.append(subFilter);
   152         }
   153         }
   153         break;
   154         m_currentFilter.append(nameFilter);
   154 
   155     }
   155         case 1:
   156     if (!m_phoneEdit->text().isEmpty()) {
   156         {
   157         QContactDetailFilter phoneFilter;
   157             // phone number
   158         phoneFilter.setDetailDefinitionName(QContactPhoneNumber::DefinitionName,
   158             defName = QString(QLatin1String(QContactPhoneNumber::DefinitionName));
   159                                             QContactPhoneNumber::FieldNumber);
   159             fieldName = QString(QLatin1String(QContactPhoneNumber::FieldNumber));
   160         phoneFilter.setValue(m_phoneEdit->text());
   160 
   161         phoneFilter.setMatchFlags(QContactFilter::MatchContains);
   161             exprName = "Phone Number";
   162         m_currentFilter.append(phoneFilter);
       
   163     }
       
   164     if (!m_emailEdit->text().isEmpty()) {
       
   165         QContactDetailFilter emailFilter;
       
   166         emailFilter.setDetailDefinitionName(QContactEmailAddress::DefinitionName,
       
   167                                             QContactEmailAddress::FieldEmailAddress);
       
   168         emailFilter.setValue(m_emailEdit->text());
       
   169         emailFilter.setMatchFlags(QContactFilter::MatchContains);
       
   170         m_currentFilter.append(emailFilter);
       
   171     }
       
   172     if (!m_addressEdit->text().isEmpty()) {
       
   173         // Search all fields of the address by building a union filter
       
   174         QContactUnionFilter addressFilter;
       
   175         QStringList addressFields;
       
   176         addressFields << QContactAddress::FieldCountry;
       
   177         addressFields << QContactAddress::FieldLocality;
       
   178         addressFields << QContactAddress::FieldPostcode;
       
   179         addressFields << QContactAddress::FieldPostOfficeBox;
       
   180         addressFields << QContactAddress::FieldRegion;
       
   181         addressFields << QContactAddress::FieldStreet;
       
   182         foreach (const QString& fieldName, addressFields) {
       
   183             QContactDetailFilter subFilter;
       
   184             subFilter.setDetailDefinitionName(QContactAddress::DefinitionName, fieldName);
       
   185             subFilter.setValue(m_addressEdit->text());
       
   186             subFilter.setMatchFlags(QContactFilter::MatchContains);
       
   187             addressFilter.append(subFilter);
   162         }
   188         }
   163         break;
   189         m_currentFilter.append(addressFilter);
   164 
   190     }
   165         default:
   191     emit showListPage(m_currentFilter);
   166         {
   192 }
   167             // email address
   193 
   168             defName = QString(QLatin1String(QContactEmailAddress::DefinitionName));
   194 void FilterPage::cancelClicked()
   169             fieldName = QString(QLatin1String(QContactEmailAddress::FieldEmailAddress));
   195 {
   170 
   196     m_nameEdit->setText(m_name);
   171             exprName = "Email Address";
   197     m_phoneEdit->setText(m_phone);
   172         }
   198     m_emailEdit->setText(m_email);
   173         break;
   199     m_addressEdit->setText(m_address);
   174 
   200     emit showListPage(m_currentFilter);
   175     }
   201 }
   176     fil.setDetailDefinitionName(defName, fieldName);
       
   177     fil.setValue(m_valueCriteriaEdit->text());
       
   178 
       
   179     int flag = m_criteriaTypeCombo->itemData(m_criteriaTypeCombo->currentIndex()).toInt();
       
   180     fil.setMatchFlags(QContactFilter::MatchFlags(flag));
       
   181     exprMatch = m_criteriaTypeCombo->currentText().toLower();
       
   182 
       
   183     // if OR then join with OR
       
   184     if (m_joinMethodCombo->currentIndex() == 1) {
       
   185         QContactUnionFilter ufil;
       
   186         ufil << m_cumulativeFilter << fil;
       
   187         QContactIntersectionFilter ifil;
       
   188         ifil << ufil;
       
   189         m_cumulativeFilter = ifil;
       
   190         exprJoin = "OR";
       
   191     } else {
       
   192         // otherwise, just AND.
       
   193         QContactIntersectionFilter ifil(m_cumulativeFilter);
       
   194         ifil << fil;
       
   195         m_cumulativeFilter = ifil;
       
   196         exprJoin = "AND";
       
   197     }
       
   198 
       
   199     // set the expression so far
       
   200     if (!m_cumulativeExpression.isEmpty())
       
   201         m_cumulativeExpression += " " + exprJoin + " ";
       
   202     m_cumulativeExpression += exprName + " " + exprMatch + " \"" + m_valueCriteriaEdit->text() + "\"";
       
   203 
       
   204     // and clear the UI ready for the next filter expression.
       
   205     m_valueCriteriaEdit->setText("");
       
   206     m_fieldCriteriaCombo->setCurrentIndex(0);
       
   207     m_criteriaTypeCombo->setCurrentIndex(0);
       
   208     m_joinMethodCombo->setCurrentIndex(0);
       
   209     m_cumulativeExpressionLabel->setText(m_cumulativeExpression);
       
   210 }
       
   211 
       
   212 void FilterPage::clearClicked()
       
   213 {
       
   214     m_cumulativeExpression = QString();
       
   215     m_valueCriteriaEdit->setText("");
       
   216     m_fieldCriteriaCombo->setCurrentIndex(0);
       
   217     m_criteriaTypeCombo->setCurrentIndex(0);
       
   218     m_joinMethodCombo->setCurrentIndex(0);
       
   219     m_cumulativeExpressionLabel->setText("Match All Contacts");
       
   220     m_cumulativeFilter = QContactFilter();
       
   221 }
       
   222 
       
   223 void FilterPage::filterClicked()
       
   224 {
       
   225     emit showListPage(m_cumulativeFilter);
       
   226 }