Mailcommon

daterulewidgethandler.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 "daterulewidgethandler.h"
8
9#include <KDateComboBox>
10#include <KLocalizedString>
11
12#include <KLazyLocalizedString>
13#include <QComboBox>
14#include <QDate>
15#include <QObject>
16#include <QStackedWidget>
17using namespace MailCommon;
18
19static const struct {
21 const KLazyLocalizedString displayName;
22} DateFunctions[] = {{SearchRule::FuncEquals, kli18n("is equal to")},
23 {SearchRule::FuncNotEqual, kli18n("is not equal to")},
24 {SearchRule::FuncIsGreater, kli18n("is after")},
25 {SearchRule::FuncIsLessOrEqual, kli18n("is before or equal to")},
26 {SearchRule::FuncIsLess, kli18n("is before")},
27 {SearchRule::FuncIsGreaterOrEqual, kli18n("is after or equal to")}};
28static const int DateFunctionCount = sizeof(DateFunctions) / sizeof(*DateFunctions);
29
30//---------------------------------------------------------------------------
31
32QWidget *DateRuleWidgetHandler::createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool /*isBalooSearch*/) const
33{
34 if (number != 0) {
35 return nullptr;
36 }
37
38 auto funcCombo = new QComboBox(functionStack);
39 funcCombo->setMinimumWidth(50);
40 funcCombo->setObjectName(QLatin1StringView("dateRuleFuncCombo"));
41 for (int i = 0; i < DateFunctionCount; ++i) {
42 funcCombo->addItem(DateFunctions[i].displayName.toString());
43 }
44 funcCombo->adjustSize();
45 QObject::connect(funcCombo, SIGNAL(activated(int)), receiver, SLOT(slotFunctionChanged()));
46 return funcCombo;
47}
48
49//---------------------------------------------------------------------------
50
51QWidget *DateRuleWidgetHandler::createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
52{
53 if (number != 0) {
54 return nullptr;
55 }
56
57 auto dateCombo = new KDateComboBox(valueStack);
58 dateCombo->setObjectName(QLatin1StringView("KDateComboBox"));
60 QObject::connect(dateCombo, SIGNAL(dateChanged(QDate)), receiver, SLOT(slotValueChanged()));
61 return dateCombo;
62}
63
64//---------------------------------------------------------------------------
65
66SearchRule::Function DateRuleWidgetHandler::currentFunction(const QStackedWidget *functionStack) const
67{
68 const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("dateRuleFuncCombo"));
69
70 if (funcCombo && funcCombo->currentIndex() >= 0) {
71 return DateFunctions[funcCombo->currentIndex()].id;
72 }
73
74 return SearchRule::FuncNone;
75}
76
77//---------------------------------------------------------------------------
78
79SearchRule::Function DateRuleWidgetHandler::function(const QByteArray &field, const QStackedWidget *functionStack) const
80{
81 if (!handlesField(field)) {
82 return SearchRule::FuncNone;
83 }
84
85 return currentFunction(functionStack);
86}
87
88//---------------------------------------------------------------------------
89
90QString DateRuleWidgetHandler::currentValue(const QStackedWidget *valueStack) const
91{
92 const KDateComboBox *dateInput = valueStack->findChild<KDateComboBox *>(QStringLiteral("KDateComboBox"));
93
94 if (dateInput) {
95 return dateInput->date().toString(Qt::ISODate);
96 }
97
98 return {};
99}
100
101//---------------------------------------------------------------------------
102
103QString DateRuleWidgetHandler::value(const QByteArray &field, const QStackedWidget *, const QStackedWidget *valueStack) const
104{
105 if (!handlesField(field)) {
106 return {};
107 }
108
109 return currentValue(valueStack);
110}
111
112//---------------------------------------------------------------------------
113
114QString DateRuleWidgetHandler::prettyValue(const QByteArray &field, const QStackedWidget *, const QStackedWidget *valueStack) const
115{
116 if (!handlesField(field)) {
117 return {};
118 }
119
120 return currentValue(valueStack);
121}
122
123//---------------------------------------------------------------------------
124
125bool DateRuleWidgetHandler::handlesField(const QByteArray &field) const
126{
127 return field == "<date>";
128}
129
130//---------------------------------------------------------------------------
131
132void DateRuleWidgetHandler::reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
133{
134 // reset the function combo box
135 const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("dateRuleFuncCombo"));
136
137 if (funcCombo) {
138 funcCombo->blockSignals(true);
139 funcCombo->setCurrentIndex(0);
140 funcCombo->blockSignals(false);
141 }
142
143 // reset the value widget
144 auto dateInput = valueStack->findChild<KDateComboBox *>(QStringLiteral("KDateComboBox"));
145
146 if (dateInput) {
147 dateInput->blockSignals(true);
148 dateInput->setDate(QDate::currentDate());
149 dateInput->blockSignals(false);
150 }
151}
152
153//---------------------------------------------------------------------------
154
155bool DateRuleWidgetHandler::setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule, bool /*isBalooSearch*/) const
156{
157 if (!rule || !handlesField(rule->field())) {
158 reset(functionStack, valueStack);
159 return false;
160 }
161
162 // set the function
163 const SearchRule::Function func = rule->function();
164 int funcIndex = 0;
165 for (; funcIndex < DateFunctionCount; ++funcIndex) {
166 if (func == DateFunctions[funcIndex].id) {
167 break;
168 }
169 }
170
171 const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("dateRuleFuncCombo"));
172
173 if (funcCombo) {
174 funcCombo->blockSignals(true);
175 if (funcIndex < DateFunctionCount) {
176 funcCombo->setCurrentIndex(funcIndex);
177 } else {
178 funcCombo->setCurrentIndex(0);
179 }
180 funcCombo->blockSignals(false);
181 functionStack->setCurrentWidget(funcCombo);
182 }
183
184 // set the value
185 const QString value = rule->contents();
186
187 auto dateInput = valueStack->findChild<KDateComboBox *>(QStringLiteral("KDateComboBox"));
188
189 if (dateInput) {
190 dateInput->blockSignals(true);
191 dateInput->setDate(QDate::fromString(value, Qt::ISODate));
192 dateInput->blockSignals(false);
193 valueStack->setCurrentWidget(dateInput);
194 }
195 return true;
196}
197
198//---------------------------------------------------------------------------
199
200bool DateRuleWidgetHandler::update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
201{
202 if (!handlesField(field)) {
203 return false;
204 }
205
206 // raise the correct function widget
207 functionStack->setCurrentWidget(functionStack->findChild<QWidget *>(QStringLiteral("dateRuleFuncCombo")));
208
209 // raise the correct value widget
210 auto dateInput = valueStack->findChild<KDateComboBox *>(QStringLiteral("KDateComboBox"));
211
212 if (dateInput) {
213 valueStack->setCurrentWidget(dateInput);
214 }
215 return true;
216}
void setDate(const QDate &date)
QDate date() const
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
The filter dialog.
QDate currentDate()
QDate fromString(QStringView string, QStringView format, QCalendar cal)
QString toString(QStringView format, QCalendar cal) const const
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)
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.