• 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
tagrulewidgethandler.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 "tagrulewidgethandler.h"
19 #include "search/searchpattern.h"
20 #include "widgets/regexplineedit.h"
21 using MailCommon::RegExpLineEdit;
22 
23 #include <pimcommon/widgets/minimumcombobox.h>
24 
25 #include <KDebug>
26 #include <KIcon>
27 #include <KLocale>
28 
29 #include <Nepomuk2/Tag>
30 
31 
32 #include <QLineEdit>
33 #include <QStackedWidget>
34 
35 using namespace MailCommon;
36 
37 static const struct {
38  SearchRule::Function id;
39  const char *displayName;
40 } TagFunctions[] = {
41  { SearchRule::FuncContains, I18N_NOOP( "contains" ) },
42  { SearchRule::FuncContainsNot, I18N_NOOP( "does not contain" ) },
43  { SearchRule::FuncEquals, I18N_NOOP( "equals" ) },
44  { SearchRule::FuncNotEqual, I18N_NOOP( "does not equal" ) },
45  { SearchRule::FuncRegExp, I18N_NOOP( "matches regular expr." ) },
46  { SearchRule::FuncNotRegExp, I18N_NOOP( "does not match reg. expr." ) }
47 };
48 static const int TagFunctionCount =
49  sizeof( TagFunctions ) / sizeof( *TagFunctions );
50 
51 //---------------------------------------------------------------------------
52 
53 QWidget *TagRuleWidgetHandler::createFunctionWidget(
54  int number, QStackedWidget *functionStack, const QObject *receiver, bool isNepomukSearch ) const
55 {
56  if ( number != 0 ) {
57  return 0;
58  }
59 
60  PimCommon::MinimumComboBox *funcCombo = new PimCommon::MinimumComboBox( functionStack );
61  funcCombo->setObjectName( QLatin1String("tagRuleFuncCombo") );
62  for ( int i = 0; i < TagFunctionCount; ++i ) {
63  if (isNepomukSearch) {
64  if (TagFunctions[i].id == SearchRule::FuncContains || TagFunctions[i].id == SearchRule::FuncContainsNot) {
65  funcCombo->addItem( i18n( TagFunctions[i].displayName ) );
66  }
67  } else {
68  funcCombo->addItem( i18n( TagFunctions[i].displayName ) );
69  }
70  }
71  funcCombo->adjustSize();
72  QObject::connect( funcCombo, SIGNAL(activated(int)),
73  receiver, SLOT(slotFunctionChanged()) );
74  return funcCombo;
75 }
76 
77 //---------------------------------------------------------------------------
78 
79 QWidget *TagRuleWidgetHandler::createValueWidget( int number,
80  QStackedWidget *valueStack,
81  const QObject *receiver ) const
82 {
83  if ( number == 0 ) {
84  RegExpLineEdit *lineEdit = new RegExpLineEdit( valueStack );
85  lineEdit->setObjectName( QLatin1String("tagRuleRegExpLineEdit") );
86  QObject::connect( lineEdit, SIGNAL(textChanged(QString)),
87  receiver, SLOT(slotValueChanged()) );
88  QObject::connect( lineEdit, SIGNAL(returnPressed()),
89  receiver, SLOT(slotReturnPressed()) );
90  return lineEdit;
91  }
92 
93  if ( number == 1 ) {
94  PimCommon::MinimumComboBox *valueCombo = new PimCommon::MinimumComboBox( valueStack );
95  valueCombo->setObjectName( QLatin1String("tagRuleValueCombo") );
96  valueCombo->setEditable( true );
97  valueCombo->addItem( QString() ); // empty entry for user input
98  foreach ( const Nepomuk2::Tag &tag, Nepomuk2::Tag::allTags() ) {
99  QString iconName = tag.genericIcon();
100  if ( iconName.isEmpty() )
101  iconName = QLatin1String("mail-tagged");
102  valueCombo->addItem( KIcon( iconName ), tag.label(), tag.uri() );
103  }
104  valueCombo->adjustSize();
105  QObject::connect( valueCombo, SIGNAL(activated(int)),
106  receiver, SLOT(slotValueChanged()) );
107  return valueCombo;
108  }
109 
110  return 0;
111 }
112 
113 //---------------------------------------------------------------------------
114 
115 SearchRule::Function TagRuleWidgetHandler::function( const QByteArray &field,
116  const QStackedWidget *functionStack ) const
117 {
118  if ( !handlesField( field ) ) {
119  return SearchRule::FuncNone;
120  }
121 
122  const PimCommon::MinimumComboBox *funcCombo =
123  functionStack->findChild<PimCommon::MinimumComboBox*>( QLatin1String("tagRuleFuncCombo") );
124 
125  if ( funcCombo && funcCombo->currentIndex() >= 0 ) {
126  return TagFunctions[funcCombo->currentIndex()].id;
127  }
128  return SearchRule::FuncNone;
129 }
130 
131 //---------------------------------------------------------------------------
132 
133 QString TagRuleWidgetHandler::value( const QByteArray &field,
134  const QStackedWidget *functionStack,
135  const QStackedWidget *valueStack ) const
136 {
137  if ( !handlesField( field ) ) {
138  return QString();
139  }
140 
141  SearchRule::Function func = function( field, functionStack );
142  if ( func == SearchRule::FuncRegExp || func == SearchRule::FuncNotRegExp ) {
143  // Use regexp line edit
144  const RegExpLineEdit *lineEdit =
145  valueStack->findChild<RegExpLineEdit*>( QLatin1String("tagRuleRegExpLineEdit") );
146 
147  if ( lineEdit ) {
148  return lineEdit->text();
149  } else {
150  return QString();
151  }
152  }
153 
154  // Use combo box
155  const PimCommon::MinimumComboBox *tagCombo =
156  valueStack->findChild<PimCommon::MinimumComboBox*>( QLatin1String("tagRuleValueCombo") );
157 
158  if ( tagCombo ) {
159  return tagCombo->currentText();
160  } else {
161  return QString();
162  }
163 }
164 
165 //---------------------------------------------------------------------------
166 
167 QString TagRuleWidgetHandler::prettyValue( const QByteArray &field,
168  const QStackedWidget *funcStack,
169  const QStackedWidget *valueStack ) const
170 {
171  return value( field, funcStack, valueStack );
172 }
173 
174 //---------------------------------------------------------------------------
175 
176 bool TagRuleWidgetHandler::handlesField( const QByteArray &field ) const
177 {
178  return ( field == "<tag>" );
179 }
180 
181 //---------------------------------------------------------------------------
182 
183 void TagRuleWidgetHandler::reset( QStackedWidget *functionStack,
184  QStackedWidget *valueStack ) const
185 {
186  // reset the function combo box
187  PimCommon::MinimumComboBox *funcCombo =
188  functionStack->findChild<PimCommon::MinimumComboBox*>( QLatin1String("tagRuleFuncCombo") );
189 
190  if ( funcCombo ) {
191  funcCombo->blockSignals( true );
192  funcCombo->setCurrentIndex( 0 );
193  funcCombo->blockSignals( false );
194  }
195 
196  // reset the status value combo box and reg exp line edit
197  RegExpLineEdit *lineEdit =
198  valueStack->findChild<RegExpLineEdit*>( QLatin1String("tagRuleRegExpLineEdit") );
199 
200  if ( lineEdit ) {
201  lineEdit->blockSignals( true );
202  lineEdit->clear();
203  lineEdit->blockSignals( false );
204  lineEdit->showEditButton( false );
205  valueStack->setCurrentWidget( lineEdit );
206  }
207 
208  PimCommon::MinimumComboBox *tagCombo = valueStack->findChild<PimCommon::MinimumComboBox*>( QLatin1String("tagRuleValueCombo") );
209  if ( tagCombo ) {
210  tagCombo->blockSignals( true );
211  tagCombo->setCurrentIndex( 0 );
212  tagCombo->blockSignals( false );
213  }
214 }
215 
216 //---------------------------------------------------------------------------
217 
218 bool TagRuleWidgetHandler::setRule( QStackedWidget *functionStack,
219  QStackedWidget *valueStack,
220  const SearchRule::Ptr rule, bool isNepomukSearch ) const
221 {
222  if ( !rule || !handlesField( rule->field() ) ) {
223  reset( functionStack, valueStack );
224  return false;
225  }
226 
227  // set the function
228  const SearchRule::Function func = rule->function();
229 
230  if (isNepomukSearch ) {
231  if(func != SearchRule::FuncContains && func != SearchRule::FuncContainsNot) {
232  reset( functionStack, valueStack );
233  return false;
234  }
235  }
236 
237  int funcIndex = 0;
238  for ( ; funcIndex < TagFunctionCount; ++funcIndex ) {
239  if ( func == TagFunctions[funcIndex].id ) {
240  break;
241  }
242  }
243 
244  PimCommon::MinimumComboBox *funcCombo =
245  functionStack->findChild<PimCommon::MinimumComboBox*>( QLatin1String("tagRuleFuncCombo") );
246 
247  if ( funcCombo ) {
248  funcCombo->blockSignals( true );
249  if ( funcIndex < TagFunctionCount ) {
250  funcCombo->setCurrentIndex( funcIndex );
251  } else {
252  funcCombo->setCurrentIndex( 0 );
253  }
254  funcCombo->blockSignals( false );
255  functionStack->setCurrentWidget( funcCombo );
256  }
257 
258  // set the value
259  if ( func == SearchRule::FuncRegExp || func == SearchRule::FuncNotRegExp ) {
260  // set reg exp value
261  RegExpLineEdit *lineEdit = valueStack->findChild<RegExpLineEdit*>( QLatin1String("tagRuleRegExpLineEdit") );
262 
263  if ( lineEdit ) {
264  lineEdit->blockSignals( true );
265  lineEdit->setText( rule->contents() );
266  lineEdit->blockSignals( false );
267  lineEdit->showEditButton( true );
268  valueStack->setCurrentWidget( lineEdit );
269  }
270  } else {
271  // set combo box value
272  int valueIndex = -1;
273  int tagIndex = 0;
274  foreach ( const Nepomuk2::Tag &tag, Nepomuk2::Tag::allTags() ) {
275  if ( tag.label() == rule->contents() ) {
276  valueIndex = tagIndex;
277  break;
278  }
279  tagIndex++;
280  }
281 
282  PimCommon::MinimumComboBox *tagCombo =
283  valueStack->findChild<PimCommon::MinimumComboBox*>( QLatin1String("tagRuleValueCombo") );
284 
285  if ( tagCombo ) {
286  tagCombo->blockSignals( true );
287  if ( valueIndex == -1 ) {
288  tagCombo->setCurrentIndex( 0 );
289  // Still show tag if it was deleted from MsgTagMgr
290  QLineEdit *lineEdit = tagCombo->lineEdit(); // krazy:exclude=qclasses
291  Q_ASSERT( lineEdit );
292  lineEdit->setText( rule->contents() );
293  } else {
294  // Existing tags numbered from 1
295  tagCombo->setCurrentIndex( valueIndex + 1 );
296  }
297  tagCombo->blockSignals( false );
298  valueStack->setCurrentWidget( tagCombo );
299  }
300  }
301  return true;
302 }
303 
304 //---------------------------------------------------------------------------
305 
306 bool TagRuleWidgetHandler::update( const QByteArray &field,
307  QStackedWidget *functionStack,
308  QStackedWidget *valueStack ) const
309 {
310  if ( !handlesField( field ) ) {
311  return false;
312  }
313 
314  // raise the correct function widget
315  functionStack->setCurrentWidget( functionStack->findChild<QWidget*>( QLatin1String("tagRuleFuncCombo") ) );
316 
317  // raise the correct value widget
318  SearchRule::Function func = function( field, functionStack );
319  if ( func == SearchRule::FuncRegExp || func == SearchRule::FuncNotRegExp ) {
320  valueStack->setCurrentWidget( valueStack->findChild<QWidget*>( QLatin1String("tagRuleRegExpLineEdit" )) );
321  } else {
322  valueStack->setCurrentWidget( valueStack->findChild<QWidget*>( QLatin1String("tagRuleValueCombo") ) );
323  }
324 
325  return true;
326 }
327 
MailCommon::TagRuleWidgetHandler::prettyValue
QString prettyValue(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
Definition: tagrulewidgethandler.cpp:167
id
SearchRule::Function id
Definition: tagrulewidgethandler.cpp:38
MailCommon::SearchRule::FuncContainsNot
Definition: searchpattern.h:83
MailCommon::RegExpLineEdit::setText
void setText(const QString &)
Definition: regexplineedit.cpp:102
QWidget
MailCommon::RegExpLineEdit
Definition: regexplineedit.h:46
MailCommon::TagRuleWidgetHandler::reset
void reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
Definition: tagrulewidgethandler.cpp:183
displayName
const char * displayName
Definition: tagrulewidgethandler.cpp:39
MailCommon::TagRuleWidgetHandler::value
QString value(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
Definition: tagrulewidgethandler.cpp:133
MailCommon::RegExpLineEdit::clear
void clear()
Definition: regexplineedit.cpp:92
MailCommon::SearchRule::Ptr
boost::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchpattern.h:69
QObject
MailCommon::TagRuleWidgetHandler::handlesField
bool handlesField(const QByteArray &field) const
Definition: tagrulewidgethandler.cpp:176
MailCommon::SearchRule::FuncNotRegExp
Definition: searchpattern.h:87
MailCommon::TagRuleWidgetHandler::setRule
bool setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule, bool isNepomukSearch) const
Definition: tagrulewidgethandler.cpp:218
MailCommon::SearchRule::Function
Function
Describes operators for comparison of field and contents.
Definition: searchpattern.h:80
MailCommon::SearchRule::FuncNotEqual
Definition: searchpattern.h:85
MailCommon::TagRuleWidgetHandler::function
SearchRule::Function function(const QByteArray &field, const QStackedWidget *functionStack) const
Definition: tagrulewidgethandler.cpp:115
MailCommon::TagRuleWidgetHandler::update
bool update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
Definition: tagrulewidgethandler.cpp:306
MailCommon::SearchRule::FuncNone
Definition: searchpattern.h:81
MailCommon::SearchRule::FuncRegExp
Definition: searchpattern.h:86
searchpattern.h
regexplineedit.h
MailCommon::SearchRule::FuncContains
Definition: searchpattern.h:82
TagFunctions
static const struct @9 TagFunctions[]
tagrulewidgethandler.h
I18N_NOOP
#define I18N_NOOP(t)
Definition: searchpatternedit.cpp:46
MailCommon::RegExpLineEdit::showEditButton
void showEditButton(bool)
Definition: regexplineedit.cpp:107
MailCommon::TagRuleWidgetHandler::createValueWidget
QWidget * createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
Definition: tagrulewidgethandler.cpp:79
MailCommon::SearchRule::FuncEquals
Definition: searchpattern.h:84
MailCommon::TagRuleWidgetHandler::createFunctionWidget
QWidget * createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool isNepomukSearch) const
Definition: tagrulewidgethandler.cpp:53
TagFunctionCount
static const int TagFunctionCount
Definition: tagrulewidgethandler.cpp:48
MailCommon::RegExpLineEdit::text
QString text() const
Definition: regexplineedit.cpp:97
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