Libksieve

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

KDE's Doxygen guidelines are available online.