Warning, file /pim/kalarm/src/deferdlg.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  deferdlg.cpp  -  dialog to defer an alarm
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2002-2023 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "deferdlg.h"
0010 
0011 #include "alarmtimewidget.h"
0012 #include "resources/resources.h"
0013 #include "lib/messagebox.h"
0014 #include "kalarmcalendar/kaevent.h"
0015 
0016 #include <KLocalizedString>
0017 
0018 #include <QVBoxLayout>
0019 #include <QStyle>
0020 #include <QDialogButtonBox>
0021 #include <QPushButton>
0022 
0023 
0024 /******************************************************************************
0025 * Constructor.
0026 * If 'cancelButton' is true, the Cancel Deferral button will be shown to allow
0027 * any existing deferral to be cancelled.
0028 */
0029 DeferAlarmDlg::DeferAlarmDlg(const DateTime& initialDT, bool anyTimeOption, bool cancelButton, QWidget* parent)
0030     : QDialog(parent)
0031 {
0032     setWindowModality(Qt::WindowModal);
0033     setWindowTitle(i18nc("@title:window", "Defer Alarm"));
0034 
0035     auto layout = new QVBoxLayout(this);
0036 
0037     mTimeWidget = new AlarmTimeWidget((anyTimeOption ? AlarmTimeWidget::DEFER_ANY_TIME : AlarmTimeWidget::DEFER_TIME), this);
0038     mTimeWidget->setDateTime(initialDT);
0039     mTimeWidget->setMinDateTimeIsCurrent();
0040     connect(mTimeWidget, &AlarmTimeWidget::pastMax, this, &DeferAlarmDlg::slotPastLimit);
0041     layout->addWidget(mTimeWidget);
0042     layout->addSpacing(style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing));
0043 
0044     mButtonBox = new QDialogButtonBox(this);
0045     layout->addWidget(mButtonBox);
0046     QPushButton* okButton = mButtonBox->addButton(QDialogButtonBox::Ok);
0047     okButton->setWhatsThis(i18nc("@info:whatsthis", "Defer the alarm until the specified time."));
0048     mButtonBox->addButton(QDialogButtonBox::Cancel);
0049     connect(mButtonBox, &QDialogButtonBox::accepted,
0050             this, &DeferAlarmDlg::slotOk);
0051     connect(mButtonBox, &QDialogButtonBox::rejected,
0052             this, &QDialog::reject);
0053     if (cancelButton)
0054     {
0055         QPushButton* deferButton = mButtonBox->addButton(i18nc("@action:button", "Cancel Deferral"), QDialogButtonBox::ActionRole);
0056         deferButton->setWhatsThis(i18nc("@info:whatsthis", "Cancel the deferred alarm. This does not affect future recurrences."));
0057         connect(mButtonBox, &QDialogButtonBox::clicked, this,
0058                 [this, deferButton](const QAbstractButton* btn)
0059                 {
0060                     if (btn == deferButton)
0061                         slotCancelDeferral();
0062                 });
0063     }
0064 }
0065 
0066 /******************************************************************************
0067 * Called when the OK button is clicked.
0068 */
0069 void DeferAlarmDlg::slotOk()
0070 {
0071     mAlarmDateTime = mTimeWidget->getDateTime(&mDeferMinutes);
0072     if (!mAlarmDateTime.isValid())
0073         return;
0074     KAEvent::DeferLimit limitType = KAEvent::DeferLimit::None;
0075     DateTime endTime;
0076     if (!mLimitEventId.isEmpty())
0077     {
0078         // Get the event being deferred
0079         Resource resource = Resources::resource(mLimitEventId.resourceId());
0080         const KAEvent event = resource.event(mLimitEventId.eventId());
0081         if (event.isValid())
0082             endTime = event.deferralLimit(&limitType);
0083     }
0084     else
0085     {
0086         endTime = mLimitDateTime;
0087         limitType = mLimitDateTime.isValid() ? KAEvent::DeferLimit::Main : KAEvent::DeferLimit::None;
0088     }
0089     if (endTime.isValid()  &&  mAlarmDateTime > endTime)
0090     {
0091         QString text;
0092         switch (limitType)
0093         {
0094             case KAEvent::DeferLimit::Repetition:
0095                 text = i18nc("@info", "Cannot defer past the alarm's next sub-repetition (currently %1)",
0096                              endTime.formatLocale());
0097                 break;
0098             case KAEvent::DeferLimit::Recurrence:
0099                 text = i18nc("@info", "Cannot defer past the alarm's next recurrence (currently %1)",
0100                              endTime.formatLocale());
0101                 break;
0102             case KAEvent::DeferLimit::Reminder:
0103                 text = i18nc("@info", "Cannot defer past the alarm's next reminder (currently %1)",
0104                             endTime.formatLocale());
0105                 break;
0106             case KAEvent::DeferLimit::Main:
0107                 text = i18nc("@info", "Cannot defer reminder past the main alarm time (%1)",
0108                             endTime.formatLocale());
0109                 break;
0110             case KAEvent::DeferLimit::None:
0111                 break;   // can't happen with a valid endTime
0112         }
0113         KAMessageBox::error(this, text);
0114     }
0115     else
0116         accept();
0117 }
0118 
0119 /******************************************************************************
0120 * Select the 'Time from now' radio button and preset its value.
0121 */
0122 void DeferAlarmDlg::setDeferMinutes(int minutes)
0123 {
0124     mTimeWidget->selectTimeFromNow(minutes);
0125 }
0126 
0127 /******************************************************************************
0128 * Called the maximum date/time for the date/time edit widget has been passed.
0129 */
0130 void DeferAlarmDlg::slotPastLimit()
0131 {
0132     mButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0133 }
0134 
0135 /******************************************************************************
0136 * Set the time limit for deferral based on the next occurrence of the alarm
0137 * with the specified ID.
0138 */
0139 void DeferAlarmDlg::setLimit(const DateTime& limit)
0140 {
0141     mLimitEventId.clear();
0142     mLimitDateTime = limit;
0143     mTimeWidget->setMaxDateTime(mLimitDateTime);
0144 }
0145 
0146 /******************************************************************************
0147 * Set the time limit for deferral based on the next occurrence of the alarm
0148 * with the specified ID.
0149 */
0150 DateTime DeferAlarmDlg::setLimit(const KAEvent& event)
0151 {
0152     Q_ASSERT(event.resourceId() >= 0);
0153     mLimitEventId = EventId(event);
0154     Resource resource = Resources::resource(event.resourceId());
0155     const KAEvent evnt = resource.event(event.id());
0156     mLimitDateTime = evnt.isValid() ? evnt.deferralLimit() : DateTime();
0157     mTimeWidget->setMaxDateTime(mLimitDateTime);
0158     return mLimitDateTime;
0159 }
0160 
0161 /******************************************************************************
0162 * Called when the Cancel Deferral button is clicked.
0163 */
0164 void DeferAlarmDlg::slotCancelDeferral()
0165 {
0166     mAlarmDateTime = DateTime();
0167     accept();
0168 }
0169 
0170 #include "moc_deferdlg.cpp"
0171 
0172 // vim: et sw=4: