Make it possible to use a distinct selection model in the foldertreewidget.
[kdepim.git] / kalarm / deferdlg.cpp
blob91c2c89e986181671bbb9c1ddc82005c5648681a
1 /*
2 * deferdlg.cpp - dialog to defer an alarm
3 * Program: kalarm
4 * Copyright © 2002-2010 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"
23 #include <QVBoxLayout>
25 #include <kglobal.h>
26 #include <klocale.h>
27 #include <kmessagebox.h>
28 #include <kdebug.h>
30 #include <kcal/event.h>
31 #include <kcal/recurrence.h>
33 #include "alarmcalendar.h"
34 #include "alarmtimewidget.h"
35 #include "datetime.h"
36 #include "functions.h"
37 #include "kaevent.h"
38 #include "kalarmapp.h"
39 #include "deferdlg.moc"
42 DeferAlarmDlg::DeferAlarmDlg(const DateTime& initialDT, bool anyTimeOption, bool cancelButton, QWidget* parent)
43 : KDialog(parent)
45 setWindowModality(Qt::WindowModal);
46 setCaption(i18nc("@title:window", "Defer Alarm"));
47 setButtons(Ok | Cancel | User1);
48 setButtonGuiItem(User1, KGuiItem(i18nc("@action:button", "Cancel Deferral")));
49 if (!cancelButton)
50 showButton(User1, false);
51 connect(this, SIGNAL(okClicked()), SLOT(slotOk()));
52 connect(this, SIGNAL(user1Clicked()), SLOT(slotCancelDeferral()));
54 QWidget* page = new QWidget(this);
55 setMainWidget(page);
56 QVBoxLayout* layout = new QVBoxLayout(page);
57 layout->setMargin(0);
58 layout->setSpacing(spacingHint());
60 mTimeWidget = new AlarmTimeWidget((anyTimeOption ? AlarmTimeWidget::DEFER_ANY_TIME : AlarmTimeWidget::DEFER_TIME), page);
61 mTimeWidget->setDateTime(initialDT);
62 mTimeWidget->setMinDateTimeIsCurrent();
63 connect(mTimeWidget, SIGNAL(pastMax()), SLOT(slotPastLimit()));
64 layout->addWidget(mTimeWidget);
65 layout->addSpacing(spacingHint());
67 setButtonWhatsThis(Ok, i18nc("@info:whatsthis", "Defer the alarm until the specified time."));
68 setButtonWhatsThis(User1, i18nc("@info:whatsthis", "Cancel the deferred alarm. This does not affect future recurrences."));
72 /******************************************************************************
73 * Called when the OK button is clicked.
75 void DeferAlarmDlg::slotOk()
77 mAlarmDateTime = mTimeWidget->getDateTime(&mDeferMinutes);
78 if (!mAlarmDateTime.isValid())
79 return;
80 KAEvent::DeferLimitType limitType;
81 DateTime endTime;
82 if (!mLimitEventID.isEmpty())
84 // Get the event being deferred
85 const KAEvent* event = AlarmCalendar::getEvent(mLimitEventID);
86 if (event)
87 endTime = event->deferralLimit(&limitType);
89 else
91 endTime = mLimitDateTime;
92 limitType = mLimitDateTime.isValid() ? KAEvent::LIMIT_MAIN : KAEvent::LIMIT_NONE;
94 if (endTime.isValid() && mAlarmDateTime > endTime)
96 QString text;
97 switch (limitType)
99 case KAEvent::LIMIT_REPETITION:
100 text = i18nc("@info", "Cannot defer past the alarm's next sub-repetition (currently %1)",
101 endTime.formatLocale());
102 break;
103 case KAEvent::LIMIT_RECURRENCE:
104 text = i18nc("@info", "Cannot defer past the alarm's next recurrence (currently %1)",
105 endTime.formatLocale());
106 break;
107 case KAEvent::LIMIT_REMINDER:
108 text = i18nc("@info", "Cannot defer past the alarm's next reminder (currently %1)",
109 endTime.formatLocale());
110 break;
111 case KAEvent::LIMIT_MAIN:
112 text = i18nc("@info", "Cannot defer reminder past the main alarm time (%1)",
113 endTime.formatLocale());
114 break;
115 case KAEvent::LIMIT_NONE:
116 break; // can't happen with a valid endTime
118 KMessageBox::sorry(this, text);
120 else
121 accept();
124 /******************************************************************************
125 * Select the 'Time from now' radio button and preset its value.
127 void DeferAlarmDlg::setDeferMinutes(int minutes)
129 mTimeWidget->selectTimeFromNow(minutes);
132 /******************************************************************************
133 * Called the maximum date/time for the date/time edit widget has been passed.
135 void DeferAlarmDlg::slotPastLimit()
137 enableButtonOk(false);
140 /******************************************************************************
141 * Set the time limit for deferral based on the next occurrence of the alarm
142 * with the specified ID.
144 void DeferAlarmDlg::setLimit(const DateTime& limit)
146 mLimitEventID .clear();
147 mLimitDateTime = limit;
148 mTimeWidget->setMaxDateTime(mLimitDateTime);
151 /******************************************************************************
152 * Set the time limit for deferral based on the next occurrence of the alarm
153 * with the specified ID.
155 DateTime DeferAlarmDlg::setLimit(const QString& eventID)
157 mLimitEventID = eventID;
158 const KAEvent* event = AlarmCalendar::getEvent(mLimitEventID);
159 mLimitDateTime = event ? event->deferralLimit() : DateTime();
160 mTimeWidget->setMaxDateTime(mLimitDateTime);
161 return mLimitDateTime;
164 /******************************************************************************
165 * Called when the Cancel Deferral button is clicked.
167 void DeferAlarmDlg::slotCancelDeferral()
169 mAlarmDateTime = DateTime();
170 accept();