--- a/qtmobility/examples/samplephonebook/filterpage.cpp Fri Apr 16 15:51:22 2010 +0300
+++ b/qtmobility/examples/samplephonebook/filterpage.cpp Mon May 03 13:18:40 2010 +0300
@@ -46,83 +46,62 @@
FilterPage::FilterPage(QWidget* parent)
: QWidget(parent)
{
- m_valueCriteriaEdit = new QLineEdit(this);
- m_fieldCriteriaCombo = new QComboBox(this);
- m_criteriaTypeCombo = new QComboBox(this);
- m_joinMethodCombo = new QComboBox(this);
- m_cumulativeExpressionLabel = new QLabel("Match All Contacts", this);
- m_cumulativeExpressionLabel->setWordWrap(true);
- m_cumulativeExpressionLabel->setFocusPolicy(Qt::StrongFocus);
-
-#ifdef Q_OS_SYMBIAN
- // In symbian use softkeys instead of normal buttons
- m_addBtn = new QAction("Add", this);
- m_addBtn->setSoftKeyRole(QAction::PositiveSoftKey);
- addAction(m_addBtn);
- connect(m_addBtn, SIGNAL(triggered(bool)), this, SLOT(addClicked()));
- m_filterBtn = new QAction("Filter", this);
- m_filterBtn->setSoftKeyRole(QAction::NegativeSoftKey);
- addAction(m_filterBtn);
- connect(m_filterBtn, SIGNAL(triggered(bool)), this, SLOT(filterClicked()));
-#else
- m_addBtn = new QPushButton("Add", this);
- connect(m_addBtn, SIGNAL(clicked()), this, SLOT(addClicked()));
- m_filterBtn = new QPushButton("Filter", this);
- connect(m_filterBtn, SIGNAL(clicked()), this, SLOT(filterClicked()));
-#endif
- m_clearBtn = new QPushButton("Clear", this);
- connect(m_clearBtn, SIGNAL(clicked()), this, SLOT(clearClicked()));
-
- QStringList filterableFields;
- filterableFields.append("Name");
- filterableFields.append("Phone Number");
- filterableFields.append("Email");
- m_fieldCriteriaCombo->addItems(filterableFields);
+ m_nameEdit = new QLineEdit(this);
+ m_phoneEdit = new QLineEdit(this);
+ m_emailEdit = new QLineEdit(this);
+ m_addressEdit = new QLineEdit(this);
+ QLabel *nameLabel = new QLabel(tr("Name"), this);
+ QLabel *phoneLabel = new QLabel(tr("Phone"), this);
+ QLabel *emailLabel = new QLabel(tr("Email"), this);
+ QLabel *addressLabel = new QLabel(tr("Address"), this);
- m_criteriaTypeCombo->addItem("Equals", QContactFilter::MatchExactly);
- m_criteriaTypeCombo->addItem("Contains", QContactFilter::MatchContains);
- m_criteriaTypeCombo->addItem("Starts with", QContactFilter::MatchStartsWith);
- m_criteriaTypeCombo->addItem("Ends with", QContactFilter::MatchEndsWith);
+ QFormLayout *formLayout = new QFormLayout;
+ if (QApplication::desktop()->availableGeometry().width() < 360) {
+ // Narrow screen: put label on separate line to textbox
+ formLayout->addRow(nameLabel);
+ formLayout->addRow(m_nameEdit);
+ formLayout->addRow(phoneLabel);
+ formLayout->addRow(m_phoneEdit);
+ formLayout->addRow(emailLabel);
+ formLayout->addRow(m_emailEdit);
+ formLayout->addRow(addressLabel);
+ formLayout->addRow(m_addressEdit);
+ } else {
+ // Wide screen: put label on same line as textbox
+ formLayout->addRow(nameLabel, m_nameEdit);
+ formLayout->addRow(phoneLabel, m_phoneEdit);
+ formLayout->addRow(emailLabel, m_emailEdit);
+ formLayout->addRow(addressLabel, m_addressEdit);
+ }
- QStringList joinTypes;
- joinTypes.append("AND");
- joinTypes.append("OR");
- m_joinMethodCombo->addItems(joinTypes);
-
- QFormLayout *formLayout = new QFormLayout;
- formLayout->addRow(new QLabel("Search String:", this));
- formLayout->addRow(m_valueCriteriaEdit);
- formLayout->addRow(new QLabel("Search Field:", this));
- formLayout->addRow(m_fieldCriteriaCombo);
- formLayout->addRow(new QLabel("Criteria Type:", this));
- formLayout->addRow(m_criteriaTypeCombo);
- formLayout->addRow(new QLabel("Join Method:", this));
- formLayout->addRow(m_joinMethodCombo);
- QFrame* separatorFrame = new QFrame(this);
- separatorFrame->setFrameShape(QFrame::HLine);
- separatorFrame->setFrameShadow(QFrame::Plain);
- separatorFrame->setLineWidth(2);
- formLayout->addRow(separatorFrame);
- formLayout->addRow(new QLabel("Filter Expression:", this));
- formLayout->addRow(m_cumulativeExpressionLabel);
-#ifdef Q_OS_SYMBIAN
- formLayout->addRow(m_clearBtn);
-#endif
-
QVBoxLayout *pageLayout = new QVBoxLayout;
-
+
QScrollArea *formScrollArea = new QScrollArea(this);
formScrollArea->setWidgetResizable(true);
QWidget *formContainer = new QWidget(formScrollArea);
formContainer->setLayout(formLayout);
formScrollArea->setWidget(formContainer);
pageLayout->addWidget(formScrollArea);
-
-#ifndef Q_OS_SYMBIAN
+
+#ifdef Q_OS_SYMBIAN
+ m_filterBtn = new QAction(tr("Filter"), this);
+ m_filterBtn->setSoftKeyRole(QAction::PositiveSoftKey);
+ addAction(m_filterBtn);
+ connect(m_filterBtn, SIGNAL(triggered(bool)), this, SLOT(filterClicked()));
+ m_cancelBtn = new QAction(tr("Cancel"), this);
+ m_cancelBtn->setSoftKeyRole(QAction::NegativeSoftKey);
+ addAction(m_cancelBtn);
+ connect(m_cancelBtn, SIGNAL(triggered(bool)), this, SLOT(cancelClicked()));
+#else
+ m_filterBtn = new QPushButton(tr("&Filter"), this);
+ m_filterBtn->setDefault(true);
+ connect(m_filterBtn, SIGNAL(clicked()), this, SLOT(filterClicked()));
+ m_cancelBtn = new QPushButton(tr("&Cancel"), this);
+ connect(m_cancelBtn, SIGNAL(clicked()), this, SLOT(cancelClicked()));
+
QHBoxLayout *btnLayout = new QHBoxLayout;
- btnLayout->addWidget(m_addBtn);
- btnLayout->addWidget(m_clearBtn);
btnLayout->addWidget(m_filterBtn);
+ btnLayout->addWidget(m_cancelBtn);
pageLayout->addLayout(btnLayout);
#endif
@@ -133,94 +112,90 @@
{
}
-void FilterPage::addClicked()
+void FilterPage::clearFilter()
{
- QContactDetailFilter fil;
- QString defName;
- QString fieldName;
- QString exprName;
- QString exprMatch;
- QString exprJoin;
- switch (m_fieldCriteriaCombo->currentIndex()) {
- case 0:
- {
- // name
- defName = QString(QLatin1String(QContactDisplayLabel::DefinitionName));
- fieldName = QString(QLatin1String(QContactDisplayLabel::FieldLabel));
-
- exprName = "Name";
- }
- break;
-
- case 1:
- {
- // phone number
- defName = QString(QLatin1String(QContactPhoneNumber::DefinitionName));
- fieldName = QString(QLatin1String(QContactPhoneNumber::FieldNumber));
-
- exprName = "Phone Number";
- }
- break;
-
- default:
- {
- // email address
- defName = QString(QLatin1String(QContactEmailAddress::DefinitionName));
- fieldName = QString(QLatin1String(QContactEmailAddress::FieldEmailAddress));
-
- exprName = "Email Address";
- }
- break;
-
- }
- fil.setDetailDefinitionName(defName, fieldName);
- fil.setValue(m_valueCriteriaEdit->text());
-
- int flag = m_criteriaTypeCombo->itemData(m_criteriaTypeCombo->currentIndex()).toInt();
- fil.setMatchFlags(QContactFilter::MatchFlags(flag));
- exprMatch = m_criteriaTypeCombo->currentText().toLower();
-
- // if OR then join with OR
- if (m_joinMethodCombo->currentIndex() == 1) {
- QContactUnionFilter ufil;
- ufil << m_cumulativeFilter << fil;
- QContactIntersectionFilter ifil;
- ifil << ufil;
- m_cumulativeFilter = ifil;
- exprJoin = "OR";
- } else {
- // otherwise, just AND.
- QContactIntersectionFilter ifil(m_cumulativeFilter);
- ifil << fil;
- m_cumulativeFilter = ifil;
- exprJoin = "AND";
- }
-
- // set the expression so far
- if (!m_cumulativeExpression.isEmpty())
- m_cumulativeExpression += " " + exprJoin + " ";
- m_cumulativeExpression += exprName + " " + exprMatch + " \"" + m_valueCriteriaEdit->text() + "\"";
-
- // and clear the UI ready for the next filter expression.
- m_valueCriteriaEdit->setText("");
- m_fieldCriteriaCombo->setCurrentIndex(0);
- m_criteriaTypeCombo->setCurrentIndex(0);
- m_joinMethodCombo->setCurrentIndex(0);
- m_cumulativeExpressionLabel->setText(m_cumulativeExpression);
-}
-
-void FilterPage::clearClicked()
-{
- m_cumulativeExpression = QString();
- m_valueCriteriaEdit->setText("");
- m_fieldCriteriaCombo->setCurrentIndex(0);
- m_criteriaTypeCombo->setCurrentIndex(0);
- m_joinMethodCombo->setCurrentIndex(0);
- m_cumulativeExpressionLabel->setText("Match All Contacts");
- m_cumulativeFilter = QContactFilter();
+ m_name.clear();
+ m_phone.clear();
+ m_email.clear();
+ m_address.clear();
+ m_nameEdit->clear();
+ m_phoneEdit->clear();
+ m_emailEdit->clear();
+ m_addressEdit->clear();
+ m_currentFilter = QContactIntersectionFilter();
+ emit showListPage(m_currentFilter);
}
void FilterPage::filterClicked()
{
- emit showListPage(m_cumulativeFilter);
+ m_name = m_nameEdit->text();
+ m_phone = m_phoneEdit->text();
+ m_email = m_emailEdit->text();
+ m_address = m_addressEdit->text();
+ // The intersection filter ensures that non-empty field value must be found in the contact.
+ m_currentFilter = QContactIntersectionFilter();
+ if (!m_nameEdit->text().isEmpty()) {
+ // Search all fields of the name by building a union filter
+ QContactUnionFilter nameFilter;
+ QStringList nameFields;
+ nameFields << QContactName::FieldCustomLabel;
+ nameFields << QContactName::FieldFirstName;
+ nameFields << QContactName::FieldLastName;
+ nameFields << QContactName::FieldMiddleName;
+ nameFields << QContactName::FieldPrefix;
+ nameFields << QContactName::FieldSuffix;
+ foreach (const QString& fieldName, nameFields) {
+ QContactDetailFilter subFilter;
+ subFilter.setDetailDefinitionName(QContactName::DefinitionName, fieldName);
+ subFilter.setValue(m_nameEdit->text());
+ subFilter.setMatchFlags(QContactFilter::MatchContains);
+ nameFilter.append(subFilter);
+ }
+ m_currentFilter.append(nameFilter);
+ }
+ if (!m_phoneEdit->text().isEmpty()) {
+ QContactDetailFilter phoneFilter;
+ phoneFilter.setDetailDefinitionName(QContactPhoneNumber::DefinitionName,
+ QContactPhoneNumber::FieldNumber);
+ phoneFilter.setValue(m_phoneEdit->text());
+ phoneFilter.setMatchFlags(QContactFilter::MatchContains);
+ m_currentFilter.append(phoneFilter);
+ }
+ if (!m_emailEdit->text().isEmpty()) {
+ QContactDetailFilter emailFilter;
+ emailFilter.setDetailDefinitionName(QContactEmailAddress::DefinitionName,
+ QContactEmailAddress::FieldEmailAddress);
+ emailFilter.setValue(m_emailEdit->text());
+ emailFilter.setMatchFlags(QContactFilter::MatchContains);
+ m_currentFilter.append(emailFilter);
+ }
+ if (!m_addressEdit->text().isEmpty()) {
+ // Search all fields of the address by building a union filter
+ QContactUnionFilter addressFilter;
+ QStringList addressFields;
+ addressFields << QContactAddress::FieldCountry;
+ addressFields << QContactAddress::FieldLocality;
+ addressFields << QContactAddress::FieldPostcode;
+ addressFields << QContactAddress::FieldPostOfficeBox;
+ addressFields << QContactAddress::FieldRegion;
+ addressFields << QContactAddress::FieldStreet;
+ foreach (const QString& fieldName, addressFields) {
+ QContactDetailFilter subFilter;
+ subFilter.setDetailDefinitionName(QContactAddress::DefinitionName, fieldName);
+ subFilter.setValue(m_addressEdit->text());
+ subFilter.setMatchFlags(QContactFilter::MatchContains);
+ addressFilter.append(subFilter);
+ }
+ m_currentFilter.append(addressFilter);
+ }
+ emit showListPage(m_currentFilter);
}
+
+void FilterPage::cancelClicked()
+{
+ m_nameEdit->setText(m_name);
+ m_phoneEdit->setText(m_phone);
+ m_emailEdit->setText(m_email);
+ m_addressEdit->setText(m_address);
+ emit showListPage(m_currentFilter);
+}