• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kalarm

  • sources
  • kde-4.14
  • kdepim
  • kalarm
repetitionbutton.cpp
Go to the documentation of this file.
1 /*
2  * repetitionbutton.cpp - pushbutton and dialog to specify alarm repetition
3  * Program: kalarm
4  * Copyright © 2004-2011 by David Jarvie <djarvie@kde.org>
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 #include "kalarm.h"
22 #include "repetitionbutton.h"
23 
24 #include "buttongroup.h"
25 #include "radiobutton.h"
26 #include "spinbox.h"
27 #include "timeperiod.h"
28 #include "timeselector.h"
29 
30 #include <kdialog.h>
31 #include <klocale.h>
32 
33 #include <QGroupBox>
34 #include <QVBoxLayout>
35 #include <QHBoxLayout>
36 
37 #ifdef USE_AKONADI
38 using namespace KCalCore;
39 #else
40 using namespace KCal;
41 #endif
42 
43 
44 /*=============================================================================
45 = Class RepetitionButton
46 = Button to display the Simple Alarm Repetition dialog.
47 =============================================================================*/
48 
49 RepetitionButton::RepetitionButton(const QString& caption, bool waitForInitialisation, QWidget* parent)
50  : QPushButton(caption, parent),
51  mDialog(0),
52  mMaxDuration(-1),
53  mDateOnly(false),
54  mWaitForInit(waitForInitialisation),
55  mReadOnly(false)
56 {
57  setCheckable(true);
58  setChecked(false);
59  connect(this, SIGNAL(clicked()), SLOT(slotPressed()));
60 }
61 
62 void RepetitionButton::set(const Repetition& repetition)
63 {
64  mRepetition = repetition;
65  setChecked(mRepetition);
66 }
67 
68 /******************************************************************************
69 * Set the data for the dialog.
70 */
71 void RepetitionButton::set(const Repetition& repetition, bool dateOnly, int maxDuration)
72 {
73  mRepetition = repetition;
74  mMaxDuration = maxDuration;
75  mDateOnly = dateOnly;
76  setChecked(mRepetition);
77 }
78 
79 /******************************************************************************
80 * Create the alarm repetition dialog.
81 * If 'waitForInitialisation' is true, the dialog won't be displayed until set()
82 * is called to initialise its data.
83 */
84 void RepetitionButton::activate(bool waitForInitialisation)
85 {
86  if (!mDialog)
87  mDialog = new RepetitionDlg(i18nc("@title:window", "Alarm Sub-Repetition"), mReadOnly, this);
88  mDialog->set(mRepetition, mDateOnly, mMaxDuration);
89  if (waitForInitialisation)
90  emit needsInitialisation(); // request dialog initialisation
91  else
92  displayDialog(); // display the dialog now
93 }
94 
95 /******************************************************************************
96 * Set the data for the dialog and display it.
97 * To be called only after needsInitialisation() has been emitted.
98 */
99 void RepetitionButton::initialise(const Repetition& repetition, bool dateOnly, int maxDuration)
100 {
101  mRepetition = (maxDuration > 0 && repetition.intervalMinutes() > maxDuration)
102  ? Repetition() : repetition;
103  mMaxDuration = maxDuration;
104  mDateOnly = dateOnly;
105  if (mDialog)
106  {
107  mDialog->set(mRepetition, dateOnly, maxDuration);
108  displayDialog(); // display the dialog now
109  }
110  else
111  setChecked(mRepetition);
112 }
113 
114 /******************************************************************************
115 * Display the alarm sub-repetition dialog.
116 * Alarm repetition has the following restrictions:
117 * 1) Not allowed for a repeat-at-login alarm
118 * 2) For a date-only alarm, the repeat interval must be a whole number of days.
119 * 3) The overall repeat duration must be less than the recurrence interval.
120 */
121 void RepetitionButton::displayDialog()
122 {
123  bool change = false;
124  if (mReadOnly)
125  {
126  mDialog->setReadOnly(true);
127  mDialog->exec();
128  }
129  else if (mDialog->exec() == QDialog::Accepted)
130  {
131  mRepetition = mDialog->repetition();
132  change = true;
133  }
134  setChecked(mRepetition);
135  delete mDialog;
136  mDialog = 0;
137  if (change)
138  emit changed(); // delete dialog first, or initialise() will redisplay dialog
139 }
140 
141 
142 /*=============================================================================
143 = Class RepetitionDlg
144 = Simple alarm repetition dialog.
145 =============================================================================*/
146 
147 static const int MAX_COUNT = 9999; // maximum range for count spinbox
148 
149 
150 RepetitionDlg::RepetitionDlg(const QString& caption, bool readOnly, QWidget* parent)
151  : KDialog(parent),
152  mMaxDuration(-1),
153  mDateOnly(false),
154  mReadOnly(readOnly)
155 {
156  setCaption(caption);
157  setButtons(Ok|Cancel);
158  int spacing = spacingHint();
159  QWidget* page = new QWidget(this);
160  setMainWidget(page);
161  QVBoxLayout* topLayout = new QVBoxLayout(page);
162  topLayout->setMargin(0);
163  topLayout->setSpacing(spacing);
164 
165  mTimeSelector = new TimeSelector(i18nc("@option:check Repeat every 10 minutes", "Repeat every"),
166  i18nc("@info:whatsthis", "Instead of the alarm triggering just once at each recurrence, "
167  "checking this option makes the alarm trigger multiple times at each recurrence."),
168  i18nc("@info:whatsthis", "Enter the time between repetitions of the alarm"),
169  true, page);
170  mTimeSelector->setFixedSize(mTimeSelector->sizeHint());
171 #ifdef USE_AKONADI
172  connect(mTimeSelector, SIGNAL(valueChanged(KCalCore::Duration)), SLOT(intervalChanged(KCalCore::Duration)));
173 #else
174  connect(mTimeSelector, SIGNAL(valueChanged(KCal::Duration)), SLOT(intervalChanged(KCal::Duration)));
175 #endif
176  connect(mTimeSelector, SIGNAL(toggled(bool)), SLOT(repetitionToggled(bool)));
177  topLayout->addWidget(mTimeSelector, 0, Qt::AlignLeft);
178 
179  mButtonBox = new QGroupBox(page);
180  topLayout->addWidget(mButtonBox);
181  mButtonGroup = new ButtonGroup(mButtonBox);
182  connect(mButtonGroup, SIGNAL(buttonSet(QAbstractButton*)), SLOT(typeClicked()));
183 
184  QVBoxLayout* vlayout = new QVBoxLayout(mButtonBox);
185  vlayout->setMargin(marginHint());
186  vlayout->setSpacing(spacing);
187  QHBoxLayout* layout = new QHBoxLayout();
188  layout->setMargin(0);
189  vlayout->addLayout(layout);
190  mCountButton = new RadioButton(i18nc("@option:radio", "Number of repetitions:"), mButtonBox);
191  mCountButton->setFixedSize(mCountButton->sizeHint());
192  mCountButton->setWhatsThis(i18nc("@info:whatsthis", "Check to specify the number of times the alarm should repeat after each recurrence"));
193  mButtonGroup->addButton(mCountButton);
194  layout->addWidget(mCountButton);
195  mCount = new SpinBox(1, MAX_COUNT, mButtonBox);
196  mCount->setFixedSize(mCount->sizeHint());
197  mCount->setSingleShiftStep(10);
198  mCount->setSelectOnStep(false);
199  connect(mCount, SIGNAL(valueChanged(int)), SLOT(countChanged(int)));
200  mCount->setWhatsThis(i18nc("@info:whatsthis", "Enter the number of times to trigger the alarm after its initial occurrence"));
201  layout->addWidget(mCount);
202  mCountButton->setFocusWidget(mCount);
203  layout->addStretch();
204 
205  layout = new QHBoxLayout();
206  layout->setMargin(0);
207  vlayout->addLayout(layout);
208  mDurationButton = new RadioButton(i18nc("@option:radio", "Duration:"), mButtonBox);
209  mDurationButton->setFixedSize(mDurationButton->sizeHint());
210  mDurationButton->setWhatsThis(i18nc("@info:whatsthis", "Check to specify how long the alarm is to be repeated"));
211  mButtonGroup->addButton(mDurationButton);
212  layout->addWidget(mDurationButton);
213  mDuration = new TimePeriod(true, mButtonBox);
214  mDuration->setFixedSize(mDuration->sizeHint());
215 #ifdef USE_AKONADI
216  connect(mDuration, SIGNAL(valueChanged(KCalCore::Duration)), SLOT(durationChanged(KCalCore::Duration)));
217 #else
218  connect(mDuration, SIGNAL(valueChanged(KCal::Duration)), SLOT(durationChanged(KCal::Duration)));
219 #endif
220  mDuration->setWhatsThis(i18nc("@info:whatsthis", "Enter the length of time to repeat the alarm"));
221  layout->addWidget(mDuration);
222  mDurationButton->setFocusWidget(mDuration);
223  layout->addStretch();
224 
225  mCountButton->setChecked(true);
226  repetitionToggled(false);
227  setReadOnly(mReadOnly);
228 }
229 
230 /******************************************************************************
231 * Set the state of all controls to reflect the data in the specified alarm.
232 */
233 void RepetitionDlg::set(const Repetition& repetition, bool dateOnly, int maxDuration)
234 {
235  if (dateOnly != mDateOnly)
236  {
237  mDateOnly = dateOnly;
238  mTimeSelector->setDateOnly(mDateOnly);
239  mDuration->setDateOnly(mDateOnly);
240  }
241  mMaxDuration = maxDuration;
242  if (mMaxDuration)
243  {
244  int maxhm = (mMaxDuration > 0) ? mMaxDuration : 9999;
245  int maxdw = (mMaxDuration > 0) ? mMaxDuration / 1440 : 9999;
246  mTimeSelector->setMaximum(maxhm, maxdw);
247  mDuration->setMaximum(maxhm, maxdw);
248  }
249  // Set the units - needed later if the control is unchecked initially.
250  TimePeriod::Units units = mDateOnly ? TimePeriod::Days : TimePeriod::HoursMinutes;
251  mTimeSelector->setPeriod(repetition.interval(), mDateOnly, units);
252  if (!mMaxDuration || !repetition)
253  mTimeSelector->setChecked(false);
254  else
255  {
256  bool on = mTimeSelector->isChecked();
257  repetitionToggled(on); // enable/disable controls
258  if (on)
259  intervalChanged(repetition.interval()); // ensure mCount range is set
260  mCount->setValue(repetition.count());
261  mDuration->setPeriod(repetition.duration(), mDateOnly, units);
262  mCountButton->setChecked(true);
263  }
264  mTimeSelector->setEnabled(mMaxDuration);
265 }
266 
267 /******************************************************************************
268 * Set the read-only status.
269 */
270 void RepetitionDlg::setReadOnly(bool ro)
271 {
272  ro = ro || mReadOnly;
273  mTimeSelector->setReadOnly(ro);
274  mCountButton->setReadOnly(ro);
275  mCount->setReadOnly(ro);
276  mDurationButton->setReadOnly(ro);
277  mDuration->setReadOnly(ro);
278 }
279 
280 /******************************************************************************
281 * Get the entered interval and repeat count.
282 */
283 Repetition RepetitionDlg::repetition() const
284 {
285  int count = 0;
286  Duration interval = mTimeSelector->period();
287  if (interval)
288  {
289  if (mCountButton->isChecked())
290  count = mCount->value();
291  else if (mDurationButton->isChecked())
292  count = mDuration->period().asSeconds() / interval.asSeconds();
293  }
294  return Repetition(interval, count);
295 }
296 
297 /******************************************************************************
298 * Called when the time interval widget has changed value.
299 * Adjust the maximum repetition count accordingly.
300 */
301 void RepetitionDlg::intervalChanged(const Duration& interval)
302 {
303  if (mTimeSelector->isChecked() && interval.asSeconds() > 0)
304  {
305  mCount->setRange(1, (mMaxDuration >= 0 ? mMaxDuration / (interval.asSeconds()/60) : MAX_COUNT));
306  if (mCountButton->isChecked())
307  countChanged(mCount->value());
308  else
309  durationChanged(mDuration->period());
310  }
311 }
312 
313 /******************************************************************************
314 * Called when the count spinbox has changed value.
315 * Adjust the duration accordingly.
316 */
317 void RepetitionDlg::countChanged(int count)
318 {
319  Duration interval = mTimeSelector->period();
320  if (interval)
321  {
322  bool blocked = mDuration->signalsBlocked();
323  mDuration->blockSignals(true);
324  mDuration->setPeriod(interval * count, mDateOnly,
325  (mDateOnly ? TimePeriod::Days : TimePeriod::HoursMinutes));
326  mDuration->blockSignals(blocked);
327  }
328 }
329 
330 /******************************************************************************
331 * Called when the duration widget has changed value.
332 * Adjust the count accordingly.
333 */
334 void RepetitionDlg::durationChanged(const Duration& duration)
335 {
336  Duration interval = mTimeSelector->period();
337  if (interval)
338  {
339  bool blocked = mCount->signalsBlocked();
340  mCount->blockSignals(true);
341  mCount->setValue(duration.asSeconds() / interval.asSeconds());
342  mCount->blockSignals(blocked);
343  }
344 }
345 
346 /******************************************************************************
347 * Called when the time period widget is toggled on or off.
348 */
349 void RepetitionDlg::repetitionToggled(bool on)
350 {
351  if (mMaxDuration == 0)
352  on = false;
353  mButtonBox->setEnabled(on);
354  mCount->setEnabled(on && mCountButton->isChecked());
355  mDuration->setEnabled(on && mDurationButton->isChecked());
356 }
357 
358 /******************************************************************************
359 * Called when one of the count or duration radio buttons is toggled.
360 */
361 void RepetitionDlg::typeClicked()
362 {
363  if (mTimeSelector->isChecked())
364  {
365  mCount->setEnabled(mCountButton->isChecked());
366  mDuration->setEnabled(mDurationButton->isChecked());
367  }
368 }
369 
370 // vim: et sw=4:
QWidget
buttongroup.h
TimePeriod::Days
TimeSelector::setMaximum
void setMaximum(int hourmin, int days)
Definition: timeselector.cpp:115
RepetitionDlg::repetition
Repetition repetition() const
Definition: repetitionbutton.cpp:283
TimeSelector::isChecked
bool isChecked() const
Definition: timeselector.cpp:101
TimePeriod::Units
Units
TimePeriod::period
KCal::Duration period() const
TimePeriod::setPeriod
void setPeriod(const KCal::Duration &period, bool dateOnly, Units defaultUnits)
RadioButton
TimeSelector::setReadOnly
void setReadOnly(bool)
Definition: timeselector.cpp:89
TimeSelector::setChecked
void setChecked(bool on)
Definition: timeselector.cpp:106
TimePeriod::HoursMinutes
QHBoxLayout
KDialog
QFrame::sizeHint
virtual QSize sizeHint() const
RepetitionButton::activate
void activate()
Definition: repetitionbutton.h:53
QSpinBox::setRange
void setRange(int minimum, int maximum)
timeperiod.h
QWidget::setEnabled
void setEnabled(bool)
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QGroupBox
TimePeriod
RepetitionDlg
Definition: repetitionbutton.h:78
QAbstractButton::clicked
void clicked(bool checked)
RepetitionButton::set
void set(const Repetition &)
Definition: repetitionbutton.cpp:62
RepetitionButton::changed
void changed()
QVBoxLayout
RepetitionButton::needsInitialisation
void needsInitialisation()
ButtonGroup::addButton
void addButton(QAbstractButton *button)
QAbstractButton::setCheckable
void setCheckable(bool)
QString
RepetitionDlg::setReadOnly
void setReadOnly(bool)
Definition: repetitionbutton.cpp:270
QLayout::setMargin
void setMargin(int margin)
QObject::signalsBlocked
bool signalsBlocked() const
QObject::blockSignals
bool blockSignals(bool block)
SpinBox
QAbstractSpinBox::setReadOnly
void setReadOnly(bool r)
QWidget::setFixedSize
void setFixedSize(const QSize &s)
RadioButton::setFocusWidget
void setFocusWidget(QWidget *widget, bool enable=true)
repetitionbutton.h
QAbstractButton::setChecked
void setChecked(bool)
TimePeriod::setReadOnly
virtual void setReadOnly(bool readOnly)
QAbstractButton
RadioButton::setReadOnly
virtual void setReadOnly(bool readOnly)
QWidget::setWhatsThis
void setWhatsThis(const QString &)
RepetitionDlg::RepetitionDlg
RepetitionDlg(const QString &caption, bool readOnly, QWidget *parent=0)
Definition: repetitionbutton.cpp:150
QAbstractSpinBox::sizeHint
virtual QSize sizeHint() const
TimeSelector::setDateOnly
void setDateOnly(bool dateOnly=true)
Definition: timeselector.cpp:120
QBoxLayout::addStretch
void addStretch(int stretch)
QSpinBox::setValue
void setValue(int val)
TimePeriod::setDateOnly
void setDateOnly(bool dateOnly)
kalarm.h
MAX_COUNT
static const int MAX_COUNT
Definition: repetitionbutton.cpp:147
TimeSelector
Definition: timeselector.h:31
TimeSelector::setPeriod
void setPeriod(const KCal::Duration &, bool dateOnly, TimePeriod::Units defaultUnits)
Definition: timeselector.cpp:140
RepetitionButton::initialise
void initialise(const Repetition &, bool dateOnly, int maxDuration=-1)
Definition: repetitionbutton.cpp:99
timeselector.h
QPushButton
RepetitionButton::RepetitionButton
RepetitionButton(const QString &caption, bool waitForInitialisation, QWidget *parent)
Definition: repetitionbutton.cpp:49
RepetitionButton::repetition
Repetition repetition() const
Definition: repetitionbutton.h:54
RepetitionDlg::set
void set(const Repetition &, bool dateOnly=false, int maxDuration=-1)
Definition: repetitionbutton.cpp:233
TimePeriod::setMaximum
void setMaximum(int hourmin, int days)
QRadioButton::sizeHint
virtual QSize sizeHint() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
spinbox.h
QBoxLayout::setSpacing
void setSpacing(int spacing)
radiobutton.h
QBoxLayout::addLayout
void addLayout(QLayout *layout, int stretch)
TimeSelector::period
KCal::Duration period() const
Definition: timeselector.cpp:129
ButtonGroup
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm

Skip menu "kalarm"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal