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>
40 #include <klocalizedstring.h>
43 #include <QtCore/QTimer>
44 #include <QHBoxLayout>
45 #include <QHeaderView>
48 #include <QVBoxLayout>
50 using namespace Akonadi;
55 class SearchLineEdit :
public KLineEdit
58 SearchLineEdit( QWidget *receiver, QWidget *parent = 0 )
59 : KLineEdit( parent ), mReceiver( receiver )
64 virtual void keyPressEvent( QKeyEvent *event )
66 if ( event->key() == Qt::Key_Down ) {
67 QMetaObject::invokeMethod( mReceiver,
"setFocus" );
70 KLineEdit::keyPressEvent( event );
80 class EmailAddressSelectionWidget::Private
84 : q( qq ), mModel( model )
92 QAbstractItemModel *mModel;
93 QLabel *mDescriptionLabel;
94 SearchLineEdit *mSearchLine;
97 Akonadi::EntityTreeView *mView;
101 EmailAddressSelectionProxyModel *mSelectionModel;
104 void EmailAddressSelectionWidget::Private::init()
106 KGlobal::locale()->insertCatalog( QLatin1String(
"akonadicontact" ) );
109 Akonadi::Session *session =
new Akonadi::Session(
"InternalEmailAddressSelectionWidgetModel", q );
111 Akonadi::ItemFetchScope scope;
112 scope.fetchFullPayload(
true );
113 scope.fetchAttribute<Akonadi::EntityDisplayAttribute>();
115 Akonadi::ChangeRecorder *changeRecorder =
new Akonadi::ChangeRecorder( q );
116 changeRecorder->setSession( session );
117 changeRecorder->fetchCollection(
true );
118 changeRecorder->setItemFetchScope( scope );
119 changeRecorder->setCollectionMonitored( Akonadi::Collection::root() );
120 changeRecorder->setMimeTypeMonitored( KABC::Addressee::mimeType(),
true );
121 changeRecorder->setMimeTypeMonitored( KABC::ContactGroup::mimeType(),
true );
130 QVBoxLayout *layout =
new QVBoxLayout( q );
132 mDescriptionLabel =
new QLabel;
133 mDescriptionLabel->hide();
134 layout->addWidget( mDescriptionLabel );
136 QHBoxLayout *searchLayout =
new QHBoxLayout;
137 layout->addLayout( searchLayout );
141 mView =
new Akonadi::EntityTreeView;
143 mView =
new QTreeView;
146 QLabel *label =
new QLabel( i18nc(
"@label Search in a list of contacts",
"Search:" ) );
147 mSearchLine =
new SearchLineEdit( mView );
148 label->setBuddy( mSearchLine );
149 searchLayout->addWidget( label );
150 searchLayout->addWidget( mSearchLine );
152 #ifndef QT_NO_DRAGANDDROP
153 mView->setDragDropMode( QAbstractItemView::NoDragDrop );
155 layout->addWidget( mView );
160 filter->setSourceModel( mModel );
162 mSelectionModel =
new EmailAddressSelectionProxyModel( q );
163 mSelectionModel->setSourceModel( filter );
165 mView->setModel( mSelectionModel );
166 mView->header()->hide();
168 q->connect( mSearchLine, SIGNAL(textChanged(QString)),
169 filter, SLOT(setFilterString(QString)) );
173 q->connect( mView, SIGNAL(doubleClicked(Akonadi::Item)),
174 q, SIGNAL(doubleClicked()));
176 Control::widgetNeedsAkonadi( q );
178 mSearchLine->setFocus();
180 QTimer::singleShot( 1000, mView, SLOT(expandAll()) );
186 d( new Private( this, 0 ) )
192 d( new Private( this, model ) )
205 if ( !d->mView->selectionModel() ) {
209 const QModelIndexList selectedRows = d->mView->selectionModel()->selectedRows( 0 );
210 foreach (
const QModelIndex &index, selectedRows ) {
212 selection.d->mName = index.data( EmailAddressSelectionProxyModel::NameRole ).toString();
213 selection.d->mEmailAddress = index.data( EmailAddressSelectionProxyModel::EmailAddressRole ).toString();
214 selection.d->mItem = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
216 if ( !selection.d->mEmailAddress.isEmpty() ) {
217 selections << selection;
226 return d->mSearchLine;