kalarm/lib

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