phonebookui/pbkcommonui/src/cntfetchcontactsview.cpp
changeset 53 e6aff7b69165
parent 51 81c360d47083
child 54 47627ab5d7a4
equal deleted inserted replaced
51:81c360d47083 53:e6aff7b69165
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cntfetchcontactsview.h"
       
    19 
       
    20 #include <cntlistmodel.h>
       
    21 
       
    22 #include <QGraphicsLinearLayout>
       
    23 
       
    24 #include <qcontactid.h>
       
    25 #include <hbdialog.h>
       
    26 #include <hbscrollbar.h>
       
    27 #include <hbindexfeedback.h>
       
    28 #include <hbaction.h>
       
    29 #include <hblabel.h>
       
    30 #include <hbaction.h>
       
    31 #include <hblineedit.h>
       
    32 #include <hbmainwindow.h>
       
    33 #include <hblistviewitem.h>
       
    34 #include <hblistview.h>
       
    35 #include <hbsearchpanel.h>
       
    36 #include <hbstaticvkbhost.h>
       
    37 
       
    38 /*!
       
    39 Given a contact manager, CntFetchContacts is responsible for 
       
    40 retrieving a set of contacts, if any were chosen by the user. 
       
    41 */
       
    42 CntFetchContacts::CntFetchContacts(QContactManager &aManager) :
       
    43     QObject(),
       
    44     mPopup(NULL),
       
    45     mCntModel(NULL),
       
    46     mListView(NULL),
       
    47     mEmptyListLabel(NULL),
       
    48     mSelectionMode(HbAbstractItemView::MultiSelection),
       
    49     mManager(&aManager),
       
    50     mWasCanceled(false),
       
    51     mLabel(NULL),
       
    52     mVirtualKeyboard(NULL),
       
    53     mPrimaryAction(NULL),
       
    54     mSecondaryAction(NULL),
       
    55     mIndexFeedback(NULL)
       
    56 {
       
    57     mSearchPanel = new HbSearchPanel();
       
    58     mSearchPanel->setVisible(false);
       
    59     mSearchPanel->setCancelEnabled(false);
       
    60     connect(mSearchPanel, SIGNAL(criteriaChanged(QString)), this, SLOT(setFilter(QString)));
       
    61     
       
    62     HbLineEdit *editor = static_cast<HbLineEdit*>(mSearchPanel->primitive("lineedit"));
       
    63     editor->setInputMethodHints(Qt::ImhNoPredictiveText);
       
    64     
       
    65     mLayout = new QGraphicsLinearLayout(Qt::Vertical);
       
    66     
       
    67     mContainerWidget = new HbWidget();
       
    68 }
       
    69 
       
    70 CntFetchContacts::~CntFetchContacts()
       
    71 {
       
    72     delete mCntModel;
       
    73     mCntModel = NULL;
       
    74 }
       
    75 
       
    76 /*!
       
    77 Query to see if the user decided to press Cancel after selecting 
       
    78 group members. Must be called to see if results are valid.
       
    79 */
       
    80 bool CntFetchContacts::wasCanceled() const
       
    81 {
       
    82     return mWasCanceled;
       
    83 }
       
    84 
       
    85 void CntFetchContacts::setDetails(QString aTitle, QString aButtonText)
       
    86 {
       
    87     mButtonText = aButtonText;
       
    88     
       
    89     if (!mLabel) {
       
    90         mLabel = new HbLabel(aTitle);
       
    91     }
       
    92     else {
       
    93         mLabel->setPlainText(aTitle);
       
    94     }
       
    95 }
       
    96 
       
    97 /*!
       
    98 Brings up a list of contacts, awaiting user response. This function is asynchronous.
       
    99 When a response is given, a clicked signal will be sent.
       
   100 */
       
   101 void CntFetchContacts::displayContacts(HbAbstractItemView::SelectionMode aMode,
       
   102                                                    QSet<QContactLocalId> aContacts)
       
   103 {
       
   104     doInitialize(aMode,aContacts);
       
   105     markMembersInView();
       
   106     showPopup();
       
   107 }
       
   108 
       
   109 QSet<QContactLocalId> CntFetchContacts::getSelectedContacts() const
       
   110 {
       
   111    return mCurrentlySelected;
       
   112 }
       
   113 
       
   114 void CntFetchContacts::setFilter(const QString &filterString)
       
   115 {
       
   116     QStringList searchList = filterString.split(QRegExp("\\s+"), QString::SkipEmptyParts);
       
   117 
       
   118     QContactDetailFilter detailfilter;
       
   119     detailfilter.setDetailDefinitionName(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel);
       
   120     detailfilter.setMatchFlags(QContactFilter::MatchStartsWith);
       
   121     detailfilter.setValue(searchList);
       
   122     
       
   123     mCntModel->setFilter(detailfilter);
       
   124 
       
   125     markMembersInView();
       
   126 
       
   127     if (mCntModel->rowCount() == 0) {
       
   128         mLayout->removeItem(mListView);
       
   129         
       
   130         if (mEmptyListLabel) {
       
   131             qreal searchHeight = mSearchPanel->size().height();
       
   132             HbLabel* heading = static_cast<HbLabel*>(mPopup->headingWidget());
       
   133             qreal heightToSet =  mPopup->size().height() - mVirtualKeyboard->keyboardArea().height() - searchHeight;
       
   134             mEmptyListLabel->setMaximumHeight(heightToSet);
       
   135             mEmptyListLabel->setVisible(true);
       
   136             mLayout->insertItem(0, mEmptyListLabel);
       
   137         }
       
   138 
       
   139         mListView->setVisible(false);
       
   140         mSearchPanel->setVisible(true);
       
   141     }
       
   142     else {
       
   143         if (mEmptyListLabel) {
       
   144             mEmptyListLabel->setVisible(false);
       
   145         }
       
   146         mLayout->removeItem(mEmptyListLabel);
       
   147         mLayout->insertItem(0, mListView);
       
   148         mListView->setVisible(true);
       
   149     }
       
   150 }
       
   151 
       
   152 void CntFetchContacts::handleKeypadOpen()
       
   153 {
       
   154     qreal searchHeight = mSearchPanel->size().height();
       
   155     HbLabel* heading = static_cast<HbLabel*>(mPopup->headingWidget());
       
   156     qreal heightToSet =  mPopup->size().height() - mVirtualKeyboard->keyboardArea().height() - searchHeight;
       
   157 
       
   158     if (mEmptyListLabel) {
       
   159         mEmptyListLabel->setMaximumHeight(heightToSet);
       
   160     }
       
   161     
       
   162     mListView->setMaximumHeight(heightToSet);
       
   163 }
       
   164 
       
   165 void CntFetchContacts::handleKeypadClose()
       
   166 {
       
   167     mListView->setMaximumHeight(mPopup->size().height());
       
   168 
       
   169     if (mEmptyListLabel) {
       
   170         mEmptyListLabel->setMaximumHeight(mPopup->size().height());
       
   171     }
       
   172 }
       
   173 
       
   174 /*!
       
   175 Notify client that we're done.
       
   176 */
       
   177 void CntFetchContacts::handleUserResponse(HbAction* action)
       
   178 {
       
   179     bool userCanceled = (action == mSecondaryAction); 
       
   180     if (userCanceled) {
       
   181         mCurrentlySelected.clear();
       
   182         
       
   183         mWasCanceled = true;
       
   184     }
       
   185     else {
       
   186         mWasCanceled = false;
       
   187     }
       
   188     
       
   189     mPopup->setVisible(false);
       
   190     mListView->setModel(NULL);
       
   191     delete mCntModel;
       
   192     mCntModel = NULL;
       
   193     
       
   194     emit clicked();
       
   195 }
       
   196 
       
   197 void CntFetchContacts::memberSelectionChanged(const QModelIndex &index)
       
   198 {
       
   199     if (!index.isValid()) return;
       
   200 
       
   201     if (mSelectionMode == HbAbstractItemView::SingleSelection) {
       
   202         mCurrentlySelected.clear();
       
   203     }
       
   204 
       
   205     QContactLocalId contactId = mCntModel->contact(index).localId();
       
   206 
       
   207     bool isSelected = mListView->selectionModel()->isSelected(index);
       
   208     if (isSelected != mCurrentlySelected.contains(contactId)) {
       
   209         if (isSelected) {
       
   210             mCurrentlySelected.insert(contactId);
       
   211         }
       
   212         else {
       
   213             mCurrentlySelected.remove(contactId);
       
   214         }
       
   215     }
       
   216     
       
   217     // Check for the case where there is a cancel button only. If so, 
       
   218     // after selecting any contact, should dismiss the dialog immediately.
       
   219     if (mButtonText.isEmpty() && mSelectionMode == HbAbstractItemView::SingleSelection) {
       
   220         disconnect(mVirtualKeyboard, SIGNAL(keypadOpened()), this, SLOT(handleKeypadOpen()));
       
   221         disconnect(mVirtualKeyboard, SIGNAL(keypadClosed()), this, SLOT(handleKeypadClose()));
       
   222         mPopup->close();
       
   223     }
       
   224 }
       
   225 
       
   226 void CntFetchContacts::doInitialize(HbAbstractItemView::SelectionMode aMode,
       
   227                                                 QSet<QContactLocalId> aContacts)
       
   228 {
       
   229     mSelectionMode = aMode;
       
   230     mCurrentlySelected = aContacts;
       
   231 
       
   232     if (!mPopup) {
       
   233         mPopup = new HbDialog;
       
   234         mPopup->setAttribute(Qt::WA_DeleteOnClose, true);
       
   235     }
       
   236 
       
   237     QContactDetailFilter contactsFilter;
       
   238     contactsFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
       
   239     contactsFilter.setValue(QString(QLatin1String(QContactType::TypeContact)));
       
   240     if (!mCntModel) {
       
   241         mCntModel = new CntListModel(mManager, contactsFilter, false);
       
   242     }
       
   243     
       
   244     if (!mListView) {
       
   245         mListView = new HbListView(mPopup);
       
   246         mListView->setModel(mCntModel);
       
   247         mListView->setSelectionMode(mSelectionMode);
       
   248         mListView->setFrictionEnabled(true);
       
   249         mListView->setScrollingStyle(HbScrollArea::PanWithFollowOn);
       
   250         mListView->verticalScrollBar()->setInteractive(true);
       
   251 
       
   252         HbListViewItem *prototype = mListView->listItemPrototype();
       
   253         prototype->setGraphicsSize(HbListViewItem::Thumbnail);
       
   254         prototype->setStretchingStyle(HbListViewItem::StretchLandscape);
       
   255 
       
   256         mIndexFeedback = new HbIndexFeedback(mPopup);
       
   257         mIndexFeedback->setIndexFeedbackPolicy(HbIndexFeedback::IndexFeedbackSingleCharacter);
       
   258         mIndexFeedback->setItemView(mListView);
       
   259 
       
   260         // Note that the layout takes ownership of the item(s) it contains.
       
   261         if (mCntModel->rowCount()== 0) {
       
   262             mListView->setVisible(false);
       
   263             if (!mEmptyListLabel) {
       
   264                 mEmptyListLabel = new HbTextItem(hbTrId("txt_phob_info_no_matching_contacts"));
       
   265                 mEmptyListLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
       
   266                 mEmptyListLabel->setFontSpec(HbFontSpec(HbFontSpec::Primary));
       
   267                 mEmptyListLabel->setAlignment(Qt::AlignCenter);
       
   268                 mLayout->insertItem(0, mEmptyListLabel);
       
   269             }
       
   270         }
       
   271         else {
       
   272             mLayout->addItem(mListView);
       
   273         }
       
   274 
       
   275         mCntModel->showMyCard(false);
       
   276     }
       
   277     
       
   278     // Handle the case where the model was removed for the list view
       
   279     if (!mListView->model()) {
       
   280         mListView->setModel(mCntModel);
       
   281     }
       
   282     
       
   283     // Main window is NULL in unit tests
       
   284     HbMainWindow* window = mListView->mainWindow();
       
   285     if (window) {
       
   286         mContainerWidget->setPreferredHeight(mListView->mainWindow()->size().height());
       
   287     }
       
   288     mContainerWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
       
   289     
       
   290     mSearchPanel->setVisible(true);
       
   291     mLayout->addItem(mSearchPanel);
       
   292     mContainerWidget->setLayout(mLayout);
       
   293     
       
   294     connect(mListView, SIGNAL(activated(const QModelIndex&)),
       
   295             this, SLOT(memberSelectionChanged(const QModelIndex&)), Qt::UniqueConnection);
       
   296 }
       
   297 
       
   298 void CntFetchContacts::showPopup()
       
   299 {
       
   300     mPopup->setTimeout(HbPopup::NoTimeout);
       
   301     mPopup->setDismissPolicy(HbPopup::NoDismiss);
       
   302     mPopup->setModal(true);
       
   303     mPopup->setContentWidget(mContainerWidget);
       
   304 
       
   305     if (!mVirtualKeyboard) {
       
   306         mVirtualKeyboard = new HbStaticVkbHost(mPopup);
       
   307         connect(mVirtualKeyboard, SIGNAL(keypadOpened()), this, SLOT(handleKeypadOpen()));
       
   308         connect(mVirtualKeyboard, SIGNAL(keypadClosed()), this, SLOT(handleKeypadClose()));
       
   309     }
       
   310     
       
   311     if (!mLabel) {
       
   312         mLabel = new HbLabel(hbTrId("txt_phob_title_contacts"));
       
   313     }
       
   314     mPopup->setHeadingWidget(mLabel);
       
   315 
       
   316     if (!mButtonText.isEmpty() && !mPrimaryAction) {
       
   317         mPrimaryAction = new HbAction(mButtonText, mPopup);
       
   318         mPopup->addAction(mPrimaryAction);
       
   319     }
       
   320     
       
   321     if (!mSecondaryAction) {
       
   322         mSecondaryAction = new HbAction(hbTrId("txt_common_button_cancel"), mPopup);
       
   323         mPopup->addAction(mSecondaryAction);
       
   324     }
       
   325 
       
   326     mPopup->open(this, SLOT(handleUserResponse(HbAction*)));
       
   327 }
       
   328 
       
   329 void CntFetchContacts::markMembersInView()
       
   330 {
       
   331     // If there are no contacts matching the current filter,
       
   332     // show "no matching contacts" label
       
   333     if (mCntModel->rowCount() == 0) {
       
   334         if (!mEmptyListLabel) {
       
   335             mEmptyListLabel = new HbTextItem(hbTrId("txt_phob_info_no_matching_contacts"));
       
   336             mEmptyListLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
       
   337             mEmptyListLabel->setFontSpec(HbFontSpec(HbFontSpec::Primary));
       
   338             mEmptyListLabel->setAlignment(Qt::AlignCenter);
       
   339             mLayout->insertItem(1, mEmptyListLabel);
       
   340         }
       
   341     }
       
   342     else {
       
   343         mLayout->removeItem(mEmptyListLabel);
       
   344         delete mEmptyListLabel;
       
   345         mEmptyListLabel = 0;
       
   346     }
       
   347 
       
   348     // Mark group members in the listview
       
   349     foreach (QContactLocalId id, mCurrentlySelected) {
       
   350         QContact contact = mManager->contact(id);
       
   351         QModelIndex contactIndex = mCntModel->indexOfContact(contact);
       
   352         mListView->selectionModel()->select(contactIndex, QItemSelectionModel::Select);
       
   353     }
       
   354 }
       
   355 
       
   356 // End of file