Mailcommon

numericdoublerulewidgethandler.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 "numericdoublerulewidgethandler.h"
8#include "search/searchpattern.h"
9
10#include <KLocalizedString>
11
12#include <KLazyLocalizedString>
13#include <QComboBox>
14#include <QDoubleSpinBox>
15#include <QStackedWidget>
16using namespace MailCommon;
17
18static const struct {
20 const KLazyLocalizedString displayName;
21} NumericDoubleFunctions[] = {{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 NumericDoubleFunctionCount = sizeof(NumericDoubleFunctions) / sizeof(*NumericDoubleFunctions);
28
29QWidget *NumericDoubleRuleWidgetHandler::createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool /*isBalooSearch*/) const
30{
31 if (number != 0) {
32 return nullptr;
33 }
34
35 auto funcCombo = new QComboBox(functionStack);
36 funcCombo->setMinimumWidth(50);
37 funcCombo->setObjectName(QLatin1StringView("numericDoubleRuleFuncCombo"));
38 for (int i = 0; i < NumericDoubleFunctionCount; ++i) {
39 funcCombo->addItem(NumericDoubleFunctions[i].displayName.toString());
40 }
41 funcCombo->adjustSize();
42 QObject::connect(funcCombo, SIGNAL(activated(int)), receiver, SLOT(slotFunctionChanged()));
43 return funcCombo;
44}
45
46//---------------------------------------------------------------------------
47
48QWidget *NumericDoubleRuleWidgetHandler::createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
49{
50 if (number != 0) {
51 return nullptr;
52 }
53
54 auto numInput = new QDoubleSpinBox(valueStack);
55 numInput->setObjectName(QLatin1StringView("QDoubleSpinBox"));
56 QObject::connect(numInput, SIGNAL(valueChanged(double)), receiver, SLOT(slotValueChanged()));
57 return numInput;
58}
59
60//---------------------------------------------------------------------------
61
62SearchRule::Function NumericDoubleRuleWidgetHandler::currentFunction(const QStackedWidget *functionStack) const
63{
64 const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("numericDoubleRuleFuncCombo"));
65
66 if (funcCombo && funcCombo->currentIndex() >= 0) {
67 return NumericDoubleFunctions[funcCombo->currentIndex()].id;
68 }
69
70 return SearchRule::FuncNone;
71}
72
73//---------------------------------------------------------------------------
74
75SearchRule::Function NumericDoubleRuleWidgetHandler::function(const QByteArray &field, const QStackedWidget *functionStack) const
76{
77 if (!handlesField(field)) {
78 return SearchRule::FuncNone;
79 }
80
81 return currentFunction(functionStack);
82}
83
84//---------------------------------------------------------------------------
85
86QString NumericDoubleRuleWidgetHandler::currentValue(const QStackedWidget *valueStack) const
87{
88 const QDoubleSpinBox *numInput = valueStack->findChild<QDoubleSpinBox *>(QStringLiteral("QDoubleSpinBox"));
89
90 if (numInput) {
91 return QString::number(int(numInput->value() * 1024));
92 }
93
94 return {};
95}
96
97//---------------------------------------------------------------------------
98
99QString NumericDoubleRuleWidgetHandler::value(const QByteArray &field, const QStackedWidget *, const QStackedWidget *valueStack) const
100{
101 if (!handlesField(field)) {
102 return {};
103 }
104
105 return currentValue(valueStack);
106}
107
108//---------------------------------------------------------------------------
109
110QString NumericDoubleRuleWidgetHandler::prettyValue(const QByteArray &field, const QStackedWidget *, const QStackedWidget *valueStack) const
111{
112 if (!handlesField(field)) {
113 return {};
114 }
115
116 return currentValue(valueStack);
117}
118
119//---------------------------------------------------------------------------
120
121bool NumericDoubleRuleWidgetHandler::handlesField(const QByteArray &field) const
122{
123 return field == "<size>";
124}
125
126//---------------------------------------------------------------------------
127
128void NumericDoubleRuleWidgetHandler::reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
129{
130 // reset the function combo box
131 const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("numericDoubleRuleFuncCombo"));
132
133 if (funcCombo) {
134 funcCombo->blockSignals(true);
135 funcCombo->setCurrentIndex(0);
136 funcCombo->blockSignals(false);
137 }
138
139 // reset the value widget
140 auto numInput = valueStack->findChild<QDoubleSpinBox *>(QStringLiteral("QDoubleSpinBox"));
141
142 if (numInput) {
143 numInput->blockSignals(true);
144 numInput->setValue(0.0);
145 numInput->blockSignals(false);
146 }
147}
148
149//---------------------------------------------------------------------------
150
151void initDoubleNumInput(QDoubleSpinBox *numInput, const QByteArray &field)
152{
153 if (field == "<size>") {
154 numInput->setMinimum(0);
155 numInput->setSingleStep(1);
156 numInput->setMaximum(10000000);
157 numInput->setSuffix(i18nc("spinbox suffix: unit for kilobyte", " kB"));
158 }
159}
160
161//---------------------------------------------------------------------------
162
163bool NumericDoubleRuleWidgetHandler::setRule(QStackedWidget *functionStack,
164 QStackedWidget *valueStack,
165 const SearchRule::Ptr rule,
166 bool /*isBalooSearch*/) const
167{
168 if (!rule || !handlesField(rule->field())) {
169 reset(functionStack, valueStack);
170 return false;
171 }
172
173 // set the function
174 const SearchRule::Function func = rule->function();
175 int funcIndex = 0;
176 for (; funcIndex < NumericDoubleFunctionCount; ++funcIndex) {
177 if (func == NumericDoubleFunctions[funcIndex].id) {
178 break;
179 }
180 }
181
182 const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("numericDoubleRuleFuncCombo"));
183
184 if (funcCombo) {
185 funcCombo->blockSignals(true);
186 if (funcIndex < NumericDoubleFunctionCount) {
187 funcCombo->setCurrentIndex(funcIndex);
188 } else {
189 funcCombo->setCurrentIndex(0);
190 }
191 funcCombo->blockSignals(false);
192 functionStack->setCurrentWidget(funcCombo);
193 }
194
195 // set the value
196 bool ok;
197 int value = rule->contents().toInt(&ok);
198 if (!ok) {
199 value = 0;
200 }
201
202 auto numInput = valueStack->findChild<QDoubleSpinBox *>(QStringLiteral("QDoubleSpinBox"));
203
204 if (numInput) {
205 initDoubleNumInput(numInput, rule->field());
206 numInput->blockSignals(true);
207 numInput->setValue(value / (1024.0));
208 numInput->blockSignals(false);
209 valueStack->setCurrentWidget(numInput);
210 }
211 return true;
212}
213
214//---------------------------------------------------------------------------
215
216bool NumericDoubleRuleWidgetHandler::update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
217{
218 if (!handlesField(field)) {
219 return false;
220 }
221
222 // raise the correct function widget
223 functionStack->setCurrentWidget(functionStack->findChild<QWidget *>(QStringLiteral("numericDoubleRuleFuncCombo")));
224
225 // raise the correct value widget
226 auto numInput = valueStack->findChild<QDoubleSpinBox *>(QStringLiteral("QDoubleSpinBox"));
227
228 if (numInput) {
229 initDoubleNumInput(numInput, field);
230 valueStack->setCurrentWidget(numInput);
231 }
232 return true;
233}
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
QString i18nc(const char *context, const char *text, const TYPE &arg...)
The filter dialog.
void setMaximum(double max)
void setMinimum(double min)
void setSingleStep(double val)
void setSuffix(const QString &suffix)
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 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 Fri Dec 6 2024 12:02:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.