23 #include "emailaddressselectionwidget.h"
25 #include "emailaddressselection_p.h"
26 #include "emailaddressselectionproxymodel_p.h"
28 #include <akonadi/changerecorder.h>
29 #include <akonadi/contact/contactsfilterproxymodel.h>
30 #include <akonadi/contact/contactstreemodel.h>
31 #include <akonadi/control.h>
32 #include <akonadi/entitydisplayattribute.h>
33 #include <akonadi/entitytreeview.h>
34 #include <akonadi/itemfetchscope.h>
35 #include <akonadi/session.h>
36 #include <kabc/addressee.h>
37 #include <kabc/contactgroup.h>
38 #include <klineedit.h>
42 #include <QtCore/QTimer>
43 #include <QtGui/QHBoxLayout>
44 #include <QtGui/QHeaderView>
45 #include <QtGui/QKeyEvent>
46 #include <QtGui/QLabel>
47 #include <QtGui/QVBoxLayout>
49 using namespace Akonadi;
54 class SearchLineEdit :
public KLineEdit
57 SearchLineEdit( QWidget *receiver, QWidget *parent = 0 )
58 : KLineEdit( parent ), mReceiver( receiver )
63 virtual void keyPressEvent( QKeyEvent *event )
65 if ( event->key() == Qt::Key_Down )
66 QMetaObject::invokeMethod( mReceiver,
"setFocus" );
68 KLineEdit::keyPressEvent( event );
78 class EmailAddressSelectionWidget::Private
82 : q( qq ), mModel( model )
90 QAbstractItemModel *mModel;
91 QLabel *mDescriptionLabel;
92 SearchLineEdit *mSearchLine;
99 EmailAddressSelectionProxyModel *mSelectionModel;
102 void EmailAddressSelectionWidget::Private::init()
104 KGlobal::locale()->insertCatalog( QLatin1String(
"akonadicontact" ) );
114 changeRecorder->setSession( session );
115 changeRecorder->fetchCollection(
true );
116 changeRecorder->setItemFetchScope( scope );
118 changeRecorder->setMimeTypeMonitored( KABC::Addressee::mimeType(),
true );
119 changeRecorder->setMimeTypeMonitored( KABC::ContactGroup::mimeType(),
true );
128 QVBoxLayout *layout =
new QVBoxLayout( q );
130 mDescriptionLabel =
new QLabel;
131 mDescriptionLabel->hide();
132 layout->addWidget( mDescriptionLabel );
134 QHBoxLayout *searchLayout =
new QHBoxLayout;
135 layout->addLayout( searchLayout );
141 mView =
new QTreeView;
144 QLabel *label =
new QLabel( i18nc(
"@label Search in a list of contacts",
"Search:" ) );
145 mSearchLine =
new SearchLineEdit( mView );
146 label->setBuddy( mSearchLine );
147 searchLayout->addWidget( label );
148 searchLayout->addWidget( mSearchLine );
150 #ifndef QT_NO_DRAGANDDROP
151 mView->setDragDropMode( QAbstractItemView::NoDragDrop );
153 layout->addWidget( mView );
158 filter->setSourceModel( mModel );
160 mSelectionModel =
new EmailAddressSelectionProxyModel( q );
161 mSelectionModel->setSourceModel( filter );
163 mView->setModel( mSelectionModel );
164 mView->header()->hide();
166 q->connect( mSearchLine, SIGNAL(textChanged(QString)),
167 filter, SLOT(setFilterString(QString)) );
171 mSearchLine->setFocus();
173 QTimer::singleShot( 1000, mView, SLOT(expandAll()) );
179 d( new Private( this, 0 ) )
185 d( new Private( this, model ) )
198 if ( !d->mView->selectionModel() )
201 const QModelIndexList selectedRows = d->mView->selectionModel()->selectedRows( 0 );
202 foreach (
const QModelIndex &index, selectedRows ) {
204 selection.d->mName = index.data( EmailAddressSelectionProxyModel::NameRole ).toString();
205 selection.d->mEmailAddress = index.data( EmailAddressSelectionProxyModel::EmailAddressRole ).toString();
208 if ( !selection.d->mEmailAddress.isEmpty() )
209 selections << selection;
217 return d->mSearchLine;
225 #include "emailaddressselectionwidget.moc"