Waste less space in nested layouts.
[kdepim.git] / kalarm / reminder.cpp
blobdb89773b07a204360361630e0f8c9d9b973637f7
1 /*
2 * reminder.cpp - reminder setting widget
3 * Program: kalarm
4 * Copyright © 2003-2005,2007-2009 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>
24 #include <QHBoxLayout>
26 #include <kglobal.h>
27 #include <klocale.h>
28 #include <kdialog.h>
29 #include <kdatetime.h>
30 #include <kdebug.h>
31 #include <kcal/duration.h>
33 #include "preferences.h"
34 #include "checkbox.h"
35 #include "timeselector.h"
36 #include "reminder.moc"
39 // Collect these widget labels together to ensure consistent wording and
40 // translations across different modules.
41 QString Reminder::i18n_chk_FirstRecurrenceOnly() { return i18nc("@option:check", "Reminder for first recurrence only"); }
44 Reminder::Reminder(const QString& reminderWhatsThis, const QString& valueWhatsThis,
45 bool allowHourMinute, bool showOnceOnly, QWidget* parent)
46 : QFrame(parent),
47 mReadOnly(false),
48 mOnceOnlyEnabled(showOnceOnly)
50 QVBoxLayout* topLayout = new QVBoxLayout(this);
51 topLayout->setMargin(0);
52 topLayout->setSpacing(KDialog::spacingHint());
54 mTime = new TimeSelector(i18nc("@option:check", "Reminder:"), i18nc("@label", "in advance"),
55 reminderWhatsThis, valueWhatsThis, allowHourMinute, this);
56 mTime->setFixedSize(mTime->sizeHint());
57 connect(mTime, SIGNAL(toggled(bool)), SLOT(slotReminderToggled(bool)));
58 connect(mTime, SIGNAL(valueChanged(const KCal::Duration&)), SIGNAL(changed()));
59 topLayout->addWidget(mTime, 0, Qt::AlignLeft);
61 if (showOnceOnly)
63 QHBoxLayout* layout = new QHBoxLayout();
64 layout->setMargin(0);
65 layout->addSpacing(3*KDialog::spacingHint());
66 topLayout->addLayout(layout);
67 mOnceOnly = new CheckBox(i18n_chk_FirstRecurrenceOnly(), this);
68 mOnceOnly->setFixedSize(mOnceOnly->sizeHint());
69 connect(mOnceOnly, SIGNAL(toggled(bool)), SIGNAL(changed()));
70 mOnceOnly->setWhatsThis(i18nc("@info:whatsthis", "Display the reminder only before the first time the alarm is scheduled"));
71 layout->addWidget(mOnceOnly);
72 layout->addStretch();
74 else
75 mOnceOnly = 0;
78 /******************************************************************************
79 * Set the read-only status.
81 void Reminder::setReadOnly(bool ro)
83 if ((int)ro != (int)mReadOnly)
85 mReadOnly = ro;
86 mTime->setReadOnly(mReadOnly);
87 if (mOnceOnly)
88 mOnceOnly->setReadOnly(mReadOnly);
92 bool Reminder::isReminder() const
94 return mTime->isChecked();
97 bool Reminder::isOnceOnly() const
99 return mOnceOnly && mOnceOnly->isEnabled() && mOnceOnly->isChecked();
102 void Reminder::setOnceOnly(bool onceOnly)
104 if (mOnceOnly)
105 mOnceOnly->setChecked(onceOnly);
108 /******************************************************************************
109 * Specify whether the once-only checkbox is allowed to be enabled.
111 void Reminder::enableOnceOnly(bool enable)
113 if (mOnceOnly)
115 mOnceOnlyEnabled = enable;
116 mOnceOnly->setEnabled(enable && mTime->isChecked());
120 void Reminder::setMaximum(int hourmin, int days)
122 mTime->setMaximum(hourmin, days);
125 /******************************************************************************
126 * Get the specified number of minutes in advance of the main alarm the
127 * reminder is to be.
129 int Reminder::minutes() const
131 return mTime->period().asSeconds() / 60;
134 /******************************************************************************
135 * Initialise the controls with a specified reminder time.
137 void Reminder::setMinutes(int minutes, bool dateOnly)
139 KCal::Duration period;
140 if (minutes % (24*60))
141 period = KCal::Duration(minutes * 60, KCal::Duration::Seconds);
142 else
143 period = KCal::Duration(minutes / (24*60), KCal::Duration::Days);
144 mTime->setPeriod(period, dateOnly, Preferences::defaultReminderUnits());
147 /******************************************************************************
148 * Set the advance reminder units to days if "Any time" is checked.
150 void Reminder::setDateOnly(bool dateOnly)
152 mTime->setDateOnly(dateOnly);
155 /******************************************************************************
156 * Set the input focus on the count field.
158 void Reminder::setFocusOnCount()
160 mTime->setFocusOnCount();
163 /******************************************************************************
164 * Called when the Reminder checkbox is toggled.
166 void Reminder::slotReminderToggled(bool on)
168 if (mOnceOnly)
169 mOnceOnly->setEnabled(on && mOnceOnlyEnabled);
172 /******************************************************************************
173 * Called when the start time relating to the reminder has changed.
174 * Sets the default reminder time units appropriately, if no reminder time is
175 * currently set.
177 void Reminder::setDefaultUnits(const KDateTime& dt)
179 if (mTime->isChecked())
180 return; // don't change units if reminder is already set
181 TimePeriod::Units units;
182 TimePeriod::Units currentUnits = mTime->units();
183 if (KDateTime::currentDateTime(dt.timeSpec()).daysTo(dt) < 7)
185 if (currentUnits == TimePeriod::Minutes || currentUnits == TimePeriod::HoursMinutes)
186 return;
187 units = (Preferences::defaultReminderUnits() == TimePeriod::Minutes)
188 ? TimePeriod::Minutes : TimePeriod::HoursMinutes;
190 else
192 if (currentUnits == TimePeriod::Days || currentUnits == TimePeriod::Weeks)
193 return;
194 units = TimePeriod::Days;
196 mTime->setUnits(units);