File indexing completed on 2024-11-17 04:51:15

0001 /*
0002   SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "numericdoublerulewidgethandler.h"
0008 #include "search/searchpattern.h"
0009 
0010 #include <KLocalizedString>
0011 
0012 #include <KLazyLocalizedString>
0013 #include <QComboBox>
0014 #include <QDoubleSpinBox>
0015 #include <QStackedWidget>
0016 using namespace MailCommon;
0017 
0018 static const struct {
0019     SearchRule::Function id;
0020     const KLazyLocalizedString displayName;
0021 } NumericDoubleFunctions[] = {{SearchRule::FuncEquals, kli18n("is equal to")},
0022                               {SearchRule::FuncNotEqual, kli18n("is not equal to")},
0023                               {SearchRule::FuncIsGreater, kli18n("is greater than")},
0024                               {SearchRule::FuncIsLessOrEqual, kli18n("is less than or equal to")},
0025                               {SearchRule::FuncIsLess, kli18n("is less than")},
0026                               {SearchRule::FuncIsGreaterOrEqual, kli18n("is greater than or equal to")}};
0027 static const int NumericDoubleFunctionCount = sizeof(NumericDoubleFunctions) / sizeof(*NumericDoubleFunctions);
0028 
0029 QWidget *NumericDoubleRuleWidgetHandler::createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool /*isBalooSearch*/) const
0030 {
0031     if (number != 0) {
0032         return nullptr;
0033     }
0034 
0035     auto funcCombo = new QComboBox(functionStack);
0036     funcCombo->setMinimumWidth(50);
0037     funcCombo->setObjectName(QLatin1StringView("numericDoubleRuleFuncCombo"));
0038     for (int i = 0; i < NumericDoubleFunctionCount; ++i) {
0039         funcCombo->addItem(NumericDoubleFunctions[i].displayName.toString());
0040     }
0041     funcCombo->adjustSize();
0042     QObject::connect(funcCombo, SIGNAL(activated(int)), receiver, SLOT(slotFunctionChanged()));
0043     return funcCombo;
0044 }
0045 
0046 //---------------------------------------------------------------------------
0047 
0048 QWidget *NumericDoubleRuleWidgetHandler::createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
0049 {
0050     if (number != 0) {
0051         return nullptr;
0052     }
0053 
0054     auto numInput = new QDoubleSpinBox(valueStack);
0055     numInput->setObjectName(QLatin1StringView("QDoubleSpinBox"));
0056     QObject::connect(numInput, SIGNAL(valueChanged(double)), receiver, SLOT(slotValueChanged()));
0057     return numInput;
0058 }
0059 
0060 //---------------------------------------------------------------------------
0061 
0062 SearchRule::Function NumericDoubleRuleWidgetHandler::currentFunction(const QStackedWidget *functionStack) const
0063 {
0064     const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("numericDoubleRuleFuncCombo"));
0065 
0066     if (funcCombo && funcCombo->currentIndex() >= 0) {
0067         return NumericDoubleFunctions[funcCombo->currentIndex()].id;
0068     }
0069 
0070     return SearchRule::FuncNone;
0071 }
0072 
0073 //---------------------------------------------------------------------------
0074 
0075 SearchRule::Function NumericDoubleRuleWidgetHandler::function(const QByteArray &field, const QStackedWidget *functionStack) const
0076 {
0077     if (!handlesField(field)) {
0078         return SearchRule::FuncNone;
0079     }
0080 
0081     return currentFunction(functionStack);
0082 }
0083 
0084 //---------------------------------------------------------------------------
0085 
0086 QString NumericDoubleRuleWidgetHandler::currentValue(const QStackedWidget *valueStack) const
0087 {
0088     const QDoubleSpinBox *numInput = valueStack->findChild<QDoubleSpinBox *>(QStringLiteral("QDoubleSpinBox"));
0089 
0090     if (numInput) {
0091         return QString::number(int(numInput->value() * 1024));
0092     }
0093 
0094     return {};
0095 }
0096 
0097 //---------------------------------------------------------------------------
0098 
0099 QString NumericDoubleRuleWidgetHandler::value(const QByteArray &field, const QStackedWidget *, const QStackedWidget *valueStack) const
0100 {
0101     if (!handlesField(field)) {
0102         return {};
0103     }
0104 
0105     return currentValue(valueStack);
0106 }
0107 
0108 //---------------------------------------------------------------------------
0109 
0110 QString NumericDoubleRuleWidgetHandler::prettyValue(const QByteArray &field, const QStackedWidget *, const QStackedWidget *valueStack) const
0111 {
0112     if (!handlesField(field)) {
0113         return {};
0114     }
0115 
0116     return currentValue(valueStack);
0117 }
0118 
0119 //---------------------------------------------------------------------------
0120 
0121 bool NumericDoubleRuleWidgetHandler::handlesField(const QByteArray &field) const
0122 {
0123     return field == "<size>";
0124 }
0125 
0126 //---------------------------------------------------------------------------
0127 
0128 void NumericDoubleRuleWidgetHandler::reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
0129 {
0130     // reset the function combo box
0131     const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("numericDoubleRuleFuncCombo"));
0132 
0133     if (funcCombo) {
0134         funcCombo->blockSignals(true);
0135         funcCombo->setCurrentIndex(0);
0136         funcCombo->blockSignals(false);
0137     }
0138 
0139     // reset the value widget
0140     auto numInput = valueStack->findChild<QDoubleSpinBox *>(QStringLiteral("QDoubleSpinBox"));
0141 
0142     if (numInput) {
0143         numInput->blockSignals(true);
0144         numInput->setValue(0.0);
0145         numInput->blockSignals(false);
0146     }
0147 }
0148 
0149 //---------------------------------------------------------------------------
0150 
0151 void initDoubleNumInput(QDoubleSpinBox *numInput, const QByteArray &field)
0152 {
0153     if (field == "<size>") {
0154         numInput->setMinimum(0);
0155         numInput->setSingleStep(1);
0156         numInput->setMaximum(10000000);
0157         numInput->setSuffix(i18nc("spinbox suffix: unit for kilobyte", " kB"));
0158     }
0159 }
0160 
0161 //---------------------------------------------------------------------------
0162 
0163 bool NumericDoubleRuleWidgetHandler::setRule(QStackedWidget *functionStack,
0164                                              QStackedWidget *valueStack,
0165                                              const SearchRule::Ptr rule,
0166                                              bool /*isBalooSearch*/) const
0167 {
0168     if (!rule || !handlesField(rule->field())) {
0169         reset(functionStack, valueStack);
0170         return false;
0171     }
0172 
0173     // set the function
0174     const SearchRule::Function func = rule->function();
0175     int funcIndex = 0;
0176     for (; funcIndex < NumericDoubleFunctionCount; ++funcIndex) {
0177         if (func == NumericDoubleFunctions[funcIndex].id) {
0178             break;
0179         }
0180     }
0181 
0182     const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("numericDoubleRuleFuncCombo"));
0183 
0184     if (funcCombo) {
0185         funcCombo->blockSignals(true);
0186         if (funcIndex < NumericDoubleFunctionCount) {
0187             funcCombo->setCurrentIndex(funcIndex);
0188         } else {
0189             funcCombo->setCurrentIndex(0);
0190         }
0191         funcCombo->blockSignals(false);
0192         functionStack->setCurrentWidget(funcCombo);
0193     }
0194 
0195     // set the value
0196     bool ok;
0197     int value = rule->contents().toInt(&ok);
0198     if (!ok) {
0199         value = 0;
0200     }
0201 
0202     auto numInput = valueStack->findChild<QDoubleSpinBox *>(QStringLiteral("QDoubleSpinBox"));
0203 
0204     if (numInput) {
0205         initDoubleNumInput(numInput, rule->field());
0206         numInput->blockSignals(true);
0207         numInput->setValue(value / (1024.0));
0208         numInput->blockSignals(false);
0209         valueStack->setCurrentWidget(numInput);
0210     }
0211     return true;
0212 }
0213 
0214 //---------------------------------------------------------------------------
0215 
0216 bool NumericDoubleRuleWidgetHandler::update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
0217 {
0218     if (!handlesField(field)) {
0219         return false;
0220     }
0221 
0222     // raise the correct function widget
0223     functionStack->setCurrentWidget(functionStack->findChild<QWidget *>(QStringLiteral("numericDoubleRuleFuncCombo")));
0224 
0225     // raise the correct value widget
0226     auto numInput = valueStack->findChild<QDoubleSpinBox *>(QStringLiteral("QDoubleSpinBox"));
0227 
0228     if (numInput) {
0229         initDoubleNumInput(numInput, field);
0230         valueStack->setCurrentWidget(numInput);
0231     }
0232     return true;
0233 }