2
This file is part of kdepim.
3
Copyright (c) 2009 Kevin Krammer <kevin.krammer@gmx.at>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
Boston, MA 02110-1301, USA.
21
#include "storecollectiondialog.h"
23
#include "storecollectionfilterproxymodel.h"
25
#include <akonadi/collectionmodel.h>
26
#include <akonadi/collectionview.h>
31
#include <QVBoxLayout>
33
using namespace Akonadi;
35
static QModelIndex findCollection( const Collection &collection,
36
const QModelIndex &parent, QAbstractItemModel *model)
38
const int rowCount = model->rowCount( parent );
40
for ( int row = 0; row < rowCount; ++row ) {
41
QModelIndex index = model->index( row, 0, parent );
42
if ( !index.isValid() )
45
QVariant data = model->data( index, CollectionModel::CollectionIdRole );
46
if ( !data.isValid() )
49
if ( data.toInt() == collection.id() ) {
53
index = findCollection( collection, index, model );
54
if ( index.isValid() )
61
StoreCollectionDialog::StoreCollectionDialog( QWidget* parent )
67
setCaption( i18nc( "@title:window", "Target Folder Selection") );
69
setButtons( KDialog::Ok | KDialog::Cancel );
71
CollectionModel *model = new CollectionModel( this );
73
QWidget *widget = new QWidget( this );
75
QVBoxLayout *mainLayout = new QVBoxLayout( widget );
76
mainLayout->setMargin( KDialog::marginHint() );
77
mainLayout->setSpacing( KDialog::spacingHint() );
79
// initially hide label until a text is set
80
mLabel = new QLabel( widget );
83
mainLayout->addWidget( mLabel );
85
mFilterModel = new StoreCollectionFilterProxyModel( this );
86
mFilterModel->setSourceModel( model );
88
mView = new CollectionView( widget );
89
mView->setSelectionMode( QAbstractItemView::SingleSelection );
90
mView->setModel( mFilterModel );
92
connect( mView, SIGNAL( currentChanged( const Akonadi::Collection& ) ),
93
this, SLOT( collectionChanged( const Akonadi::Collection& ) ) );
94
connect( mView->model(), SIGNAL( rowsInserted( const QModelIndex&, int, int ) ),
95
this, SLOT( collectionsInserted( const QModelIndex&, int, int ) ) );
97
mainLayout->addWidget( mView );
99
setMainWidget( widget );
102
StoreCollectionDialog::~StoreCollectionDialog()
106
void StoreCollectionDialog::setLabelText( const QString &labelText )
108
mLabel->setText( labelText );
112
void StoreCollectionDialog::setMimeType( const QString &mimeType )
114
mFilterModel->clearFilters();
115
mFilterModel->addMimeTypeFilter( mimeType );
118
void StoreCollectionDialog::setSelectedCollection( const Collection &collection )
120
mSelectedCollection = collection;
122
QModelIndex index = findCollection( mSelectedCollection, mView->rootIndex(), mView->model() );
123
if ( index.isValid() ) {
124
mView->setCurrentIndex( index );
128
Collection StoreCollectionDialog::selectedCollection() const
130
return mSelectedCollection;
133
void StoreCollectionDialog::setSubResourceModel( const AbstractSubResourceModel *subResourceModel )
135
mFilterModel->setSubResourceModel( subResourceModel );
138
void StoreCollectionDialog::collectionChanged( const Collection &collection )
140
mSelectedCollection = collection;
143
void StoreCollectionDialog::collectionsInserted( const QModelIndex &parent, int start, int end )
145
QAbstractItemModel *model = mView->model();
147
for ( int row = start; row <= end; ++row ) {
148
QModelIndex index = model->index( row, 0, parent );
149
if ( !index.isValid() )
152
QVariant data = model->data( index, CollectionModel::CollectionIdRole );
153
if ( !data.isValid() )
156
if ( data.toLongLong() == mSelectedCollection.id() ) {
157
mView->setCurrentIndex( index );
161
index = findCollection( mSelectedCollection, index, model );
162
if ( index.isValid() ) {
163
mView->setCurrentIndex( index );
169
// kate: space-indent on; indent-width 2; replace-tabs on;