Build with clang.
[kdepim.git] / mailcommon / filtercontroller.cpp
blob3e77f31d4c7caa74ab8a2f6f62a800ae5f13c7d6
1 /*
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
19 02110-1301, USA.
22 #include "filtercontroller.h"
24 #include "filtereditdialog_p.h"
25 #include "filtermodel_p.h"
27 #include <klocale.h>
28 #include <kmessagebox.h>
30 #include <QtCore/QAbstractItemModel>
31 #include <QtGui/QAction>
32 #include <QtGui/QItemSelectionModel>
34 using namespace MailCommon;
36 class FilterController::Private
38 public:
40 void selectionChanged();
41 void addFilter();
42 void editFilter();
43 void removeFilter();
44 void moveUpFilter();
45 void moveDownFilter();
47 FilterModel *mModel;
48 QItemSelectionModel *mSelectionModel;
49 QAction *mAddAction;
50 QAction *mEditAction;
51 QAction *mRemoveAction;
52 QAction *mMoveUpAction;
53 QAction *mMoveDownAction;
56 void FilterController::Private::selectionChanged()
58 const bool filterSelected = mSelectionModel->hasSelection();
60 if ( filterSelected ) {
61 mEditAction->setEnabled( true );
62 mRemoveAction->setEnabled( true );
64 const QModelIndex index = mSelectionModel->selectedRows().first();
65 mMoveUpAction->setEnabled( index.row() != 0 );
66 mMoveDownAction->setEnabled( index.row() != (mModel->rowCount() - 1) );
67 } else {
68 mEditAction->setEnabled( false );
69 mRemoveAction->setEnabled( false );
70 mMoveUpAction->setEnabled( false );
71 mMoveDownAction->setEnabled( false );
75 void FilterController::Private::addFilter()
77 mModel->insertRow( mModel->rowCount() );
79 FilterEditDialog dlg;
80 dlg.setCaption( i18n( "Add Filter" ) );
81 dlg.load( mModel->rowCount() - 1 );
83 if ( dlg.exec() )
84 dlg.save();
85 else
86 mModel->removeRow( mModel->rowCount() - 1 );
89 void FilterController::Private::editFilter()
91 if ( !mSelectionModel->hasSelection() )
92 return;
94 const QModelIndex index = mSelectionModel->selectedRows().first();
96 FilterEditDialog dlg;
97 dlg.setCaption( i18n( "Edit Filter" ) );
98 dlg.load( index.row() );
99 if ( dlg.exec() )
100 dlg.save();
103 void FilterController::Private::removeFilter()
105 if ( !mSelectionModel->hasSelection() )
106 return;
108 const QModelIndex index = mSelectionModel->selectedRows().first();
110 const int result = KMessageBox::questionYesNo( 0, i18n( "Do you really want to remove filter <b>%1</b>?",
111 index.data( Qt::DisplayRole ).toString() ),
112 i18n( "Remove Filter" ) );
113 if ( result == KMessageBox::No )
114 return;
116 mModel->removeRow( index.row() );
119 void FilterController::Private::moveUpFilter()
121 if ( !mSelectionModel->hasSelection() )
122 return;
124 const QModelIndex index = mSelectionModel->selectedRows().first();
125 mModel->moveRow( index.row(), index.row() - 1 );
127 // moveRow will reset the model, so restore the selection
128 mSelectionModel->select( mModel->index( index.row() - 1, 0 ), QItemSelectionModel::ClearAndSelect );
131 void FilterController::Private::moveDownFilter()
133 if ( !mSelectionModel->hasSelection() )
134 return;
136 const QModelIndex index = mSelectionModel->selectedRows().first();
137 mModel->moveRow( index.row(), index.row() + 1 );
139 // moveRow will reset the model, so restore the selection
140 mSelectionModel->select( mModel->index( index.row() + 1, 0 ), QItemSelectionModel::ClearAndSelect );
144 FilterController::FilterController( QObject *parent )
145 : QObject( parent ), d( new Private )
147 d->mModel = new FilterModel( this );
148 d->mSelectionModel = new QItemSelectionModel( d->mModel );
150 d->mAddAction = new QAction( i18n( "Add" ), this );
151 d->mEditAction = new QAction( i18n( "Edit" ), this );
152 d->mRemoveAction = new QAction( i18n( "Remove" ), this );
153 d->mMoveUpAction = new QAction( i18n( "Move Up" ), this );
154 d->mMoveDownAction = new QAction( i18n( "Move Down" ), this );
156 connect( d->mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
157 this, SLOT(selectionChanged()) );
159 connect( d->mAddAction, SIGNAL(triggered(bool)), SLOT(addFilter()) );
160 connect( d->mEditAction, SIGNAL(triggered(bool)), SLOT(editFilter()) );
161 connect( d->mRemoveAction, SIGNAL(triggered(bool)), SLOT(removeFilter()) );
162 connect( d->mMoveUpAction, SIGNAL(triggered(bool)), SLOT(moveUpFilter()) );
163 connect( d->mMoveDownAction, SIGNAL(triggered(bool)), SLOT(moveDownFilter()) );
165 d->selectionChanged();
168 FilterController::~FilterController()
170 delete d;
173 QAbstractItemModel* FilterController::model() const
175 return d->mModel;
178 QItemSelectionModel* FilterController::selectionModel() const
180 return d->mSelectionModel;
183 QAction* FilterController::addAction() const
185 return d->mAddAction;
188 QAction* FilterController::editAction() const
190 return d->mEditAction;
193 QAction* FilterController::removeAction() const
195 return d->mRemoveAction;
198 QAction* FilterController::moveUpAction() const
200 return d->mMoveUpAction;
203 QAction* FilterController::moveDownAction() const
205 return d->mMoveDownAction;
208 #include "filtercontroller.moc"