• 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
timeedit.cpp
Go to the documentation of this file.
1 /*
2  * timeedit.cpp - time-of-day edit widget, with AM/PM shown depending on locale
3  * Program: kalarm
4  * Copyright © 2001-2006 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 "timespinbox.h"
25 #include "timeedit.h"
26 
27 #include <kglobal.h>
28 #include <klocale.h>
29 
30 #include <QTime>
31 
32 TimeEdit::TimeEdit(QWidget* parent)
33  : KHBox(parent),
34  mAmPm(0),
35  mAmIndex(-1),
36  mPmIndex(-1),
37  mReadOnly(false)
38 {
39  bool use12hour = KGlobal::locale()->use12Clock();
40  mSpinBox = new TimeSpinBox(!use12hour, this);
41  mSpinBox->setFixedSize(mSpinBox->sizeHint());
42  connect(mSpinBox, SIGNAL(valueChanged(int)), SLOT(slotValueChanged(int)));
43  if (use12hour)
44  {
45  mAmPm = new ComboBox(this);
46  setAmPmCombo(1, 1); // add "am" and "pm" options to the combo box
47  mAmPm->setFixedSize(mAmPm->sizeHint());
48  connect(mAmPm, SIGNAL(highlighted(int)), SLOT(slotAmPmChanged(int)));
49  }
50 }
51 
52 void TimeEdit::setReadOnly(bool ro)
53 {
54  if (ro != mReadOnly)
55  {
56  mReadOnly = ro;
57  mSpinBox->setReadOnly(ro);
58  if (mAmPm)
59  mAmPm->setReadOnly(ro);
60  }
61 }
62 
63 int TimeEdit::value() const
64 {
65  return mSpinBox->value();
66 }
67 
68 bool TimeEdit::isValid() const
69 {
70  return mSpinBox->isValid();
71 }
72 
73 /******************************************************************************
74  * Set the edit value as valid or invalid.
75  * If newly invalid, the value is displayed as asterisks.
76  * If newly valid, the value is set to the minimum value.
77  */
78 void TimeEdit::setValid(bool valid)
79 {
80  bool oldValid = mSpinBox->isValid();
81  if ((valid && !oldValid)
82  || (!valid && oldValid))
83  {
84  mSpinBox->setValid(valid);
85  if (mAmPm)
86  mAmPm->setCurrentIndex(0);
87  }
88 }
89 
90 /******************************************************************************
91  * Set the widget's value.
92  */
93 void TimeEdit::setValue(int minutes)
94 {
95  if (mAmPm)
96  {
97  int i = (minutes >= 720) ? mPmIndex : mAmIndex;
98  mAmPm->setCurrentIndex(i >= 0 ? i : 0);
99  }
100  mSpinBox->setValue(minutes);
101 }
102 
103 bool TimeEdit::wrapping() const
104 {
105  return mSpinBox->wrapping();
106 }
107 
108 void TimeEdit::setWrapping(bool on)
109 {
110  mSpinBox->setWrapping(on);
111 }
112 
113 int TimeEdit::minimum() const
114 {
115  return mSpinBox->minimum();
116 }
117 
118 int TimeEdit::maximum() const
119 {
120  return mSpinBox->maximum();
121 }
122 
123 void TimeEdit::setMinimum(int minutes)
124 {
125  if (mAmPm)
126  setAmPmCombo((minutes < 720 ? 1 : 0), -1); // insert/remove "am" in combo box
127  mSpinBox->setMinimum(minutes);
128 }
129 
130 void TimeEdit::setMaximum(int minutes)
131 {
132  if (mAmPm)
133  setAmPmCombo(-1, (minutes < 720 ? 0 : 1)); // insert/remove "pm" in combo box
134  mSpinBox->setMaximum(minutes);
135 }
136 
137 /******************************************************************************
138  * Called when the spin box value has changed.
139  */
140 void TimeEdit::slotValueChanged(int value)
141 {
142  if (mAmPm)
143  {
144  bool pm = (mAmPm->currentIndex() == mPmIndex);
145  if (pm && value < 720)
146  mAmPm->setCurrentIndex(mAmIndex);
147  else if (!pm && value >= 720)
148  mAmPm->setCurrentIndex(mPmIndex);
149  }
150  emit valueChanged(value);
151 }
152 
153 /******************************************************************************
154  * Called when a new selection has been made by the user in the AM/PM combo box.
155  * Adjust the current time value by 12 hours.
156  */
157 void TimeEdit::slotAmPmChanged(int item)
158 {
159  if (mAmPm)
160  {
161  int value = mSpinBox->value();
162  if (item == mPmIndex && value < 720)
163  mSpinBox->setValue(value + 720);
164  else if (item != mPmIndex && value >= 720)
165  mSpinBox->setValue(value - 720);
166  }
167 }
168 
169 /******************************************************************************
170  * Set up the AM/PM combo box to contain the specified items.
171  */
172 void TimeEdit::setAmPmCombo(int am, int pm)
173 {
174  if (am > 0 && mAmIndex < 0)
175  {
176  // Insert "am"
177  mAmIndex = 0;
178  mAmPm->insertItem(mAmIndex, i18nc("@item:inlistbox Morning, as in 2am", "am"));
179  if (mPmIndex >= 0)
180  mPmIndex = 1;
181  mAmPm->setCurrentIndex(mPmIndex >= 0 ? mPmIndex : mAmIndex);
182  }
183  else if (am == 0 && mAmIndex >= 0)
184  {
185  // Remove "am"
186  mAmPm->removeItem(mAmIndex);
187  mAmIndex = -1;
188  if (mPmIndex >= 0)
189  mPmIndex = 0;
190  mAmPm->setCurrentIndex(mPmIndex);
191  }
192 
193  if (pm > 0 && mPmIndex < 0)
194  {
195  // Insert "pm"
196  mPmIndex = mAmIndex + 1;
197  mAmPm->insertItem(mPmIndex, i18nc("@item:inlistbox Afternoon, as in 2pm", "pm"));
198  if (mAmIndex < 0)
199  mAmPm->setCurrentIndex(mPmIndex);
200  }
201  else if (pm == 0 && mPmIndex >= 0)
202  {
203  // Remove "pm"
204  mAmPm->removeItem(mPmIndex);
205  mPmIndex = -1;
206  mAmPm->setCurrentIndex(mAmIndex);
207  }
208 }
209 #include "moc_timeedit.cpp"
210 // vim: et sw=4:
QWidget
TimeSpinBox::setValid
void setValid(bool)
Sets the spin box as holding a valid or invalid value.
Definition: timespinbox.cpp:172
TimeSpinBox
Hours/minutes time entry widget.
Definition: timespinbox.h:45
TimeEdit::setReadOnly
virtual void setReadOnly(bool readOnly)
Sets whether the widget is read-only for the user.
Definition: timeedit.cpp:52
TimeEdit::isValid
bool isValid() const
Returns true if the widget contains a valid value.
Definition: timeedit.cpp:68
TimeEdit::minimum
int minimum() const
Returns the minimum value of the widget in minutes.
Definition: timeedit.cpp:113
TimeEdit::maximum
int maximum() const
Returns the maximum value of the widget in minutes.
Definition: timeedit.cpp:118
TimeSpinBox::isValid
bool isValid() const
Returns true if the spin box holds a valid value.
Definition: timespinbox.cpp:237
TimeEdit::setMinimum
void setMinimum(int minutes)
Sets the minimum value of the widget.
Definition: timeedit.cpp:123
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
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
TimeSpinBox::setMinimum
virtual void setMinimum(int minutes)
Sets the maximum value which can be held in the spin box.
Definition: timespinbox.cpp:194
ComboBox::setReadOnly
virtual void setReadOnly(bool readOnly)
Sets whether the combo box is read-only for the user.
Definition: combobox.cpp:32
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
TimeEdit::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: timeedit.cpp:108
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
TimeEdit::valueChanged
void valueChanged(int minutes)
This signal is emitted every time the value of the widget changes (for whatever reason).
TimeEdit::setValue
virtual void setValue(int minutes)
Sets the value of the widget.
Definition: timeedit.cpp:93
SpinBox2::maximum
int maximum() const
Returns the maximum value of the spin box.
Definition: spinbox2.h:134
combobox.h
TimeEdit::setValid
void setValid(bool valid)
Sets whether the edit value is valid.
Definition: timeedit.cpp:78
KHBox
ComboBox
A KComboBox with read-only option.
Definition: combobox.h:39
TimeEdit::TimeEdit
TimeEdit(QWidget *parent=0)
Constructor.
Definition: timeedit.cpp:32
TimeEdit::value
int value() const
Returns the entered time as a value in minutes.
Definition: timeedit.cpp:63
TimeSpinBox::setMaximum
virtual void setMaximum(int minutes)
Sets the maximum value which can be held in the spin box.
Definition: timespinbox.h:83
timeedit.h
timespinbox.h
TimeEdit::setMaximum
void setMaximum(int minutes)
Sets the maximum value of the widget.
Definition: timeedit.cpp:130
SpinBox2::minimum
int minimum() const
Returns the minimum value of the spin box.
Definition: spinbox2.h:132
TimeEdit::wrapping
bool wrapping() const
Returns true if it is possible to step the value from the highest value to the lowest value and vice ...
Definition: timeedit.cpp:103
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