akonadi
collectioncombobox.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "collectioncombobox.h"
00023
00024 #include "asyncselectionhandler_p.h"
00025
00026 #include <akonadi/changerecorder.h>
00027 #include <akonadi/collectionfetchscope.h>
00028 #include <akonadi/collectionfilterproxymodel.h>
00029 #include <akonadi/entityrightsfiltermodel.h>
00030 #include <akonadi/entitytreemodel.h>
00031 #include <akonadi/session.h>
00032
00033 #include "kdescendantsproxymodel_p.h"
00034
00035 #include <QtCore/QAbstractItemModel>
00036
00037 using namespace Akonadi;
00038
00039 class CollectionComboBox::Private
00040 {
00041 public:
00042 Private( QAbstractItemModel *customModel, CollectionComboBox *parent )
00043 : mParent( parent ), mMonitor( 0 ), mModel( 0 )
00044 {
00045 QAbstractItemModel *baseModel;
00046
00047 if ( customModel ) {
00048 baseModel = customModel;
00049 } else {
00050 mMonitor = new Akonadi::ChangeRecorder( mParent );
00051 mMonitor->fetchCollection( true );
00052 mMonitor->setCollectionMonitored( Akonadi::Collection::root() );
00053
00054 mModel = new EntityTreeModel( mMonitor, mParent );
00055 mModel->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation );
00056
00057 KDescendantsProxyModel *proxyModel = new KDescendantsProxyModel( parent );
00058 proxyModel->setDisplayAncestorData( true );
00059 proxyModel->setSourceModel( mModel );
00060
00061 baseModel = proxyModel;
00062 }
00063
00064 mMimeTypeFilterModel = new CollectionFilterProxyModel( parent );
00065 mMimeTypeFilterModel->setSourceModel( baseModel );
00066
00067 mRightsFilterModel = new EntityRightsFilterModel( parent );
00068 mRightsFilterModel->setSourceModel( mMimeTypeFilterModel );
00069
00070 mParent->setModel( mRightsFilterModel );
00071
00072 mSelectionHandler = new AsyncSelectionHandler( mRightsFilterModel, mParent );
00073 mParent->connect( mSelectionHandler, SIGNAL( collectionAvailable( const QModelIndex& ) ),
00074 mParent, SLOT( activated( const QModelIndex& ) ) );
00075
00076 mParent->connect( mParent, SIGNAL( activated( int ) ),
00077 mParent, SLOT( activated( int ) ) );
00078 }
00079
00080 ~Private()
00081 {
00082 }
00083
00084 void activated( int index );
00085 void activated( const QModelIndex& index );
00086
00087 CollectionComboBox *mParent;
00088
00089 ChangeRecorder *mMonitor;
00090 EntityTreeModel *mModel;
00091 CollectionFilterProxyModel *mMimeTypeFilterModel;
00092 EntityRightsFilterModel *mRightsFilterModel;
00093 AsyncSelectionHandler *mSelectionHandler;
00094 };
00095
00096 void CollectionComboBox::Private::activated( int index )
00097 {
00098 const QModelIndex modelIndex = mParent->model()->index( index, 0 );
00099 if ( modelIndex.isValid() )
00100 emit mParent->currentChanged( modelIndex.data( EntityTreeModel::CollectionRole).value<Collection>() );
00101 }
00102
00103 void CollectionComboBox::Private::activated( const QModelIndex &index )
00104 {
00105 mParent->setCurrentIndex( index.row() );
00106 }
00107
00108
00109 CollectionComboBox::CollectionComboBox( QWidget *parent )
00110 : KComboBox( parent ), d( new Private( 0, this ) )
00111 {
00112 }
00113
00114 CollectionComboBox::CollectionComboBox( QAbstractItemModel *model, QWidget *parent )
00115 : KComboBox( parent ), d( new Private( model, this ) )
00116 {
00117 }
00118
00119 CollectionComboBox::~CollectionComboBox()
00120 {
00121 delete d;
00122 }
00123
00124 void CollectionComboBox::setMimeTypeFilter( const QStringList &contentMimeTypes )
00125 {
00126 d->mMimeTypeFilterModel->clearFilters();
00127 d->mMimeTypeFilterModel->addMimeTypeFilters( contentMimeTypes );
00128
00129 if ( d->mMonitor )
00130 foreach ( const QString &mimeType, contentMimeTypes )
00131 d->mMonitor->setMimeTypeMonitored( mimeType, true );
00132 }
00133
00134 QStringList CollectionComboBox::mimeTypeFilter() const
00135 {
00136 return d->mMimeTypeFilterModel->mimeTypeFilters();
00137 }
00138
00139 void CollectionComboBox::setAccessRightsFilter( Collection::Rights rights )
00140 {
00141 d->mRightsFilterModel->setAccessRights( rights );
00142 }
00143
00144 Collection::Rights CollectionComboBox::accessRightsFilter() const
00145 {
00146 return d->mRightsFilterModel->accessRights();
00147 }
00148
00149 void CollectionComboBox::setDefaultCollection( const Collection &collection )
00150 {
00151 d->mSelectionHandler->waitForCollection( collection );
00152 }
00153
00154 Akonadi::Collection CollectionComboBox::currentCollection() const
00155 {
00156 const QModelIndex modelIndex = model()->index( currentIndex(), 0 );
00157 if ( modelIndex.isValid() )
00158 return modelIndex.data( Akonadi::EntityTreeModel::CollectionRole ).value<Collection>();
00159 else
00160 return Akonadi::Collection();
00161 }
00162
00163 #include "collectioncombobox.moc"