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 "filtercontroller.h"
24 #include "filtereditdialog_p.h"
25 #include "filtermodel_p.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
40 void selectionChanged();
45 void moveDownFilter();
48 QItemSelectionModel
*mSelectionModel
;
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) );
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() );
80 dlg
.setCaption( i18n( "Add Filter" ) );
81 dlg
.load( mModel
->rowCount() - 1 );
86 mModel
->removeRow( mModel
->rowCount() - 1 );
89 void FilterController::Private::editFilter()
91 if ( !mSelectionModel
->hasSelection() )
94 const QModelIndex index
= mSelectionModel
->selectedRows().first();
97 dlg
.setCaption( i18n( "Edit Filter" ) );
98 dlg
.load( index
.row() );
103 void FilterController::Private::removeFilter()
105 if ( !mSelectionModel
->hasSelection() )
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
)
116 mModel
->removeRow( index
.row() );
119 void FilterController::Private::moveUpFilter()
121 if ( !mSelectionModel
->hasSelection() )
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() )
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()
173 QAbstractItemModel
* FilterController::model() const
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"