• 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
timespinbox.cpp
Go to the documentation of this file.
1 /*
2  * timespinbox.cpp - time spinbox widget
3  * Program: kalarm
4  * Copyright © 2001-2008 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 "timespinbox.h"
24 
25 #include <klocale.h>
26 #include <QLineEdit>
27 
28 
29 /*=============================================================================
30 = Class TimeSpinBox
31 = This is a spin box displaying a time in the format hh:mm, with a pair of
32 = spin buttons for each of the hour and minute values.
33 = It can operate in 3 modes:
34 = 1) a time of day using the 24-hour clock.
35 = 2) a time of day using the 12-hour clock. The value is held as 0:00 - 23:59,
36 = but is displayed as 12:00 - 11:59. This is for use in a TimeEdit widget.
37 = 3) a length of time, not restricted to the length of a day.
38 =============================================================================*/
39 
40 /******************************************************************************
41  * Construct a wrapping 00:00 - 23:59, or 12:00 - 11:59 time spin box.
42  */
43 TimeSpinBox::TimeSpinBox(bool use24hour, QWidget* parent)
44  : SpinBox2(0, 1439, 60, parent),
45  mMinimumValue(0),
46  m12Hour(!use24hour),
47  mPm(false),
48  mInvalid(false),
49  mEnteredSetValue(false)
50 {
51  setWrapping(true);
52  setReverseWithLayout(false); // keep buttons the same way round even if right-to-left language
53  setShiftSteps(5, 360); // shift-left button increments 5 min / 6 hours
54  setSelectOnStep(false);
55  setAlignment(Qt::AlignHCenter);
56  connect(this, SIGNAL(valueChanged(int)), SLOT(slotValueChanged(int)));
57 }
58 
59 /******************************************************************************
60  * Construct a non-wrapping time spin box.
61  */
62 TimeSpinBox::TimeSpinBox(int minMinute, int maxMinute, QWidget* parent)
63  : SpinBox2(minMinute, maxMinute, 60, parent),
64  mMinimumValue(minMinute),
65  m12Hour(false),
66  mInvalid(false),
67  mEnteredSetValue(false)
68 {
69  setReverseWithLayout(false); // keep buttons the same way round even if right-to-left language
70  setShiftSteps(5, 300); // shift-left button increments 5 min / 5 hours
71  setSelectOnStep(false);
72  setAlignment(Qt::AlignRight);
73 }
74 
75 QString TimeSpinBox::shiftWhatsThis()
76 {
77  return i18nc("@info:whatsthis", "Press the Shift key while clicking the spin buttons to adjust the time by a larger step (6 hours / 5 minutes).");
78 }
79 
80 QTime TimeSpinBox::time() const
81 {
82  return QTime(value() / 60, value() % 60);
83 }
84 
85 QString TimeSpinBox::textFromValue(int v) const
86 {
87  if (m12Hour)
88  {
89  if (v < 60)
90  v += 720; // convert 0:nn to 12:nn
91  else if (v >= 780)
92  v -= 720; // convert 13 - 23 hours to 1 - 11
93  }
94  QString s;
95  s.sprintf((wrapping() ? "%02d:%02d" : "%d:%02d"), v/60, v%60);
96  return s;
97 }
98 
99 /******************************************************************************
100  * Convert the user-entered text to a value in minutes.
101  * The allowed formats are:
102  * [hour]:[minute], where minute must be non-blank, or
103  * hhmm, 4 digits, where hour < 24.
104  * Reply = 0 if error.
105  */
106 int TimeSpinBox::valueFromText(const QString&) const
107 {
108  QString text = cleanText();
109  int colon = text.indexOf(QLatin1Char(':'));
110  if (colon >= 0)
111  {
112  // [h]:m format for any time value
113  QString hour = text.left(colon).trimmed();
114  QString minute = text.mid(colon + 1).trimmed();
115  if (!minute.isEmpty())
116  {
117  bool okmin;
118  bool okhour = true;
119  int m = minute.toUInt(&okmin);
120  int h = 0;
121  if (!hour.isEmpty())
122  h = hour.toUInt(&okhour);
123  if (okhour && okmin && m < 60)
124  {
125  if (m12Hour)
126  {
127  if (h == 0 || h > 12)
128  h = 100; // error
129  else if (h == 12)
130  h = 0; // convert 12:nn to 0:nn
131  if (mPm)
132  h += 12; // convert to PM
133  }
134  int t = h * 60 + m;
135  if (t >= mMinimumValue && t <= maximum())
136  return t;
137  }
138  }
139  }
140  else if (text.length() == 4)
141  {
142  // hhmm format for time of day
143  bool okn;
144  int mins = text.toUInt(&okn);
145  if (okn)
146  {
147  int m = mins % 100;
148  int h = mins / 100;
149  if (m12Hour)
150  {
151  if (h == 0 || h > 12)
152  h = 100; // error
153  else if (h == 12)
154  h = 0; // convert 12:nn to 0:nn
155  if (mPm)
156  h += 12; // convert to PM
157  }
158  int t = h * 60 + m;
159  if (h < 24 && m < 60 && t >= mMinimumValue && t <= maximum())
160  return t;
161  }
162 
163  }
164  return 0;
165 }
166 
167 /******************************************************************************
168  * Set the spin box as valid or invalid.
169  * If newly invalid, the value is displayed as asterisks.
170  * If newly valid, the value is set to the minimum value.
171  */
172 void TimeSpinBox::setValid(bool valid)
173 {
174  if (valid && mInvalid)
175  {
176  mInvalid = false;
177  if (value() < mMinimumValue)
178  SpinBox2::setValue(mMinimumValue);
179  setSpecialValueText(QString());
180  SpinBox2::setMinimum(mMinimumValue);
181  }
182  else if (!valid && !mInvalid)
183  {
184  mInvalid = true;
185  SpinBox2::setMinimum(mMinimumValue - 1);
186  setSpecialValueText(QLatin1String("**:**"));
187  SpinBox2::setValue(mMinimumValue - 1);
188  }
189 }
190 
191 /******************************************************************************
192 * Set the spin box's minimum value.
193 */
194 void TimeSpinBox::setMinimum(int minutes)
195 {
196  mMinimumValue = minutes;
197  SpinBox2::setMinimum(mMinimumValue - (mInvalid ? 1 : 0));
198 }
199 
200 /******************************************************************************
201  * Set the spin box's value.
202  */
203 void TimeSpinBox::setValue(int minutes)
204 {
205  if (!mEnteredSetValue)
206  {
207  mEnteredSetValue = true;
208  mPm = (minutes >= 720);
209  if (minutes > maximum())
210  setValid(false);
211  else
212  {
213  if (mInvalid)
214  {
215  mInvalid = false;
216  setSpecialValueText(QString());
217  SpinBox2::setMinimum(mMinimumValue);
218  }
219  SpinBox2::setValue(minutes);
220  mEnteredSetValue = false;
221  }
222  }
223 }
224 
225 /******************************************************************************
226  * Step the spin box value.
227  * If it was invalid, set it valid and set the value to the minimum.
228  */
229 void TimeSpinBox::stepBy(int increment)
230 {
231  if (mInvalid)
232  setValid(true);
233  else
234  SpinBox2::stepBy(increment);
235 }
236 
237 bool TimeSpinBox::isValid() const
238 {
239  return value() >= mMinimumValue;
240 }
241 
242 void TimeSpinBox::slotValueChanged(int value)
243 {
244  mPm = (value >= 720);
245 }
246 
247 QSize TimeSpinBox::sizeHint() const
248 {
249  QSize sz = SpinBox2::sizeHint();
250  QFontMetrics fm(font());
251  return QSize(sz.width() + fm.width(QLatin1Char(':')), sz.height());
252 }
253 
254 QSize TimeSpinBox::minimumSizeHint() const
255 {
256  QSize sz = SpinBox2::minimumSizeHint();
257  QFontMetrics fm(font());
258  return QSize(sz.width() + fm.width(QLatin1Char(':')), sz.height());
259 }
260 
261 /******************************************************************************
262  * Validate the time spin box input.
263  * The entered time must either be 4 digits, or it must contain a colon, but
264  * hours may be blank.
265  */
266 QValidator::State TimeSpinBox::validate(QString& text, int&) const
267 {
268  QString cleanText = text.trimmed();
269  if (cleanText.isEmpty())
270  return QValidator::Intermediate;
271  QValidator::State state = QValidator::Acceptable;
272  int maxMinute = maximum();
273  QString hour;
274  bool ok;
275  int hr = 0;
276  int mn = 0;
277  int colon = cleanText.indexOf(QLatin1Char(':'));
278  if (colon >= 0)
279  {
280  QString minute = cleanText.mid(colon + 1);
281  if (minute.isEmpty())
282  state = QValidator::Intermediate;
283  else if ((mn = minute.toUInt(&ok)) >= 60 || !ok)
284  return QValidator::Invalid;
285 
286  hour = cleanText.left(colon);
287  }
288  else if (maxMinute >= 1440)
289  {
290  // The hhmm form of entry is only allowed for time-of-day, i.e. <= 2359
291  hour = cleanText;
292  state = QValidator::Intermediate;
293  }
294  else
295  {
296  if (cleanText.length() > 4)
297  return QValidator::Invalid;
298  if (cleanText.length() < 4)
299  state = QValidator::Intermediate;
300  hour = cleanText.left(2);
301  QString minute = cleanText.mid(2);
302  if (!minute.isEmpty()
303  && ((mn = minute.toUInt(&ok)) >= 60 || !ok))
304  return QValidator::Invalid;
305  }
306 
307  if (!hour.isEmpty())
308  {
309  hr = hour.toUInt(&ok);
310  if (m12Hour)
311  {
312  if (hr == 0 || hr > 12)
313  hr = 100; // error;
314  else if (hr == 12)
315  hr = 0; // convert 12:nn to 0:nn
316  if (mPm)
317  hr += 12; // convert to PM
318  }
319  if (!ok || hr > maxMinute/60)
320  return QValidator::Invalid;
321  }
322  if (state == QValidator::Acceptable)
323  {
324  int t = hr * 60 + mn;
325  if (t < minimum() || t > maxMinute)
326  return QValidator::Invalid;
327  }
328  return state;
329 }
330 #include "moc_timespinbox.cpp"
331 // vim: et sw=4:
TimeSpinBox::TimeSpinBox
TimeSpinBox(bool use24hour, QWidget *parent=0)
Constructor for a wrapping time spin box which can be used to enter a time of day.
Definition: timespinbox.cpp:43
SpinBox2::setValue
void setValue(int val)
Sets the current value to val.
Definition: spinbox2.h:220
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
TimeSpinBox::time
QTime time() const
Returns the current value held in the spin box.
Definition: timespinbox.cpp:80
QWidget
TimeSpinBox::textFromValue
virtual QString textFromValue(int v) const
Definition: timespinbox.cpp:85
SpinBox2::setSpecialValueText
void setSpecialValueText(const QString &text)
Sets the special-value text which, if non-null, is displayed instead of a numeric value when the curr...
Definition: spinbox2.h:103
SpinBox2::text
QString text() const
Returns the spin box's text, including any prefix() and suffix().
Definition: spinbox2.h:88
QSize::width
int width() const
TimeSpinBox::setValid
void setValid(bool)
Sets the spin box as holding a valid or invalid value.
Definition: timespinbox.cpp:172
SpinBox2::setAlignment
void setAlignment(Qt::Alignment a)
Set the text alignment of the widget.
Definition: spinbox2.h:119
text
virtual QByteArray text(quint32 serialNumber) const =0
SpinBox2::cleanText
QString cleanText() const
Returns the spin box's text with no prefix(), suffix() or leading or trailing whitespace.
Definition: spinbox2.h:98
QFontMetrics
QTime
TimeSpinBox::isValid
bool isValid() const
Returns true if the spin box holds a valid value.
Definition: timespinbox.cpp:237
SpinBox2::setWrapping
void setWrapping(bool on)
Sets whether it is possible to step the value from the highest value to the lowest value and vice ver...
Definition: spinbox2.cpp:137
SpinBox2::setReverseWithLayout
void setReverseWithLayout(bool reverse)
Sets whether the spin button pairs should be reversed for a right-to-left language.
Definition: spinbox2.cpp:119
SpinBox2::setShiftSteps
void setShiftSteps(int line, int page)
Sets the shifted step increments for the two pairs of spin buttons, i.e.
Definition: spinbox2.cpp:178
TimeSpinBox::sizeHint
virtual QSize sizeHint() const
Definition: timespinbox.cpp:247
SpinBox2::valueChanged
void valueChanged(int value)
Signal which is emitted whenever the value of the spin box changes.
SpinBox2::stepBy
virtual void stepBy(int increment)
Increments the current value by adding the unshifted increment for the right-hand spin buttons...
Definition: spinbox2.h:216
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
SpinBox2::minimumSizeHint
virtual QSize minimumSizeHint() const
Definition: spinbox2.cpp:250
QString
TimeSpinBox::setMinimum
virtual void setMinimum(int minutes)
Sets the maximum value which can be held in the spin box.
Definition: timespinbox.cpp:194
TimeSpinBox::stepBy
virtual void stepBy(int increment)
Called whenever the user triggers a step, to adjust the value of the spin box.
Definition: timespinbox.cpp:229
SpinBox2::wrapping
bool wrapping() const
Returns whether it is possible to step the value from the highest value to the lowest value and vice ...
Definition: spinbox2.h:116
SpinBox2::value
int value() const
Returns the current value of the spin box.
Definition: spinbox2.h:142
SpinBox2
Spin box with a pair of spin buttons on either side.
Definition: spinbox2.h:57
QSize
QLatin1Char
QWidget::font
const QFont & font() const
SpinBox2::setMinimum
virtual void setMinimum(int val)
Sets the minimum value of the spin box.
Definition: spinbox2.cpp:207
QFontMetrics::width
int width(const QString &text, int len) const
TimeSpinBox::setValue
virtual void setValue(int minutes)
Sets the value of the spin box.
Definition: timespinbox.cpp:203
TimeSpinBox::validate
virtual QValidator::State validate(QString &, int &pos) const
Determine whether the current input is valid.
Definition: timespinbox.cpp:266
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
QString::mid
QString mid(int position, int n) const
TimeSpinBox::valueFromText
virtual int valueFromText(const QString &) const
Definition: timespinbox.cpp:106
QLatin1String
QString::sprintf
QString & sprintf(const char *cformat,...)
QSize::height
int height() const
SpinBox2::maximum
int maximum() const
Returns the maximum value of the spin box.
Definition: spinbox2.h:134
QString::length
int length() const
SpinBox2::sizeHint
virtual QSize sizeHint() const
Definition: spinbox2.cpp:242
QString::left
QString left(int n) const
TimeSpinBox::minimumSizeHint
virtual QSize minimumSizeHint() const
Definition: timespinbox.cpp:254
TimeSpinBox::shiftWhatsThis
static QString shiftWhatsThis()
Returns a text describing use of the shift key as an accelerator for the spin buttons, designed for incorporation into WhatsThis texts.
Definition: timespinbox.cpp:75
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
timespinbox.h
SpinBox2::minimum
int minimum() const
Returns the minimum value of the spin box.
Definition: spinbox2.h:132
QString::toUInt
uint toUInt(bool *ok, int base) const
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