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