Libksieve

selectdatewidget.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 #include "selectdatewidget.h"
20 #include "sievedatespinbox.h"
21 
22 #include <KLocalizedString>
23 #include <QComboBox>
24 #include <QLineEdit>
25 #include <KDateComboBox>
26 #include <KTimeComboBox>
27 
28 #include <QHBoxLayout>
29 #include <QLabel>
30 #include <QStackedWidget>
31 #include <QSpinBox>
32 #include "libksieve_debug.h"
33 #include <QTime>
34 
35 using namespace KSieveUi;
36 SelectDateWidget::SelectDateWidget(QWidget *parent)
37  : QWidget(parent)
38 {
39  initialize();
40 }
41 
42 SelectDateWidget::~SelectDateWidget()
43 {
44 }
45 
46 void SelectDateWidget::initialize()
47 {
48  QHBoxLayout *lay = new QHBoxLayout(this);
49  lay->setContentsMargins(0, 0, 0, 0);
50 
51  mDateType = new QComboBox;
52  connect(mDateType, QOverload<int>::of(&QComboBox::activated), this, &SelectDateWidget::slotDateTypeActivated);
53  mDateType->addItem(i18n("Year"), QVariant::fromValue(KSieveUi::SelectDateWidget::Year));
54  mDateType->addItem(i18n("Month"), QVariant::fromValue(KSieveUi::SelectDateWidget::Month));
55  mDateType->addItem(i18n("Day"), QVariant::fromValue(KSieveUi::SelectDateWidget::Day));
56  mDateType->addItem(i18n("Date"), QVariant::fromValue(KSieveUi::SelectDateWidget::Date));
57  mDateType->addItem(i18n("Julian"), QVariant::fromValue(KSieveUi::SelectDateWidget::Julian));
58  mDateType->addItem(i18n("Hour"), QVariant::fromValue(KSieveUi::SelectDateWidget::Hour));
59  mDateType->addItem(i18n("Minute"), QVariant::fromValue(KSieveUi::SelectDateWidget::Minute));
60  mDateType->addItem(i18n("Second"), QVariant::fromValue(KSieveUi::SelectDateWidget::Second));
61  mDateType->addItem(i18n("Time"), QVariant::fromValue(KSieveUi::SelectDateWidget::Time));
62  mDateType->addItem(i18n("iso8601"), QVariant::fromValue(KSieveUi::SelectDateWidget::Iso8601));
63  mDateType->addItem(i18n("std11"), QVariant::fromValue(KSieveUi::SelectDateWidget::Std11));
64  mDateType->addItem(i18n("Zone"), QVariant::fromValue(KSieveUi::SelectDateWidget::Zone));
65  mDateType->addItem(i18n("Weekday"), QVariant::fromValue(KSieveUi::SelectDateWidget::Weekday));
66  lay->addWidget(mDateType);
67 
68  QLabel *lab = new QLabel(i18n("value:"));
69  lay->addWidget(lab);
70 
71  mStackWidget = new QStackedWidget;
72  lay->addWidget(mStackWidget);
73 
74  mDateLineEdit = new QLineEdit;
75  mStackWidget->addWidget(mDateLineEdit);
76  connect(mDateLineEdit, &QLineEdit::textChanged, this, &SelectDateWidget::valueChanged);
77 
78  mDateValue = new SieveDateSpinBox;
79  mStackWidget->addWidget(mDateValue);
80  connect(mDateValue, QOverload<int>::of(&QSpinBox::valueChanged), this, &SelectDateWidget::valueChanged);
81 
82  mDateEdit = new KDateComboBox;
83  mStackWidget->addWidget(mDateEdit);
84  connect(mDateEdit, &KDateComboBox::dateChanged, this, &SelectDateWidget::valueChanged);
85 
86  mTimeEdit = new KTimeComboBox;
87  mStackWidget->addWidget(mTimeEdit);
88  connect(mTimeEdit, &KTimeComboBox::timeChanged, this, &SelectDateWidget::valueChanged);
89 
90  mStackWidget->setCurrentWidget(mDateValue);
91 }
92 
93 void SelectDateWidget::slotDateTypeActivated(int index)
94 {
95  const DateType type = mDateType->itemData(index).value<KSieveUi::SelectDateWidget::DateType>();
96  switch (type) {
97  case Year:
98  case Month:
99  case Day:
100  case Hour:
101  case Minute:
102  case Second:
103  case Weekday:
104  mDateValue->setType(type);
105  mStackWidget->setCurrentWidget(mDateValue);
106  break;
107  case Date:
108  mStackWidget->setCurrentWidget(mDateEdit);
109  break;
110  case Julian:
111  mStackWidget->setCurrentWidget(mDateLineEdit);
112  break;
113  case Time:
114  mStackWidget->setCurrentWidget(mTimeEdit);
115  break;
116  case Iso8601:
117  mStackWidget->setCurrentWidget(mDateLineEdit);
118  break;
119  case Std11:
120  mStackWidget->setCurrentWidget(mDateLineEdit);
121  break;
122  case Zone:
123  mStackWidget->setCurrentWidget(mDateLineEdit);
124  break;
125  }
126  Q_EMIT valueChanged();
127 }
128 
129 QString SelectDateWidget::dateValue(SelectDateWidget::DateType type) const
130 {
131  QString str;
132  switch (type) {
133  case Year:
134  case Month:
135  case Day:
136  case Hour:
137  case Minute:
138  case Second:
139  case Weekday:
140  str = mDateValue->code();
141  break;
142  case Date:
143  str = QLocale::c().toString(mDateEdit->date());
144  break;
145  case Julian:
146  //TODO
147  str = QLocale::c().toString(mDateEdit->date());
148  break;
149  case Time:
150  str = QLocale::c().toString(mTimeEdit->time());
151  break;
152  case Iso8601:
153  str = QLocale::c().toString(mDateEdit->date());
154  break;
155  case Std11:
156  str = QLocale::c().toString(mDateEdit->date());
157  break;
158  case Zone:
159  str = QLocale::c().toString(mDateEdit->date());
160  break;
161  }
162  return str;
163 }
164 
165 SelectDateWidget::DateType SelectDateWidget::dateTypeFromString(const QString &str)
166 {
167  if (str == QLatin1String("year")) {
168  return Year;
169  } else if (str == QLatin1String("month")) {
170  return Month;
171  } else if (str == QLatin1String("day")) {
172  return Day;
173  } else if (str == QLatin1String("date")) {
174  return Date;
175  } else if (str == QLatin1String("julian")) {
176  return Julian;
177  } else if (str == QLatin1String("hour")) {
178  return Hour;
179  } else if (str == QLatin1String("minute")) {
180  return Minute;
181  } else if (str == QLatin1String("second")) {
182  return Second;
183  } else if (str == QLatin1String("time")) {
184  return Time;
185  } else if (str == QLatin1String("iso8601")) {
186  return Iso8601;
187  } else if (str == QLatin1String("std11")) {
188  return Std11;
189  } else if (str == QLatin1String("zone")) {
190  return Zone;
191  } else if (str == QLatin1String("weekday")) {
192  return Weekday;
193  } else {
194  qCDebug(LIBKSIEVE_LOG) << " date type unknown :" << str;
195  }
196  return Year;
197 }
198 
199 QString SelectDateWidget::dateType(SelectDateWidget::DateType type) const
200 {
201  QString str;
202  switch (type) {
203  case Year:
204  str = QStringLiteral("year");
205  break;
206  case Month:
207  str = QStringLiteral("month");
208  break;
209  case Day:
210  str = QStringLiteral("day");
211  break;
212  case Date:
213  str = QStringLiteral("date");
214  break;
215  case Julian:
216  str = QStringLiteral("julian");
217  break;
218  case Hour:
219  str = QStringLiteral("hour");
220  break;
221  case Minute:
222  str = QStringLiteral("minute");
223  break;
224  case Second:
225  str = QStringLiteral("second");
226  break;
227  case Time:
228  str = QStringLiteral("time");
229  break;
230  case Iso8601:
231  str = QStringLiteral("iso8601");
232  break;
233  case Std11:
234  str = QStringLiteral("std11");
235  break;
236  case Zone:
237  str = QStringLiteral("zone");
238  break;
239  case Weekday:
240  str = QStringLiteral("weekday");
241  break;
242  }
243  return str;
244 }
245 
246 QString SelectDateWidget::code() const
247 {
248  const DateType type = mDateType->itemData(mDateType->currentIndex()).value<KSieveUi::SelectDateWidget::DateType>();
249  return QStringLiteral("\"%1\" \"%2\"").arg(dateType(type), dateValue(type));
250 }
251 
252 void SelectDateWidget::setCode(const QString &type, const QString &value)
253 {
254  const int index = dateTypeFromString(type);
255  if (index != -1) {
256  mDateType->setCurrentIndex(index);
257  } else {
258  mDateType->setCurrentIndex(0);
259  }
260  const DateType dateType = mDateType->itemData(index).value<KSieveUi::SelectDateWidget::DateType>();
261  switch (dateType) {
262  case Month:
263  case Day:
264  case Hour:
265  case Minute:
266  case Second:
267  case Weekday:
268  case Year:
269  mStackWidget->setCurrentWidget(mDateValue);
270  mDateValue->setType(dateType);
271  mDateValue->setCode(value);
272  break;
273  case Date:
274  mStackWidget->setCurrentWidget(mDateEdit);
275  mDateEdit->setDate(QLocale::c().toDate(value));
276  break;
277  case Julian:
278  mStackWidget->setCurrentWidget(mDateLineEdit);
279  mDateLineEdit->setText(value);
280  break;
281  case Time:
282  mStackWidget->setCurrentWidget(mTimeEdit);
283  mTimeEdit->setTime(QLocale::c().toTime(value));
284  break;
285  case Iso8601:
286  case Std11:
287  case Zone:
288  mStackWidget->setCurrentWidget(mDateLineEdit);
289  mDateLineEdit->setText(value);
290  break;
291  }
292 }
void setContentsMargins(int left, int top, int right, int bottom)
QString toString(qlonglong i) const const
KCRASH_EXPORT void initialize()
void textChanged(const QString &text)
void valueChanged(int i)
QLocale c()
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
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
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Jul 9 2020 23:07:44 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.