• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 <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 
109 void CollectionComboBox::Private::activated( const QModelIndex &index )
110 {
111  mParent->setCurrentIndex( index.row() );
112 }
113 
114 MobileEventHandler::MobileEventHandler( CollectionComboBox *comboBox, CollectionFilterProxyModel *mimeTypeFilter,
115  EntityRightsFilterModel *accessRightsFilter, QAbstractItemModel *customModel )
116  : QObject( comboBox ),
117  mComboBox( comboBox ),
118  mMimeTypeFilter( mimeTypeFilter ),
119  mAccessRightsFilter( accessRightsFilter ),
120  mCustomModel( customModel )
121 {
122 }
123 
124 bool MobileEventHandler::eventFilter( QObject *object, QEvent *event )
125 {
126  if ( object == mComboBox && mComboBox->isEnabled() && event->type() == QEvent::MouseButtonPress ) {
127 
128  const QMouseEvent *mouseEvent = static_cast<QMouseEvent*>( event );
129 
130  // we receive mouse events from other widgets as well, so check for ours
131  if ( mComboBox->rect().contains( mouseEvent->pos() ) ) {
132  QMetaObject::invokeMethod( this, "openDialog", Qt::QueuedConnection );
133  }
134 
135  return true;
136  }
137 
138  return QObject::eventFilter( object, event );
139 }
140 
141 void MobileEventHandler::openDialog()
142 {
143  Akonadi::CollectionDialog dialog( mCustomModel );
144  dialog.setMimeTypeFilter( mMimeTypeFilter->mimeTypeFilters() );
145  dialog.setAccessRightsFilter( mAccessRightsFilter->accessRights() );
146 
147  if ( dialog.exec() ) {
148  const Akonadi::Collection collection = dialog.selectedCollection();
149  const QModelIndex index = Akonadi::EntityTreeModel::modelIndexForCollection( mComboBox->model(), collection );
150  mComboBox->setCurrentIndex( index.row() );
151  }
152 }
153 
154 
155 CollectionComboBox::CollectionComboBox( QWidget *parent )
156  : KComboBox( parent ), d( new Private( 0, this ) )
157 {
158 #ifdef KDEPIM_MOBILE_UI
159  MobileEventHandler *handler = new MobileEventHandler( this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel );
160  installEventFilter( handler );
161 #endif
162 }
163 
164 CollectionComboBox::CollectionComboBox( QAbstractItemModel *model, QWidget *parent )
165  : KComboBox( parent ), d( new Private( model, this ) )
166 {
167 #ifdef KDEPIM_MOBILE_UI
168  MobileEventHandler *handler = new MobileEventHandler( this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel );
169  installEventFilter( handler );
170 #endif
171 }
172 
173 CollectionComboBox::~CollectionComboBox()
174 {
175  delete d;
176 }
177 
178 void CollectionComboBox::setMimeTypeFilter( const QStringList &contentMimeTypes )
179 {
180  d->mMimeTypeFilterModel->clearFilters();
181  d->mMimeTypeFilterModel->addMimeTypeFilters( contentMimeTypes );
182 
183  if ( d->mMonitor ) {
184  foreach ( const QString &mimeType, contentMimeTypes ) {
185  d->mMonitor->setMimeTypeMonitored( mimeType, true );
186  }
187  }
188 }
189 
190 QStringList CollectionComboBox::mimeTypeFilter() const
191 {
192  return d->mMimeTypeFilterModel->mimeTypeFilters();
193 }
194 
195 void CollectionComboBox::setAccessRightsFilter( Collection::Rights rights )
196 {
197  d->mRightsFilterModel->setAccessRights( rights );
198 }
199 
200 Akonadi::Collection::Rights CollectionComboBox::accessRightsFilter() const
201 {
202  return d->mRightsFilterModel->accessRights();
203 }
204 
205 void CollectionComboBox::setDefaultCollection( const Collection &collection )
206 {
207  d->mSelectionHandler->waitForCollection( collection );
208 }
209 
210 Akonadi::Collection CollectionComboBox::currentCollection() const
211 {
212  const QModelIndex modelIndex = model()->index( currentIndex(), 0 );
213  if ( modelIndex.isValid() ) {
214  return modelIndex.data( Akonadi::EntityTreeModel::CollectionRole ).value<Collection>();
215  } else {
216  return Akonadi::Collection();
217  }
218 }
219 
220 #include "moc_collectioncombobox.cpp"
221 #include "moc_collectioncombobox_p.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:32 by doxygen 1.8.3.1 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.10.5 API Reference

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