22#include "collectioncombobox.h"
23#include "collectioncombobox_p.h"
25#include "asyncselectionhandler_p.h"
26#include "collectiondialog.h"
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>
35#include <kdescendantsproxymodel.h>
36#include "collectionutils_p.h"
38#include <QtCore/QAbstractItemModel>
39#include <QtCore/QEvent>
45class CollectionComboBox::Private
54 mBaseModel = customModel;
56 mMonitor =
new Akonadi::ChangeRecorder(mParent);
57 mMonitor->fetchCollection(
true);
60 mModel =
new EntityTreeModel(mMonitor, mParent);
66 KDescendantsProxyModel *proxyModel =
new KDescendantsProxyModel(parent);
67 proxyModel->setDisplayAncestorData(
true);
68 proxyModel->setSourceModel(mBaseModel);
70 mMimeTypeFilterModel =
new CollectionFilterProxyModel(parent);
71 mMimeTypeFilterModel->setSourceModel(proxyModel);
73 mRightsFilterModel =
new EntityRightsFilterModel(parent);
74 mRightsFilterModel->setSourceModel(mMimeTypeFilterModel);
76 mParent->setModel(mRightsFilterModel);
77 mParent->model()->sort(mParent->modelColumn());
79 mSelectionHandler =
new AsyncSelectionHandler(mRightsFilterModel, mParent);
80 mParent->connect(mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
81 mParent, SLOT(activated(QModelIndex)));
83 mParent->connect(mParent, SIGNAL(activated(
int)),
84 mParent, SLOT(activated(
int)));
91 void activated(
int index);
92 void activated(
const QModelIndex &index);
96 ChangeRecorder *mMonitor;
97 EntityTreeModel *mModel;
98 QAbstractItemModel *mBaseModel;
99 CollectionFilterProxyModel *mMimeTypeFilterModel;
100 EntityRightsFilterModel *mRightsFilterModel;
101 AsyncSelectionHandler *mSelectionHandler;
104void CollectionComboBox::Private::activated(
int index)
106 const QModelIndex modelIndex = mParent->model()->index(index, 0);
107 if (modelIndex.isValid()) {
112void CollectionComboBox::Private::activated(
const QModelIndex &index)
114 mParent->setCurrentIndex(index.row());
120 , mComboBox(comboBox)
123 , mCustomModel(customModel)
127bool MobileEventHandler::eventFilter(QObject *
object, QEvent *event)
129 if (
object == mComboBox && mComboBox->isEnabled() && event->type() == QEvent::MouseButtonPress) {
131 const QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>(event);
134 if (mComboBox->rect().contains(mouseEvent->pos())) {
135 QMetaObject::invokeMethod(
this,
"openDialog", Qt::QueuedConnection);
141 return QObject::eventFilter(
object, event);
144void MobileEventHandler::openDialog()
146 QPointer<Akonadi::CollectionDialog> dialog(
new Akonadi::CollectionDialog(mCustomModel));
147 dialog->setMimeTypeFilter(mMimeTypeFilter->mimeTypeFilters());
148 dialog->setAccessRightsFilter(mAccessRightsFilter->accessRights());
150 if (dialog->exec() == QDialog::Accepted && dialog != 0) {
151 const Akonadi::Collection collection = dialog->selectedCollection();
153 mComboBox->setCurrentIndex(index.row());
160 , d(new Private(0, this))
162#ifdef KDEPIM_MOBILE_UI
163 MobileEventHandler *handler =
new MobileEventHandler(
this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel);
164 installEventFilter(handler);
170 , d(new Private(model, this))
172#ifdef KDEPIM_MOBILE_UI
173 MobileEventHandler *handler =
new MobileEventHandler(
this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel);
174 installEventFilter(handler);
185 d->mMimeTypeFilterModel->clearFilters();
186 d->mMimeTypeFilterModel->addMimeTypeFilters(contentMimeTypes);
189 foreach (
const QString &mimeType, contentMimeTypes) {
190 d->mMonitor->setMimeTypeMonitored(mimeType,
true);
197 return d->mMimeTypeFilterModel->mimeTypeFilters();
202 d->mRightsFilterModel->setAccessRights(rights);
207 return d->mRightsFilterModel->accessRights();
212 d->mSelectionHandler->waitForCollection(collection);
217 const QModelIndex modelIndex = model()->index(currentIndex(), 0);
218 if (modelIndex.isValid()) {
227 d->mMimeTypeFilterModel->setExcludeVirtualCollections(b);
232 return d->mMimeTypeFilterModel->excludeVirtualCollections();
235#include "moc_collectioncombobox.cpp"
236#include "moc_collectioncombobox_p.cpp"
CollectionComboBox(QWidget *parent=0)
Creates a new collection combobox.
void setMimeTypeFilter(const QStringList &mimetypes)
Sets the content mimetypes the collections shall be filtered by.
bool excludeVirtualCollections() const
QStringList mimeTypeFilter() const
Returns the content mimetype the collections are filtered by.
Collection::Rights accessRightsFilter() const
Returns the access rights the collections are filtered by.
void setAccessRightsFilter(Collection::Rights rights)
Sets the access rights the collections shall be filtered by.
Akonadi::Collection currentCollection() const
Returns the current selection.
void setExcludeVirtualCollections(bool b)
~CollectionComboBox()
Destroys the collection combobox.
void setDefaultCollection(const Collection &collection)
Sets the collection that shall be selected by default.
Represents a collection of PIM items.
static Collection root()
Returns the root collection.
@ NoItemPopulation
Do not include items in the model.
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
Returns a QModelIndex in model which points to collection.
@ CollectionRole
The collection.
FreeBusyManager::Singleton.