2
Copyright (C) 2010 Klarälvdalens Datakonsult AB,
3
a KDAB Group company, info@kdab.net,
4
author Tobias Koenig <tokoe@kdab.com>
6
This library is free software; you can redistribute it and/or modify it
7
under the terms of the GNU Library General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or (at your
9
option) any later version.
11
This library is distributed in the hope that it will be useful, but WITHOUT
12
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14
License for more details.
16
You should have received a copy of the GNU Library General Public License
17
along with this library; see the file COPYING.LIB. If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
#include "favoritescontroller.h"
24
#include "favoriteslistmodel.h"
26
#include <akonadi/etmviewstatesaver.h>
27
#include <kconfiggroup.h>
29
#include <kmessagebox.h>
31
#include <QtCore/QAbstractItemModel>
32
#include <QtGui/QAction>
33
#include <QtGui/QItemSelectionModel>
35
static const char * const sFavoritePrefix = "Favorite_";
36
static const int sFavoritePrefixLength = 9;
38
class FavoritesController::Private
41
QStringList rereadFavoritesList() const;
43
void selectionChanged();
44
void removeFavorite();
45
void moveUpFavorite();
46
void moveDownFavorite();
48
KSharedConfig::Ptr mConfig;
49
FavoritesListModel *mModel;
50
QItemSelectionModel *mSelectionModel;
51
QItemSelectionModel *mCollectionSelectionModel;
52
QAction *mRemoveAction;
53
QAction *mMoveUpAction;
54
QAction *mMoveDownAction;
57
QStringList FavoritesController::Private::rereadFavoritesList() const
60
foreach ( const QString &group, mConfig->groupList() ) {
61
if ( group.startsWith( sFavoritePrefix ) )
62
names.append( QString( group ).remove( 0, sFavoritePrefixLength ) );
68
void FavoritesController::Private::selectionChanged()
70
const bool favoriteSelected = mSelectionModel->hasSelection();
72
if ( favoriteSelected ) {
73
mRemoveAction->setEnabled( true );
75
const QModelIndex index = mSelectionModel->selectedRows().first();
76
mMoveUpAction->setEnabled( index.row() != 0 );
77
mMoveDownAction->setEnabled( index.row() != (mModel->rowCount() - 1) );
79
mRemoveAction->setEnabled( false );
80
mMoveUpAction->setEnabled( false );
81
mMoveDownAction->setEnabled( false );
85
void FavoritesController::Private::removeFavorite()
87
if ( !mSelectionModel->hasSelection() )
90
const QModelIndex index = mSelectionModel->selectedRows().first();
92
const int result = KMessageBox::questionYesNo( 0, i18n( "Do you really want to remove favorite <b>%1</b>?",
93
index.data( Qt::DisplayRole ).toString() ),
94
i18n( "Remove Favorite" ) );
95
if ( result == KMessageBox::No )
98
mModel->removeItem( index.row() );
101
void FavoritesController::Private::moveUpFavorite()
103
if ( !mSelectionModel->hasSelection() )
106
const QModelIndex index = mSelectionModel->selectedRows().first();
107
mModel->moveUp( index.row() );
110
void FavoritesController::Private::moveDownFavorite()
112
if ( !mSelectionModel->hasSelection() )
115
const QModelIndex index = mSelectionModel->selectedRows().first();
116
mModel->moveDown( index.row() );
120
FavoritesController::FavoritesController( const KSharedConfig::Ptr &config, QObject *parent )
121
: QObject( parent ), d( new Private )
124
d->mModel = new FavoritesListModel( config, this );
125
d->mSelectionModel = new QItemSelectionModel( d->mModel );
126
d->mCollectionSelectionModel = 0;
128
d->mRemoveAction = new QAction( i18n( "Remove" ), this );
129
d->mMoveUpAction = new QAction( i18n( "Move Up" ), this );
130
d->mMoveDownAction = new QAction( i18n( "Move Down" ), this );
132
connect( d->mSelectionModel, SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ),
133
this, SLOT( selectionChanged() ) );
135
connect( d->mRemoveAction, SIGNAL( triggered( bool ) ), SLOT( removeFavorite() ) );
136
connect( d->mMoveUpAction, SIGNAL( triggered( bool ) ), SLOT( moveUpFavorite() ) );
137
connect( d->mMoveDownAction, SIGNAL( triggered( bool ) ), SLOT( moveDownFavorite() ) );
139
d->selectionChanged();
142
FavoritesController::~FavoritesController()
147
QAbstractItemModel* FavoritesController::model() const
152
QItemSelectionModel* FavoritesController::selectionModel() const
154
return d->mSelectionModel;
157
void FavoritesController::setCollectionSelectionModel( QItemSelectionModel *model )
159
d->mCollectionSelectionModel = model;
162
QAction* FavoritesController::removeAction() const
164
return d->mRemoveAction;
167
QAction* FavoritesController::moveUpAction() const
169
return d->mMoveUpAction;
172
QAction* FavoritesController::moveDownAction() const
174
return d->mMoveDownAction;
177
void FavoritesController::loadFavorite( const QString &name ) const
179
Q_ASSERT( d->mCollectionSelectionModel );
181
Akonadi::ETMViewStateSaver *saver = new Akonadi::ETMViewStateSaver;
182
saver->setSelectionModel( d->mCollectionSelectionModel );
184
KConfigGroup group( d->mConfig, sFavoritePrefix + name );
185
if ( !group.isValid() ) {
190
saver->restoreState( group );
193
void FavoritesController::saveFavorite( const QString &name )
195
Q_ASSERT( d->mCollectionSelectionModel );
197
Akonadi::ETMViewStateSaver saver;
198
saver.setSelectionModel( d->mCollectionSelectionModel );
200
KConfigGroup group( d->mConfig, sFavoritePrefix + name );
201
saver.saveState( group );
203
d->mModel->setStringList( d->rereadFavoritesList() );
206
#include "favoritescontroller.moc"