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

akonadi

  • sources
  • kde-4.14
  • kdepimlibs
  • 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)
50  , mMonitor(0)
51  , mModel(0)
52  {
53  if (customModel) {
54  mBaseModel = customModel;
55  } else {
56  mMonitor = new Akonadi::ChangeRecorder(mParent);
57  mMonitor->fetchCollection(true);
58  mMonitor->setCollectionMonitored(Akonadi::Collection::root());
59 
60  mModel = new EntityTreeModel(mMonitor, mParent);
61  mModel->setItemPopulationStrategy(EntityTreeModel::NoItemPopulation);
62 
63  mBaseModel = mModel;
64  }
65 
66  KDescendantsProxyModel *proxyModel = new KDescendantsProxyModel(parent);
67  proxyModel->setDisplayAncestorData(true);
68  proxyModel->setSourceModel(mBaseModel);
69 
70  mMimeTypeFilterModel = new CollectionFilterProxyModel(parent);
71  mMimeTypeFilterModel->setSourceModel(proxyModel);
72 
73  mRightsFilterModel = new EntityRightsFilterModel(parent);
74  mRightsFilterModel->setSourceModel(mMimeTypeFilterModel);
75 
76  mParent->setModel(mRightsFilterModel);
77  mParent->model()->sort(mParent->modelColumn());
78 
79  mSelectionHandler = new AsyncSelectionHandler(mRightsFilterModel, mParent);
80  mParent->connect(mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
81  mParent, SLOT(activated(QModelIndex)));
82 
83  mParent->connect(mParent, SIGNAL(activated(int)),
84  mParent, SLOT(activated(int)));
85  }
86 
87  ~Private()
88  {
89  }
90 
91  void activated(int index);
92  void activated(const QModelIndex &index);
93 
94  CollectionComboBox *mParent;
95 
96  ChangeRecorder *mMonitor;
97  EntityTreeModel *mModel;
98  QAbstractItemModel *mBaseModel;
99  CollectionFilterProxyModel *mMimeTypeFilterModel;
100  EntityRightsFilterModel *mRightsFilterModel;
101  AsyncSelectionHandler *mSelectionHandler;
102 };
103 
104 void CollectionComboBox::Private::activated(int index)
105 {
106  const QModelIndex modelIndex = mParent->model()->index(index, 0);
107  if (modelIndex.isValid()) {
108  emit mParent->currentChanged(modelIndex.data(EntityTreeModel::CollectionRole).value<Collection>());
109  }
110 }
111 
112 void CollectionComboBox::Private::activated(const QModelIndex &index)
113 {
114  mParent->setCurrentIndex(index.row());
115 }
116 
117 MobileEventHandler::MobileEventHandler(CollectionComboBox *comboBox, CollectionFilterProxyModel *mimeTypeFilter,
118  EntityRightsFilterModel *accessRightsFilter, QAbstractItemModel *customModel)
119  : QObject(comboBox)
120  , mComboBox(comboBox)
121  , mMimeTypeFilter(mimeTypeFilter)
122  , mAccessRightsFilter(accessRightsFilter)
123  , mCustomModel(customModel)
124 {
125 }
126 
127 bool MobileEventHandler::eventFilter(QObject *object, QEvent *event)
128 {
129  if (object == mComboBox && mComboBox->isEnabled() && event->type() == QEvent::MouseButtonPress) {
130 
131  const QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
132 
133  // we receive mouse events from other widgets as well, so check for ours
134  if (mComboBox->rect().contains(mouseEvent->pos())) {
135  QMetaObject::invokeMethod(this, "openDialog", Qt::QueuedConnection);
136  }
137 
138  return true;
139  }
140 
141  return QObject::eventFilter(object, event);
142 }
143 
144 void MobileEventHandler::openDialog()
145 {
146  QPointer<Akonadi::CollectionDialog> dialog(new Akonadi::CollectionDialog(mCustomModel));
147  dialog->setMimeTypeFilter(mMimeTypeFilter->mimeTypeFilters());
148  dialog->setAccessRightsFilter(mAccessRightsFilter->accessRights());
149 
150  if (dialog->exec() == QDialog::Accepted && dialog != 0) {
151  const Akonadi::Collection collection = dialog->selectedCollection();
152  const QModelIndex index = Akonadi::EntityTreeModel::modelIndexForCollection(mComboBox->model(), collection);
153  mComboBox->setCurrentIndex(index.row());
154  }
155  delete dialog;
156 }
157 
158 CollectionComboBox::CollectionComboBox(QWidget *parent)
159  : KComboBox(parent)
160  , d(new Private(0, this))
161 {
162 #ifdef KDEPIM_MOBILE_UI
163  MobileEventHandler *handler = new MobileEventHandler(this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel);
164  installEventFilter(handler);
165 #endif
166 }
167 
168 CollectionComboBox::CollectionComboBox(QAbstractItemModel *model, QWidget *parent)
169  : KComboBox(parent)
170  , d(new Private(model, this))
171 {
172 #ifdef KDEPIM_MOBILE_UI
173  MobileEventHandler *handler = new MobileEventHandler(this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel);
174  installEventFilter(handler);
175 #endif
176 }
177 
178 CollectionComboBox::~CollectionComboBox()
179 {
180  delete d;
181 }
182 
183 void CollectionComboBox::setMimeTypeFilter(const QStringList &contentMimeTypes)
184 {
185  d->mMimeTypeFilterModel->clearFilters();
186  d->mMimeTypeFilterModel->addMimeTypeFilters(contentMimeTypes);
187 
188  if (d->mMonitor) {
189  foreach (const QString &mimeType, contentMimeTypes) {
190  d->mMonitor->setMimeTypeMonitored(mimeType, true);
191  }
192  }
193 }
194 
195 QStringList CollectionComboBox::mimeTypeFilter() const
196 {
197  return d->mMimeTypeFilterModel->mimeTypeFilters();
198 }
199 
200 void CollectionComboBox::setAccessRightsFilter(Collection::Rights rights)
201 {
202  d->mRightsFilterModel->setAccessRights(rights);
203 }
204 
205 Akonadi::Collection::Rights CollectionComboBox::accessRightsFilter() const
206 {
207  return d->mRightsFilterModel->accessRights();
208 }
209 
210 void CollectionComboBox::setDefaultCollection(const Collection &collection)
211 {
212  d->mSelectionHandler->waitForCollection(collection);
213 }
214 
215 Akonadi::Collection CollectionComboBox::currentCollection() const
216 {
217  const QModelIndex modelIndex = model()->index(currentIndex(), 0);
218  if (modelIndex.isValid()) {
219  return modelIndex.data(Akonadi::EntityTreeModel::CollectionRole).value<Collection>();
220  } else {
221  return Akonadi::Collection();
222  }
223 }
224 
225 void CollectionComboBox::setExcludeVirtualCollections(bool b)
226 {
227  d->mMimeTypeFilterModel->setExcludeVirtualCollections(b);
228 }
229 
230 bool CollectionComboBox::excludeVirtualCollections() const
231 {
232  return d->mMimeTypeFilterModel->excludeVirtualCollections();
233 }
234 
235 #include "moc_collectioncombobox.cpp"
236 #include "moc_collectioncombobox_p.cpp"
Akonadi::CollectionComboBox::CollectionComboBox
CollectionComboBox(QWidget *parent=0)
Creates a new collection combobox.
Definition: collectioncombobox.cpp:158
QModelIndex
QEvent
QWidget
QAbstractItemModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
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::CollectionComboBox::setExcludeVirtualCollections
void setExcludeVirtualCollections(bool b)
Definition: collectioncombobox.cpp:225
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:409
QPointer
QVariant::value
T value() const
QMouseEvent
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:210
Akonadi::CollectionComboBox::accessRightsFilter
Collection::Rights accessRightsFilter() const
Returns the access rights the collections are filtered by.
Definition: collectioncombobox.cpp:205
Akonadi::CollectionComboBox::currentCollection
Akonadi::Collection currentCollection() const
Returns the current selection.
Definition: collectioncombobox.cpp:215
QModelIndex::isValid
bool isValid() const
Akonadi::CollectionComboBox::~CollectionComboBox
~CollectionComboBox()
Destroys the collection combobox.
Definition: collectioncombobox.cpp:178
Akonadi::CollectionComboBox::excludeVirtualCollections
bool excludeVirtualCollections() const
Definition: collectioncombobox.cpp:230
QObject
QModelIndex::row
int row() const
Akonadi::Collection::root
static Collection root()
Returns the root collection.
Definition: collection.cpp:192
QObject::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
Akonadi::EntityTreeModel::CollectionRole
The collection.
Definition: entitytreemodel.h:336
QString
QStringList
Akonadi::EntityTreeModel::modelIndexForCollection
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
Returns a QModelIndex in model which points to collection.
Definition: entitytreemodel.cpp:1238
Akonadi::CollectionComboBox::mimeTypeFilter
QStringList mimeTypeFilter() const
Returns the content mimetype the collections are filtered by.
Definition: collectioncombobox.cpp:195
QMetaObject::invokeMethod
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
QModelIndex::model
const QAbstractItemModel * model() const
QModelIndex::data
QVariant data(int role) const
Akonadi::CollectionComboBox::setAccessRightsFilter
void setAccessRightsFilter(Collection::Rights rights)
Sets the access rights the collections shall be filtered by.
Definition: collectioncombobox.cpp:200
Akonadi::EntityTreeModel
A model for collections and items together.
Definition: entitytreemodel.h:318
QAbstractItemModel
Akonadi::CollectionComboBox::setMimeTypeFilter
void setMimeTypeFilter(const QStringList &mimetypes)
Sets the content mimetypes the collections shall be filtered by.
Definition: collectioncombobox.cpp:183
QMouseEvent::pos
const QPoint & pos() const
Akonadi::ChangeRecorder
Records and replays change notification.
Definition: changerecorder.h:47
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:38:02 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs 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

Search



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