phonebookui/pbkcommonui/src/cntfetchcontactpopup.cpp
changeset 53 e6aff7b69165
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 #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 }
       
    87 
       
    88 CntFetchContactPopup* CntFetchContactPopup::createMultiSelectionPopup( QString aTitle, QString aAction, QContactManager& aManager )
       
    89 {
       
    90     CntFetchContactPopup* popup = new CntFetchContactPopup( aManager );
       
    91     popup->constructPopupDialog( aTitle, aAction, HbAbstractItemView::MultiSelection );
       
    92     
       
    93     return popup;
       
    94 }
       
    95 
       
    96 CntFetchContactPopup* CntFetchContactPopup::createSingleSelectionPopup( QString aTitle, QContactManager& aManager )
       
    97 {
       
    98     CntFetchContactPopup* popup = new CntFetchContactPopup( aManager );
       
    99     popup->constructPopupDialog( aTitle, "", HbAbstractItemView::NoSelection );
       
   100     
       
   101     return popup;
       
   102 }
       
   103 
       
   104 void CntFetchContactPopup::setSelectedContacts( QSet<QContactLocalId> aIds )
       
   105 {
       
   106     CNT_ENTRY
       
   107     if ( mListView->selectionMode() == HbAbstractItemView::MultiSelection )
       
   108     {
       
   109         mIdList.clear();
       
   110         
       
   111         foreach ( QContactLocalId id, aIds ) 
       
   112         {
       
   113             mIdList.append( id );
       
   114             QContact contact = mManager.contact(id);
       
   115             QModelIndex contactIndex = mModel->indexOfContact(contact);
       
   116             mSelectionModel->select( contactIndex, QItemSelectionModel::Select );
       
   117         }
       
   118     }
       
   119     CNT_EXIT
       
   120 }
       
   121 
       
   122 void CntFetchContactPopup::showPopup()
       
   123 {
       
   124     CNT_ENTRY
       
   125     HbMainWindow* window = mPopup->mainWindow();
       
   126     if ( window )
       
   127     {
       
   128         connect(window, SIGNAL(orientationChanged(Qt::Orientation)), 
       
   129                 this, SLOT(loadLayout(Qt::Orientation)) );
       
   130     }
       
   131     
       
   132     mPopup->open( this, SLOT(dialogDismissed(HbAction*)) );
       
   133     CNT_EXIT
       
   134 }
       
   135 
       
   136 void CntFetchContactPopup::handleKeypadOpen()
       
   137 {
       
   138     CNT_ENTRY
       
   139     qreal height = mPopup->size().height() - 
       
   140             mVirtualKeyboard->keyboardArea().height() - 
       
   141             mSearch->size().height();
       
   142     
       
   143     // in single selection we don't have the "mark all" option
       
   144     if ( mMarkAll )
       
   145     {
       
   146         height = height - mMarkAll->size().height();
       
   147     }
       
   148 
       
   149     mEmptyView->setMaximumHeight( height );
       
   150     mListView->setMaximumHeight( height );
       
   151     CNT_EXIT
       
   152 }
       
   153 
       
   154 void CntFetchContactPopup::handleKeypadClosed()
       
   155 {
       
   156     CNT_ENTRY
       
   157     
       
   158     qreal height =  mPopup->size().height() - mSearch->size().height();
       
   159     if ( mMarkAll )
       
   160     {
       
   161         height = height - mMarkAll->size().height();
       
   162     }   
       
   163     
       
   164     mListView->setMaximumHeight( height );
       
   165     mEmptyView->setMaximumHeight( height );
       
   166     CNT_EXIT
       
   167 }
       
   168 
       
   169 void CntFetchContactPopup::contactSelected( const QModelIndex& aIndex )
       
   170 {
       
   171     CNT_ENTRY
       
   172     if ( aIndex.isValid() )
       
   173     {
       
   174         QContact contact = mModel->contact( aIndex );
       
   175         mIdList.append( contact.localId() );
       
   176         
       
   177         emit fetchReady( mIdList.toSet() );
       
   178     }
       
   179     
       
   180     disconnect(mListView, SIGNAL(activated(const QModelIndex&)), 
       
   181                     this, SLOT(contactSelected(const QModelIndex&)) );
       
   182     mPopup->close();
       
   183     CNT_EXIT
       
   184 }
       
   185 
       
   186 void CntFetchContactPopup::contactsSelected(
       
   187         const QItemSelection& aSelected, 
       
   188         const QItemSelection& aDeselected )
       
   189 {
       
   190     CNT_ENTRY
       
   191     
       
   192     // remove all deselected items
       
   193     foreach ( QModelIndex index, aDeselected.indexes() )
       
   194     {
       
   195         QContact contact = mModel->contact( index );
       
   196         QContactLocalId id = contact.localId();
       
   197         if ( mIdList.contains(id) )
       
   198         {
       
   199             mIdList.removeAll( id );
       
   200         }
       
   201     }
       
   202     
       
   203     // add all selected items
       
   204     foreach ( QModelIndex index, aSelected.indexes() )
       
   205     {
       
   206         QContact contact = mModel->contact( index );
       
   207         QContactLocalId id = contact.localId();
       
   208         if ( !mIdList.contains(id) )
       
   209         {
       
   210             mIdList.append( id );
       
   211         }
       
   212     }
       
   213         
       
   214     mMarkAll->setSelectedContactCount( mIdList.size() );
       
   215     
       
   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             mDoc->load( CNT_FETCHLIST_XML, multi ? "find_list" : "find_list_single");
       
   374         }
       
   375     }
       
   376     else
       
   377     {
       
   378         if ( aOrientation == Qt::Horizontal )
       
   379         {
       
   380             mPopup->setHeadingWidget( NULL );
       
   381             mDoc->load( CNT_FETCHLIST_XML, "find_empty_landscape" );
       
   382         }
       
   383         else
       
   384         {
       
   385             mHeading = new HbLabel( mTitle );
       
   386             mPopup->setHeadingWidget( mHeading );
       
   387             mDoc->load( CNT_FETCHLIST_XML, multi ? "find_empty" : "find_empty_single" );
       
   388         }
       
   389      }
       
   390     
       
   391     CNT_EXIT
       
   392 }
       
   393 
       
   394 void CntFetchContactPopup::closePopup()
       
   395 {
       
   396     CNT_ENTRY
       
   397     disconnect(mVirtualKeyboard, SIGNAL(keypadOpened()), this, SLOT(handleKeypadOpen()));
       
   398     disconnect(mVirtualKeyboard, SIGNAL(keypadClosed()), this, SLOT(handleKeypadClosed()));
       
   399            
       
   400     deleteLater();
       
   401     CNT_EXIT
       
   402 }
       
   403 // End of File