akonadi
collectioncombobox.cpp
00001 /* 00002 This file is part of Akonadi Contact. 00003 00004 Copyright (c) 2007-2009 Tobias Koenig <tokoe@kde.org> 00005 00006 This library is free software; you can redistribute it and/or modify it 00007 under the terms of the GNU Library General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or (at your 00009 option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, but WITHOUT 00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00014 License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to the 00018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00019 02110-1301, USA. 00020 */ 00021 00022 #include "collectioncombobox.h" 00023 #include "collectioncombobox_p.h" 00024 00025 #include "asyncselectionhandler_p.h" 00026 #include "collectiondialog.h" 00027 00028 #include <akonadi/changerecorder.h> 00029 #include <akonadi/collectionfetchscope.h> 00030 #include <akonadi/collectionfilterproxymodel.h> 00031 #include <akonadi/entityrightsfiltermodel.h> 00032 #include <akonadi/entitytreemodel.h> 00033 #include <akonadi/session.h> 00034 00035 #include <kdescendantsproxymodel.h> 00036 #include "collectionutils_p.h" 00037 00038 #include <QtCore/QAbstractItemModel> 00039 #include <QtCore/QEvent> 00040 #include <QtGui/QMouseEvent> 00041 00042 using namespace Akonadi; 00043 00044 class CollectionComboBox::Private 00045 { 00046 public: 00047 Private( QAbstractItemModel *customModel, CollectionComboBox *parent ) 00048 : mParent( parent ), mMonitor( 0 ), mModel( 0 ) 00049 { 00050 if ( customModel ) { 00051 mBaseModel = customModel; 00052 } else { 00053 mMonitor = new Akonadi::ChangeRecorder( mParent ); 00054 mMonitor->fetchCollection( true ); 00055 mMonitor->setCollectionMonitored( Akonadi::Collection::root() ); 00056 00057 mModel = new EntityTreeModel( mMonitor, mParent ); 00058 mModel->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation ); 00059 00060 mBaseModel = mModel; 00061 } 00062 00063 KDescendantsProxyModel *proxyModel = new KDescendantsProxyModel( parent ); 00064 proxyModel->setDisplayAncestorData( true ); 00065 proxyModel->setSourceModel( mBaseModel ); 00066 00067 mMimeTypeFilterModel = new CollectionFilterProxyModel( parent ); 00068 mMimeTypeFilterModel->setSourceModel( proxyModel ); 00069 00070 mRightsFilterModel = new EntityRightsFilterModel( parent ); 00071 mRightsFilterModel->setSourceModel( mMimeTypeFilterModel ); 00072 00073 mParent->setModel( mRightsFilterModel ); 00074 mParent->model()->sort( mParent->modelColumn() ); 00075 00076 mSelectionHandler = new AsyncSelectionHandler( mRightsFilterModel, mParent ); 00077 mParent->connect( mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)), 00078 mParent, SLOT(activated(QModelIndex)) ); 00079 00080 mParent->connect( mParent, SIGNAL(activated(int)), 00081 mParent, SLOT(activated(int)) ); 00082 } 00083 00084 ~Private() 00085 { 00086 } 00087 00088 void activated( int index ); 00089 void activated( const QModelIndex& index ); 00090 00091 CollectionComboBox *mParent; 00092 00093 ChangeRecorder *mMonitor; 00094 EntityTreeModel *mModel; 00095 QAbstractItemModel *mBaseModel; 00096 CollectionFilterProxyModel *mMimeTypeFilterModel; 00097 EntityRightsFilterModel *mRightsFilterModel; 00098 AsyncSelectionHandler *mSelectionHandler; 00099 }; 00100 00101 void CollectionComboBox::Private::activated( int index ) 00102 { 00103 const QModelIndex modelIndex = mParent->model()->index( index, 0 ); 00104 if ( modelIndex.isValid() ) 00105 emit mParent->currentChanged( modelIndex.data( EntityTreeModel::CollectionRole).value<Collection>() ); 00106 } 00107 00108 void CollectionComboBox::Private::activated( const QModelIndex &index ) 00109 { 00110 mParent->setCurrentIndex( index.row() ); 00111 } 00112 00113 MobileEventHandler::MobileEventHandler( CollectionComboBox *comboBox, CollectionFilterProxyModel *mimeTypeFilter, 00114 EntityRightsFilterModel *accessRightsFilter, QAbstractItemModel *customModel ) 00115 : QObject( comboBox ), 00116 mComboBox( comboBox ), 00117 mMimeTypeFilter( mimeTypeFilter ), 00118 mAccessRightsFilter( accessRightsFilter ), 00119 mCustomModel( customModel ) 00120 { 00121 } 00122 00123 bool MobileEventHandler::eventFilter( QObject *object, QEvent *event ) 00124 { 00125 if ( object == mComboBox && mComboBox->isEnabled() && event->type() == QEvent::MouseButtonPress ) { 00126 00127 const QMouseEvent *mouseEvent = static_cast<QMouseEvent*>( event ); 00128 00129 // we receive mouse events from other widgets as well, so check for ours 00130 if ( mComboBox->rect().contains( mouseEvent->pos() ) ) { 00131 QMetaObject::invokeMethod( this, "openDialog", Qt::QueuedConnection ); 00132 } 00133 00134 return true; 00135 } 00136 00137 return QObject::eventFilter( object, event ); 00138 } 00139 00140 void MobileEventHandler::openDialog() 00141 { 00142 Akonadi::CollectionDialog dialog( mCustomModel ); 00143 dialog.setMimeTypeFilter( mMimeTypeFilter->mimeTypeFilters() ); 00144 dialog.setAccessRightsFilter( mAccessRightsFilter->accessRights() ); 00145 00146 if ( dialog.exec() ) { 00147 const Akonadi::Collection collection = dialog.selectedCollection(); 00148 const QModelIndex index = Akonadi::EntityTreeModel::modelIndexForCollection( mComboBox->model(), collection ); 00149 mComboBox->setCurrentIndex( index.row() ); 00150 } 00151 } 00152 00153 00154 CollectionComboBox::CollectionComboBox( QWidget *parent ) 00155 : KComboBox( parent ), d( new Private( 0, this ) ) 00156 { 00157 #ifdef KDEPIM_MOBILE_UI 00158 MobileEventHandler *handler = new MobileEventHandler( this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel ); 00159 installEventFilter( handler ); 00160 #endif 00161 } 00162 00163 CollectionComboBox::CollectionComboBox( QAbstractItemModel *model, QWidget *parent ) 00164 : KComboBox( parent ), d( new Private( model, this ) ) 00165 { 00166 #ifdef KDEPIM_MOBILE_UI 00167 MobileEventHandler *handler = new MobileEventHandler( this, d->mMimeTypeFilterModel, d->mRightsFilterModel, d->mBaseModel ); 00168 installEventFilter( handler ); 00169 #endif 00170 } 00171 00172 CollectionComboBox::~CollectionComboBox() 00173 { 00174 delete d; 00175 } 00176 00177 void CollectionComboBox::setMimeTypeFilter( const QStringList &contentMimeTypes ) 00178 { 00179 d->mMimeTypeFilterModel->clearFilters(); 00180 d->mMimeTypeFilterModel->addMimeTypeFilters( contentMimeTypes ); 00181 00182 if ( d->mMonitor ) 00183 foreach ( const QString &mimeType, contentMimeTypes ) 00184 d->mMonitor->setMimeTypeMonitored( mimeType, true ); 00185 } 00186 00187 QStringList CollectionComboBox::mimeTypeFilter() const 00188 { 00189 return d->mMimeTypeFilterModel->mimeTypeFilters(); 00190 } 00191 00192 void CollectionComboBox::setAccessRightsFilter( Collection::Rights rights ) 00193 { 00194 d->mRightsFilterModel->setAccessRights( rights ); 00195 } 00196 00197 Akonadi::Collection::Rights CollectionComboBox::accessRightsFilter() const 00198 { 00199 return d->mRightsFilterModel->accessRights(); 00200 } 00201 00202 void CollectionComboBox::setDefaultCollection( const Collection &collection ) 00203 { 00204 d->mSelectionHandler->waitForCollection( collection ); 00205 } 00206 00207 Akonadi::Collection CollectionComboBox::currentCollection() const 00208 { 00209 const QModelIndex modelIndex = model()->index( currentIndex(), 0 ); 00210 if ( modelIndex.isValid() ) 00211 return modelIndex.data( Akonadi::EntityTreeModel::CollectionRole ).value<Collection>(); 00212 else 00213 return Akonadi::Collection(); 00214 } 00215 00216 #include "collectioncombobox.moc" 00217 #include "collectioncombobox_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:25:17 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:25:17 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.