phonebookui/pbkcommonui/src/cntfetchcontactpopup.cpp
changeset 59 a642906a277a
child 65 ae724a111993
equal deleted inserted replaced
47:7cbcb2896f0e 59:a642906a277a
       
     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 #include "cntfetchcontactpopup.h"
       
    18 #include "cntdocumentloader.h"
       
    19 #include "cntfetchmarkall.h"
       
    20 #include "cntdebug.h"
       
    21 
       
    22 #include <cntlistmodel.h>
       
    23 #include <hblistview.h>
       
    24 #include <hbstyleloader.h>
       
    25 #include <hbindexfeedback.h>
       
    26 #include <hblistviewitem.h>
       
    27 #include <hbscrollbar.h>
       
    28 #include <hbmainwindow.h>
       
    29 #include <hbdialog.h>
       
    30 #include <hblabel.h>
       
    31 #include <hbsearchpanel.h>
       
    32 #include <hblineedit.h>
       
    33 #include <hbaction.h>
       
    34 #include <hbtextitem.h>
       
    35 #include <hbstaticvkbhost.h>
       
    36 
       
    37 #include <QGraphicsLinearLayout>
       
    38 #include <QCoreApplication>
       
    39 const char *CNT_FETCHLIST_XML = ":/xml/contacts_fetchdialog.docml";
       
    40 
       
    41 CntFetchContactPopup::CntFetchContactPopup( QContactManager& aMgr ) : 
       
    42 mManager(aMgr),
       
    43 mPopup( NULL ),
       
    44 mListView( NULL ),
       
    45 mEmptyView( NULL ),
       
    46 mHeading( NULL ),
       
    47 mPrimaryAction( NULL ),
       
    48 mSearch( NULL ),
       
    49 mModel( NULL ),
       
    50 mMarkAll( NULL ),
       
    51 mDoc( NULL )
       
    52 {
       
    53     mDoc = new CntDocumentLoader();
       
    54         
       
    55     bool ok;
       
    56     mDoc->load( CNT_FETCHLIST_XML, &ok );
       
    57     
       
    58     if ( !ok )
       
    59     {
       
    60         qFatal("Unable to read %S", CNT_FETCHLIST_XML );
       
    61     }
       
    62     mPopup = static_cast<HbDialog*>( mDoc->findWidget( "dialog" ) );
       
    63     mSearch = static_cast<HbSearchPanel*>( mDoc->findWidget( "searchPanel" ) );
       
    64     mMarkAll = static_cast<CntFetchMarkAll*>( mDoc->findWidget("markAll") );
       
    65     mEmptyView = static_cast<HbTextItem*>( mDoc->findWidget("emptyLabel" ));
       
    66     mListView = static_cast<HbListView*>( mDoc->findWidget("listView") );
       
    67     mHeading = static_cast<HbLabel*>( mDoc->findWidget("heading") );
       
    68    
       
    69     connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(closePopup()) );
       
    70     connect(mPopup, SIGNAL(aboutToClose()), this, SLOT(closePopup()) );
       
    71     connect(mSearch, SIGNAL(criteriaChanged(QString)), this, SLOT(setFilter(QString)));
       
    72     connect(mMarkAll, SIGNAL(markAll(int)), this, SLOT(selectAll(int)) );
       
    73     
       
    74     HbLineEdit *editor = static_cast<HbLineEdit*>( mSearch->primitive("lineedit"));
       
    75     editor->setInputMethodHints(Qt::ImhNoPredictiveText);
       
    76     mSearch->setCancelEnabled( false );
       
    77     
       
    78     mVirtualKeyboard = new HbStaticVkbHost(editor);
       
    79     connect(mVirtualKeyboard, SIGNAL(keypadOpened()), this, SLOT(handleKeypadOpen()));
       
    80     connect(mVirtualKeyboard, SIGNAL(keypadClosed()), this, SLOT(handleKeypadClosed()));
       
    81 }
       
    82 
       
    83 CntFetchContactPopup::~CntFetchContactPopup()
       
    84 {
       
    85     delete mDoc;
       
    86     delete mModel;
       
    87 }
       
    88 
       
    89 CntFetchContactPopup* CntFetchContactPopup::createMultiSelectionPopup( QString aTitle, QString aAction, QContactManager& aManager )
       
    90 {
       
    91     CntFetchContactPopup* popup = new CntFetchContactPopup( aManager );
       
    92     popup->constructPopupDialog( aTitle, aAction, HbAbstractItemView::MultiSelection );
       
    93     
       
    94     return popup;
       
    95 }
       
    96 
       
    97 CntFetchContactPopup* CntFetchContactPopup::createSingleSelectionPopup( QString aTitle, QContactManager& aManager )
       
    98 {
       
    99     CntFetchContactPopup* popup = new CntFetchContactPopup( aManager );
       
   100     popup->constructPopupDialog( aTitle, "", HbAbstractItemView::NoSelection );
       
   101     
       
   102     return popup;
       
   103 }
       
   104 
       
   105 void CntFetchContactPopup::setSelectedContacts( QSet<QContactLocalId> aIds )
       
   106 {
       
   107     CNT_ENTRY
       
   108     if ( mListView->selectionMode() == HbAbstractItemView::MultiSelection )
       
   109     {
       
   110         mIdList.clear();
       
   111         
       
   112         foreach ( QContactLocalId id, aIds ) 
       
   113         {
       
   114             mIdList.append( id );
       
   115             QContact contact = mManager.contact(id);
       
   116             QModelIndex contactIndex = mModel->indexOfContact(contact);
       
   117             mSelectionModel->select( contactIndex, QItemSelectionModel::Select );
       
   118         }
       
   119     }
       
   120     CNT_EXIT
       
   121 }
       
   122 
       
   123 void CntFetchContactPopup::showPopup()
       
   124 {
       
   125     CNT_ENTRY
       
   126     HbMainWindow* window = mPopup->mainWindow();
       
   127     if ( window )
       
   128     {
       
   129         connect(window, SIGNAL(orientationChanged(Qt::Orientation)), 
       
   130                 this, SLOT(loadLayout(Qt::Orientation)) );
       
   131     }
       
   132     
       
   133     mPopup->open( this, SLOT(dialogDismissed(HbAction*)) );
       
   134     CNT_EXIT
       
   135 }
       
   136 
       
   137 void CntFetchContactPopup::handleKeypadOpen()
       
   138 {
       
   139     CNT_ENTRY
       
   140     qreal height = mPopup->size().height() - 
       
   141             mVirtualKeyboard->keyboardArea().height() - 
       
   142             mSearch->size().height();
       
   143     
       
   144     // in single selection we don't have the "mark all" option
       
   145     if ( mMarkAll )
       
   146     {
       
   147         height = height - mMarkAll->size().height();
       
   148     }
       
   149 
       
   150     mEmptyView->setMaximumHeight( height );
       
   151     mListView->setMaximumHeight( height );
       
   152     CNT_EXIT
       
   153 }
       
   154 
       
   155 void CntFetchContactPopup::handleKeypadClosed()
       
   156 {
       
   157     CNT_ENTRY
       
   158     
       
   159     qreal height =  mPopup->size().height() - mSearch->size().height();
       
   160     if ( mMarkAll )
       
   161     {
       
   162         height = height - mMarkAll->size().height();
       
   163     }   
       
   164     
       
   165     mListView->setMaximumHeight( height );
       
   166     mEmptyView->setMaximumHeight( height );
       
   167     CNT_EXIT
       
   168 }
       
   169 
       
   170 void CntFetchContactPopup::contactSelected( const QModelIndex& aIndex )
       
   171 {
       
   172     CNT_ENTRY
       
   173     if ( aIndex.isValid() )
       
   174     {
       
   175         QContact contact = mModel->contact( aIndex );
       
   176         mIdList.append( contact.localId() );
       
   177         
       
   178         emit fetchReady( mIdList.toSet() );
       
   179     }
       
   180     
       
   181     disconnect(mListView, SIGNAL(activated(const QModelIndex&)), 
       
   182                     this, SLOT(contactSelected(const QModelIndex&)) );
       
   183     mPopup->close();
       
   184     CNT_EXIT
       
   185 }
       
   186 
       
   187 void CntFetchContactPopup::contactsSelected(
       
   188         const QItemSelection& aSelected, 
       
   189         const QItemSelection& aDeselected )
       
   190 {
       
   191     CNT_ENTRY
       
   192     
       
   193     // remove all deselected items
       
   194     foreach ( QModelIndex index, aDeselected.indexes() )
       
   195     {
       
   196         QContact contact = mModel->contact( index );
       
   197         QContactLocalId id = contact.localId();
       
   198         if ( mIdList.contains(id) )
       
   199         {
       
   200             mIdList.removeAll( id );
       
   201         }
       
   202     }
       
   203     
       
   204     // add all selected items
       
   205     foreach ( QModelIndex index, aSelected.indexes() )
       
   206     {
       
   207         QContact contact = mModel->contact( index );
       
   208         QContactLocalId id = contact.localId();
       
   209         if ( !mIdList.contains(id) )
       
   210         {
       
   211             mIdList.append( id );
       
   212         }
       
   213     }
       
   214         
       
   215     mMarkAll->setSelectedContactCount( mIdList.size() );
       
   216     CNT_EXIT
       
   217 }
       
   218 
       
   219 void CntFetchContactPopup::selectAll( int aState )
       
   220 {
       
   221     CNT_ENTRY
       
   222     if ( mListView->selectionMode() == HbAbstractItemView::MultiSelection )
       
   223     {
       
   224         switch ( aState )
       
   225         {
       
   226         case Qt::Checked: 
       
   227             mListView->selectAll();
       
   228             mMarkAll->setSelectedContactCount( mIdList.count() );
       
   229             break;
       
   230             
       
   231         case Qt::Unchecked:
       
   232             mListView->clearSelection();
       
   233             mMarkAll->setSelectedContactCount( 0 );
       
   234             break;
       
   235         default:
       
   236             break;
       
   237         }
       
   238     }
       
   239     CNT_EXIT
       
   240 }
       
   241 
       
   242 void CntFetchContactPopup::dialogDismissed( HbAction* aAction )
       
   243 {
       
   244     CNT_ENTRY
       
   245    
       
   246     if ( aAction != mPrimaryAction )
       
   247     {
       
   248         emit fetchCancelled();
       
   249     }
       
   250     else if ( aAction ) 
       
   251     {
       
   252         disconnect(mSelectionModel, SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),
       
   253                         this, SLOT(contactsSelected(const QItemSelection&, const QItemSelection&)) );
       
   254         
       
   255         QModelIndexList indexList = mSelectionModel->selectedIndexes();
       
   256         foreach ( QModelIndex index, indexList )
       
   257         {
       
   258             QContact contact = mModel->contact( index );
       
   259             mIdList.append( contact.localId() );
       
   260         }
       
   261         emit fetchReady( mIdList.toSet() );
       
   262     }
       
   263     else
       
   264     {
       
   265         // ignore.
       
   266     }
       
   267     CNT_EXIT
       
   268 }
       
   269 
       
   270 void CntFetchContactPopup::setFilter( const QString& aFilter )
       
   271 {
       
   272     CNT_ENTRY
       
   273     QContactDetailFilter detailfilter;
       
   274     detailfilter.setMatchFlags( QContactFilter::MatchStartsWith );
       
   275     detailfilter.setValue( aFilter.split(QRegExp("\\s+"), QString::SkipEmptyParts) );
       
   276     detailfilter.setDetailDefinitionName(
       
   277             QContactDisplayLabel::DefinitionName,
       
   278             QContactDisplayLabel::FieldLabel);
       
   279        
       
   280     mModel->setFilter(detailfilter);
       
   281     
       
   282     HbMainWindow* window = mPopup->mainWindow();
       
   283     if ( window )
       
   284     {
       
   285         loadLayout( window->orientation() );
       
   286     }
       
   287     
       
   288     setSelectedContacts( mIdList.toSet() );
       
   289     CNT_EXIT
       
   290 }
       
   291 
       
   292 void CntFetchContactPopup::constructPopupDialog( QString aTitle, QString aAction, HbAbstractItemView::SelectionMode aMode )
       
   293 {
       
   294     CNT_ENTRY
       
   295     mIdList.clear();
       
   296     mTitle = aTitle;
       
   297     
       
   298     mPopup->setAttribute( Qt::WA_DeleteOnClose, true );    
       
   299     
       
   300     QContactDetailFilter contactsFilter;
       
   301     contactsFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
       
   302     contactsFilter.setValue(QString(QLatin1String(QContactType::TypeContact)));
       
   303     
       
   304     mModel = new CntListModel( &mManager, contactsFilter, false);
       
   305     mModel->showMyCard(false);
       
   306     mSelectionModel = new QItemSelectionModel( mModel );
       
   307     
       
   308     mListView->setFrictionEnabled(true);
       
   309     mListView->setScrollingStyle(HbScrollArea::PanWithFollowOn);
       
   310     mListView->verticalScrollBar()->setInteractive(true);
       
   311 
       
   312     HbIndexFeedback* indexFeedback = new HbIndexFeedback(mPopup);
       
   313     indexFeedback->setIndexFeedbackPolicy(HbIndexFeedback::IndexFeedbackSingleCharacter);
       
   314     indexFeedback->setItemView(mListView);
       
   315          
       
   316     HbListViewItem *prototype = mListView->listItemPrototype();
       
   317     prototype->setGraphicsSize(HbListViewItem::Thumbnail);
       
   318     prototype->setStretchingStyle(HbListViewItem::StretchLandscape);
       
   319         
       
   320     mListView->setSelectionMode( aMode );
       
   321     mListView->setModel( mModel );
       
   322     mListView->setSelectionModel( mSelectionModel );
       
   323     
       
   324     HbMainWindow* window = mPopup->mainWindow();
       
   325     if ( window )
       
   326     {
       
   327         loadLayout( window->orientation() );
       
   328     }
       
   329     
       
   330     if ( !aAction.isEmpty() )
       
   331     {
       
   332         mPrimaryAction = new HbAction( aAction, mPopup );
       
   333         mPopup->addAction( mPrimaryAction );
       
   334     }
       
   335         
       
   336     HbAction* secondaryAction = new HbAction(hbTrId("txt_common_button_cancel"), mPopup);
       
   337     mPopup->addAction( secondaryAction );
       
   338         
       
   339     if ( aMode != HbListView::MultiSelection )
       
   340     {
       
   341         connect(mListView, SIGNAL(activated(const QModelIndex&)), 
       
   342                 this, SLOT(contactSelected(const QModelIndex&)) );
       
   343         mMarkAll->setVisible( false );
       
   344     }
       
   345     else
       
   346     {
       
   347         mMarkAll->setSelectedContactCount( 0 );
       
   348         mMarkAll->setMaxContactCount( mModel->rowCount() );
       
   349         connect(mSelectionModel, SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),
       
   350                 this, SLOT(contactsSelected(const QItemSelection&, const QItemSelection&)) );
       
   351             
       
   352     }
       
   353         
       
   354     CNT_EXIT
       
   355 }
       
   356 
       
   357 void CntFetchContactPopup::loadLayout( Qt::Orientation aOrientation )
       
   358 {
       
   359     CNT_ENTRY
       
   360     
       
   361     bool multi = mListView->selectionMode() == HbAbstractItemView::MultiSelection;
       
   362     if ( mModel->rowCount() > 0 )
       
   363     {
       
   364         if ( aOrientation == Qt::Horizontal )
       
   365         {
       
   366             mPopup->setHeadingWidget( NULL );
       
   367             mDoc->load( CNT_FETCHLIST_XML, "find_list_landscape");
       
   368         }
       
   369         else
       
   370         {
       
   371             mHeading = new HbLabel( mTitle );
       
   372             mPopup->setHeadingWidget( mHeading );
       
   373             qreal popupHeight = mPopup->mainWindow()->layoutRect().height();
       
   374             mPopup->setMinimumHeight(popupHeight);
       
   375             mDoc->load( CNT_FETCHLIST_XML, multi ? "find_list" : "find_list_single");
       
   376         }
       
   377     }
       
   378     else
       
   379     {
       
   380         if ( aOrientation == Qt::Horizontal )
       
   381         {
       
   382             mPopup->setHeadingWidget( NULL );
       
   383             mDoc->load( CNT_FETCHLIST_XML, "find_empty_landscape" );
       
   384         }
       
   385         else
       
   386         {
       
   387             mHeading = new HbLabel( mTitle );
       
   388             mPopup->setHeadingWidget( mHeading );
       
   389             qreal popupHeight = mPopup->mainWindow()->layoutRect().height();
       
   390             mPopup->setMinimumHeight(popupHeight);
       
   391             mDoc->load( CNT_FETCHLIST_XML, multi ? "find_empty" : "find_empty_single" );
       
   392         }
       
   393      }
       
   394     
       
   395     CNT_EXIT
       
   396 }
       
   397 
       
   398 void CntFetchContactPopup::closePopup()
       
   399 {
       
   400     CNT_ENTRY
       
   401     disconnect(mVirtualKeyboard, SIGNAL(keypadOpened()), this, SLOT(handleKeypadOpen()));
       
   402     disconnect(mVirtualKeyboard, SIGNAL(keypadClosed()), this, SLOT(handleKeypadClosed()));
       
   403            
       
   404     deleteLater();
       
   405     CNT_EXIT
       
   406 }
       
   407 // End of File