File indexing completed on 2024-06-23 05:18:29

0001 /*
0002    SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "followupreminderselectdatedialog.h"
0008 
0009 #include <KDateComboBox>
0010 #include <KLocalizedString>
0011 #include <KMessageBox>
0012 #include <KSharedConfig>
0013 
0014 #include <Akonadi/CollectionComboBox>
0015 
0016 #include <KCalendarCore/Todo>
0017 
0018 #include <QDialogButtonBox>
0019 #include <QFormLayout>
0020 #include <QLineEdit>
0021 #include <QVBoxLayout>
0022 using namespace MessageComposer;
0023 class MessageComposer::FollowUpReminderSelectDateDialogPrivate
0024 {
0025 public:
0026     KDateComboBox *mDateComboBox = nullptr;
0027     Akonadi::CollectionComboBox *mCollectionCombobox = nullptr;
0028     QPushButton *mOkButton = nullptr;
0029 };
0030 
0031 FollowUpReminderSelectDateDialog::FollowUpReminderSelectDateDialog(QWidget *parent, QAbstractItemModel *model)
0032     : QDialog(parent)
0033     , d(new MessageComposer::FollowUpReminderSelectDateDialogPrivate)
0034 {
0035     setWindowTitle(i18nc("@title:window", "Select Date"));
0036     auto topLayout = new QVBoxLayout(this);
0037 
0038     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0039     d->mOkButton = buttonBox->button(QDialogButtonBox::Ok);
0040     d->mOkButton->setObjectName(QLatin1StringView("ok_button"));
0041     d->mOkButton->setDefault(true);
0042     d->mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0043     connect(buttonBox, &QDialogButtonBox::accepted, this, &FollowUpReminderSelectDateDialog::accept);
0044     connect(buttonBox, &QDialogButtonBox::rejected, this, &FollowUpReminderSelectDateDialog::reject);
0045     setModal(true);
0046 
0047     auto mainWidget = new QWidget(this);
0048     topLayout->addWidget(mainWidget);
0049     topLayout->addWidget(buttonBox);
0050     auto mainLayout = new QVBoxLayout(mainWidget);
0051     mainLayout->setContentsMargins({});
0052     auto formLayout = new QFormLayout;
0053     formLayout->setContentsMargins({});
0054     mainLayout->addLayout(formLayout);
0055 
0056     d->mDateComboBox = new KDateComboBox;
0057     QDate currentDate = QDate::currentDate().addDays(1);
0058     d->mDateComboBox->setMinimumDate(QDate::currentDate());
0059     d->mDateComboBox->setObjectName(QLatin1StringView("datecombobox"));
0060 
0061     d->mDateComboBox->setDate(currentDate);
0062 
0063     formLayout->addRow(i18n("Date:"), d->mDateComboBox);
0064 
0065     d->mCollectionCombobox = new Akonadi::CollectionComboBox(model);
0066     d->mCollectionCombobox->setMinimumWidth(250);
0067     d->mCollectionCombobox->setAccessRightsFilter(Akonadi::Collection::CanCreateItem);
0068     d->mCollectionCombobox->setMimeTypeFilter(QStringList() << KCalendarCore::Todo::todoMimeType());
0069     d->mCollectionCombobox->setObjectName(QLatin1StringView("collectioncombobox"));
0070 
0071     formLayout->addRow(i18n("Store to-do in:"), d->mCollectionCombobox);
0072 
0073     connect(d->mDateComboBox->lineEdit(), &QLineEdit::textChanged, this, &FollowUpReminderSelectDateDialog::slotDateChanged);
0074     connect(d->mCollectionCombobox, &Akonadi::CollectionComboBox::currentIndexChanged, this, &FollowUpReminderSelectDateDialog::updateOkButton);
0075     updateOkButton();
0076 }
0077 
0078 FollowUpReminderSelectDateDialog::~FollowUpReminderSelectDateDialog() = default;
0079 
0080 void FollowUpReminderSelectDateDialog::updateOkButton()
0081 {
0082     d->mOkButton->setEnabled(!d->mDateComboBox->lineEdit()->text().isEmpty() && d->mDateComboBox->date().isValid() && (d->mCollectionCombobox->count() > 0)
0083                              && d->mCollectionCombobox->currentCollection().isValid());
0084 }
0085 
0086 void FollowUpReminderSelectDateDialog::slotDateChanged()
0087 {
0088     updateOkButton();
0089 }
0090 
0091 QDate FollowUpReminderSelectDateDialog::selectedDate() const
0092 {
0093     return d->mDateComboBox->date();
0094 }
0095 
0096 Akonadi::Collection FollowUpReminderSelectDateDialog::collection() const
0097 {
0098     return d->mCollectionCombobox->currentCollection();
0099 }
0100 
0101 void FollowUpReminderSelectDateDialog::accept()
0102 {
0103     const QDate date = selectedDate();
0104     if (date < QDate::currentDate()) {
0105         KMessageBox::error(this, i18n("The selected date must be greater than the current date."), i18nc("@title:window", "Invalid date"));
0106         return;
0107     }
0108     if (!d->mCollectionCombobox->currentCollection().isValid()) {
0109         KMessageBox::error(this, i18n("The selected folder is not valid."), i18nc("@title:window", "Invalid folder"));
0110         return;
0111     }
0112     QDialog::accept();
0113 }
0114 
0115 #include "moc_followupreminderselectdatedialog.cpp"