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

kalarm/lib

  • sources
  • kde-4.12
  • kdepim
  • kalarm
  • lib
timeperiod.cpp
Go to the documentation of this file.
1 /*
2  * timeperiod.h - time period data entry widget
3  * Program: kalarm
4  * Copyright © 2003-2005,2007,2008,2010 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 
23 #include "combobox.h"
24 #include "spinbox.h"
25 #include "timespinbox.h"
26 #include "timeperiod.moc"
27 
28 #include <klocale.h>
29 #include <kdialog.h>
30 
31 #include <QStackedWidget>
32 
33 #ifdef USE_AKONADI
34 using namespace KCalCore;
35 #else
36 using namespace KCal;
37 #endif
38 
39 // Collect these widget labels together to ensure consistent wording and
40 // translations across different modules.
41 QString TimePeriod::i18n_minutes() { return i18nc("@item:inlistbox Time units", "minutes"); }
42 QString TimePeriod::i18n_hours_mins() { return i18nc("@item:inlistbox Time units", "hours/minutes"); }
43 QString TimePeriod::i18n_days() { return i18nc("@item:inlistbox Time units", "days"); }
44 QString TimePeriod::i18n_weeks() { return i18nc("@item:inlistbox Time units", "weeks"); }
45 
46 static const int maxMinutes = 1000*60-1; // absolute maximum value for hours:minutes = 999H59M
47 
48 /*=============================================================================
49 = Class TimePeriod
50 = Contains a time unit combo box, plus a time spinbox, to select a time period.
51 =============================================================================*/
52 
53 TimePeriod::TimePeriod(bool allowHourMinute, QWidget* parent)
54  : KHBox(parent),
55  mMaxDays(9999),
56  mNoHourMinute(!allowHourMinute),
57  mReadOnly(false)
58 {
59  setSpacing(KDialog::spacingHint());
60 
61  mSpinStack = new QStackedWidget(this);
62  mSpinBox = new SpinBox(mSpinStack);
63  mSpinBox->setSingleStep(1);
64  mSpinBox->setSingleShiftStep(10);
65  mSpinBox->setRange(1, mMaxDays);
66  connect(mSpinBox, SIGNAL(valueChanged(int)), SLOT(slotDaysChanged(int)));
67  mSpinStack->addWidget(mSpinBox);
68 
69  mTimeSpinBox = new TimeSpinBox(0, 99999, mSpinStack);
70  mTimeSpinBox->setRange(1, maxMinutes); // max 999H59M
71  connect(mTimeSpinBox, SIGNAL(valueChanged(int)), SLOT(slotTimeChanged(int)));
72  mSpinStack->addWidget(mTimeSpinBox);
73 
74  mSpinStack->setFixedSize(mSpinBox->sizeHint().expandedTo(mTimeSpinBox->sizeHint()));
75  mHourMinuteRaised = mNoHourMinute;
76  showHourMin(!mNoHourMinute);
77 
78  mUnitsCombo = new ComboBox(this);
79  mUnitsCombo->setEditable(false);
80  if (mNoHourMinute)
81  mDateOnlyOffset = 2;
82  else
83  {
84  mDateOnlyOffset = 0;
85  mUnitsCombo->addItem(i18n_minutes());
86  mUnitsCombo->addItem(i18n_hours_mins());
87  }
88  mUnitsCombo->addItem(i18n_days());
89  mUnitsCombo->addItem(i18n_weeks());
90  mMaxUnitShown = Weeks;
91  mUnitsCombo->setFixedSize(mUnitsCombo->sizeHint());
92  connect(mUnitsCombo, SIGNAL(activated(int)), SLOT(slotUnitsSelected(int)));
93 
94  setFocusProxy(mUnitsCombo);
95  setTabOrder(mUnitsCombo, mSpinStack);
96 }
97 
98 void TimePeriod::setReadOnly(bool ro)
99 {
100  if (ro != mReadOnly)
101  {
102  mReadOnly = ro;
103  mSpinBox->setReadOnly(ro);
104  mTimeSpinBox->setReadOnly(ro);
105  mUnitsCombo->setReadOnly(ro);
106  }
107 }
108 
109 /******************************************************************************
110 * Set whether the editor text is to be selected whenever spin buttons are
111 * clicked. Default is to select them.
112 */
113 void TimePeriod::setSelectOnStep(bool sel)
114 {
115  mSpinBox->setSelectOnStep(sel);
116  mTimeSpinBox->setSelectOnStep(sel);
117 }
118 
119 /******************************************************************************
120 * Set the input focus on the count field.
121 */
122 void TimePeriod::setFocusOnCount()
123 {
124  mSpinStack->setFocus();
125 }
126 
127 /******************************************************************************
128 * Set the maximum values for the hours:minutes and days/weeks spinboxes.
129 * If 'hourmin' = 0, the hours:minutes maximum is left unchanged.
130 */
131 void TimePeriod::setMaximum(int hourmin, int days)
132 {
133  Duration oldmins = period();
134  if (hourmin > 0)
135  {
136  if (hourmin > maxMinutes)
137  hourmin = maxMinutes;
138  mTimeSpinBox->setRange(1, hourmin);
139  }
140  mMaxDays = (days >= 0) ? days : 0;
141  adjustDayWeekShown();
142  setUnitRange();
143  Duration mins = period();
144  if (mins != oldmins)
145  emit valueChanged(mins);
146 }
147 
148 /******************************************************************************
149  * Get the specified time period.
150  * Reply = 0 if error.
151  */
152 Duration TimePeriod::period() const
153 {
154  int factor = 1;
155  switch (mUnitsCombo->currentIndex() + mDateOnlyOffset)
156  {
157  case HoursMinutes:
158  return Duration(mTimeSpinBox->value() * 60, Duration::Seconds);
159  case Minutes:
160  return Duration(mSpinBox->value() * 60, Duration::Seconds);
161  case Weeks:
162  factor = 7;
163  // fall through to DAYS
164  case Days:
165  return Duration(mSpinBox->value() * factor, Duration::Days);
166  }
167  return 0;
168 }
169 
170 /******************************************************************************
171 * Initialise the controls with a specified time period.
172 * The time unit combo-box is initialised to 'defaultUnits', but if 'dateOnly'
173 * is true, it will never be initialised to minutes or hours/minutes.
174 */
175 void TimePeriod::setPeriod(const Duration& perod, bool dateOnly, TimePeriod::Units defaultUnits)
176 {
177  Duration oldinterval = period();
178  if (!dateOnly && mNoHourMinute)
179  dateOnly = true;
180  int item;
181  if (perod)
182  {
183  int count = perod.value();
184  if (perod.isDaily())
185  {
186  if (count % 7)
187  item = Days;
188  else
189  {
190  item = Weeks;
191  count /= 7;
192  }
193  }
194  else
195  {
196  count /= 60; // minutes
197  item = (defaultUnits == Minutes && count <= mSpinBox->maximum()) ? Minutes : HoursMinutes;
198  }
199  if (item < mDateOnlyOffset)
200  item = mDateOnlyOffset;
201  else if (item > mMaxUnitShown)
202  item = mMaxUnitShown;
203  mUnitsCombo->setCurrentIndex(item - mDateOnlyOffset);
204  if (item == HoursMinutes)
205  mTimeSpinBox->setValue(count);
206  else
207  mSpinBox->setValue(count);
208  item = setDateOnly(perod, dateOnly, false);
209  }
210  else
211  {
212  item = defaultUnits;
213  if (item < mDateOnlyOffset)
214  item = mDateOnlyOffset;
215  else if (item > mMaxUnitShown)
216  item = mMaxUnitShown;
217  mUnitsCombo->setCurrentIndex(item - mDateOnlyOffset);
218  if ((dateOnly && !mDateOnlyOffset) || (!dateOnly && mDateOnlyOffset))
219  item = setDateOnly(perod, dateOnly, false);
220  }
221  setUnitRange();
222  showHourMin(item == HoursMinutes && !mNoHourMinute);
223 
224  Duration newinterval = period();
225  if (newinterval != oldinterval)
226  emit valueChanged(newinterval);
227 }
228 
229 /******************************************************************************
230 * Enable/disable hours/minutes units (if hours/minutes were permitted in the
231 * constructor).
232 */
233 TimePeriod::Units TimePeriod::setDateOnly(const Duration& perod, bool dateOnly, bool signal)
234 {
235  Duration oldinterval = 0;
236  if (signal)
237  oldinterval = period();
238  int index = mUnitsCombo->currentIndex();
239  Units units = static_cast<Units>(index + mDateOnlyOffset);
240  if (!mNoHourMinute)
241  {
242  if (!dateOnly && mDateOnlyOffset)
243  {
244  // Change from date-only to allow hours/minutes
245  mUnitsCombo->insertItem(0, i18n_minutes());
246  mUnitsCombo->insertItem(1, i18n_hours_mins());
247  mDateOnlyOffset = 0;
248  adjustDayWeekShown();
249  mUnitsCombo->setCurrentIndex(index += 2);
250  }
251  else if (dateOnly && !mDateOnlyOffset)
252  {
253  // Change from allowing hours/minutes to date-only
254  mUnitsCombo->removeItem(0);
255  mUnitsCombo->removeItem(0);
256  mDateOnlyOffset = 2;
257  if (index > 2)
258  index -= 2;
259  else
260  index = 0;
261  adjustDayWeekShown();
262  mUnitsCombo->setCurrentIndex(index);
263  if (units == HoursMinutes || units == Minutes)
264  {
265  // Set units to days and round up the warning period
266  units = Days;
267  mUnitsCombo->setCurrentIndex(Days - mDateOnlyOffset);
268  mSpinBox->setValue(perod.asDays());
269  }
270  showHourMin(false);
271  }
272  }
273 
274  if (signal)
275  {
276  Duration newinterval = period();
277  if (newinterval != oldinterval)
278  emit valueChanged(newinterval);
279  }
280  return units;
281 }
282 
283 /******************************************************************************
284 * Adjust the days/weeks units shown to suit the maximum days limit.
285 */
286 void TimePeriod::adjustDayWeekShown()
287 {
288  Units newMaxUnitShown = (mMaxDays >= 7) ? Weeks : (mMaxDays || mDateOnlyOffset) ? Days : HoursMinutes;
289  if (newMaxUnitShown > mMaxUnitShown)
290  {
291  if (mMaxUnitShown < Days)
292  mUnitsCombo->addItem(i18n_days());
293  if (newMaxUnitShown == Weeks)
294  mUnitsCombo->addItem(i18n_weeks());
295  }
296  else if (newMaxUnitShown < mMaxUnitShown)
297  {
298  if (mMaxUnitShown == Weeks)
299  mUnitsCombo->removeItem(Weeks - mDateOnlyOffset);
300  if (newMaxUnitShown < Days)
301  mUnitsCombo->removeItem(Days - mDateOnlyOffset);
302  }
303  mMaxUnitShown = newMaxUnitShown;
304 }
305 
306 /******************************************************************************
307 * Set the maximum value which may be entered into the day/week count field,
308 * depending on the current unit selection.
309 */
310 void TimePeriod::setUnitRange()
311 {
312  int maxval;
313  switch (static_cast<Units>(mUnitsCombo->currentIndex() + mDateOnlyOffset))
314  {
315  case Weeks:
316  maxval = mMaxDays / 7;
317  if (maxval)
318  break;
319  mUnitsCombo->setCurrentIndex(Days - mDateOnlyOffset);
320  // fall through to Days
321  case Days:
322  maxval = mMaxDays ? mMaxDays : 1;
323  break;
324  case Minutes:
325  maxval = mTimeSpinBox->maximum();
326  break;
327  case HoursMinutes:
328  default:
329  return;
330  }
331  mSpinBox->setRange(1, maxval);
332 }
333 
334 /******************************************************************************
335 * Set the time units selection.
336 */
337 void TimePeriod::setUnits(Units units)
338 {
339  Units oldUnits = static_cast<Units>(mUnitsCombo->currentIndex() + mDateOnlyOffset);
340  if (units == oldUnits)
341  return;
342  if (oldUnits == HoursMinutes && units == Minutes)
343  {
344  if (mTimeSpinBox->value() > mSpinBox->maximum())
345  return;
346  mSpinBox->setValue(mTimeSpinBox->value());
347  }
348  else if (oldUnits == Minutes && units == HoursMinutes)
349  mTimeSpinBox->setValue(mSpinBox->value());
350  if (units >= mDateOnlyOffset && units <= mMaxUnitShown)
351  {
352  int item = units - mDateOnlyOffset;
353  mUnitsCombo->setCurrentIndex(item);
354  slotUnitsSelected(item);
355  }
356 }
357 
358 /******************************************************************************
359 * Return the current time units selection.
360 */
361 TimePeriod::Units TimePeriod::units() const
362 {
363  return static_cast<Units>(mUnitsCombo->currentIndex() + mDateOnlyOffset);
364 }
365 
366 /******************************************************************************
367 * Called when a new item is made current in the time units combo box.
368 */
369 void TimePeriod::slotUnitsSelected(int index)
370 {
371  setUnitRange();
372  showHourMin(index + mDateOnlyOffset == HoursMinutes);
373  emit valueChanged(period());
374 }
375 
376 /******************************************************************************
377 * Called when the value of the days/weeks spin box changes.
378 */
379 void TimePeriod::slotDaysChanged(int)
380 {
381  if (!mHourMinuteRaised)
382  emit valueChanged(period());
383 }
384 
385 /******************************************************************************
386 * Called when the value of the time spin box changes.
387 */
388 void TimePeriod::slotTimeChanged(int)
389 {
390  if (mHourMinuteRaised)
391  emit valueChanged(period());
392 }
393 
394 /******************************************************************************
395  * Set the currently displayed count widget.
396  */
397 void TimePeriod::showHourMin(bool hourMinute)
398 {
399  if (hourMinute != mHourMinuteRaised)
400  {
401  mHourMinuteRaised = hourMinute;
402  if (hourMinute)
403  {
404  mSpinStack->setCurrentWidget(mTimeSpinBox);
405  mSpinStack->setFocusProxy(mTimeSpinBox);
406  }
407  else
408  {
409  mSpinStack->setCurrentWidget(mSpinBox);
410  mSpinStack->setFocusProxy(mSpinBox);
411  }
412  }
413 }
414 
415 /******************************************************************************
416  * Set separate WhatsThis texts for the count spinboxes and the units combobox.
417  * If the hours:minutes text is omitted, both spinboxes are set to the same
418  * WhatsThis text.
419  */
420 void TimePeriod::setWhatsThises(const QString& units, const QString& dayWeek, const QString& hourMin)
421 {
422  mUnitsCombo->setWhatsThis(units);
423  mSpinBox->setWhatsThis(dayWeek);
424  mTimeSpinBox->setWhatsThis(hourMin.isNull() ? dayWeek : hourMin);
425 }
426 
427 // vim: et sw=4:
TimePeriod::setSelectOnStep
void setSelectOnStep(bool select)
Sets whether the editor text is to be selected whenever spin buttons are clicked. ...
Definition: timeperiod.cpp:113
TimePeriod::Days
Definition: timeperiod.h:65
TimePeriod::Units
Units
Units for the time period.
Definition: timeperiod.h:65
TimePeriod::period
KCal::Duration period() const
Gets the entered time period.
Definition: timeperiod.cpp:152
TimePeriod::setPeriod
void setPeriod(const KCal::Duration &period, bool dateOnly, Units defaultUnits)
Initialises the time period value.
Definition: timeperiod.cpp:175
TimePeriod::valueChanged
void valueChanged(const KCal::Duration &period)
This signal is emitted whenever the value held in the widget changes.
TimeSpinBox
Hours/minutes time entry widget.
Definition: timespinbox.h:45
TimePeriod::setFocusOnCount
void setFocusOnCount()
Sets the input focus to the count field.
Definition: timeperiod.cpp:122
SpinBox::setSingleStep
void setSingleStep(int step)
Sets the unshifted step increment, i.e.
Definition: spinbox.cpp:106
QWidget
TimePeriod::HoursMinutes
Definition: timeperiod.h:65
SpinBox::setSelectOnStep
void setSelectOnStep(bool sel)
Sets whether the spin box value text should be selected when its value is stepped.
Definition: spinbox.h:67
TimePeriod::TimePeriod
TimePeriod(bool allowMinute, QWidget *parent)
Constructor.
Definition: timeperiod.cpp:53
SpinBox::maximum
int maximum() const
Returns the maximum value of the spin box.
Definition: spinbox.h:73
TimePeriod::setUnits
void setUnits(Units units)
Sets the time units.
Definition: timeperiod.cpp:337
TimeSpinBox::sizeHint
virtual QSize sizeHint() const
Definition: timespinbox.cpp:247
SpinBox2::setReadOnly
virtual void setReadOnly(bool readOnly)
Sets whether the spin box can be changed by the user.
Definition: spinbox2.cpp:109
SpinBox::setRange
void setRange(int minValue, int maxValue)
Sets the minimum and maximum values of the spin box.
Definition: spinbox.h:79
TimePeriod::setWhatsThises
void setWhatsThises(const QString &units, const QString &dayWeek, const QString &hourMin=QString())
Sets separate WhatsThis texts for the count spin boxes and the units combo box.
Definition: timeperiod.cpp:420
ComboBox::setReadOnly
virtual void setReadOnly(bool readOnly)
Sets whether the combo box is read-only for the user.
Definition: combobox.cpp:33
SpinBox2::value
int value() const
Returns the current value of the spin box.
Definition: spinbox2.h:142
SpinBox
Spin box with accelerated shift key stepping and read-only option.
Definition: spinbox.h:44
TimeSpinBox::setValue
virtual void setValue(int minutes)
Sets the value of the spin box.
Definition: timespinbox.cpp:203
SpinBox2::setRange
void setRange(int minValue, int maxValue)
Sets the minimum and maximum values of the spin box.
Definition: spinbox2.h:140
TimePeriod::setReadOnly
virtual void setReadOnly(bool readOnly)
Sets whether the widget is read-only for the user.
Definition: timeperiod.cpp:98
SpinBox2::setSelectOnStep
void setSelectOnStep(bool sel)
Sets whether the spin box value text should be selected when its value is stepped.
Definition: spinbox2.h:79
SpinBox::setSingleShiftStep
void setSingleShiftStep(int step)
Sets the shifted step increment, i.e.
Definition: spinbox.cpp:113
TimePeriod::setDateOnly
void setDateOnly(bool dateOnly)
Enables or disables minutes and hours/minutes units in the combo box.
Definition: timeperiod.h:112
SpinBox2::maximum
int maximum() const
Returns the maximum value of the spin box.
Definition: spinbox2.h:134
combobox.h
TimePeriod::Weeks
Definition: timeperiod.h:65
KHBox
ComboBox
A KComboBox with read-only option.
Definition: combobox.h:39
TimePeriod::setMaximum
void setMaximum(int hourmin, int days)
Sets the maximum values for the minutes and hours/minutes, and days/weeks spin boxes.
Definition: timeperiod.cpp:131
TimePeriod::Minutes
Definition: timeperiod.h:65
spinbox.h
TimePeriod::units
Units units() const
Gets the currently selected time units.
Definition: timeperiod.cpp:361
timespinbox.h
SpinBox::setReadOnly
virtual void setReadOnly(bool readOnly)
Sets whether the spin box can be changed by the user.
Definition: spinbox.cpp:76
maxMinutes
static const int maxMinutes
Definition: timeperiod.cpp:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:59:21 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm/lib

Skip menu "kalarm/lib"
  • 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

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