Mailcommon

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

KDE's Doxygen guidelines are available online.