Libksieve

selectdatewidget.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6#include "selectdatewidget.h"
7#include "sievedatespinbox.h"
8
9#include <KDateComboBox>
10#include <KLineEditEventHandler>
11#include <KLocalizedString>
12#include <KTimeComboBox>
13#include <QComboBox>
14#include <QLineEdit>
15
16#include "libksieveui_debug.h"
17#include <QHBoxLayout>
18#include <QLabel>
19#include <QStackedWidget>
20#include <QTime>
21
22using namespace KSieveUi;
23SelectDateWidget::SelectDateWidget(QWidget *parent)
24 : QWidget(parent)
25{
26 initialize();
27}
28
29SelectDateWidget::~SelectDateWidget() = default;
30
31void SelectDateWidget::initialize()
32{
33 auto lay = new QHBoxLayout(this);
34 lay->setContentsMargins({});
35
36 mDateType = new QComboBox(this);
37 connect(mDateType, &QComboBox::activated, this, &SelectDateWidget::slotDateTypeActivated);
38 mDateType->addItem(i18n("Year"), QVariant::fromValue(KSieveUi::SelectDateWidget::Year));
39 mDateType->addItem(i18n("Month"), QVariant::fromValue(KSieveUi::SelectDateWidget::Month));
40 mDateType->addItem(i18n("Day"), QVariant::fromValue(KSieveUi::SelectDateWidget::Day));
41 mDateType->addItem(i18n("Date"), QVariant::fromValue(KSieveUi::SelectDateWidget::Date));
42 mDateType->addItem(i18n("Julian"), QVariant::fromValue(KSieveUi::SelectDateWidget::Julian));
43 mDateType->addItem(i18n("Hour"), QVariant::fromValue(KSieveUi::SelectDateWidget::Hour));
44 mDateType->addItem(i18n("Minute"), QVariant::fromValue(KSieveUi::SelectDateWidget::Minute));
45 mDateType->addItem(i18n("Second"), QVariant::fromValue(KSieveUi::SelectDateWidget::Second));
46 mDateType->addItem(i18n("Time"), QVariant::fromValue(KSieveUi::SelectDateWidget::Time));
47 mDateType->addItem(i18n("iso8601"), QVariant::fromValue(KSieveUi::SelectDateWidget::Iso8601));
48 mDateType->addItem(i18n("std11"), QVariant::fromValue(KSieveUi::SelectDateWidget::Std11));
49 mDateType->addItem(i18n("Zone"), QVariant::fromValue(KSieveUi::SelectDateWidget::Zone));
50 mDateType->addItem(i18n("Weekday"), QVariant::fromValue(KSieveUi::SelectDateWidget::Weekday));
51 lay->addWidget(mDateType);
52
53 auto lab = new QLabel(i18nc("@label:textbox", "value:"), this);
54 lay->addWidget(lab);
55
56 mStackWidget = new QStackedWidget;
57 lay->addWidget(mStackWidget);
58
59 mDateLineEdit = new QLineEdit(this);
61 mStackWidget->addWidget(mDateLineEdit);
62 connect(mDateLineEdit, &QLineEdit::textChanged, this, &SelectDateWidget::valueChanged);
63
64 mDateValue = new SieveDateSpinBox(this);
65 mStackWidget->addWidget(mDateValue);
66 connect(mDateValue, &QSpinBox::valueChanged, this, &SelectDateWidget::valueChanged);
67
68 mDateEdit = new KDateComboBox(this);
69 mStackWidget->addWidget(mDateEdit);
70 connect(mDateEdit, &KDateComboBox::dateChanged, this, &SelectDateWidget::valueChanged);
71
72 mTimeEdit = new KTimeComboBox(this);
73 mStackWidget->addWidget(mTimeEdit);
74 connect(mTimeEdit, &KTimeComboBox::timeChanged, this, &SelectDateWidget::valueChanged);
75
76 mStackWidget->setCurrentWidget(mDateValue);
77}
78
79void SelectDateWidget::slotDateTypeActivated(int index)
80{
81 const auto type = mDateType->itemData(index).value<KSieveUi::SelectDateWidget::DateType>();
82 switch (type) {
83 case Year:
84 case Month:
85 case Day:
86 case Hour:
87 case Minute:
88 case Second:
89 case Weekday:
90 mDateValue->setType(type);
91 mStackWidget->setCurrentWidget(mDateValue);
92 break;
93 case Date:
94 mStackWidget->setCurrentWidget(mDateEdit);
95 break;
96 case Julian:
97 mStackWidget->setCurrentWidget(mDateLineEdit);
98 break;
99 case Time:
100 mStackWidget->setCurrentWidget(mTimeEdit);
101 break;
102 case Iso8601:
103 mStackWidget->setCurrentWidget(mDateLineEdit);
104 break;
105 case Std11:
106 mStackWidget->setCurrentWidget(mDateLineEdit);
107 break;
108 case Zone:
109 mStackWidget->setCurrentWidget(mDateLineEdit);
110 break;
111 }
112 Q_EMIT valueChanged();
113}
114
115QString SelectDateWidget::dateValue(SelectDateWidget::DateType type) const
116{
117 QString str;
118 switch (type) {
119 case Year:
120 case Month:
121 case Day:
122 case Hour:
123 case Minute:
124 case Second:
125 case Weekday:
126 str = mDateValue->code();
127 break;
128 case Date:
129 str = QLocale::c().toString(mDateEdit->date());
130 break;
131 case Julian:
132 // TODO
133 str = QLocale::c().toString(mDateEdit->date());
134 break;
135 case Time:
136 str = QLocale::c().toString(mTimeEdit->time());
137 break;
138 case Iso8601:
139 str = QLocale::c().toString(mDateEdit->date());
140 break;
141 case Std11:
142 str = QLocale::c().toString(mDateEdit->date());
143 break;
144 case Zone:
145 str = QLocale::c().toString(mDateEdit->date());
146 break;
147 }
148 return str;
149}
150
151SelectDateWidget::DateType SelectDateWidget::dateTypeFromString(const QString &str)
152{
153 if (str == QLatin1StringView("year")) {
154 return Year;
155 } else if (str == QLatin1StringView("month")) {
156 return Month;
157 } else if (str == QLatin1StringView("day")) {
158 return Day;
159 } else if (str == QLatin1StringView("date")) {
160 return Date;
161 } else if (str == QLatin1StringView("julian")) {
162 return Julian;
163 } else if (str == QLatin1StringView("hour")) {
164 return Hour;
165 } else if (str == QLatin1StringView("minute")) {
166 return Minute;
167 } else if (str == QLatin1StringView("second")) {
168 return Second;
169 } else if (str == QLatin1StringView("time")) {
170 return Time;
171 } else if (str == QLatin1StringView("iso8601")) {
172 return Iso8601;
173 } else if (str == QLatin1StringView("std11")) {
174 return Std11;
175 } else if (str == QLatin1StringView("zone")) {
176 return Zone;
177 } else if (str == QLatin1StringView("weekday")) {
178 return Weekday;
179 } else {
180 qCDebug(LIBKSIEVEUI_LOG) << " date type unknown :" << str;
181 }
182 return Year;
183}
184
185QString SelectDateWidget::dateType(SelectDateWidget::DateType type) const
186{
187 QString str;
188 switch (type) {
189 case Year:
190 str = QStringLiteral("year");
191 break;
192 case Month:
193 str = QStringLiteral("month");
194 break;
195 case Day:
196 str = QStringLiteral("day");
197 break;
198 case Date:
199 str = QStringLiteral("date");
200 break;
201 case Julian:
202 str = QStringLiteral("julian");
203 break;
204 case Hour:
205 str = QStringLiteral("hour");
206 break;
207 case Minute:
208 str = QStringLiteral("minute");
209 break;
210 case Second:
211 str = QStringLiteral("second");
212 break;
213 case Time:
214 str = QStringLiteral("time");
215 break;
216 case Iso8601:
217 str = QStringLiteral("iso8601");
218 break;
219 case Std11:
220 str = QStringLiteral("std11");
221 break;
222 case Zone:
223 str = QStringLiteral("zone");
224 break;
225 case Weekday:
226 str = QStringLiteral("weekday");
227 break;
228 }
229 return str;
230}
231
232QString SelectDateWidget::code() const
233{
234 const auto type = mDateType->itemData(mDateType->currentIndex()).value<KSieveUi::SelectDateWidget::DateType>();
235 return QStringLiteral("\"%1\" \"%2\"").arg(dateType(type), dateValue(type));
236}
237
238void SelectDateWidget::setCode(const QString &type, const QString &value)
239{
240 const int index = dateTypeFromString(type);
241 if (index != -1) {
242 mDateType->setCurrentIndex(index);
243 } else {
244 mDateType->setCurrentIndex(0);
245 }
246 const auto dateType = mDateType->itemData(index).value<KSieveUi::SelectDateWidget::DateType>();
247 switch (dateType) {
248 case Month:
249 case Day:
250 case Hour:
251 case Minute:
252 case Second:
253 case Weekday:
254 case Year:
255 mStackWidget->setCurrentWidget(mDateValue);
256 mDateValue->setType(dateType);
257 mDateValue->setCode(value);
258 break;
259 case Date:
260 mStackWidget->setCurrentWidget(mDateEdit);
261 mDateEdit->setDate(QLocale::c().toDate(value));
262 break;
263 case Julian:
264 mStackWidget->setCurrentWidget(mDateLineEdit);
265 mDateLineEdit->setText(value);
266 break;
267 case Time:
268 mStackWidget->setCurrentWidget(mTimeEdit);
269 mTimeEdit->setTime(QLocale::c().toTime(value));
270 break;
271 case Iso8601:
272 case Std11:
273 case Zone:
274 mStackWidget->setCurrentWidget(mDateLineEdit);
275 mDateLineEdit->setText(value);
276 break;
277 }
278}
279
280#include "moc_selectdatewidget.cpp"
void setDate(const QDate &date)
QDate date() const
void dateChanged(const QDate &date)
QTime time() const
void setTime(const QTime &time)
void timeChanged(const QTime &time)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
Type type(const QSqlDatabase &db)
void catchReturnKey(QObject *lineEdit)
void initialize(StandardShortcut id)
void activated(int index)
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
QVariant itemData(int index, int role) const const
void setText(const QString &)
void textChanged(const QString &text)
QLocale c()
QString toString(QDate date, FormatType format) const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void valueChanged(int i)
int addWidget(QWidget *widget)
void setCurrentWidget(QWidget *widget)
QVariant fromValue(T &&value)
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.