File indexing completed on 2024-11-17 04:51:14
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 "daterulewidgethandler.h" 0008 0009 #include <KDateComboBox> 0010 #include <KLocalizedString> 0011 0012 #include <KLazyLocalizedString> 0013 #include <QComboBox> 0014 #include <QDate> 0015 #include <QObject> 0016 #include <QStackedWidget> 0017 using namespace MailCommon; 0018 0019 static const struct { 0020 SearchRule::Function id; 0021 const KLazyLocalizedString displayName; 0022 } DateFunctions[] = {{SearchRule::FuncEquals, kli18n("is equal to")}, 0023 {SearchRule::FuncNotEqual, kli18n("is not equal to")}, 0024 {SearchRule::FuncIsGreater, kli18n("is after")}, 0025 {SearchRule::FuncIsLessOrEqual, kli18n("is before or equal to")}, 0026 {SearchRule::FuncIsLess, kli18n("is before")}, 0027 {SearchRule::FuncIsGreaterOrEqual, kli18n("is after or equal to")}}; 0028 static const int DateFunctionCount = sizeof(DateFunctions) / sizeof(*DateFunctions); 0029 0030 //--------------------------------------------------------------------------- 0031 0032 QWidget *DateRuleWidgetHandler::createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool /*isBalooSearch*/) const 0033 { 0034 if (number != 0) { 0035 return nullptr; 0036 } 0037 0038 auto funcCombo = new QComboBox(functionStack); 0039 funcCombo->setMinimumWidth(50); 0040 funcCombo->setObjectName(QLatin1StringView("dateRuleFuncCombo")); 0041 for (int i = 0; i < DateFunctionCount; ++i) { 0042 funcCombo->addItem(DateFunctions[i].displayName.toString()); 0043 } 0044 funcCombo->adjustSize(); 0045 QObject::connect(funcCombo, SIGNAL(activated(int)), receiver, SLOT(slotFunctionChanged())); 0046 return funcCombo; 0047 } 0048 0049 //--------------------------------------------------------------------------- 0050 0051 QWidget *DateRuleWidgetHandler::createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const 0052 { 0053 if (number != 0) { 0054 return nullptr; 0055 } 0056 0057 auto dateCombo = new KDateComboBox(valueStack); 0058 dateCombo->setObjectName(QLatin1StringView("KDateComboBox")); 0059 dateCombo->setOptions(KDateComboBox::SelectDate | KDateComboBox::DatePicker | KDateComboBox::DateKeywords); 0060 QObject::connect(dateCombo, SIGNAL(dateChanged(QDate)), receiver, SLOT(slotValueChanged())); 0061 return dateCombo; 0062 } 0063 0064 //--------------------------------------------------------------------------- 0065 0066 SearchRule::Function DateRuleWidgetHandler::currentFunction(const QStackedWidget *functionStack) const 0067 { 0068 const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("dateRuleFuncCombo")); 0069 0070 if (funcCombo && funcCombo->currentIndex() >= 0) { 0071 return DateFunctions[funcCombo->currentIndex()].id; 0072 } 0073 0074 return SearchRule::FuncNone; 0075 } 0076 0077 //--------------------------------------------------------------------------- 0078 0079 SearchRule::Function DateRuleWidgetHandler::function(const QByteArray &field, const QStackedWidget *functionStack) const 0080 { 0081 if (!handlesField(field)) { 0082 return SearchRule::FuncNone; 0083 } 0084 0085 return currentFunction(functionStack); 0086 } 0087 0088 //--------------------------------------------------------------------------- 0089 0090 QString DateRuleWidgetHandler::currentValue(const QStackedWidget *valueStack) const 0091 { 0092 const KDateComboBox *dateInput = valueStack->findChild<KDateComboBox *>(QStringLiteral("KDateComboBox")); 0093 0094 if (dateInput) { 0095 return dateInput->date().toString(Qt::ISODate); 0096 } 0097 0098 return {}; 0099 } 0100 0101 //--------------------------------------------------------------------------- 0102 0103 QString DateRuleWidgetHandler::value(const QByteArray &field, const QStackedWidget *, const QStackedWidget *valueStack) const 0104 { 0105 if (!handlesField(field)) { 0106 return {}; 0107 } 0108 0109 return currentValue(valueStack); 0110 } 0111 0112 //--------------------------------------------------------------------------- 0113 0114 QString DateRuleWidgetHandler::prettyValue(const QByteArray &field, const QStackedWidget *, const QStackedWidget *valueStack) const 0115 { 0116 if (!handlesField(field)) { 0117 return {}; 0118 } 0119 0120 return currentValue(valueStack); 0121 } 0122 0123 //--------------------------------------------------------------------------- 0124 0125 bool DateRuleWidgetHandler::handlesField(const QByteArray &field) const 0126 { 0127 return field == "<date>"; 0128 } 0129 0130 //--------------------------------------------------------------------------- 0131 0132 void DateRuleWidgetHandler::reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const 0133 { 0134 // reset the function combo box 0135 const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("dateRuleFuncCombo")); 0136 0137 if (funcCombo) { 0138 funcCombo->blockSignals(true); 0139 funcCombo->setCurrentIndex(0); 0140 funcCombo->blockSignals(false); 0141 } 0142 0143 // reset the value widget 0144 auto dateInput = valueStack->findChild<KDateComboBox *>(QStringLiteral("KDateComboBox")); 0145 0146 if (dateInput) { 0147 dateInput->blockSignals(true); 0148 dateInput->setDate(QDate::currentDate()); 0149 dateInput->blockSignals(false); 0150 } 0151 } 0152 0153 //--------------------------------------------------------------------------- 0154 0155 bool DateRuleWidgetHandler::setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule, bool /*isBalooSearch*/) const 0156 { 0157 if (!rule || !handlesField(rule->field())) { 0158 reset(functionStack, valueStack); 0159 return false; 0160 } 0161 0162 // set the function 0163 const SearchRule::Function func = rule->function(); 0164 int funcIndex = 0; 0165 for (; funcIndex < DateFunctionCount; ++funcIndex) { 0166 if (func == DateFunctions[funcIndex].id) { 0167 break; 0168 } 0169 } 0170 0171 const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("dateRuleFuncCombo")); 0172 0173 if (funcCombo) { 0174 funcCombo->blockSignals(true); 0175 if (funcIndex < DateFunctionCount) { 0176 funcCombo->setCurrentIndex(funcIndex); 0177 } else { 0178 funcCombo->setCurrentIndex(0); 0179 } 0180 funcCombo->blockSignals(false); 0181 functionStack->setCurrentWidget(funcCombo); 0182 } 0183 0184 // set the value 0185 const QString value = rule->contents(); 0186 0187 auto dateInput = valueStack->findChild<KDateComboBox *>(QStringLiteral("KDateComboBox")); 0188 0189 if (dateInput) { 0190 dateInput->blockSignals(true); 0191 dateInput->setDate(QDate::fromString(value, Qt::ISODate)); 0192 dateInput->blockSignals(false); 0193 valueStack->setCurrentWidget(dateInput); 0194 } 0195 return true; 0196 } 0197 0198 //--------------------------------------------------------------------------- 0199 0200 bool DateRuleWidgetHandler::update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const 0201 { 0202 if (!handlesField(field)) { 0203 return false; 0204 } 0205 0206 // raise the correct function widget 0207 functionStack->setCurrentWidget(functionStack->findChild<QWidget *>(QStringLiteral("dateRuleFuncCombo"))); 0208 0209 // raise the correct value widget 0210 auto dateInput = valueStack->findChild<KDateComboBox *>(QStringLiteral("KDateComboBox")); 0211 0212 if (dateInput) { 0213 valueStack->setCurrentWidget(dateInput); 0214 } 0215 return true; 0216 }