diff --git a/akonadi/collectiondialog.cpp b/akonadi/collectiondialog.cpp index 3446b4030..ba026b8fb 100644 --- a/akonadi/collectiondialog.cpp +++ b/akonadi/collectiondialog.cpp @@ -1,120 +1,130 @@ /* Copyright 2008 Ingo Klöcker This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "collectiondialog.h" #include #include #include #include using namespace Akonadi; class CollectionDialog::Private { public: - Private(); - + CollectionDialog *q; CollectionModel *collectionModel; CollectionFilterProxyModel *filterModel; CollectionView *collectionView; + + explicit Private(CollectionDialog *q); + void slotSelectionChanged(); }; -CollectionDialog::Private::Private() - : collectionModel( 0 ), +CollectionDialog::Private::Private(CollectionDialog *q) + : q(q), + collectionModel( 0 ), filterModel( 0 ), collectionView( 0 ) { } +void CollectionDialog::Private::slotSelectionChanged() +{ + q->enableButton(KDialog::Ok, collectionView->selectionModel()->selectedIndexes().count() > 0); +} CollectionDialog::CollectionDialog( QWidget *parent ) : KDialog( parent ), - d( new Private ) + d( new Private(this) ) { QWidget *widget = mainWidget(); QVBoxLayout *layout = new QVBoxLayout( widget ); d->collectionModel = new CollectionModel( this ); d->filterModel = new CollectionFilterProxyModel( this ); d->filterModel->setDynamicSortFilter( true ); d->filterModel->setSortCaseSensitivity( Qt::CaseInsensitive ); d->filterModel->setSourceModel( d->collectionModel ); d->collectionView = new CollectionView( widget ); d->collectionView->setModel( d->filterModel ); layout->addWidget( d->collectionView ); + + connect(d->collectionView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(slotSelectionChanged())); + enableButton(KDialog::Ok, false); } CollectionDialog::~CollectionDialog() { delete d; } Akonadi::Collection CollectionDialog::selectedCollection() const { if ( selectionMode() == QAbstractItemView::SingleSelection ) { const QModelIndex index = d->collectionView->currentIndex(); if ( index.isValid() ) return index.model()->data( index, CollectionModel::CollectionRole ).value(); } return Collection(); } Akonadi::Collection::List CollectionDialog::selectedCollections() const { Collection::List collections; const QItemSelectionModel *selectionModel = d->collectionView->selectionModel(); const QModelIndexList selectedIndexes = selectionModel->selectedIndexes(); foreach ( const QModelIndex &index, selectedIndexes ) { if ( index.isValid() ) { const Collection collection = index.model()->data( index, CollectionModel::CollectionRole ).value(); if ( collection.isValid() ) collections.append( collection ); } } return collections; } void CollectionDialog::setMimeTypeFilter( const QStringList &mimeTypes ) { d->filterModel->clearFilters(); d->filterModel->addMimeTypeFilters( mimeTypes ); } QStringList CollectionDialog::mimeTypeFilter() const { return d->filterModel->mimeTypeFilters(); } void CollectionDialog::setSelectionMode( QAbstractItemView::SelectionMode mode ) { d->collectionView->setSelectionMode( mode ); } QAbstractItemView::SelectionMode CollectionDialog::selectionMode() const { return d->collectionView->selectionMode(); } #include "collectiondialog.moc" diff --git a/akonadi/collectiondialog.h b/akonadi/collectiondialog.h index 7bb0282f0..2abc5039f 100644 --- a/akonadi/collectiondialog.h +++ b/akonadi/collectiondialog.h @@ -1,118 +1,122 @@ /* Copyright 2008 Ingo Klöcker This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef AKONADI_COLLECTIONDIALOG_H #define AKONADI_COLLECTIONDIALOG_H #include "akonadi_export.h" #include #include #include namespace Akonadi { /** * @short A collection selection dialog. * - * Provides a dialog that lists all collections that are available - * on the Akonadi storage. + * Provides a dialog that lists collections that are available + * on the Akonadi storage and allows to select one or multiple + * collections. * The list of shown collections can be filtered by mime type. * * Example: * * @code * * // show the user a dialog to select a collection of contacts * Akonadi::CollectionDialog dlg( this ); * dlg.setMimeTypeFilter( QStringList() << QString( "text/directory" ) ); * * if ( dlg.exec() ) { * const Akonadi::Collection collection = dlg.selectedCollection(); * ... * } * * @endcode * * @author Ingo Klöcker * @since 4.3 */ class AKONADI_EXPORT CollectionDialog : public KDialog { Q_OBJECT Q_DISABLE_COPY( CollectionDialog ) public: /** * Creates a collection dialog. * * @param parent The parent widget. */ explicit CollectionDialog( QWidget *parent = 0 ); /** * Destroys the collection dialog. */ ~CollectionDialog(); /** - * Returns the selected collection. + * Returns the selected collection if the selection mode is + * QAbstractItemView::SingleSelection what is the default. If + * another selection mode was set then an invalid collection + * is returned. */ Akonadi::Collection selectedCollection() const; /** * Returns the list of selected collections. */ Akonadi::Collection::List selectedCollections() const; /** * Sets the mime types any of which the selected collection(s) shall support. - * @see Akonadi::CollectionDialog::setMimeTypeFilter() */ void setMimeTypeFilter( const QStringList &mimeTypes ); /** * Returns the mime types any of which the selected collection(s) shall support. - * @see Akonadi::CollectionDialog::mimeTypeFilter() */ QStringList mimeTypeFilter() const; /** * Sets the selection mode. * @see QAbstractItemView::setSelectionMode() */ void setSelectionMode( QAbstractItemView::SelectionMode mode ); /** * Returns the selection mode. * @see QAbstractItemView::selectionMode() */ QAbstractItemView::SelectionMode selectionMode() const; private: class Private; Private * const d; + + Q_PRIVATE_SLOT( d, void slotSelectionChanged() ) }; } // namespace Akonadi #endif // AKONADI_COLLECTIONDIALOG_H diff --git a/akonadi/collectionrequester.h b/akonadi/collectionrequester.h index 1862c54e8..6c9762a9e 100644 --- a/akonadi/collectionrequester.h +++ b/akonadi/collectionrequester.h @@ -1,117 +1,115 @@ /* Copyright 2008 Ingo Klöcker This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef AKONADI_COLLECTIONREQUESTER_H #define AKONADI_COLLECTIONREQUESTER_H #include "akonadi_export.h" #include #include namespace Akonadi { /** * @short A widget to request an Akonadi collection from the user. * * This class is a widget showing a read-only lineedit displaying * the currently chosen collection and a button invoking a dialog * for choosing a collection. * * Example: * * @code * * // create a collection requester to select a collection of contacts * Akonadi::CollectionRequester requester( Akonadi::Collection::root(), this ); * requester.setMimeTypeFilter( QStringList() << QString( "text/directory" ) ); * * ... * * const Akonadi::Collection collection = requester.collection(); * if ( collection.isValid() ) { * ... * } * * @endcode * * @author Ingo Klöcker * @since 4.3 */ class AKONADI_EXPORT CollectionRequester : public KHBox { Q_OBJECT Q_DISABLE_COPY(CollectionRequester) public: /** * Creates a collection requester. * * @param parent The parent widget. */ explicit CollectionRequester( QWidget *parent = 0 ); /** * Creates a collection requester with an initial @p collection. * * @param collection The initial collection. * @param parent The parent widget. */ explicit CollectionRequester( const Akonadi::Collection &collection, QWidget *parent = 0 ); /** * Destroys the collection requester. */ ~CollectionRequester(); /** * Returns the currently chosen collection, or an empty collection if none * none was chosen. */ Akonadi::Collection collection() const; /** * Sets the mime types any of which the selected collection shall support. - * @see Akonadi::CollectionDialog::setMimeTypeFilter() */ void setMimeTypeFilter( const QStringList &mimeTypes ); /** * Returns the mime types any of which the selected collection shall support. - * @see Akonadi::CollectionDialog::mimeTypeFilter() */ QStringList mimeTypeFilter() const; public Q_SLOTS: /** * Sets the @p collection of the requester. */ void setCollection( const Akonadi::Collection& collection ); private: class Private; Private * const d; Q_PRIVATE_SLOT( d, void _k_slotOpenDialog() ) }; } // namespace Akonadi #endif // AKONADI_COLLECTIONREQUESTER_H