• 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
  • search
numericdoublerulewidgethandler.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013 Montel Laurent <montel@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "numericdoublerulewidgethandler.h"
19 #include <pimcommon/widgets/minimumcombobox.h>
20 #include "search/searchpattern.h"
21 #include "widgets/regexplineedit.h"
22 using MailCommon::RegExpLineEdit;
23 
24 
25 #include <KDebug>
26 #include <KLocale>
27 
28 #include <KDoubleNumInput>
29 #include <QStackedWidget>
30 
31 
32 using namespace MailCommon;
33 
34 static const struct {
35  SearchRule::Function id;
36  const char *displayName;
37 } NumericFunctions[] = {
38  { SearchRule::FuncEquals, I18N_NOOP( "is equal to" ) },
39  { SearchRule::FuncNotEqual, I18N_NOOP( "is not equal to" ) },
40  { SearchRule::FuncIsGreater, I18N_NOOP( "is greater than" ) },
41  { SearchRule::FuncIsLessOrEqual, I18N_NOOP( "is less than or equal to" ) },
42  { SearchRule::FuncIsLess, I18N_NOOP( "is less than" ) },
43  { SearchRule::FuncIsGreaterOrEqual, I18N_NOOP( "is greater than or equal to" ) }
44 };
45 static const int NumericFunctionCount =
46  sizeof( NumericFunctions ) / sizeof( *NumericFunctions );
47 
48 
49 QWidget *NumericDoubleRuleWidgetHandler::createFunctionWidget(
50  int number, QStackedWidget *functionStack, const QObject *receiver, bool /*isNepomukSearch*/ ) const
51 {
52  if ( number != 0 ) {
53  return 0;
54  }
55 
56  PimCommon::MinimumComboBox *funcCombo = new PimCommon::MinimumComboBox( functionStack );
57  funcCombo->setObjectName( QLatin1String("numericDoubleRuleFuncCombo") );
58  for ( int i = 0; i < NumericFunctionCount; ++i ) {
59  funcCombo->addItem( i18n( NumericFunctions[i].displayName ) );
60  }
61  funcCombo->adjustSize();
62  QObject::connect( funcCombo, SIGNAL(activated(int)),
63  receiver, SLOT(slotFunctionChanged()) );
64  return funcCombo;
65 }
66 
67 //---------------------------------------------------------------------------
68 
69 QWidget *NumericDoubleRuleWidgetHandler::createValueWidget( int number,
70  QStackedWidget *valueStack,
71  const QObject *receiver ) const
72 {
73  if ( number != 0 ) {
74  return 0;
75  }
76 
77  KDoubleNumInput *numInput = new KDoubleNumInput( valueStack );
78  numInput->setSliderEnabled( false );
79  numInput->setObjectName( QLatin1String("KDoubleNumInput") );
80  QObject::connect( numInput, SIGNAL(valueChanged(double)),
81  receiver, SLOT(slotValueChanged()) );
82  return numInput;
83 }
84 
85 //---------------------------------------------------------------------------
86 
87 SearchRule::Function NumericDoubleRuleWidgetHandler::currentFunction(
88  const QStackedWidget *functionStack ) const
89 {
90  const PimCommon::MinimumComboBox *funcCombo =
91  functionStack->findChild<PimCommon::MinimumComboBox*>( QLatin1String("numericDoubleRuleFuncCombo") );
92 
93  if ( funcCombo && funcCombo->currentIndex() >= 0 ) {
94  return NumericFunctions[funcCombo->currentIndex()].id;
95  }
96 
97  return SearchRule::FuncNone;
98 }
99 
100 //---------------------------------------------------------------------------
101 
102 SearchRule::Function NumericDoubleRuleWidgetHandler::function( const QByteArray &field,
103  const QStackedWidget *functionStack ) const
104 {
105  if ( !handlesField( field ) ) {
106  return SearchRule::FuncNone;
107  }
108 
109  return currentFunction( functionStack );
110 }
111 
112 //---------------------------------------------------------------------------
113 
114 QString NumericDoubleRuleWidgetHandler::currentValue( const QStackedWidget *valueStack ) const
115 {
116  const KDoubleNumInput *numInput = valueStack->findChild<KDoubleNumInput*>( QLatin1String("KDoubleNumInput") );
117 
118  if ( numInput ) {
119  return QString::number( int(numInput->value()*1024) );
120  }
121 
122  return QString();
123 }
124 
125 //---------------------------------------------------------------------------
126 
127 QString NumericDoubleRuleWidgetHandler::value( const QByteArray &field,
128  const QStackedWidget *,
129  const QStackedWidget *valueStack ) const
130 {
131  if ( !handlesField( field ) ) {
132  return QString();
133  }
134 
135  return currentValue( valueStack );
136 }
137 
138 //---------------------------------------------------------------------------
139 
140 QString NumericDoubleRuleWidgetHandler::prettyValue( const QByteArray &field,
141  const QStackedWidget *,
142  const QStackedWidget *valueStack ) const
143 {
144  if ( !handlesField( field ) ) {
145  return QString();
146  }
147 
148  return currentValue( valueStack );
149 }
150 
151 //---------------------------------------------------------------------------
152 
153 bool NumericDoubleRuleWidgetHandler::handlesField( const QByteArray &field ) const
154 {
155  return field == "<size>";
156 }
157 
158 //---------------------------------------------------------------------------
159 
160 void NumericDoubleRuleWidgetHandler::reset( QStackedWidget *functionStack,
161  QStackedWidget *valueStack ) const
162 {
163  // reset the function combo box
164  PimCommon::MinimumComboBox *funcCombo =
165  functionStack->findChild<PimCommon::MinimumComboBox*>( QLatin1String("numericDoubleRuleFuncCombo") );
166 
167  if ( funcCombo ) {
168  funcCombo->blockSignals( true );
169  funcCombo->setCurrentIndex( 0 );
170  funcCombo->blockSignals( false );
171  }
172 
173  // reset the value widget
174  KDoubleNumInput *numInput = valueStack->findChild<KDoubleNumInput*>( QLatin1String("KDoubleNumInput") );
175 
176  if ( numInput ) {
177  numInput->blockSignals( true );
178  numInput->setValue( 0.0 );
179  numInput->blockSignals( false );
180  }
181 }
182 
183 //---------------------------------------------------------------------------
184 
185 void initDoubleNumInput( KDoubleNumInput *numInput, const QByteArray &field )
186 {
187  if ( field == "<size>" ) {
188  numInput->setMinimum( 0 );
189  numInput->setSingleStep(1);
190  numInput->setSuffix( i18nc( "spinbox suffix: unit for kilobyte", " kB" ) );
191  numInput->setSliderEnabled( false );
192  }
193 }
194 
195 //---------------------------------------------------------------------------
196 
197 bool NumericDoubleRuleWidgetHandler::setRule( QStackedWidget *functionStack,
198  QStackedWidget *valueStack,
199  const SearchRule::Ptr rule, bool /*isNepomukSearch*/ ) const
200 {
201  if ( !rule || !handlesField( rule->field() ) ) {
202  reset( functionStack, valueStack );
203  return false;
204  }
205 
206  // set the function
207  const SearchRule::Function func = rule->function();
208  int funcIndex = 0;
209  for ( ; funcIndex < NumericFunctionCount; ++funcIndex ) {
210  if ( func == NumericFunctions[funcIndex].id ) {
211  break;
212  }
213  }
214 
215  PimCommon::MinimumComboBox *funcCombo =
216  functionStack->findChild<PimCommon::MinimumComboBox*>( QLatin1String("numericDoubleRuleFuncCombo") );
217 
218  if ( funcCombo ) {
219  funcCombo->blockSignals( true );
220  if ( funcIndex < NumericFunctionCount ) {
221  funcCombo->setCurrentIndex( funcIndex );
222  } else {
223  funcCombo->setCurrentIndex( 0 );
224  }
225  funcCombo->blockSignals( false );
226  functionStack->setCurrentWidget( funcCombo );
227  }
228 
229  // set the value
230  bool ok;
231  int value = rule->contents().toInt( &ok );
232  if ( !ok ) {
233  value = 0;
234  }
235 
236  KDoubleNumInput *numInput = valueStack->findChild<KDoubleNumInput*>( QLatin1String("KDoubleNumInput") );
237 
238  if ( numInput ) {
239  initDoubleNumInput( numInput, rule->field() );
240  numInput->blockSignals( true );
241  numInput->setValue( value/(1024.0) );
242  numInput->blockSignals( false );
243  valueStack->setCurrentWidget( numInput );
244  }
245  return true;
246 }
247 
248 //---------------------------------------------------------------------------
249 
250 bool NumericDoubleRuleWidgetHandler::update( const QByteArray &field,
251  QStackedWidget *functionStack,
252  QStackedWidget *valueStack ) const
253 {
254  if ( !handlesField( field ) ) {
255  return false;
256  }
257 
258  // raise the correct function widget
259  functionStack->setCurrentWidget( functionStack->findChild<QWidget*>( QLatin1String("numericDoubleRuleFuncCombo") ) );
260 
261  // raise the correct value widget
262  KDoubleNumInput *numInput = valueStack->findChild<KDoubleNumInput*>( QLatin1String("KDoubleNumInput") );
263 
264  if ( numInput ) {
265  initDoubleNumInput( numInput, field );
266  valueStack->setCurrentWidget( numInput );
267  }
268  return true;
269 }
MailCommon::NumericDoubleRuleWidgetHandler::createFunctionWidget
QWidget * createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool isNepomukSearch) const
Definition: numericdoublerulewidgethandler.cpp:49
MailCommon::SearchRule::FuncIsGreaterOrEqual
Definition: searchpattern.h:91
numericdoublerulewidgethandler.h
MailCommon::NumericDoubleRuleWidgetHandler::prettyValue
QString prettyValue(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
Definition: numericdoublerulewidgethandler.cpp:140
MailCommon::NumericDoubleRuleWidgetHandler::createValueWidget
QWidget * createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
Definition: numericdoublerulewidgethandler.cpp:69
QWidget
MailCommon::RegExpLineEdit
Definition: regexplineedit.h:46
MailCommon::SearchRule::Ptr
boost::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchpattern.h:69
QObject
NumericFunctionCount
static const int NumericFunctionCount
Definition: numericdoublerulewidgethandler.cpp:45
MailCommon::SearchRule::Function
Function
Describes operators for comparison of field and contents.
Definition: searchpattern.h:80
MailCommon::SearchRule::FuncNotEqual
Definition: searchpattern.h:85
NumericFunctions
static const struct @4 NumericFunctions[]
MailCommon::SearchRule::FuncNone
Definition: searchpattern.h:81
id
SearchRule::Function id
Definition: numericdoublerulewidgethandler.cpp:35
MailCommon::NumericDoubleRuleWidgetHandler::handlesField
bool handlesField(const QByteArray &field) const
Definition: numericdoublerulewidgethandler.cpp:153
displayName
const char * displayName
Definition: numericdoublerulewidgethandler.cpp:36
searchpattern.h
regexplineedit.h
MailCommon::SearchRule::FuncIsGreater
Definition: searchpattern.h:88
MailCommon::NumericDoubleRuleWidgetHandler::reset
void reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
Definition: numericdoublerulewidgethandler.cpp:160
MailCommon::SearchRule::FuncIsLessOrEqual
Definition: searchpattern.h:89
initDoubleNumInput
void initDoubleNumInput(KDoubleNumInput *numInput, const QByteArray &field)
Definition: numericdoublerulewidgethandler.cpp:185
MailCommon::NumericDoubleRuleWidgetHandler::setRule
bool setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule, bool isNepomukSearch) const
Definition: numericdoublerulewidgethandler.cpp:197
I18N_NOOP
#define I18N_NOOP(t)
Definition: searchpatternedit.cpp:46
MailCommon::NumericDoubleRuleWidgetHandler::update
bool update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
Definition: numericdoublerulewidgethandler.cpp:250
MailCommon::SearchRule::FuncIsLess
Definition: searchpattern.h:90
MailCommon::NumericDoubleRuleWidgetHandler::function
SearchRule::Function function(const QByteArray &field, const QStackedWidget *functionStack) const
Definition: numericdoublerulewidgethandler.cpp:102
MailCommon::SearchRule::FuncEquals
Definition: searchpattern.h:84
MailCommon::NumericDoubleRuleWidgetHandler::value
QString value(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
Definition: numericdoublerulewidgethandler.cpp:127
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:15 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