Continue to port to QRegularExpression
[kdepim.git] / kalarm / deferdlg.cpp
blobed5bafa3230df8c467ba52798f90acb2a4f902ea
1 /*
2 * deferdlg.cpp - dialog to defer an alarm
3 * Program: kalarm
4 * Copyright © 2002-2012 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "kalarm.h"
22 #include "deferdlg.h"
24 #include "alarmcalendar.h"
25 #include "alarmtimewidget.h"
26 #include "functions.h"
27 #include "kalarmapp.h"
28 #include "messagebox.h"
30 #include <kalarmcal/datetime.h>
31 #include <kalarmcal/kaevent.h>
33 #include <KLocalizedString>
35 #include <QVBoxLayout>
36 #include <QStyle>
37 #include <QDialogButtonBox>
38 #include <QPushButton>
39 #include "kalarm_debug.h"
42 DeferAlarmDlg::DeferAlarmDlg(const DateTime& initialDT, bool anyTimeOption, bool cancelButton, QWidget* parent)
43 : QDialog(parent)
45 setWindowModality(Qt::WindowModal);
46 setWindowTitle(i18nc("@title:window", "Defer Alarm"));
48 QVBoxLayout* layout = new QVBoxLayout(this);
49 layout->setMargin(0);
50 layout->setSpacing(style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing));
52 mTimeWidget = new AlarmTimeWidget((anyTimeOption ? AlarmTimeWidget::DEFER_ANY_TIME : AlarmTimeWidget::DEFER_TIME), this);
53 mTimeWidget->setDateTime(initialDT);
54 mTimeWidget->setMinDateTimeIsCurrent();
55 connect(mTimeWidget, &AlarmTimeWidget::pastMax, this, &DeferAlarmDlg::slotPastLimit);
56 layout->addWidget(mTimeWidget);
57 layout->addSpacing(style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing));
59 mButtonBox = new QDialogButtonBox(this);
60 layout->addWidget(mButtonBox);
61 QPushButton *okButton = mButtonBox->addButton(QDialogButtonBox::Ok);
62 okButton->setWhatsThis(i18nc("@info:whatsthis", "Defer the alarm until the specified time."));
63 if (cancelButton) {
64 mButtonBox->addButton(QDialogButtonBox::Cancel);
66 QPushButton *deferButton =mButtonBox->addButton(i18nc("@action:button", "Cancel Deferral"), QDialogButtonBox::ActionRole);
67 deferButton->setWhatsThis(i18nc("@info:whatsthis", "Cancel the deferred alarm. This does not affect future recurrences."));
68 connect(mButtonBox, &QDialogButtonBox::accepted,
69 this, &DeferAlarmDlg::slotOk);
70 connect(mButtonBox, &QDialogButtonBox::rejected,
71 this, &QDialog::reject);
72 connect(mButtonBox, &QDialogButtonBox::clicked,
73 [this, deferButton](QAbstractButton *btn) {
74 if (btn == deferButton) {
75 slotCancelDeferral();
77 });
82 /******************************************************************************
83 * Called when the OK button is clicked.
85 void DeferAlarmDlg::slotOk()
87 mAlarmDateTime = mTimeWidget->getDateTime(&mDeferMinutes);
88 if (!mAlarmDateTime.isValid())
89 return;
90 KAEvent::DeferLimitType limitType = KAEvent::LIMIT_NONE;
91 DateTime endTime;
92 if (!mLimitEventId.isEmpty())
94 // Get the event being deferred
95 const KAEvent* event = AlarmCalendar::getEvent(mLimitEventId);
96 if (event)
97 endTime = event->deferralLimit(&limitType);
99 else
101 endTime = mLimitDateTime;
102 limitType = mLimitDateTime.isValid() ? KAEvent::LIMIT_MAIN : KAEvent::LIMIT_NONE;
104 if (endTime.isValid() && mAlarmDateTime > endTime)
106 QString text;
107 switch (limitType)
109 case KAEvent::LIMIT_REPETITION:
110 text = i18nc("@info", "Cannot defer past the alarm's next sub-repetition (currently %1)",
111 endTime.formatLocale());
112 break;
113 case KAEvent::LIMIT_RECURRENCE:
114 text = i18nc("@info", "Cannot defer past the alarm's next recurrence (currently %1)",
115 endTime.formatLocale());
116 break;
117 case KAEvent::LIMIT_REMINDER:
118 text = i18nc("@info", "Cannot defer past the alarm's next reminder (currently %1)",
119 endTime.formatLocale());
120 break;
121 case KAEvent::LIMIT_MAIN:
122 text = i18nc("@info", "Cannot defer reminder past the main alarm time (%1)",
123 endTime.formatLocale());
124 break;
125 case KAEvent::LIMIT_NONE:
126 break; // can't happen with a valid endTime
128 KAMessageBox::sorry(this, text);
130 else
131 accept();
134 /******************************************************************************
135 * Select the 'Time from now' radio button and preset its value.
137 void DeferAlarmDlg::setDeferMinutes(int minutes)
139 mTimeWidget->selectTimeFromNow(minutes);
142 /******************************************************************************
143 * Called the maximum date/time for the date/time edit widget has been passed.
145 void DeferAlarmDlg::slotPastLimit()
147 mButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
150 /******************************************************************************
151 * Set the time limit for deferral based on the next occurrence of the alarm
152 * with the specified ID.
154 void DeferAlarmDlg::setLimit(const DateTime& limit)
156 mLimitEventId.clear();
157 mLimitDateTime = limit;
158 mTimeWidget->setMaxDateTime(mLimitDateTime);
161 /******************************************************************************
162 * Set the time limit for deferral based on the next occurrence of the alarm
163 * with the specified ID.
165 DateTime DeferAlarmDlg::setLimit(const KAEvent& event)
167 Q_ASSERT(event.collectionId() >= 0);
168 mLimitEventId = EventId(event);
169 const KAEvent* evnt = AlarmCalendar::getEvent(mLimitEventId);
170 mLimitDateTime = evnt ? evnt->deferralLimit() : DateTime();
171 mTimeWidget->setMaxDateTime(mLimitDateTime);
172 return mLimitDateTime;
175 /******************************************************************************
176 * Called when the Cancel Deferral button is clicked.
178 void DeferAlarmDlg::slotCancelDeferral()
180 mAlarmDateTime = DateTime();
181 accept();
183 // vim: et sw=4: