akonadi/kabc
collectioncombobox.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "collectioncombobox.h"
00021
00022 #include <QtCore/QAbstractItemModel>
00023 #include <QtGui/QComboBox>
00024 #include <QtGui/QVBoxLayout>
00025
00026 #include <akonadi/collectionmodel.h>
00027
00028 using namespace Akonadi;
00029 using namespace KABC;
00030
00031 class CollectionComboBox::Private
00032 {
00033 public:
00034 Private( CollectionComboBox *parent )
00035 : mParent( parent )
00036 {
00037 }
00038
00039 void activated( int index );
00040
00041 CollectionComboBox *mParent;
00042
00043 QComboBox *mComboBox;
00044 };
00045
00046 void CollectionComboBox::Private::activated( int index )
00047 {
00048 if ( !mComboBox->model() )
00049 return;
00050
00051 const QModelIndex modelIndex = mComboBox->model()->index( index, 0 );
00052 if ( modelIndex.isValid() )
00053 emit mParent->selectionChanged( Collection( modelIndex.data( CollectionModel::CollectionIdRole ).toLongLong() ) );
00054 }
00055
00056 CollectionComboBox::CollectionComboBox( QWidget *parent )
00057 : QWidget( parent ), d( new Private( this ) )
00058 {
00059 QVBoxLayout *layout = new QVBoxLayout( this );
00060 layout->setMargin( 0 );
00061 layout->setSpacing( 0 );
00062
00063 d->mComboBox = new QComboBox( this );
00064 layout->addWidget( d->mComboBox );
00065
00066 connect( d->mComboBox, SIGNAL( activated( int ) ), SLOT( activated( int ) ) );
00067 }
00068
00069 CollectionComboBox::~CollectionComboBox()
00070 {
00071 delete d;
00072 }
00073
00074 void CollectionComboBox::setModel( QAbstractItemModel *model )
00075 {
00076 d->mComboBox->setModel( model );
00077 }
00078
00079 Akonadi::Collection CollectionComboBox::selectedCollection() const
00080 {
00081 Q_ASSERT_X( d->mComboBox->model() != 0, "CollectionComboBox::selectionChanged", "No model set!" );
00082
00083 int index = d->mComboBox->currentIndex();
00084
00085 const QModelIndex modelIndex = d->mComboBox->model()->index( index, 0 );
00086 if ( modelIndex.isValid() )
00087 return Akonadi::Collection( modelIndex.data( Akonadi::CollectionModel::CollectionIdRole ).toLongLong() );
00088 else
00089 return Akonadi::Collection();
00090 }
00091
00092 #include "collectioncombobox.moc"