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

akonadi

  • sources
  • kde-4.12
  • 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 ), 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 CollectionComboBox::CollectionComboBox( QWidget *parent )
157  : KComboBox( parent ), d( new Private( 0, this ) )
158 {
159 #ifdef KDEPIM_MOBILE_UI
160  MobileEventHandler *handler = new MobileEventHandler( this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel );
161  installEventFilter( handler );
162 #endif
163 }
164 
165 CollectionComboBox::CollectionComboBox( QAbstractItemModel *model, QWidget *parent )
166  : KComboBox( parent ), d( new Private( model, this ) )
167 {
168 #ifdef KDEPIM_MOBILE_UI
169  MobileEventHandler *handler = new MobileEventHandler( this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel );
170  installEventFilter( handler );
171 #endif
172 }
173 
174 CollectionComboBox::~CollectionComboBox()
175 {
176  delete d;
177 }
178 
179 void CollectionComboBox::setMimeTypeFilter( const QStringList &contentMimeTypes )
180 {
181  d->mMimeTypeFilterModel->clearFilters();
182  d->mMimeTypeFilterModel->addMimeTypeFilters( contentMimeTypes );
183 
184  if ( d->mMonitor ) {
185  foreach ( const QString &mimeType, contentMimeTypes ) {
186  d->mMonitor->setMimeTypeMonitored( mimeType, true );
187  }
188  }
189 }
190 
191 QStringList CollectionComboBox::mimeTypeFilter() const
192 {
193  return d->mMimeTypeFilterModel->mimeTypeFilters();
194 }
195 
196 void CollectionComboBox::setAccessRightsFilter( Collection::Rights rights )
197 {
198  d->mRightsFilterModel->setAccessRights( rights );
199 }
200 
201 Akonadi::Collection::Rights CollectionComboBox::accessRightsFilter() const
202 {
203  return d->mRightsFilterModel->accessRights();
204 }
205 
206 void CollectionComboBox::setDefaultCollection( const Collection &collection )
207 {
208  d->mSelectionHandler->waitForCollection( collection );
209 }
210 
211 Akonadi::Collection CollectionComboBox::currentCollection() const
212 {
213  const QModelIndex modelIndex = model()->index( currentIndex(), 0 );
214  if ( modelIndex.isValid() ) {
215  return modelIndex.data( Akonadi::EntityTreeModel::CollectionRole ).value<Collection>();
216  } else {
217  return Akonadi::Collection();
218  }
219 }
220 
221 void CollectionComboBox::setExcludeVirtualCollections(bool b)
222 {
223  d->mMimeTypeFilterModel->setExcludeVirtualCollections(b);
224 }
225 
226 bool CollectionComboBox::excludeVirtualCollections() const
227 {
228  return d->mMimeTypeFilterModel->excludeVirtualCollections();
229 }
230 
231 
232 #include "moc_collectioncombobox.cpp"
233 #include "moc_collectioncombobox_p.cpp"
Akonadi::CollectionComboBox::CollectionComboBox
CollectionComboBox(QWidget *parent=0)
Creates a new collection combobox.
Definition: collectioncombobox.cpp:156
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:221
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:206
Akonadi::CollectionComboBox::accessRightsFilter
Collection::Rights accessRightsFilter() const
Returns the access rights the collections are filtered by.
Definition: collectioncombobox.cpp:201
Akonadi::CollectionComboBox::currentCollection
Akonadi::Collection currentCollection() const
Returns the current selection.
Definition: collectioncombobox.cpp:211
Akonadi::CollectionComboBox::~CollectionComboBox
~CollectionComboBox()
Destroys the collection combobox.
Definition: collectioncombobox.cpp:174
Akonadi::CollectionComboBox::excludeVirtualCollections
bool excludeVirtualCollections() const
Definition: collectioncombobox.cpp:226
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:1192
Akonadi::CollectionComboBox::mimeTypeFilter
QStringList mimeTypeFilter() const
Returns the content mimetype the collections are filtered by.
Definition: collectioncombobox.cpp:191
Akonadi::CollectionComboBox::setAccessRightsFilter
void setAccessRightsFilter(Collection::Rights rights)
Sets the access rights the collections shall be filtered by.
Definition: collectioncombobox.cpp:196
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:179
Akonadi::ChangeRecorder
Records and replays change notification.
Definition: changerecorder.h:47
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:26 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
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

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