File indexing completed on 2025-01-12 04:34:10

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "administratormoderationrangewidget.h"
0007 #include <KLocalizedString>
0008 #include <QAction>
0009 #include <QDateEdit>
0010 #include <QHBoxLayout>
0011 #include <QLabel>
0012 #include <QMenu>
0013 #include <QToolButton>
0014 
0015 AdministratorModerationRangeWidget::AdministratorModerationRangeWidget(QWidget *parent)
0016     : QWidget{parent}
0017     , mFromDate(new QDateEdit(this))
0018     , mToDate(new QDateEdit(this))
0019     , mFilterDate(new QToolButton(this))
0020 {
0021     auto mainLayout = new QHBoxLayout(this);
0022     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0023 
0024     mFromDate->setObjectName(QStringLiteral("mFromDate"));
0025     mToDate->setObjectName(QStringLiteral("mToDate"));
0026 
0027     mFilterDate->setObjectName(QStringLiteral("mFilterDate"));
0028     mFilterDate->setAutoRaise(true);
0029     mFilterDate->setPopupMode(QToolButton::InstantPopup);
0030     mFilterDate->setIcon(QIcon::fromTheme(QStringLiteral("application-menu")));
0031 
0032     auto fromLabel = new QLabel(i18n("From:"), this);
0033     fromLabel->setObjectName(QStringLiteral("fromLabel"));
0034 
0035     auto toLabel = new QLabel(i18n("To:"), this);
0036     toLabel->setObjectName(QStringLiteral("toLabel"));
0037 
0038     mainLayout->addWidget(fromLabel);
0039     mainLayout->addWidget(mFromDate);
0040     mainLayout->addWidget(toLabel);
0041     mainLayout->addWidget(mToDate);
0042     mainLayout->addWidget(mFilterDate);
0043     mainLayout->addStretch();
0044     initializeMenu();
0045     connect(mFromDate, &QDateEdit::dateChanged, this, &AdministratorModerationRangeWidget::slotRangeChanged);
0046     connect(mToDate, &QDateEdit::dateChanged, this, &AdministratorModerationRangeWidget::slotRangeChanged);
0047 }
0048 
0049 AdministratorModerationRangeWidget::~AdministratorModerationRangeWidget() = default;
0050 
0051 AdministratorModerationRangeWidget::DateTimeRange AdministratorModerationRangeWidget::range() const
0052 {
0053     AdministratorModerationRangeWidget::DateTimeRange r;
0054     r.fromDate = QDateTime(mFromDate->date(), QTime(0, 0, 0));
0055     r.toDate = QDateTime(mToDate->date(), QTime(23, 59, 59));
0056     return r;
0057 }
0058 
0059 void AdministratorModerationRangeWidget::slotRangeChanged()
0060 {
0061     const QDate fromDate = mFromDate->date();
0062     const QDate toDate = mToDate->date();
0063     if (fromDate > toDate) {
0064         QSignalBlocker b(mFromDate);
0065         QSignalBlocker b2(mToDate);
0066         mToDate->setDate(fromDate);
0067         mFromDate->setDate(toDate);
0068     }
0069     Q_EMIT rangeChanged();
0070 }
0071 
0072 void AdministratorModerationRangeWidget::initializeMenu()
0073 {
0074     auto *menu = new QMenu(mFilterDate);
0075     auto todayAction = new QAction(i18n("Today"), menu);
0076     menu->addAction(todayAction);
0077     connect(todayAction, &QAction::triggered, this, [this]() {
0078         const QDate date = QDate::currentDate();
0079         QSignalBlocker b(mFromDate);
0080         QSignalBlocker b2(mToDate);
0081         mFromDate->setDate(date);
0082         mToDate->setDate(date);
0083         Q_EMIT rangeChanged();
0084     });
0085     auto yesterdayAction = new QAction(i18n("Yesterday"), menu);
0086     menu->addAction(yesterdayAction);
0087     connect(yesterdayAction, &QAction::triggered, this, [this]() {
0088         QSignalBlocker b(mFromDate);
0089         QSignalBlocker b2(mToDate);
0090         const QDate date = QDate::currentDate().addDays(-1);
0091         mFromDate->setDate(date);
0092         mToDate->setDate(date);
0093         Q_EMIT rangeChanged();
0094     });
0095     auto thisWeekAction = new QAction(i18n("This week"), menu);
0096     menu->addAction(thisWeekAction);
0097     connect(thisWeekAction, &QAction::triggered, this, [this]() {
0098         QSignalBlocker b(mFromDate);
0099         QSignalBlocker b2(mToDate);
0100         const QDate date = QDate::currentDate();
0101         mFromDate->setDate(date.addDays(-7));
0102         mToDate->setDate(date);
0103         Q_EMIT rangeChanged();
0104     });
0105     auto previousWeekAction = new QAction(i18n("Previous week"), menu);
0106     menu->addAction(previousWeekAction);
0107     connect(previousWeekAction, &QAction::triggered, this, [this]() {
0108         QSignalBlocker b(mFromDate);
0109         QSignalBlocker b2(mToDate);
0110         const QDate date = QDate::currentDate().addDays(-7);
0111         mFromDate->setDate(date.addDays(-7));
0112         mToDate->setDate(date);
0113         Q_EMIT rangeChanged();
0114     });
0115     auto thisMonthAction = new QAction(i18n("This month"), menu);
0116     menu->addAction(thisMonthAction);
0117     connect(thisMonthAction, &QAction::triggered, this, [this]() {
0118         QSignalBlocker b(mFromDate);
0119         QSignalBlocker b2(mToDate);
0120         const QDate date = QDate::currentDate();
0121         const QDate monthDate = QDate(date.year(), date.month(), 1);
0122         mFromDate->setDate(monthDate);
0123         mToDate->setDate(QDate(date.year(), date.month(), monthDate.daysInMonth()));
0124         Q_EMIT rangeChanged();
0125     });
0126     auto previousMonthAction = new QAction(i18n("Previous month"), menu);
0127     menu->addAction(previousMonthAction);
0128     connect(previousMonthAction, &QAction::triggered, this, [this]() {
0129         QSignalBlocker b(mFromDate);
0130         QSignalBlocker b2(mToDate);
0131         const QDate date = QDate::currentDate().addMonths(-1);
0132         const QDate monthDate = QDate(date.year(), date.month(), 1);
0133         mFromDate->setDate(monthDate);
0134         mToDate->setDate(QDate(QDate(date.year(), date.month(), monthDate.daysInMonth())));
0135         Q_EMIT rangeChanged();
0136     });
0137     mFilterDate->setMenu(menu);
0138 }
0139 
0140 QDebug operator<<(QDebug d, const AdministratorModerationRangeWidget::DateTimeRange &t)
0141 {
0142     d << "fromDate : " << t.fromDate;
0143     d << "toDate : " << t.toDate;
0144     return d;
0145 }
0146 
0147 #include "moc_administratormoderationrangewidget.cpp"