File indexing completed on 2024-12-29 04:54:41

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "sievedatespinbox.h"
0008 #include "libksieveui_debug.h"
0009 using namespace KSieveUi;
0010 
0011 SieveDateSpinBox::SieveDateSpinBox(QWidget *parent)
0012     : QSpinBox(parent)
0013 {
0014     setSpecialValueText(QStringLiteral("*"));
0015 }
0016 
0017 SieveDateSpinBox::~SieveDateSpinBox() = default;
0018 
0019 void SieveDateSpinBox::setRange(int minValue, int maxValue)
0020 {
0021     // We need to define specialValueText => we set minimum to minimum - 1
0022     setMaximum(maxValue);
0023     setMinimum(minValue - 1);
0024 }
0025 
0026 void SieveDateSpinBox::setType(KSieveUi::SelectDateWidget::DateType type)
0027 {
0028     mType = type;
0029     switch (type) {
0030     case SelectDateWidget::Year:
0031         setRange(0, 9999);
0032         break;
0033     case SelectDateWidget::Month:
0034         setRange(1, 12);
0035         break;
0036     case SelectDateWidget::Day:
0037         setRange(1, 31);
0038         break;
0039     case SelectDateWidget::Hour:
0040         setRange(0, 23);
0041         break;
0042     case SelectDateWidget::Minute:
0043         setRange(0, 59);
0044         break;
0045     case SelectDateWidget::Second:
0046         setRange(0, 59);
0047         break;
0048     case SelectDateWidget::Weekday:
0049         setRange(0, 6);
0050         break;
0051     default:
0052         qCWarning(LIBKSIEVEUI_LOG) << "This type mustn't be selected " << mType;
0053     }
0054 }
0055 
0056 void SieveDateSpinBox::setCode(const QString &str)
0057 {
0058     if (str == QLatin1StringView("*")) {
0059         // setSpecialValue
0060         setValue(minimum());
0061     } else {
0062         setValue(str.toInt());
0063     }
0064 }
0065 
0066 QString SieveDateSpinBox::code() const
0067 {
0068     if (value() == minimum()) {
0069         return QStringLiteral("*");
0070     }
0071     return convertValue();
0072 }
0073 
0074 QString SieveDateSpinBox::convertValue() const
0075 {
0076     QString str;
0077     switch (mType) {
0078     case SelectDateWidget::Year:
0079         str = QStringLiteral("%1").arg(value(), 4, 10, QLatin1Char('0'));
0080         break;
0081     case SelectDateWidget::Month:
0082         str = QStringLiteral("%1").arg(value(), 2, 10, QLatin1Char('0'));
0083         break;
0084     case SelectDateWidget::Day:
0085         str = QStringLiteral("%1").arg(value(), 2, 10, QLatin1Char('0'));
0086         break;
0087     case SelectDateWidget::Hour:
0088         str = QStringLiteral("%1").arg(value(), 2, 10, QLatin1Char('0'));
0089         break;
0090     case SelectDateWidget::Minute:
0091         str = QStringLiteral("%1").arg(value(), 2, 10, QLatin1Char('0'));
0092         break;
0093     case SelectDateWidget::Second:
0094         str = QStringLiteral("%1").arg(value(), 2, 10, QLatin1Char('0'));
0095         break;
0096     case SelectDateWidget::Weekday:
0097         str = QString::number(value());
0098         break;
0099     default:
0100         qCWarning(LIBKSIEVEUI_LOG) << "This type mustn't be selected " << mType;
0101         break;
0102     }
0103     return str;
0104 }
0105 
0106 #include "moc_sievedatespinbox.cpp"