Mailcommon

numericrulewidgethandler.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "numericrulewidgethandler.h"
8#include "search/searchpattern.h"
9
10#include <KLazyLocalizedString>
11#include <KLocalization>
12#include <KLocalizedString>
13#include <QComboBox>
14#include <QSpinBox>
15#include <QStackedWidget>
16using namespace MailCommon;
17
18static 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")}};
27static const int NumericFunctionCount = sizeof(NumericFunctions) / sizeof(*NumericFunctions);
28
29//---------------------------------------------------------------------------
30
31QWidget *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(QLatin1StringView("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
50QWidget *NumericRuleWidgetHandler::createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
51{
52 if (number != 0) {
53 return nullptr;
54 }
55
56 auto numInput = new QSpinBox(valueStack);
57 numInput->setObjectName(QLatin1StringView("QSpinBox"));
58 QObject::connect(numInput, SIGNAL(valueChanged(int)), receiver, SLOT(slotValueChanged()));
59 return numInput;
60}
61
62//---------------------------------------------------------------------------
63
64SearchRule::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
77SearchRule::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
88QString NumericRuleWidgetHandler::currentValue(const QStackedWidget *valueStack) const
89{
90 const QSpinBox *numInput = valueStack->findChild<QSpinBox *>(QStringLiteral("QSpinBox"));
91
92 if (numInput) {
93 return QString::number(numInput->value());
94 }
95
96 return {};
97}
98
99//---------------------------------------------------------------------------
100
101QString 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
112QString 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
123bool NumericRuleWidgetHandler::handlesField(const QByteArray &field) const
124{
125 return field == "<age in days>";
126}
127
128//---------------------------------------------------------------------------
129
130void 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<QSpinBox *>(QStringLiteral("QSpinBox"));
143
144 if (numInput) {
145 numInput->blockSignals(true);
146 numInput->setValue(0);
147 numInput->blockSignals(false);
148 }
149}
150
151//---------------------------------------------------------------------------
152
153void initNumInput(QSpinBox *numInput, const QByteArray &field)
154{
155 if (field == "<age in days>") {
156 numInput->setMinimum(-10000);
157 KLocalization::setupSpinBoxFormatString(numInput, ki18ncp("Unit suffix where units are days.", "%v day", "%v days"));
158 }
159}
160
161//---------------------------------------------------------------------------
162
163bool 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<QSpinBox *>(QStringLiteral("QSpinBox"));
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
213bool 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<QSpinBox *>(QStringLiteral("QSpinBox"));
224
225 if (numInput) {
226 initNumInput(numInput, field);
227 valueStack->setCurrentWidget(numInput);
228 }
229 return true;
230}
QString toString() const
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition searchrule.h:29
Function
Describes operators for comparison of field and contents.
Definition searchrule.h:40
KLocalizedString KI18N_EXPORT ki18ncp(const char *context, const char *singular, const char *plural)
void setupSpinBoxFormatString(T *spinBox, const KLocalizedString &formatString)
The filter dialog.
bool blockSignals(bool block)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
void setMinimum(int min)
void setCurrentWidget(QWidget *widget)
QString number(double n, char format, int precision)
int toInt(bool *ok, int base) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.