akonadi
emailaddressselectionwidget.cpp
00001 /* 00002 This file is part of Akonadi Contact. 00003 00004 Copyright (c) 2010 KDAB 00005 Author: Tobias Koenig <tokoe@kde.org> 00006 00007 This library is free software; you can redistribute it and/or modify it 00008 under the terms of the GNU Library General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or (at your 00010 option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, but WITHOUT 00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00015 License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to the 00019 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00020 02110-1301, USA. 00021 */ 00022 00023 #include "emailaddressselectionwidget.h" 00024 00025 #include "emailaddressselection_p.h" 00026 #include "emailaddressselectionproxymodel_p.h" 00027 00028 #include <akonadi/changerecorder.h> 00029 #include <akonadi/contact/contactsfilterproxymodel.h> 00030 #include <akonadi/contact/contactstreemodel.h> 00031 #include <akonadi/control.h> 00032 #include <akonadi/entitydisplayattribute.h> 00033 #include <akonadi/entitytreeview.h> 00034 #include <akonadi/itemfetchscope.h> 00035 #include <akonadi/session.h> 00036 #include <kabc/addressee.h> 00037 #include <kabc/contactgroup.h> 00038 #include <klineedit.h> 00039 #include <klocale.h> 00040 #include <kglobal.h> 00041 00042 #include <QtCore/QTimer> 00043 #include <QtGui/QHBoxLayout> 00044 #include <QtGui/QHeaderView> 00045 #include <QtGui/QKeyEvent> 00046 #include <QtGui/QLabel> 00047 #include <QtGui/QVBoxLayout> 00048 00049 using namespace Akonadi; 00050 00054 class SearchLineEdit : public KLineEdit 00055 { 00056 public: 00057 SearchLineEdit( QWidget *receiver, QWidget *parent = 0 ) 00058 : KLineEdit( parent ), mReceiver( receiver ) 00059 { 00060 } 00061 00062 protected: 00063 virtual void keyPressEvent( QKeyEvent *event ) 00064 { 00065 if ( event->key() == Qt::Key_Down ) 00066 QMetaObject::invokeMethod( mReceiver, "setFocus" ); 00067 00068 KLineEdit::keyPressEvent( event ); 00069 } 00070 00071 private: 00072 QWidget *mReceiver; 00073 }; 00074 00078 class EmailAddressSelectionWidget::Private 00079 { 00080 public: 00081 Private( EmailAddressSelectionWidget *qq, QAbstractItemModel *model ) 00082 : q( qq ), mModel( model ) 00083 { 00084 init(); 00085 } 00086 00087 void init(); 00088 00089 EmailAddressSelectionWidget *q; 00090 QAbstractItemModel *mModel; 00091 QLabel *mDescriptionLabel; 00092 SearchLineEdit *mSearchLine; 00093 // FIXME: Temporary until EntityTreeView compiles 00094 #ifndef Q_OS_WINCE 00095 Akonadi::EntityTreeView *mView; 00096 #else 00097 QTreeView* mView; 00098 #endif 00099 EmailAddressSelectionProxyModel *mSelectionModel; 00100 }; 00101 00102 void EmailAddressSelectionWidget::Private::init() 00103 { 00104 KGlobal::locale()->insertCatalog( QLatin1String( "akonadicontact" ) ); 00105 // setup internal model if needed 00106 if ( !mModel ) { 00107 Akonadi::Session *session = new Akonadi::Session( "InternalEmailAddressSelectionWidgetModel", q ); 00108 00109 Akonadi::ItemFetchScope scope; 00110 scope.fetchFullPayload( true ); 00111 scope.fetchAttribute<Akonadi::EntityDisplayAttribute>(); 00112 00113 Akonadi::ChangeRecorder *changeRecorder = new Akonadi::ChangeRecorder( q ); 00114 changeRecorder->setSession( session ); 00115 changeRecorder->fetchCollection( true ); 00116 changeRecorder->setItemFetchScope( scope ); 00117 changeRecorder->setCollectionMonitored( Akonadi::Collection::root() ); 00118 changeRecorder->setMimeTypeMonitored( KABC::Addressee::mimeType(), true ); 00119 changeRecorder->setMimeTypeMonitored( KABC::ContactGroup::mimeType(), true ); 00120 00121 Akonadi::ContactsTreeModel *model = new Akonadi::ContactsTreeModel( changeRecorder, q ); 00122 // model->setCollectionFetchStrategy( Akonadi::ContactsTreeModel::InvisibleFetch ); 00123 00124 mModel = model; 00125 } 00126 00127 // setup ui 00128 QVBoxLayout *layout = new QVBoxLayout( q ); 00129 00130 mDescriptionLabel = new QLabel; 00131 mDescriptionLabel->hide(); 00132 layout->addWidget( mDescriptionLabel ); 00133 00134 QHBoxLayout *searchLayout = new QHBoxLayout; 00135 layout->addLayout( searchLayout ); 00136 00137 // FIXME: Temporary until EntityTreeView compiles 00138 #ifndef Q_OS_WINCE 00139 mView = new Akonadi::EntityTreeView; 00140 #else 00141 mView = new QTreeView; 00142 #endif 00143 00144 QLabel *label = new QLabel( i18nc( "@label Search in a list of contacts", "Search:" ) ); 00145 mSearchLine = new SearchLineEdit( mView ); 00146 label->setBuddy( mSearchLine ); 00147 searchLayout->addWidget( label ); 00148 searchLayout->addWidget( mSearchLine ); 00149 00150 #ifndef QT_NO_DRAGANDDROP 00151 mView->setDragDropMode( QAbstractItemView::NoDragDrop ); 00152 #endif 00153 layout->addWidget( mView ); 00154 00155 Akonadi::ContactsFilterProxyModel *filter = new Akonadi::ContactsFilterProxyModel( q ); 00156 filter->setFilterFlags( ContactsFilterProxyModel::HasEmail ); 00157 filter->setExcludeVirtualCollections( true ); 00158 filter->setSourceModel( mModel ); 00159 00160 mSelectionModel = new EmailAddressSelectionProxyModel( q ); 00161 mSelectionModel->setSourceModel( filter ); 00162 00163 mView->setModel( mSelectionModel ); 00164 mView->header()->hide(); 00165 00166 q->connect( mSearchLine, SIGNAL(textChanged(QString)), 00167 filter, SLOT(setFilterString(QString)) ); 00168 00169 Control::widgetNeedsAkonadi( q ); 00170 00171 mSearchLine->setFocus(); 00172 00173 QTimer::singleShot( 1000, mView, SLOT(expandAll()) ); 00174 } 00175 00176 00177 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QWidget * parent ) 00178 : QWidget( parent ), 00179 d( new Private( this, 0 ) ) 00180 { 00181 } 00182 00183 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QAbstractItemModel *model, QWidget * parent ) 00184 : QWidget( parent ), 00185 d( new Private( this, model ) ) 00186 { 00187 } 00188 00189 EmailAddressSelectionWidget::~EmailAddressSelectionWidget() 00190 { 00191 delete d; 00192 } 00193 00194 EmailAddressSelection::List EmailAddressSelectionWidget::selectedAddresses() const 00195 { 00196 EmailAddressSelection::List selections; 00197 00198 if ( !d->mView->selectionModel() ) 00199 return selections; 00200 00201 const QModelIndexList selectedRows = d->mView->selectionModel()->selectedRows( 0 ); 00202 foreach ( const QModelIndex &index, selectedRows ) { 00203 EmailAddressSelection selection; 00204 selection.d->mName = index.data( EmailAddressSelectionProxyModel::NameRole ).toString(); 00205 selection.d->mEmailAddress = index.data( EmailAddressSelectionProxyModel::EmailAddressRole ).toString(); 00206 selection.d->mItem = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00207 00208 if ( !selection.d->mEmailAddress.isEmpty() ) 00209 selections << selection; 00210 } 00211 00212 return selections; 00213 } 00214 00215 KLineEdit* EmailAddressSelectionWidget::searchLineEdit() const 00216 { 00217 return d->mSearchLine; 00218 } 00219 00220 QTreeView* EmailAddressSelectionWidget::view() const 00221 { 00222 return d->mView; 00223 } 00224 00225 #include "emailaddressselectionwidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:15 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:15 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.