• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.3 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
collectioncombobox.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2007-2009 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "collectioncombobox.h"
23 #include "collectioncombobox_p.h"
24 
25 #include "asyncselectionhandler_p.h"
26 #include "collectiondialog.h"
27 
28 #include <akonadi/changerecorder.h>
29 #include <akonadi/collectionfetchscope.h>
30 #include <akonadi/collectionfilterproxymodel.h>
31 #include <akonadi/entityrightsfiltermodel.h>
32 #include <akonadi/entitytreemodel.h>
33 #include <akonadi/session.h>
34 
35 #include <kdescendantsproxymodel.h>
36 #include "collectionutils_p.h"
37 
38 #include <QtCore/QAbstractItemModel>
39 #include <QtCore/QEvent>
40 #include <QPointer>
41 #include <QMouseEvent>
42 
43 using namespace Akonadi;
44 
45 class CollectionComboBox::Private
46 {
47  public:
48  Private( QAbstractItemModel *customModel, CollectionComboBox *parent )
49  : mParent( parent ), mMonitor( 0 ), mModel( 0 )
50  {
51  if ( customModel ) {
52  mBaseModel = customModel;
53  } else {
54  mMonitor = new Akonadi::ChangeRecorder( mParent );
55  mMonitor->fetchCollection( true );
56  mMonitor->setCollectionMonitored( Akonadi::Collection::root() );
57 
58  mModel = new EntityTreeModel( mMonitor, mParent );
59  mModel->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation );
60 
61  mBaseModel = mModel;
62  }
63 
64  KDescendantsProxyModel *proxyModel = new KDescendantsProxyModel( parent );
65  proxyModel->setDisplayAncestorData( true );
66  proxyModel->setSourceModel( mBaseModel );
67 
68  mMimeTypeFilterModel = new CollectionFilterProxyModel( parent );
69  mMimeTypeFilterModel->setSourceModel( proxyModel );
70 
71  mRightsFilterModel = new EntityRightsFilterModel( parent );
72  mRightsFilterModel->setSourceModel( mMimeTypeFilterModel );
73 
74  mParent->setModel( mRightsFilterModel );
75  mParent->model()->sort( mParent->modelColumn() );
76 
77  mSelectionHandler = new AsyncSelectionHandler( mRightsFilterModel, mParent );
78  mParent->connect( mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
79  mParent, SLOT(activated(QModelIndex)) );
80 
81  mParent->connect( mParent, SIGNAL(activated(int)),
82  mParent, SLOT(activated(int)) );
83  }
84 
85  ~Private()
86  {
87  }
88 
89  void activated( int index );
90  void activated( const QModelIndex& index );
91 
92  CollectionComboBox *mParent;
93 
94  ChangeRecorder *mMonitor;
95  EntityTreeModel *mModel;
96  QAbstractItemModel *mBaseModel;
97  CollectionFilterProxyModel *mMimeTypeFilterModel;
98  EntityRightsFilterModel *mRightsFilterModel;
99  AsyncSelectionHandler *mSelectionHandler;
100 };
101 
102 void CollectionComboBox::Private::activated( int index )
103 {
104  const QModelIndex modelIndex = mParent->model()->index( index, 0 );
105  if ( modelIndex.isValid() ) {
106  emit mParent->currentChanged( modelIndex.data( EntityTreeModel::CollectionRole ).value<Collection>() );
107  }
108 }
109 
110 void CollectionComboBox::Private::activated( const QModelIndex &index )
111 {
112  mParent->setCurrentIndex( index.row() );
113 }
114 
115 MobileEventHandler::MobileEventHandler( CollectionComboBox *comboBox, CollectionFilterProxyModel *mimeTypeFilter,
116  EntityRightsFilterModel *accessRightsFilter, QAbstractItemModel *customModel )
117  : QObject( comboBox ),
118  mComboBox( comboBox ),
119  mMimeTypeFilter( mimeTypeFilter ),
120  mAccessRightsFilter( accessRightsFilter ),
121  mCustomModel( customModel )
122 {
123 }
124 
125 bool MobileEventHandler::eventFilter( QObject *object, QEvent *event )
126 {
127  if ( object == mComboBox && mComboBox->isEnabled() && event->type() == QEvent::MouseButtonPress ) {
128 
129  const QMouseEvent *mouseEvent = static_cast<QMouseEvent*>( event );
130 
131  // we receive mouse events from other widgets as well, so check for ours
132  if ( mComboBox->rect().contains( mouseEvent->pos() ) ) {
133  QMetaObject::invokeMethod( this, "openDialog", Qt::QueuedConnection );
134  }
135 
136  return true;
137  }
138 
139  return QObject::eventFilter( object, event );
140 }
141 
142 void MobileEventHandler::openDialog()
143 {
144  QPointer<Akonadi::CollectionDialog> dialog( new Akonadi::CollectionDialog( mCustomModel ) );
145  dialog->setMimeTypeFilter( mMimeTypeFilter->mimeTypeFilters() );
146  dialog->setAccessRightsFilter( mAccessRightsFilter->accessRights() );
147 
148  if ( dialog->exec() == QDialog::Accepted && dialog != 0 ) {
149  const Akonadi::Collection collection = dialog->selectedCollection();
150  const QModelIndex index = Akonadi::EntityTreeModel::modelIndexForCollection( mComboBox->model(), collection );
151  mComboBox->setCurrentIndex( index.row() );
152  }
153  delete dialog;
154 }
155 
156 
157 CollectionComboBox::CollectionComboBox( QWidget *parent )
158  : KComboBox( parent ), d( new Private( 0, this ) )
159 {
160 #ifdef KDEPIM_MOBILE_UI
161  MobileEventHandler *handler = new MobileEventHandler( this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel );
162  installEventFilter( handler );
163 #endif
164 }
165 
166 CollectionComboBox::CollectionComboBox( QAbstractItemModel *model, QWidget *parent )
167  : KComboBox( parent ), d( new Private( model, this ) )
168 {
169 #ifdef KDEPIM_MOBILE_UI
170  MobileEventHandler *handler = new MobileEventHandler( this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel );
171  installEventFilter( handler );
172 #endif
173 }
174 
175 CollectionComboBox::~CollectionComboBox()
176 {
177  delete d;
178 }
179 
180 void CollectionComboBox::setMimeTypeFilter( const QStringList &contentMimeTypes )
181 {
182  d->mMimeTypeFilterModel->clearFilters();
183  d->mMimeTypeFilterModel->addMimeTypeFilters( contentMimeTypes );
184 
185  if ( d->mMonitor ) {
186  foreach ( const QString &mimeType, contentMimeTypes ) {
187  d->mMonitor->setMimeTypeMonitored( mimeType, true );
188  }
189  }
190 }
191 
192 QStringList CollectionComboBox::mimeTypeFilter() const
193 {
194  return d->mMimeTypeFilterModel->mimeTypeFilters();
195 }
196 
197 void CollectionComboBox::setAccessRightsFilter( Collection::Rights rights )
198 {
199  d->mRightsFilterModel->setAccessRights( rights );
200 }
201 
202 Akonadi::Collection::Rights CollectionComboBox::accessRightsFilter() const
203 {
204  return d->mRightsFilterModel->accessRights();
205 }
206 
207 void CollectionComboBox::setDefaultCollection( const Collection &collection )
208 {
209  d->mSelectionHandler->waitForCollection( collection );
210 }
211 
212 Akonadi::Collection CollectionComboBox::currentCollection() const
213 {
214  const QModelIndex modelIndex = model()->index( currentIndex(), 0 );
215  if ( modelIndex.isValid() ) {
216  return modelIndex.data( Akonadi::EntityTreeModel::CollectionRole ).value<Collection>();
217  } else {
218  return Akonadi::Collection();
219  }
220 }
221 
222 #include "moc_collectioncombobox.cpp"
223 #include "moc_collectioncombobox_p.cpp"
Akonadi::CollectionComboBox::CollectionComboBox
CollectionComboBox(QWidget *parent=0)
Creates a new collection combobox.
Definition: collectioncombobox.cpp:157
Akonadi::CollectionFilterProxyModel
A proxy model that filters collections by mime type.
Definition: collectionfilterproxymodel.h:54
Akonadi::AsyncSelectionHandler
Definition: asyncselectionhandler_p.h:42
Akonadi::CollectionDialog
A collection selection dialog.
Definition: collectiondialog.h:67
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::EntityTreeModel::NoItemPopulation
Do not include items in the model.
Definition: entitytreemodel.h:407
Akonadi::EntityRightsFilterModel
A proxy model that filters entities by access rights.
Definition: entityrightsfiltermodel.h:60
Akonadi::CollectionComboBox
A combobox for selecting an Akonadi collection.
Definition: collectioncombobox.h:62
Akonadi::CollectionComboBox::setDefaultCollection
void setDefaultCollection(const Collection &collection)
Sets the collection that shall be selected by default.
Definition: collectioncombobox.cpp:207
Akonadi::CollectionComboBox::accessRightsFilter
Collection::Rights accessRightsFilter() const
Returns the access rights the collections are filtered by.
Definition: collectioncombobox.cpp:202
Akonadi::CollectionComboBox::currentCollection
Akonadi::Collection currentCollection() const
Returns the current selection.
Definition: collectioncombobox.cpp:212
Akonadi::CollectionComboBox::~CollectionComboBox
~CollectionComboBox()
Destroys the collection combobox.
Definition: collectioncombobox.cpp:175
Akonadi::Collection::root
static Collection root()
Returns the root collection.
Definition: collection.cpp:192
Akonadi::EntityTreeModel::CollectionRole
The collection.
Definition: entitytreemodel.h:335
Akonadi::EntityTreeModel::modelIndexForCollection
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
Returns a QModelIndex in model which points to collection.
Definition: entitytreemodel.cpp:1191
Akonadi::CollectionComboBox::mimeTypeFilter
QStringList mimeTypeFilter() const
Returns the content mimetype the collections are filtered by.
Definition: collectioncombobox.cpp:192
Akonadi::CollectionComboBox::setAccessRightsFilter
void setAccessRightsFilter(Collection::Rights rights)
Sets the access rights the collections shall be filtered by.
Definition: collectioncombobox.cpp:197
Akonadi::EntityTreeModel
A model for collections and items together.
Definition: entitytreemodel.h:317
Akonadi::CollectionComboBox::setMimeTypeFilter
void setMimeTypeFilter(const QStringList &mimetypes)
Sets the content mimetypes the collections shall be filtered by.
Definition: collectioncombobox.cpp:180
Akonadi::ChangeRecorder
Records and replays change notification.
Definition: changerecorder.h:47
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:16 by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal