2 * reminder.cpp - reminder setting widget
4 * Copyright © 2003-2005,2007-2011 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.
23 #include "preferences.h"
26 #include "timeselector.h"
29 #include <KCalCore/Duration>
30 using namespace KCalCore
;
32 #include <KLocalizedString>
33 #include <kdatetime.h>
35 #include <QVBoxLayout>
36 #include <QHBoxLayout>
37 #include "kalarm_debug.h"
40 // Collect these widget labels together to ensure consistent wording and
41 // translations across different modules.
42 QString
Reminder::i18n_chk_FirstRecurrenceOnly() { return i18nc("@option:check", "Reminder for first recurrence only"); }
44 #define i18n_in_advance i18nc("@item:inlistbox", "in advance")
47 Reminder::Reminder(const QString
& reminderWhatsThis
, const QString
& valueWhatsThis
,
48 const QString
& beforeAfterWhatsThis
, bool allowHourMinute
,
49 bool showOnceOnly
, QWidget
* parent
)
52 mOnceOnlyEnabled(showOnceOnly
)
54 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
55 topLayout
->setMargin(0);
57 mTime
= new TimeSelector(i18nc("@option:check", "Reminder:"), reminderWhatsThis
,
58 valueWhatsThis
, allowHourMinute
, this);
59 mTimeSignCombo
= mTime
->createSignCombo();
60 mTimeSignCombo
->addItem(i18n_in_advance
);
61 mTimeSignCombo
->addItem(i18nc("@item:inlistbox", "afterwards"));
62 mTimeSignCombo
->setWhatsThis(beforeAfterWhatsThis
);
63 mTimeSignCombo
->setCurrentIndex(0); // default to "in advance"
64 mTime
->setFixedSize(mTime
->sizeHint());
65 connect(mTime
, &TimeSelector::toggled
, this, &Reminder::slotReminderToggled
);
66 connect(mTime
, &TimeSelector::valueChanged
, this, &Reminder::changed
);
67 connect(mTimeSignCombo
, static_cast<void (ComboBox::*)(int)>(&ComboBox::currentIndexChanged
), this, &Reminder::changed
);
68 topLayout
->addWidget(mTime
, 0, Qt::AlignLeft
);
72 QHBoxLayout
* layout
= new QHBoxLayout();
74 layout
->addSpacing(3 * style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing
));
75 topLayout
->addLayout(layout
);
76 mOnceOnly
= new CheckBox(i18n_chk_FirstRecurrenceOnly(), this);
77 mOnceOnly
->setFixedSize(mOnceOnly
->sizeHint());
78 connect(mOnceOnly
, &CheckBox::toggled
, this, &Reminder::changed
);
79 mOnceOnly
->setWhatsThis(i18nc("@info:whatsthis", "Display the reminder only for the first time the alarm is scheduled"));
80 layout
->addWidget(mOnceOnly
);
84 mOnceOnly
= Q_NULLPTR
;
87 /******************************************************************************
88 * Allow or disallow advance reminder selection.
90 void Reminder::setAfterOnly(bool afterOnly
)
92 if (afterOnly
&& mTimeSignCombo
->count() == 2)
93 mTimeSignCombo
->removeItem(0);
94 else if (!afterOnly
&& mTimeSignCombo
->count() == 1)
95 mTimeSignCombo
->insertItem(0, i18n_in_advance
);
98 /******************************************************************************
99 * Set the read-only status.
101 void Reminder::setReadOnly(bool ro
)
103 if ((int)ro
!= (int)mReadOnly
)
106 mTime
->setReadOnly(mReadOnly
);
108 mOnceOnly
->setReadOnly(mReadOnly
);
112 bool Reminder::isReminder() const
114 return mTime
->isChecked();
117 bool Reminder::isOnceOnly() const
119 return mOnceOnly
&& mOnceOnly
->isEnabled() && mOnceOnly
->isChecked();
122 void Reminder::setOnceOnly(bool onceOnly
)
125 mOnceOnly
->setChecked(onceOnly
);
128 /******************************************************************************
129 * Specify whether the once-only checkbox is allowed to be enabled.
131 void Reminder::enableOnceOnly(bool enable
)
135 mOnceOnlyEnabled
= enable
;
136 mOnceOnly
->setEnabled(enable
&& mTime
->isChecked());
140 void Reminder::setMaximum(int hourmin
, int days
)
142 mTime
->setMaximum(hourmin
, days
);
145 /******************************************************************************
146 * Get the specified number of minutes in advance of the main alarm the
148 * Reply > 0 if advance reminder
149 * < 0 if reminder after main alarm
150 * = 0 if no reminder.
152 int Reminder::minutes() const
154 int index
= mTimeSignCombo
->currentIndex();
155 if (mTimeSignCombo
->count() == 1)
156 ++index
; // "in advance" is not available
157 int sign
= index
? -1 : 1;
158 return mTime
->period().asSeconds() * sign
/ 60;
161 /******************************************************************************
162 * Initialise the controls with a specified reminder time.
164 void Reminder::setMinutes(int minutes
, bool dateOnly
)
166 bool neg
= (minutes
< 0);
167 minutes
= abs(minutes
);
169 if (minutes
% (24*60))
170 period
= Duration(minutes
* 60, Duration::Seconds
);
172 period
= Duration(minutes
/ (24*60), Duration::Days
);
173 mTime
->setPeriod(period
, dateOnly
, Preferences::defaultReminderUnits());
174 mTimeSignCombo
->setCurrentIndex(neg
? 1 : 0);
177 /******************************************************************************
178 * Set the reminder units to days if "Any time" is checked.
180 void Reminder::setDateOnly(bool dateOnly
)
182 mTime
->setDateOnly(dateOnly
);
185 /******************************************************************************
186 * Set the input focus on the count field.
188 void Reminder::setFocusOnCount()
190 mTime
->setFocusOnCount();
193 /******************************************************************************
194 * Called when the Reminder checkbox is toggled.
196 void Reminder::slotReminderToggled(bool on
)
199 mOnceOnly
->setEnabled(on
&& mOnceOnlyEnabled
);
202 /******************************************************************************
203 * Called when the start time relating to the reminder has changed.
204 * Sets the default reminder time units appropriately, if no reminder time is
207 void Reminder::setDefaultUnits(const KDateTime
& dt
)
209 if (mTime
->isChecked())
210 return; // don't change units if reminder is already set
211 TimePeriod::Units units
;
212 TimePeriod::Units currentUnits
= mTime
->units();
213 if (KDateTime::currentDateTime(dt
.timeSpec()).daysTo(dt
) < 7)
215 if (currentUnits
== TimePeriod::Minutes
|| currentUnits
== TimePeriod::HoursMinutes
)
217 units
= (Preferences::defaultReminderUnits() == TimePeriod::Minutes
)
218 ? TimePeriod::Minutes
: TimePeriod::HoursMinutes
;
222 if (currentUnits
== TimePeriod::Days
|| currentUnits
== TimePeriod::Weeks
)
224 units
= TimePeriod::Days
;
226 mTime
->setUnits(units
);