Mailcommon

numericrulewidgethandler.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 "numericrulewidgethandler.h"
8 #include "search/searchpattern.h"
9 
10 #include <KLocalizedString>
11 #include <KPluralHandlingSpinBox>
12 
13 #include <KLazyLocalizedString>
14 #include <QComboBox>
15 #include <QStackedWidget>
16 using namespace MailCommon;
17 
18 static const struct {
20  const KLazyLocalizedString displayName;
21 } NumericFunctions[] = {{SearchRule::FuncEquals, kli18n("is equal to")},
22  {SearchRule::FuncNotEqual, kli18n("is not equal to")},
23  {SearchRule::FuncIsGreater, kli18n("is greater than")},
24  {SearchRule::FuncIsLessOrEqual, kli18n("is less than or equal to")},
25  {SearchRule::FuncIsLess, kli18n("is less than")},
26  {SearchRule::FuncIsGreaterOrEqual, kli18n("is greater than or equal to")}};
27 static const int NumericFunctionCount = sizeof(NumericFunctions) / sizeof(*NumericFunctions);
28 
29 //---------------------------------------------------------------------------
30 
31 QWidget *NumericRuleWidgetHandler::createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool /*isBalooSearch*/) const
32 {
33  if (number != 0) {
34  return nullptr;
35  }
36 
37  auto funcCombo = new QComboBox(functionStack);
38  funcCombo->setMinimumWidth(50);
39  funcCombo->setObjectName(QStringLiteral("numericRuleFuncCombo"));
40  for (int i = 0; i < NumericFunctionCount; ++i) {
41  funcCombo->addItem(NumericFunctions[i].displayName.toString());
42  }
43  funcCombo->adjustSize();
44  QObject::connect(funcCombo, SIGNAL(activated(int)), receiver, SLOT(slotFunctionChanged()));
45  return funcCombo;
46 }
47 
48 //---------------------------------------------------------------------------
49 
50 QWidget *NumericRuleWidgetHandler::createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
51 {
52  if (number != 0) {
53  return nullptr;
54  }
55 
56  auto numInput = new KPluralHandlingSpinBox(valueStack);
57  numInput->setObjectName(QStringLiteral("KPluralHandlingSpinBox"));
58  QObject::connect(numInput, SIGNAL(valueChanged(int)), receiver, SLOT(slotValueChanged()));
59  return numInput;
60 }
61 
62 //---------------------------------------------------------------------------
63 
64 SearchRule::Function NumericRuleWidgetHandler::currentFunction(const QStackedWidget *functionStack) const
65 {
66  const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("numericRuleFuncCombo"));
67 
68  if (funcCombo && funcCombo->currentIndex() >= 0) {
69  return NumericFunctions[funcCombo->currentIndex()].id;
70  }
71 
72  return SearchRule::FuncNone;
73 }
74 
75 //---------------------------------------------------------------------------
76 
77 SearchRule::Function NumericRuleWidgetHandler::function(const QByteArray &field, const QStackedWidget *functionStack) const
78 {
79  if (!handlesField(field)) {
80  return SearchRule::FuncNone;
81  }
82 
83  return currentFunction(functionStack);
84 }
85 
86 //---------------------------------------------------------------------------
87 
88 QString NumericRuleWidgetHandler::currentValue(const QStackedWidget *valueStack) const
89 {
90  const KPluralHandlingSpinBox *numInput = valueStack->findChild<KPluralHandlingSpinBox *>(QStringLiteral("KPluralHandlingSpinBox"));
91 
92  if (numInput) {
93  return QString::number(numInput->value());
94  }
95 
96  return {};
97 }
98 
99 //---------------------------------------------------------------------------
100 
101 QString NumericRuleWidgetHandler::value(const QByteArray &field, const QStackedWidget *, const QStackedWidget *valueStack) const
102 {
103  if (!handlesField(field)) {
104  return {};
105  }
106 
107  return currentValue(valueStack);
108 }
109 
110 //---------------------------------------------------------------------------
111 
112 QString NumericRuleWidgetHandler::prettyValue(const QByteArray &field, const QStackedWidget *, const QStackedWidget *valueStack) const
113 {
114  if (!handlesField(field)) {
115  return {};
116  }
117 
118  return currentValue(valueStack);
119 }
120 
121 //---------------------------------------------------------------------------
122 
123 bool NumericRuleWidgetHandler::handlesField(const QByteArray &field) const
124 {
125  return field == "<age in days>";
126 }
127 
128 //---------------------------------------------------------------------------
129 
130 void NumericRuleWidgetHandler::reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
131 {
132  // reset the function combo box
133  const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("numericRuleFuncCombo"));
134 
135  if (funcCombo) {
136  funcCombo->blockSignals(true);
137  funcCombo->setCurrentIndex(0);
138  funcCombo->blockSignals(false);
139  }
140 
141  // reset the value widget
142  auto *numInput = valueStack->findChild<KPluralHandlingSpinBox *>(QStringLiteral("KPluralHandlingSpinBox"));
143 
144  if (numInput) {
145  numInput->blockSignals(true);
146  numInput->setValue(0);
147  numInput->blockSignals(false);
148  }
149 }
150 
151 //---------------------------------------------------------------------------
152 
153 void initNumInput(KPluralHandlingSpinBox *numInput, const QByteArray &field)
154 {
155  if (field == "<age in days>") {
156  numInput->setMinimum(-10000);
157  numInput->setSuffix(ki18ncp("Unit suffix where units are days.", " day", " days"));
158  }
159 }
160 
161 //---------------------------------------------------------------------------
162 
163 bool NumericRuleWidgetHandler::setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule, bool /*isBalooSearch*/) const
164 {
165  if (!rule || !handlesField(rule->field())) {
166  reset(functionStack, valueStack);
167  return false;
168  }
169 
170  // set the function
171  const SearchRule::Function func = rule->function();
172  int funcIndex = 0;
173  for (; funcIndex < NumericFunctionCount; ++funcIndex) {
174  if (func == NumericFunctions[funcIndex].id) {
175  break;
176  }
177  }
178 
179  const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("numericRuleFuncCombo"));
180 
181  if (funcCombo) {
182  funcCombo->blockSignals(true);
183  if (funcIndex < NumericFunctionCount) {
184  funcCombo->setCurrentIndex(funcIndex);
185  } else {
186  funcCombo->setCurrentIndex(0);
187  }
188  funcCombo->blockSignals(false);
189  functionStack->setCurrentWidget(funcCombo);
190  }
191 
192  // set the value
193  bool ok;
194  int value = rule->contents().toInt(&ok);
195  if (!ok) {
196  value = 0;
197  }
198 
199  auto *numInput = valueStack->findChild<KPluralHandlingSpinBox *>(QStringLiteral("KPluralHandlingSpinBox"));
200 
201  if (numInput) {
202  initNumInput(numInput, rule->field());
203  numInput->blockSignals(true);
204  numInput->setValue(value);
205  numInput->blockSignals(false);
206  valueStack->setCurrentWidget(numInput);
207  }
208  return true;
209 }
210 
211 //---------------------------------------------------------------------------
212 
213 bool NumericRuleWidgetHandler::update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
214 {
215  if (!handlesField(field)) {
216  return false;
217  }
218 
219  // raise the correct function widget
220  functionStack->setCurrentWidget(functionStack->findChild<QWidget *>(QStringLiteral("numericRuleFuncCombo")));
221 
222  // raise the correct value widget
223  auto *numInput = valueStack->findChild<KPluralHandlingSpinBox *>(QStringLiteral("KPluralHandlingSpinBox"));
224 
225  if (numInput) {
226  initNumInput(numInput, field);
227  valueStack->setCurrentWidget(numInput);
228  }
229  return true;
230 }
void setCurrentWidget(QWidget *widget)
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchrule.h:29
QString number(int n, int base)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString toString() const
KLocalizedString KI18N_EXPORT ki18ncp(const char *context, const char *singular, const char *plural)
bool blockSignals(bool block)
void setMinimum(int min)
Function
Describes operators for comparison of field and contents.
Definition: searchrule.h:40
T findChild(const QString &name, Qt::FindChildOptions options) const const
void setSuffix(const KLocalizedString &suffix)
KGuiItem reset()
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.