• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

mailcommon

  • sources
  • kde-4.12
  • kdepim
  • mailcommon
  • filter
filtercontroller.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net,
3  Author: Tobias Koenig <tokoe@kdab.com>
4 
5  This library is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or (at your
8  option) any later version.
9 
10  This library is distributed in the hope that it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13  License for more details.
14 
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 the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #include "filtercontroller.h"
22 #include "filtereditdialog_p.h"
23 #include "filtermodel_p.h"
24 
25 #include <KLocale>
26 #include <KMessageBox>
27 
28 #include <QAbstractItemModel>
29 #include <QAction>
30 #include <QItemSelectionModel>
31 
32 using namespace MailCommon;
33 
34 class FilterController::Private
35 {
36 public:
37 
38  void selectionChanged();
39  void addFilter();
40  void editFilter();
41  void removeFilter();
42  void moveUpFilter();
43  void moveDownFilter();
44 
45  FilterModel *mModel;
46  QItemSelectionModel *mSelectionModel;
47  QAction *mAddAction;
48  QAction *mEditAction;
49  QAction *mRemoveAction;
50  QAction *mMoveUpAction;
51  QAction *mMoveDownAction;
52 };
53 
54 void FilterController::Private::selectionChanged()
55 {
56  const bool filterSelected = mSelectionModel->hasSelection();
57 
58  if ( filterSelected ) {
59  mEditAction->setEnabled( true );
60  mRemoveAction->setEnabled( true );
61 
62  const QModelIndex index = mSelectionModel->selectedRows().first();
63  mMoveUpAction->setEnabled( index.row() != 0 );
64  mMoveDownAction->setEnabled( index.row() != ( mModel->rowCount() - 1 ) );
65  } else {
66  mEditAction->setEnabled( false );
67  mRemoveAction->setEnabled( false );
68  mMoveUpAction->setEnabled( false );
69  mMoveDownAction->setEnabled( false );
70  }
71 }
72 
73 void FilterController::Private::addFilter()
74 {
75  mModel->insertRow( mModel->rowCount() );
76 
77  FilterEditDialog dlg;
78  dlg.setCaption( i18n( "Add Filter" ) );
79  dlg.load( mModel->rowCount() - 1 );
80 
81  if ( dlg.exec() ) {
82  dlg.save();
83  } else {
84  mModel->removeRow( mModel->rowCount() - 1 );
85  }
86 }
87 
88 void FilterController::Private::editFilter()
89 {
90  if ( !mSelectionModel->hasSelection() ) {
91  return;
92  }
93 
94  const QModelIndex index = mSelectionModel->selectedRows().first();
95 
96  FilterEditDialog dlg;
97  dlg.setCaption( i18n( "Edit Filter" ) );
98  dlg.load( index.row() );
99  if ( dlg.exec() ) {
100  dlg.save();
101  }
102 }
103 
104 void FilterController::Private::removeFilter()
105 {
106  if ( !mSelectionModel->hasSelection() ) {
107  return;
108  }
109 
110  const QModelIndex index = mSelectionModel->selectedRows().first();
111 
112  const int result =
113  KMessageBox::questionYesNo(
114  0,
115  i18n( "Do you really want to remove filter <b>%1</b>?",
116  index.data( Qt::DisplayRole ).toString() ),
117  i18n( "Remove Filter" ) );
118 
119  if ( result == KMessageBox::No ) {
120  return;
121  }
122 
123  mModel->removeRow( index.row() );
124 }
125 
126 void FilterController::Private::moveUpFilter()
127 {
128  if ( !mSelectionModel->hasSelection() ) {
129  return;
130  }
131 
132  const QModelIndex index = mSelectionModel->selectedRows().first();
133  mModel->moveRow( index.row(), index.row() - 1 );
134 
135  // moveRow will reset the model, so restore the selection
136  mSelectionModel->select( mModel->index( index.row() - 1, 0 ),
137  QItemSelectionModel::ClearAndSelect );
138 }
139 
140 void FilterController::Private::moveDownFilter()
141 {
142  if ( !mSelectionModel->hasSelection() ) {
143  return;
144  }
145 
146  const QModelIndex index = mSelectionModel->selectedRows().first();
147  mModel->moveRow( index.row(), index.row() + 1 );
148 
149  // moveRow will reset the model, so restore the selection
150  mSelectionModel->select( mModel->index( index.row() + 1, 0 ),
151  QItemSelectionModel::ClearAndSelect );
152 }
153 
154 FilterController::FilterController( QObject *parent )
155  : QObject( parent ), d( new Private )
156 {
157  d->mModel = new FilterModel( this );
158  d->mSelectionModel = new QItemSelectionModel( d->mModel );
159 
160  d->mAddAction = new QAction( i18n( "Add" ), this );
161  d->mEditAction = new QAction( i18n( "Edit" ), this );
162  d->mRemoveAction = new QAction( i18n( "Remove" ), this );
163  d->mMoveUpAction = new QAction( i18n( "Move Up" ), this );
164  d->mMoveDownAction = new QAction( i18n( "Move Down" ), this );
165 
166  connect( d->mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
167  this, SLOT(selectionChanged()) );
168 
169  connect( d->mAddAction, SIGNAL(triggered(bool)), SLOT(addFilter()) );
170  connect( d->mEditAction, SIGNAL(triggered(bool)), SLOT(editFilter()) );
171  connect( d->mRemoveAction, SIGNAL(triggered(bool)), SLOT(removeFilter()) );
172  connect( d->mMoveUpAction, SIGNAL(triggered(bool)), SLOT(moveUpFilter()) );
173  connect( d->mMoveDownAction, SIGNAL(triggered(bool)), SLOT(moveDownFilter()) );
174 
175  d->selectionChanged();
176 }
177 
178 FilterController::~FilterController()
179 {
180  delete d;
181 }
182 
183 QAbstractItemModel *FilterController::model() const
184 {
185  return d->mModel;
186 }
187 
188 QItemSelectionModel *FilterController::selectionModel() const
189 {
190  return d->mSelectionModel;
191 }
192 
193 QAction *FilterController::addAction() const
194 {
195  return d->mAddAction;
196 }
197 
198 QAction *FilterController::editAction() const
199 {
200  return d->mEditAction;
201 }
202 
203 QAction *FilterController::removeAction() const
204 {
205  return d->mRemoveAction;
206 }
207 
208 QAction *FilterController::moveUpAction() const
209 {
210  return d->mMoveUpAction;
211 }
212 
213 QAction *FilterController::moveDownAction() const
214 {
215  return d->mMoveDownAction;
216 }
217 
218 #include "filtercontroller.moc"
MailCommon::FilterEditDialog::load
void load(int index)
Definition: filtereditdialog.cpp:47
MailCommon::FilterController::selectionModel
QItemSelectionModel * selectionModel() const
Returns the item selection model, which is used for adapting the state of the actions.
Definition: filtercontroller.cpp:188
MailCommon::FilterModel
Definition: filtermodel_p.h:27
MailCommon::FilterEditDialog::save
void save()
Definition: filtereditdialog.cpp:66
MailCommon::FilterController::addAction
QAction * addAction() const
Returns the action for adding a new filter.
QObject
MailCommon::FilterController::editAction
QAction * editAction() const
Returns the action for editing the currently selected filter.
MailCommon::FilterController::moveDownAction
QAction * moveDownAction() const
Returns the action for moving down the currently selected filter.
filtermodel_p.h
MailCommon::FilterController::removeAction
QAction * removeAction() const
Returns the action for removing the currently selected filter.
QAbstractItemModel
MailCommon::FilterController::~FilterController
~FilterController()
Destroys the filter controller.
Definition: filtercontroller.cpp:178
MailCommon::FilterEditDialog
Definition: filtereditdialog_p.h:34
MailCommon::FilterController::moveUpAction
QAction * moveUpAction() const
Returns the action for moving up the currently selected filter.
MailCommon::FilterController::FilterController
FilterController(QObject *parent=0)
Creates a new filter controller.
Definition: filtercontroller.cpp:154
filtercontroller.h
filtereditdialog_p.h
MailCommon::FilterController::model
QAbstractItemModel * model() const
Returns the model that represents the list of filters.
Definition: filtercontroller.cpp:183
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:14 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

mailcommon

Skip menu "mailcommon"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal