• 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
filteractionwidget.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
3  Copyright (c) 2010 Andras Mantia <andras@kdab.com>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program 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
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #include "filteractionwidget.h"
21 
22 #include "filteraction.h"
23 #include "filteractiondict.h"
24 #include "filtermanager.h"
25 #include "mailfilter.h"
26 #include <pimcommon/widgets/minimumcombobox.h>
27 
28 #include <KLocalizedString>
29 #include <KPushButton>
30 
31 #include <QGridLayout>
32 #include <QLabel>
33 
34 using namespace MailCommon;
35 
36 //=============================================================================
37 //
38 // class FilterActionWidget
39 //
40 //=============================================================================
41 
42 class FilterActionWidget::Private
43 {
44 public:
45  Private( FilterActionWidget *qq )
46  : q( qq ), mComboBox( 0 ), mAdd( 0 ), mRemove( 0 ), mLayout( 0 )
47  {
48  }
49 
50  ~Private()
51  {
52  qDeleteAll( mActionList );
53  mActionList.clear();
54  }
55 
56  void setFilterAction( QWidget *widget = 0 );
57 
58  void slotFilterTypeChanged( int index );
59  void slotAddWidget();
60  void slotRemoveWidget();
61 
62  FilterActionWidget *q;
63  QList<MailCommon::FilterAction*> mActionList;
64  KComboBox *mComboBox;
65  KPushButton *mAdd;
66  KPushButton *mRemove;
67 
68  QGridLayout *mLayout;
69 };
70 
71 void FilterActionWidget::Private::setFilterAction( QWidget *widget )
72 {
73  if ( mLayout->itemAtPosition( 1, 2 ) ) {
74  delete mLayout->itemAtPosition( 1, 2 )->widget();
75  }
76 
77  if ( widget ) {
78  mLayout->addWidget( widget, 1, 2 );
79  } else {
80  mLayout->addWidget( new QLabel( i18n( "Please select an action." ), q ), 1, 2 );
81  }
82 }
83 
84 void FilterActionWidget::Private::slotAddWidget()
85 {
86  emit q->addWidget( q );
87  emit q->filterModified();
88 }
89 
90 void FilterActionWidget::Private::slotRemoveWidget()
91 {
92  emit q->removeWidget( q );
93  emit q->filterModified();
94 }
95 
96 void FilterActionWidget::Private::slotFilterTypeChanged( int index )
97 {
98  setFilterAction( index < mActionList.count() ?
99  mActionList.at( index )->createParamWidget( q ) :
100  0 );
101 }
102 
103 FilterActionWidget::FilterActionWidget( QWidget *parent )
104  : KHBox( parent ), d( new Private( this ) )
105 {
106  QWidget *widget = new QWidget( this );
107 
108  d->mLayout = new QGridLayout( widget );
109  d->mLayout->setContentsMargins( 0, 0, 0, 0 );
110 
111  d->mComboBox = new PimCommon::MinimumComboBox( widget );
112  d->mComboBox->setEditable( false );
113  Q_ASSERT( d->mComboBox );
114  d->mLayout->addWidget( d->mComboBox, 1, 1 );
115  d->mAdd = new KPushButton( widget );
116  d->mAdd->setIcon( KIcon( QLatin1String("list-add") ) );
117  d->mAdd->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
118 
119  d->mRemove = new KPushButton( widget );
120  d->mRemove->setIcon( KIcon( QLatin1String("list-remove") ) );
121  d->mRemove->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
122 
123  setSpacing( 4 );
124 
125  int index;
126  QList<FilterActionDesc*> list = MailCommon::FilterManager::filterActionDict()->list();
127  QList<FilterActionDesc*>::const_iterator it;
128  QList<FilterActionDesc*>::const_iterator end( list.constEnd() );
129  for ( index = 0, it = list.constBegin(); it != end; ++it, ++index ) {
130  //create an instance:
131  FilterAction *action = (*it)->create();
132 
133  // append to the list of actions:
134  d->mActionList.append( action );
135 
136  // add (i18n-ized) name to combo box
137  d->mComboBox->addItem( (*it)->label,(*it)->name );
138 
139  // Register the FilterAction modification signal
140  connect( action, SIGNAL(filterActionModified()), this, SIGNAL(filterModified()) );
141  }
142 
143  // widget for the case where no action is selected.
144  d->mComboBox->addItem( QLatin1String(" ") );
145  d->mComboBox->setCurrentIndex( index );
146 
147  // don't show scroll bars.
148  d->mComboBox->setMaxCount( d->mComboBox->count() );
149 
150  // layout management:
151  // o the combo box is not to be made larger than it's sizeHint(),
152  // the parameter widget should grow instead.
153  // o the whole widget takes all space horizontally, but is fixed vertically.
154  d->mComboBox->adjustSize();
155  d->mComboBox->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed ) );
156  setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed ) );
157  updateGeometry();
158 
159  // redirect focus to the filter action combo box
160  setFocusProxy( d->mComboBox );
161 
162  // now connect the combo box and the widget stack
163  connect( d->mComboBox, SIGNAL(activated(int)),
164  this, SLOT(slotFilterTypeChanged(int)) );
165 
166  connect( d->mComboBox, SIGNAL(activated(int)),
167  this, SIGNAL(filterModified()) );
168 
169  connect( d->mAdd, SIGNAL(clicked()),
170  this, SLOT(slotAddWidget()) );
171  connect( d->mRemove, SIGNAL(clicked()),
172  this, SLOT(slotRemoveWidget()) );
173 
174  d->setFilterAction();
175  d->mLayout->addWidget( d->mAdd, 1, 3 );
176  d->mLayout->addWidget( d->mRemove, 1, 4 );
177 
178 }
179 
180 FilterActionWidget::~FilterActionWidget()
181 {
182  delete d;
183 }
184 
185 void FilterActionWidget::updateAddRemoveButton( bool addButtonEnabled, bool removeButtonEnabled )
186 {
187  d->mAdd->setEnabled( addButtonEnabled );
188  d->mRemove->setEnabled( removeButtonEnabled );
189 }
190 
191 void FilterActionWidget::setAction( const FilterAction *action )
192 {
193  bool found = false;
194  const int count = d->mComboBox->count() - 1 ; // last entry is the empty one
195 
196  const QString name = action ? action->name() : QString();
197 
198  // find the index of typeOf(action) in mComboBox
199  // and clear the other widgets on the way.
200  for ( int i = 0; i < count; ++i ) {
201  if ( action && d->mComboBox->itemData( i ) == name ) {
202  d->setFilterAction( d->mActionList.at( i )->createParamWidget( this ) );
203 
204  //...set the parameter widget to the settings
205  // of aAction...
206  action->setParamWidgetValue( d->mLayout->itemAtPosition( 1, 2 )->widget() );
207 
208  //...and show the correct entry of
209  // the combo box
210  d->mComboBox->setCurrentIndex( i ); // (mm) also raise the widget, but doesn't
211  found = true;
212  }
213  }
214 
215  if ( found ) {
216  return;
217  }
218 
219  // not found, so set the empty widget
220  d->setFilterAction();
221 
222  d->mComboBox->setCurrentIndex( count ); // last item
223 }
224 
225 FilterAction *FilterActionWidget::action() const
226 {
227  // look up the action description via the label
228  // returned by KComboBox::currentText()...
229  FilterActionDesc *description =
230  MailCommon::FilterManager::filterActionDict()->value( d->mComboBox->itemData(d->mComboBox->currentIndex()).toString() );
231 
232  if ( description ) {
233  // ...create an instance...
234  FilterAction *action = description->create();
235  if ( action ) {
236  // ...and apply the setting of the parameter widget.
237  action->applyParamWidgetValue( d->mLayout->itemAtPosition( 1, 2 )->widget() );
238  return action;
239  }
240  }
241 
242  return 0;
243 }
244 
245 //=============================================================================
246 //
247 // class FilterActionWidgetLister (the filter action editor)
248 //
249 //=============================================================================
250 
251 class FilterActionWidgetLister::Private
252 {
253 public:
254  Private( FilterActionWidgetLister *qq )
255  : q( qq ), mActionList( 0 )
256  {
257  }
258 
259  void regenerateActionListFromWidgets();
260 
261  FilterActionWidgetLister *q;
262  QList<MailCommon::FilterAction*> *mActionList;
263 };
264 
265 void FilterActionWidgetLister::Private::regenerateActionListFromWidgets()
266 {
267  if ( !mActionList ) {
268  return;
269  }
270 
271  mActionList->clear();
272 
273  foreach ( const QWidget *widget, q->widgets() ) {
274  FilterAction *action = qobject_cast<const FilterActionWidget*>( widget )->action();
275  if ( action ) {
276  mActionList->append( action );
277  }
278  }
279  q->updateAddRemoveButton();
280 }
281 
282 FilterActionWidgetLister:: FilterActionWidgetLister( QWidget *parent )
283  : KWidgetLister( false, 1, FILTER_MAX_ACTIONS, parent ), d( new Private( this ) )
284 {
285 }
286 
287 FilterActionWidgetLister::~FilterActionWidgetLister()
288 {
289  delete d;
290 }
291 
292 void FilterActionWidgetLister::setActionList( QList<FilterAction*> *list )
293 {
294  Q_ASSERT( list );
295  if ( d->mActionList && d->mActionList != list ) {
296  d->regenerateActionListFromWidgets();
297  }
298 
299  d->mActionList = list;
300 
301  static_cast<QWidget*>( parent() )->setEnabled( true );
302 
303  if ( !widgets().isEmpty() ) { // move this below next 'if'?
304  widgets().first()->blockSignals(true);
305  }
306 
307  if ( list->isEmpty() ) {
308  slotClear();
309  widgets().first()->blockSignals(false);
310  return;
311  }
312 
313  int superfluousItems = (int)d->mActionList->count() - widgetsMaximum();
314  if ( superfluousItems > 0 ) {
315  kDebug() << "FilterActionWidgetLister: Clipping action list to"
316  << widgetsMaximum() << "items!";
317 
318  for ( ; superfluousItems ; superfluousItems-- ) {
319  d->mActionList->removeLast();
320  }
321  }
322 
323  // set the right number of widgets
324  setNumberOfShownWidgetsTo( d->mActionList->count() );
325 
326  // load the actions into the widgets
327  QList<QWidget*> widgetList = widgets();
328  QList<FilterAction*>::const_iterator aEnd( d->mActionList->constEnd() );
329  QList<QWidget*>::ConstIterator wIt = widgetList.constBegin();
330  QList<QWidget*>::ConstIterator wEnd = widgetList.constEnd();
331  for ( QList<FilterAction*>::const_iterator aIt = d->mActionList->constBegin();
332  ( aIt != aEnd && wIt != wEnd ); ++aIt, ++wIt ) {
333  FilterActionWidget *w = qobject_cast<FilterActionWidget*>( *wIt );
334  w->setAction( ( *aIt ) );
335  connect( w, SIGNAL(filterModified()),
336  this, SIGNAL(filterModified()), Qt::UniqueConnection );
337  reconnectWidget( w );
338  }
339  widgets().first()->blockSignals(false);
340  updateAddRemoveButton();
341 
342 }
343 
344 void FilterActionWidgetLister::slotAddWidget( QWidget *w )
345 {
346  addWidgetAfterThisWidget( w );
347  updateAddRemoveButton();
348 }
349 
350 void FilterActionWidgetLister::slotRemoveWidget( QWidget *w )
351 {
352  removeWidget( w );
353  updateAddRemoveButton();
354 }
355 
356 void FilterActionWidgetLister::updateAddRemoveButton()
357 {
358  QList<QWidget*> widgetList = widgets();
359  const int numberOfWidget( widgetList.count() );
360  bool addButtonEnabled = false;
361  bool removeButtonEnabled = false;
362  if ( numberOfWidget <= widgetsMinimum() ) {
363  addButtonEnabled = true;
364  removeButtonEnabled = false;
365  } else if ( numberOfWidget >= widgetsMaximum() ) {
366  addButtonEnabled = false;
367  removeButtonEnabled = true;
368  } else {
369  addButtonEnabled = true;
370  removeButtonEnabled = true;
371  }
372  QList<QWidget*>::ConstIterator wIt = widgetList.constBegin();
373  QList<QWidget*>::ConstIterator wEnd = widgetList.constEnd();
374  for ( ; wIt != wEnd ;++wIt ) {
375  FilterActionWidget *w = qobject_cast<FilterActionWidget*>( *wIt );
376  w->updateAddRemoveButton( addButtonEnabled, removeButtonEnabled );
377  }
378 }
379 
380 void FilterActionWidgetLister::updateActionList()
381 {
382  d->regenerateActionListFromWidgets();
383 }
384 
385 void FilterActionWidgetLister::reset()
386 {
387  if ( d->mActionList ) {
388  d->regenerateActionListFromWidgets();
389  }
390 
391  d->mActionList = 0;
392  slotClear();
393 
394  static_cast<QWidget*>( parent() )->setEnabled( false );
395 }
396 
397 void FilterActionWidgetLister::reconnectWidget( FilterActionWidget *w )
398 {
399  connect( w, SIGNAL(addWidget(QWidget*)),
400  this, SLOT(slotAddWidget(QWidget*)), Qt::UniqueConnection );
401 
402  connect( w, SIGNAL(removeWidget(QWidget*)),
403  this, SLOT(slotRemoveWidget(QWidget*)), Qt::UniqueConnection );
404 }
405 
406 QWidget *FilterActionWidgetLister::createWidget( QWidget *parent )
407 {
408  FilterActionWidget *w = new FilterActionWidget( parent );
409  reconnectWidget( w );
410  return w;
411 }
412 
413 void FilterActionWidgetLister::clearWidget( QWidget *widget )
414 {
415  if ( widget ) {
416  FilterActionWidget *w = static_cast<FilterActionWidget*>( widget );
417  w->setAction( 0 );
418  w->disconnect( this );
419  reconnectWidget( w ) ;
420  updateAddRemoveButton();
421  }
422 }
423 
424 #include "filteractionwidget.moc"
MailCommon::FilterActionWidget::action
MailCommon::FilterAction * action() const
Returns the filter action.
Definition: filteractionwidget.cpp:225
MailCommon::FilterActionWidgetLister::filterModified
void filterModified()
MailCommon::FilterActionDict::list
MAILCOMMON_EXPORT const QList< FilterActionDesc * > & list() const
Provides read-only access to a list of all known filter actions.
Definition: filteractiondict.cpp:108
MailCommon::FilterActionWidget::filterModified
void filterModified()
MailCommon::FilterActionWidgetLister::reset
void reset()
Resets the action widgets.
Definition: filteractionwidget.cpp:385
MailCommon::FilterActionWidgetLister
A container widget for a list of FilterActionWidgets.
Definition: filteractionwidget.h:115
QWidget
MailCommon::FilterActionWidgetLister::updateAddRemoveButton
void updateAddRemoveButton()
Definition: filteractionwidget.cpp:356
mailfilter.h
MailCommon::FILTER_MAX_ACTIONS
const int FILTER_MAX_ACTIONS
Definition: mailfilter.h:40
MailCommon::FilterActionWidgetLister::slotAddWidget
void slotAddWidget(QWidget *)
Definition: filteractionwidget.cpp:344
MailCommon::FilterActionWidgetLister::setActionList
void setActionList(QList< MailCommon::FilterAction * > *list)
Sets the list of filter actions, the lister will create FilterActionWidgets for.
Definition: filteractionwidget.cpp:292
MailCommon::FilterAction
Abstract base class for mail filter actions.
Definition: filteraction.h:52
MailCommon::FilterActionWidget::updateAddRemoveButton
void updateAddRemoveButton(bool addButtonEnabled, bool removeButtonEnabled)
Definition: filteractionwidget.cpp:185
MailCommon::FilterActionWidget::setAction
void setAction(const MailCommon::FilterAction *action)
Sets the filter action.
Definition: filteractionwidget.cpp:191
MailCommon::FilterActionWidget
A widget to edit a single MailCommon::FilterAction.
Definition: filteractionwidget.h:54
filteractionwidget.h
MailCommon::FilterActionWidget::FilterActionWidget
FilterActionWidget(QWidget *parent=0)
Creates a filter action widget with no type selected.
Definition: filteractionwidget.cpp:103
MailCommon::FilterActionWidgetLister::clearWidget
virtual void clearWidget(QWidget *)
Definition: filteractionwidget.cpp:413
MailCommon::FilterActionWidget::~FilterActionWidget
~FilterActionWidget()
Destroys the filter action widget.
Definition: filteractionwidget.cpp:180
MailCommon::FilterAction::name
QString name() const
Returns identifier name, ie.
Definition: filteraction.cpp:50
MailCommon::FilterActionWidgetLister::slotRemoveWidget
void slotRemoveWidget(QWidget *)
Definition: filteractionwidget.cpp:350
MailCommon::FilterActionDesc
Auxiliary struct for FilterActionDict.
Definition: filteractiondict.h:36
MailCommon::FilterManager::filterActionDict
static FilterActionDict * filterActionDict()
Returns the global filter action dictionary.
Definition: filtermanager.cpp:109
MailCommon::FilterActionWidgetLister::~FilterActionWidgetLister
virtual ~FilterActionWidgetLister()
Destroys the filter action widget lister.
Definition: filteractionwidget.cpp:287
MailCommon::FilterActionWidgetLister::reconnectWidget
void reconnectWidget(FilterActionWidget *w)
Definition: filteractionwidget.cpp:397
MailCommon::FilterAction::setParamWidgetValue
virtual void setParamWidgetValue(QWidget *paramWidget) const
The filter action shall set it's widget's contents from it's parameter.
Definition: filteraction.cpp:74
MailCommon::FilterActionDesc::create
FilterActionNewFunc create
Definition: filteractiondict.h:39
KHBox
filtermanager.h
MailCommon::FilterActionWidgetLister::FilterActionWidgetLister
FilterActionWidgetLister(QWidget *parent=0)
Creates a new filter action widget lister.
Definition: filteractionwidget.cpp:282
MailCommon::FilterActionWidgetLister::createWidget
virtual QWidget * createWidget(QWidget *)
Definition: filteractionwidget.cpp:406
MailCommon::FilterAction::applyParamWidgetValue
virtual void applyParamWidgetValue(QWidget *paramWidget)
The filter action shall set it's parameter from the widget's contents.
Definition: filteraction.cpp:70
filteraction.h
MailCommon::FilterActionWidgetLister::updateActionList
void updateActionList()
Updates the action list according to the current action widget values.
Definition: filteractionwidget.cpp:380
filteractiondict.h
QList
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