• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 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 <QtGui/QMouseEvent>
41 
42 using namespace Akonadi;
43 
44 class CollectionComboBox::Private
45 {
46  public:
47  Private( QAbstractItemModel *customModel, CollectionComboBox *parent )
48  : mParent( parent ), mMonitor( 0 ), mModel( 0 )
49  {
50  if ( customModel ) {
51  mBaseModel = customModel;
52  } else {
53  mMonitor = new Akonadi::ChangeRecorder( mParent );
54  mMonitor->fetchCollection( true );
55  mMonitor->setCollectionMonitored( Akonadi::Collection::root() );
56 
57  mModel = new EntityTreeModel( mMonitor, mParent );
58  mModel->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation );
59 
60  mBaseModel = mModel;
61  }
62 
63  KDescendantsProxyModel *proxyModel = new KDescendantsProxyModel( parent );
64  proxyModel->setDisplayAncestorData( true );
65  proxyModel->setSourceModel( mBaseModel );
66 
67  mMimeTypeFilterModel = new CollectionFilterProxyModel( parent );
68  mMimeTypeFilterModel->setSourceModel( proxyModel );
69 
70  mRightsFilterModel = new EntityRightsFilterModel( parent );
71  mRightsFilterModel->setSourceModel( mMimeTypeFilterModel );
72 
73  mParent->setModel( mRightsFilterModel );
74  mParent->model()->sort( mParent->modelColumn() );
75 
76  mSelectionHandler = new AsyncSelectionHandler( mRightsFilterModel, mParent );
77  mParent->connect( mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
78  mParent, SLOT(activated(QModelIndex)) );
79 
80  mParent->connect( mParent, SIGNAL(activated(int)),
81  mParent, SLOT(activated(int)) );
82  }
83 
84  ~Private()
85  {
86  }
87 
88  void activated( int index );
89  void activated( const QModelIndex& index );
90 
91  CollectionComboBox *mParent;
92 
93  ChangeRecorder *mMonitor;
94  EntityTreeModel *mModel;
95  QAbstractItemModel *mBaseModel;
96  CollectionFilterProxyModel *mMimeTypeFilterModel;
97  EntityRightsFilterModel *mRightsFilterModel;
98  AsyncSelectionHandler *mSelectionHandler;
99 };
100 
101 void CollectionComboBox::Private::activated( int index )
102 {
103  const QModelIndex modelIndex = mParent->model()->index( index, 0 );
104  if ( modelIndex.isValid() )
105  emit mParent->currentChanged( modelIndex.data( EntityTreeModel::CollectionRole).value<Collection>() );
106 }
107 
108 void CollectionComboBox::Private::activated( const QModelIndex &index )
109 {
110  mParent->setCurrentIndex( index.row() );
111 }
112 
113 MobileEventHandler::MobileEventHandler( CollectionComboBox *comboBox, CollectionFilterProxyModel *mimeTypeFilter,
114  EntityRightsFilterModel *accessRightsFilter, QAbstractItemModel *customModel )
115  : QObject( comboBox ),
116  mComboBox( comboBox ),
117  mMimeTypeFilter( mimeTypeFilter ),
118  mAccessRightsFilter( accessRightsFilter ),
119  mCustomModel( customModel )
120 {
121 }
122 
123 bool MobileEventHandler::eventFilter( QObject *object, QEvent *event )
124 {
125  if ( object == mComboBox && mComboBox->isEnabled() && event->type() == QEvent::MouseButtonPress ) {
126 
127  const QMouseEvent *mouseEvent = static_cast<QMouseEvent*>( event );
128 
129  // we receive mouse events from other widgets as well, so check for ours
130  if ( mComboBox->rect().contains( mouseEvent->pos() ) ) {
131  QMetaObject::invokeMethod( this, "openDialog", Qt::QueuedConnection );
132  }
133 
134  return true;
135  }
136 
137  return QObject::eventFilter( object, event );
138 }
139 
140 void MobileEventHandler::openDialog()
141 {
142  Akonadi::CollectionDialog dialog( mCustomModel );
143  dialog.setMimeTypeFilter( mMimeTypeFilter->mimeTypeFilters() );
144  dialog.setAccessRightsFilter( mAccessRightsFilter->accessRights() );
145 
146  if ( dialog.exec() ) {
147  const Akonadi::Collection collection = dialog.selectedCollection();
148  const QModelIndex index = Akonadi::EntityTreeModel::modelIndexForCollection( mComboBox->model(), collection );
149  mComboBox->setCurrentIndex( index.row() );
150  }
151 }
152 
153 
154 CollectionComboBox::CollectionComboBox( QWidget *parent )
155  : KComboBox( parent ), d( new Private( 0, this ) )
156 {
157 #ifdef KDEPIM_MOBILE_UI
158  MobileEventHandler *handler = new MobileEventHandler( this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel );
159  installEventFilter( handler );
160 #endif
161 }
162 
163 CollectionComboBox::CollectionComboBox( QAbstractItemModel *model, QWidget *parent )
164  : KComboBox( parent ), d( new Private( model, this ) )
165 {
166 #ifdef KDEPIM_MOBILE_UI
167  MobileEventHandler *handler = new MobileEventHandler( this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel );
168  installEventFilter( handler );
169 #endif
170 }
171 
172 CollectionComboBox::~CollectionComboBox()
173 {
174  delete d;
175 }
176 
177 void CollectionComboBox::setMimeTypeFilter( const QStringList &contentMimeTypes )
178 {
179  d->mMimeTypeFilterModel->clearFilters();
180  d->mMimeTypeFilterModel->addMimeTypeFilters( contentMimeTypes );
181 
182  if ( d->mMonitor )
183  foreach ( const QString &mimeType, contentMimeTypes )
184  d->mMonitor->setMimeTypeMonitored( mimeType, true );
185 }
186 
187 QStringList CollectionComboBox::mimeTypeFilter() const
188 {
189  return d->mMimeTypeFilterModel->mimeTypeFilters();
190 }
191 
192 void CollectionComboBox::setAccessRightsFilter( Collection::Rights rights )
193 {
194  d->mRightsFilterModel->setAccessRights( rights );
195 }
196 
197 Akonadi::Collection::Rights CollectionComboBox::accessRightsFilter() const
198 {
199  return d->mRightsFilterModel->accessRights();
200 }
201 
202 void CollectionComboBox::setDefaultCollection( const Collection &collection )
203 {
204  d->mSelectionHandler->waitForCollection( collection );
205 }
206 
207 Akonadi::Collection CollectionComboBox::currentCollection() const
208 {
209  const QModelIndex modelIndex = model()->index( currentIndex(), 0 );
210  if ( modelIndex.isValid() )
211  return modelIndex.data( Akonadi::EntityTreeModel::CollectionRole ).value<Collection>();
212  else
213  return Akonadi::Collection();
214 }
215 
216 #include "collectioncombobox.moc"
217 #include "collectioncombobox_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 4 2012 14:36:03 by doxygen 1.8.1.2 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.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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