File indexing completed on 2024-12-22 05:00:51

0001 /*
0002    SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "archivemailrangewidget.h"
0008 #include "archivemailagent_debug.h"
0009 #include "widgets/hourcombobox.h"
0010 #include <KLocalizedString>
0011 #include <QCheckBox>
0012 #include <QHBoxLayout>
0013 #include <QLabel>
0014 #include <QTime>
0015 
0016 ArchiveMailRangeWidget::ArchiveMailRangeWidget(QWidget *parent)
0017     : QWidget{parent}
0018     , mStartRange(new HourComboBox(this))
0019     , mEndRange(new HourComboBox(this))
0020     , mRangeEnabled(new QCheckBox(i18n("Use Range"), this))
0021 {
0022     auto mainLayout = new QHBoxLayout(this);
0023     mainLayout->setObjectName(QLatin1StringView("mainLayout"));
0024     mainLayout->setContentsMargins({});
0025 
0026     mRangeEnabled->setObjectName(QLatin1StringView("mRangeEnabled"));
0027     mainLayout->addWidget(mRangeEnabled);
0028 
0029     mStartRange->setObjectName(QLatin1StringView("mStartRange"));
0030 
0031     mEndRange->setObjectName(QLatin1StringView("mEndRange"));
0032 
0033     mainLayout->addWidget(mStartRange);
0034     mainLayout->addWidget(mEndRange);
0035 
0036     connect(mRangeEnabled, &QCheckBox::toggled, this, &ArchiveMailRangeWidget::changeRangeState);
0037     changeRangeState(false);
0038     mEndRange->setCurrentIndex(1); // Make sure that we have 1 hour between start/end
0039     connect(mStartRange, &HourComboBox::activated, this, [this](int index) {
0040         const int startHours = mStartRange->itemData(index).toInt();
0041         int endHours = mEndRange->currentData().toInt();
0042         if (startHours == endHours) {
0043             if (endHours == 23) {
0044                 endHours = 0;
0045             } else {
0046                 endHours = startHours + 1;
0047             }
0048             mEndRange->blockSignals(true);
0049             mEndRange->setHour(endHours);
0050             mEndRange->blockSignals(false);
0051         }
0052     });
0053     connect(mEndRange, &HourComboBox::activated, this, [this](int index) {
0054         int startHours = mStartRange->currentData().toInt();
0055         const int endHours = mEndRange->itemData(index).toInt();
0056         if (startHours == endHours) {
0057             if (endHours == 0) {
0058                 startHours = 23;
0059             } else {
0060                 startHours = startHours - 1;
0061             }
0062             mStartRange->blockSignals(true);
0063             mStartRange->setHour(startHours);
0064             mStartRange->blockSignals(false);
0065         }
0066     });
0067 }
0068 
0069 ArchiveMailRangeWidget::~ArchiveMailRangeWidget() = default;
0070 
0071 void ArchiveMailRangeWidget::changeRangeState(bool enabled)
0072 {
0073     mStartRange->setEnabled(enabled);
0074     mEndRange->setEnabled(enabled);
0075 }
0076 
0077 bool ArchiveMailRangeWidget::isRangeEnabled() const
0078 {
0079     return mRangeEnabled->isChecked();
0080 }
0081 
0082 void ArchiveMailRangeWidget::setRangeEnabled(bool isEnabled)
0083 {
0084     mRangeEnabled->setChecked(isEnabled);
0085 }
0086 
0087 QList<int> ArchiveMailRangeWidget::range() const
0088 {
0089     const QList<int> timeRange{mStartRange->hour(), mEndRange->hour()};
0090     return timeRange;
0091 }
0092 
0093 void ArchiveMailRangeWidget::setRange(const QList<int> &hours)
0094 {
0095     if (hours.count() != 2) {
0096         qCWarning(ARCHIVEMAILAGENT_LOG) << "Ranges is invalid " << hours;
0097     } else {
0098         mStartRange->setHour(hours.at(0));
0099         mEndRange->setHour(hours.at(1));
0100     }
0101 }
0102 
0103 #include "moc_archivemailrangewidget.cpp"