File indexing completed on 2024-12-29 04:54:40
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 #include "selectdatewidget.h" 0007 #include "sievedatespinbox.h" 0008 0009 #include <KDateComboBox> 0010 #include <KLineEditEventHandler> 0011 #include <KLocalizedString> 0012 #include <KTimeComboBox> 0013 #include <QComboBox> 0014 #include <QLineEdit> 0015 0016 #include "libksieveui_debug.h" 0017 #include <QHBoxLayout> 0018 #include <QLabel> 0019 #include <QStackedWidget> 0020 #include <QTime> 0021 0022 using namespace KSieveUi; 0023 SelectDateWidget::SelectDateWidget(QWidget *parent) 0024 : QWidget(parent) 0025 { 0026 initialize(); 0027 } 0028 0029 SelectDateWidget::~SelectDateWidget() = default; 0030 0031 void SelectDateWidget::initialize() 0032 { 0033 auto lay = new QHBoxLayout(this); 0034 lay->setContentsMargins({}); 0035 0036 mDateType = new QComboBox(this); 0037 connect(mDateType, &QComboBox::activated, this, &SelectDateWidget::slotDateTypeActivated); 0038 mDateType->addItem(i18n("Year"), QVariant::fromValue(KSieveUi::SelectDateWidget::Year)); 0039 mDateType->addItem(i18n("Month"), QVariant::fromValue(KSieveUi::SelectDateWidget::Month)); 0040 mDateType->addItem(i18n("Day"), QVariant::fromValue(KSieveUi::SelectDateWidget::Day)); 0041 mDateType->addItem(i18n("Date"), QVariant::fromValue(KSieveUi::SelectDateWidget::Date)); 0042 mDateType->addItem(i18n("Julian"), QVariant::fromValue(KSieveUi::SelectDateWidget::Julian)); 0043 mDateType->addItem(i18n("Hour"), QVariant::fromValue(KSieveUi::SelectDateWidget::Hour)); 0044 mDateType->addItem(i18n("Minute"), QVariant::fromValue(KSieveUi::SelectDateWidget::Minute)); 0045 mDateType->addItem(i18n("Second"), QVariant::fromValue(KSieveUi::SelectDateWidget::Second)); 0046 mDateType->addItem(i18n("Time"), QVariant::fromValue(KSieveUi::SelectDateWidget::Time)); 0047 mDateType->addItem(i18n("iso8601"), QVariant::fromValue(KSieveUi::SelectDateWidget::Iso8601)); 0048 mDateType->addItem(i18n("std11"), QVariant::fromValue(KSieveUi::SelectDateWidget::Std11)); 0049 mDateType->addItem(i18n("Zone"), QVariant::fromValue(KSieveUi::SelectDateWidget::Zone)); 0050 mDateType->addItem(i18n("Weekday"), QVariant::fromValue(KSieveUi::SelectDateWidget::Weekday)); 0051 lay->addWidget(mDateType); 0052 0053 auto lab = new QLabel(i18n("value:"), this); 0054 lay->addWidget(lab); 0055 0056 mStackWidget = new QStackedWidget; 0057 lay->addWidget(mStackWidget); 0058 0059 mDateLineEdit = new QLineEdit(this); 0060 KLineEditEventHandler::catchReturnKey(mDateLineEdit); 0061 mStackWidget->addWidget(mDateLineEdit); 0062 connect(mDateLineEdit, &QLineEdit::textChanged, this, &SelectDateWidget::valueChanged); 0063 0064 mDateValue = new SieveDateSpinBox(this); 0065 mStackWidget->addWidget(mDateValue); 0066 connect(mDateValue, &QSpinBox::valueChanged, this, &SelectDateWidget::valueChanged); 0067 0068 mDateEdit = new KDateComboBox(this); 0069 mStackWidget->addWidget(mDateEdit); 0070 connect(mDateEdit, &KDateComboBox::dateChanged, this, &SelectDateWidget::valueChanged); 0071 0072 mTimeEdit = new KTimeComboBox(this); 0073 mStackWidget->addWidget(mTimeEdit); 0074 connect(mTimeEdit, &KTimeComboBox::timeChanged, this, &SelectDateWidget::valueChanged); 0075 0076 mStackWidget->setCurrentWidget(mDateValue); 0077 } 0078 0079 void SelectDateWidget::slotDateTypeActivated(int index) 0080 { 0081 const auto type = mDateType->itemData(index).value<KSieveUi::SelectDateWidget::DateType>(); 0082 switch (type) { 0083 case Year: 0084 case Month: 0085 case Day: 0086 case Hour: 0087 case Minute: 0088 case Second: 0089 case Weekday: 0090 mDateValue->setType(type); 0091 mStackWidget->setCurrentWidget(mDateValue); 0092 break; 0093 case Date: 0094 mStackWidget->setCurrentWidget(mDateEdit); 0095 break; 0096 case Julian: 0097 mStackWidget->setCurrentWidget(mDateLineEdit); 0098 break; 0099 case Time: 0100 mStackWidget->setCurrentWidget(mTimeEdit); 0101 break; 0102 case Iso8601: 0103 mStackWidget->setCurrentWidget(mDateLineEdit); 0104 break; 0105 case Std11: 0106 mStackWidget->setCurrentWidget(mDateLineEdit); 0107 break; 0108 case Zone: 0109 mStackWidget->setCurrentWidget(mDateLineEdit); 0110 break; 0111 } 0112 Q_EMIT valueChanged(); 0113 } 0114 0115 QString SelectDateWidget::dateValue(SelectDateWidget::DateType type) const 0116 { 0117 QString str; 0118 switch (type) { 0119 case Year: 0120 case Month: 0121 case Day: 0122 case Hour: 0123 case Minute: 0124 case Second: 0125 case Weekday: 0126 str = mDateValue->code(); 0127 break; 0128 case Date: 0129 str = QLocale::c().toString(mDateEdit->date()); 0130 break; 0131 case Julian: 0132 // TODO 0133 str = QLocale::c().toString(mDateEdit->date()); 0134 break; 0135 case Time: 0136 str = QLocale::c().toString(mTimeEdit->time()); 0137 break; 0138 case Iso8601: 0139 str = QLocale::c().toString(mDateEdit->date()); 0140 break; 0141 case Std11: 0142 str = QLocale::c().toString(mDateEdit->date()); 0143 break; 0144 case Zone: 0145 str = QLocale::c().toString(mDateEdit->date()); 0146 break; 0147 } 0148 return str; 0149 } 0150 0151 SelectDateWidget::DateType SelectDateWidget::dateTypeFromString(const QString &str) 0152 { 0153 if (str == QLatin1StringView("year")) { 0154 return Year; 0155 } else if (str == QLatin1StringView("month")) { 0156 return Month; 0157 } else if (str == QLatin1StringView("day")) { 0158 return Day; 0159 } else if (str == QLatin1StringView("date")) { 0160 return Date; 0161 } else if (str == QLatin1StringView("julian")) { 0162 return Julian; 0163 } else if (str == QLatin1StringView("hour")) { 0164 return Hour; 0165 } else if (str == QLatin1StringView("minute")) { 0166 return Minute; 0167 } else if (str == QLatin1StringView("second")) { 0168 return Second; 0169 } else if (str == QLatin1StringView("time")) { 0170 return Time; 0171 } else if (str == QLatin1StringView("iso8601")) { 0172 return Iso8601; 0173 } else if (str == QLatin1StringView("std11")) { 0174 return Std11; 0175 } else if (str == QLatin1StringView("zone")) { 0176 return Zone; 0177 } else if (str == QLatin1StringView("weekday")) { 0178 return Weekday; 0179 } else { 0180 qCDebug(LIBKSIEVEUI_LOG) << " date type unknown :" << str; 0181 } 0182 return Year; 0183 } 0184 0185 QString SelectDateWidget::dateType(SelectDateWidget::DateType type) const 0186 { 0187 QString str; 0188 switch (type) { 0189 case Year: 0190 str = QStringLiteral("year"); 0191 break; 0192 case Month: 0193 str = QStringLiteral("month"); 0194 break; 0195 case Day: 0196 str = QStringLiteral("day"); 0197 break; 0198 case Date: 0199 str = QStringLiteral("date"); 0200 break; 0201 case Julian: 0202 str = QStringLiteral("julian"); 0203 break; 0204 case Hour: 0205 str = QStringLiteral("hour"); 0206 break; 0207 case Minute: 0208 str = QStringLiteral("minute"); 0209 break; 0210 case Second: 0211 str = QStringLiteral("second"); 0212 break; 0213 case Time: 0214 str = QStringLiteral("time"); 0215 break; 0216 case Iso8601: 0217 str = QStringLiteral("iso8601"); 0218 break; 0219 case Std11: 0220 str = QStringLiteral("std11"); 0221 break; 0222 case Zone: 0223 str = QStringLiteral("zone"); 0224 break; 0225 case Weekday: 0226 str = QStringLiteral("weekday"); 0227 break; 0228 } 0229 return str; 0230 } 0231 0232 QString SelectDateWidget::code() const 0233 { 0234 const auto type = mDateType->itemData(mDateType->currentIndex()).value<KSieveUi::SelectDateWidget::DateType>(); 0235 return QStringLiteral("\"%1\" \"%2\"").arg(dateType(type), dateValue(type)); 0236 } 0237 0238 void SelectDateWidget::setCode(const QString &type, const QString &value) 0239 { 0240 const int index = dateTypeFromString(type); 0241 if (index != -1) { 0242 mDateType->setCurrentIndex(index); 0243 } else { 0244 mDateType->setCurrentIndex(0); 0245 } 0246 const auto dateType = mDateType->itemData(index).value<KSieveUi::SelectDateWidget::DateType>(); 0247 switch (dateType) { 0248 case Month: 0249 case Day: 0250 case Hour: 0251 case Minute: 0252 case Second: 0253 case Weekday: 0254 case Year: 0255 mStackWidget->setCurrentWidget(mDateValue); 0256 mDateValue->setType(dateType); 0257 mDateValue->setCode(value); 0258 break; 0259 case Date: 0260 mStackWidget->setCurrentWidget(mDateEdit); 0261 mDateEdit->setDate(QLocale::c().toDate(value)); 0262 break; 0263 case Julian: 0264 mStackWidget->setCurrentWidget(mDateLineEdit); 0265 mDateLineEdit->setText(value); 0266 break; 0267 case Time: 0268 mStackWidget->setCurrentWidget(mTimeEdit); 0269 mTimeEdit->setTime(QLocale::c().toTime(value)); 0270 break; 0271 case Iso8601: 0272 case Std11: 0273 case Zone: 0274 mStackWidget->setCurrentWidget(mDateLineEdit); 0275 mDateLineEdit->setText(value); 0276 break; 0277 } 0278 } 0279 0280 #include "moc_selectdatewidget.cpp"