Mailcommon

textrulerwidgethandler.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2022 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "textrulerwidgethandler.h"
8 
9 #include "search/searchpattern.h"
10 #include <KLineEdit>
11 #include <KLocalizedString>
12 #include <QComboBox>
13 #include <QStackedWidget>
14 
15 using namespace MailCommon;
16 
17 #include <KLazyLocalizedString>
18 #include <QLabel>
19 
20 // also see SearchRule::matches() and SearchRule::Function
21 // if you change the following strings!
22 static const struct {
24  const KLazyLocalizedString displayName;
25 } TextFunctions[] = {{SearchRule::FuncContains, kli18n("contains")},
26  {SearchRule::FuncContainsNot, kli18n("does not contain")},
27  {SearchRule::FuncEquals, kli18n("equals")},
28  {SearchRule::FuncNotEqual, kli18n("does not equal")},
29  {SearchRule::FuncStartWith, kli18n("starts with")},
30  {SearchRule::FuncNotStartWith, kli18n("does not start with")},
31  {SearchRule::FuncEndWith, kli18n("ends with")},
32  {SearchRule::FuncNotEndWith, kli18n("does not end with")},
33 
34  {SearchRule::FuncRegExp, kli18n("matches regular expr.")},
35  {SearchRule::FuncNotRegExp, kli18n("does not match reg. expr.")}};
36 static const int TextFunctionCount = sizeof(TextFunctions) / sizeof(*TextFunctions);
37 
38 //---------------------------------------------------------------------------
39 
40 QWidget *TextRuleWidgetHandler::createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool /*isBalooSearch*/) const
41 {
42  if (number != 0) {
43  return nullptr;
44  }
45 
46  auto funcCombo = new QComboBox(functionStack);
47  funcCombo->setMinimumWidth(50);
48  funcCombo->setObjectName(QStringLiteral("textRuleFuncCombo"));
49  for (int i = 0; i < TextFunctionCount; ++i) {
50  funcCombo->addItem(TextFunctions[i].displayName.toString());
51  }
52  funcCombo->adjustSize();
53  QObject::connect(funcCombo, SIGNAL(activated(int)), receiver, SLOT(slotFunctionChanged()));
54  return funcCombo;
55 }
56 
57 //---------------------------------------------------------------------------
58 
59 QWidget *TextRuleWidgetHandler::createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
60 {
61  if (number == 0) {
62  auto lineEdit = new KLineEdit(valueStack);
63  lineEdit->setClearButtonEnabled(true);
64  lineEdit->setTrapReturnKey(true);
65  lineEdit->setObjectName(QStringLiteral("regExpLineEdit"));
66  QObject::connect(lineEdit, SIGNAL(textChanged(QString)), receiver, SLOT(slotValueChanged()));
67  QObject::connect(lineEdit, SIGNAL(returnPressed()), receiver, SLOT(slotReturnPressed()));
68  return lineEdit;
69  }
70 
71  // blank QLabel to hide value widget for in-address-book rule
72  if (number == 1) {
73  auto label = new QLabel(valueStack);
74  label->setObjectName(QStringLiteral("textRuleValueHider"));
75  label->setBuddy(valueStack);
76  return label;
77  }
78  return nullptr;
79 }
80 
81 //---------------------------------------------------------------------------
82 
83 SearchRule::Function TextRuleWidgetHandler::currentFunction(const QStackedWidget *functionStack) const
84 {
85  const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("textRuleFuncCombo"));
86 
87  if (funcCombo && funcCombo->currentIndex() >= 0) {
88  return TextFunctions[funcCombo->currentIndex()].id;
89  }
90 
91  return SearchRule::FuncNone;
92 }
93 
94 //---------------------------------------------------------------------------
95 
96 SearchRule::Function TextRuleWidgetHandler::function(const QByteArray &, const QStackedWidget *functionStack) const
97 {
98  return currentFunction(functionStack);
99 }
100 
101 //---------------------------------------------------------------------------
102 
103 QString TextRuleWidgetHandler::currentValue(const QStackedWidget *valueStack, SearchRule::Function) const
104 {
105  // in other cases of func it is a lineedit
106  const KLineEdit *lineEdit = valueStack->findChild<KLineEdit *>(QStringLiteral("regExpLineEdit"));
107 
108  if (lineEdit) {
109  return lineEdit->text();
110  }
111 
112  // or anything else, like addressbook
113  return {};
114 }
115 
116 //---------------------------------------------------------------------------
117 
118 QString TextRuleWidgetHandler::value(const QByteArray &, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
119 {
120  SearchRule::Function func = currentFunction(functionStack);
121  return currentValue(valueStack, func);
122 }
123 
124 //---------------------------------------------------------------------------
125 
126 QString TextRuleWidgetHandler::prettyValue(const QByteArray &, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
127 {
128  SearchRule::Function func = currentFunction(functionStack);
129  return currentValue(valueStack, func);
130 }
131 
132 //---------------------------------------------------------------------------
133 
134 bool TextRuleWidgetHandler::handlesField(const QByteArray &) const
135 {
136  return true; // we handle all fields (as fallback)
137 }
138 
139 //---------------------------------------------------------------------------
140 
141 void TextRuleWidgetHandler::reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
142 {
143  // reset the function combo box
144  const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("textRuleFuncCombo"));
145 
146  if (funcCombo) {
147  funcCombo->blockSignals(true);
148  funcCombo->setCurrentIndex(0);
149  funcCombo->blockSignals(false);
150  }
151 
152  // reset the value widget
153  auto *lineEdit = valueStack->findChild<KLineEdit *>(QStringLiteral("regExpLineEdit"));
154  if (lineEdit) {
155  lineEdit->blockSignals(true);
156  lineEdit->clear();
157  lineEdit->blockSignals(false);
158  lineEdit->setClearButtonEnabled(false);
159  lineEdit->setClearButtonEnabled(true);
160  valueStack->setCurrentWidget(lineEdit);
161  }
162 }
163 
164 //---------------------------------------------------------------------------
165 
166 bool TextRuleWidgetHandler::setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule, bool /*isBalooSearch*/) const
167 {
168  if (!rule) {
169  reset(functionStack, valueStack);
170  return false;
171  }
172 
173  const SearchRule::Function func = rule->function();
174  int i = 0;
175  for (; i < TextFunctionCount; ++i) {
176  if (func == TextFunctions[i].id) {
177  break;
178  }
179  }
180 
181  const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("textRuleFuncCombo"));
182 
183  if (funcCombo) {
184  funcCombo->blockSignals(true);
185  if (i < TextFunctionCount) {
186  funcCombo->setCurrentIndex(i);
187  } else {
188  funcCombo->setCurrentIndex(0);
189  }
190  funcCombo->blockSignals(false);
191  functionStack->setCurrentWidget(funcCombo);
192  }
193  auto *lineEdit = valueStack->findChild<KLineEdit *>(QStringLiteral("regExpLineEdit"));
194 
195  if (lineEdit) {
196  lineEdit->blockSignals(true);
197  lineEdit->setText(rule->contents());
198  lineEdit->blockSignals(false);
199  lineEdit->setClearButtonEnabled(false);
200  lineEdit->setClearButtonEnabled(true);
201  valueStack->setCurrentWidget(lineEdit);
202  }
203  return true;
204 }
205 
206 //---------------------------------------------------------------------------
207 
208 bool TextRuleWidgetHandler::update(const QByteArray &, QStackedWidget *functionStack, QStackedWidget *valueStack) const
209 {
210  // raise the correct function widget
211  functionStack->setCurrentWidget(functionStack->findChild<QWidget *>(QStringLiteral("textRuleFuncCombo")));
212 
213  // raise the correct value widget
214  SearchRule::Function func = currentFunction(functionStack);
215  if (func == SearchRule::FuncIsInAddressbook || func == SearchRule::FuncIsNotInAddressbook) {
216  valueStack->setCurrentWidget(valueStack->findChild<QWidget *>(QStringLiteral("textRuleValueHider")));
217  } else {
218  auto *lineEdit = valueStack->findChild<KLineEdit *>(QStringLiteral("regExpLineEdit"));
219 
220  if (lineEdit) {
221  valueStack->setCurrentWidget(lineEdit);
222  }
223  }
224  return true;
225 }
void setCurrentWidget(QWidget *widget)
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchrule.h:29
virtual void setText(const QString &)
void setClearButtonEnabled(bool enable)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString toString() const
void clear()
bool blockSignals(bool block)
Function
Describes operators for comparison of field and contents.
Definition: searchrule.h:40
T findChild(const QString &name, Qt::FindChildOptions options) const const
QString label(StandardShortcut id)
KGuiItem reset()
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:58:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.