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 <kglobal.h>
24 #include <klocale.h>
25 
26 #include "combobox.h"
27 #include "timespinbox.h"
28 #include "timeedit.moc"
29 
30 
31 TimeEdit::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 = KGlobal::locale()->use12Clock();
39  mSpinBox = new TimeSpinBox(!use12hour, this);
40  mSpinBox->setFixedSize(mSpinBox->sizeHint());
41  connect(mSpinBox, TQT_SIGNAL(valueChanged(int)), TQT_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, TQT_SIGNAL(highlighted(int)), TQT_SLOT(slotAmPmChanged(int)));
48  }
49 }
50 
51 void TimeEdit::setReadOnly(bool ro)
52 {
53  if (ro != mReadOnly)
54  {
55  mReadOnly = ro;
56  mSpinBox->setReadOnly(ro);
57  if (mAmPm)
58  mAmPm->setReadOnly(ro);
59  }
60 }
61 
62 int TimeEdit::value() const
63 {
64  return mSpinBox->value();
65 }
66 
67 bool TimeEdit::isValid() const
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  */
77 void TimeEdit::setValid(bool valid)
78 {
79  bool oldValid = mSpinBox->isValid();
80  if (valid && !oldValid
81  || !valid && oldValid)
82  {
83  mSpinBox->setValid(valid);
84  if (mAmPm)
85  mAmPm->setCurrentItem(0);
86  }
87 }
88 
89 /******************************************************************************
90  * Set the widget's value.
91  */
92 void TimeEdit::setValue(int minutes)
93 {
94  if (mAmPm)
95  {
96  int i = (minutes >= 720) ? mPmIndex : mAmIndex;
97  mAmPm->setCurrentItem(i >= 0 ? i : 0);
98  }
99  mSpinBox->setValue(minutes);
100 }
101 
102 bool TimeEdit::wrapping() const
103 {
104  return mSpinBox->wrapping();
105 }
106 
108 {
109  mSpinBox->setWrapping(on);
110 }
111 
113 {
114  return mSpinBox->minValue();
115 }
116 
118 {
119  return mSpinBox->maxValue();
120 }
121 
122 void TimeEdit::setMinValue(int minutes)
123 {
124  if (mAmPm)
125  setAmPmCombo((minutes < 720 ? 1 : 0), -1); // insert/remove "am" in combo box
126  mSpinBox->setMinValue(minutes);
127 }
128 
129 void TimeEdit::setMaxValue(int minutes)
130 {
131  if (mAmPm)
132  setAmPmCombo(-1, (minutes < 720 ? 0 : 1)); // insert/remove "pm" in combo box
133  mSpinBox->setMaxValue(minutes);
134 }
135 
136 /******************************************************************************
137  * Called when the spin box value has changed.
138  */
139 void TimeEdit::slotValueChanged(int value)
140 {
141  if (mAmPm)
142  {
143  bool pm = (mAmPm->currentItem() == mPmIndex);
144  if (pm && value < 720)
145  mAmPm->setCurrentItem(mAmIndex);
146  else if (!pm && value >= 720)
147  mAmPm->setCurrentItem(mPmIndex);
148  }
149  emit valueChanged(value);
150 }
151 
152 /******************************************************************************
153  * Called when a new selection has been made by the user in the AM/PM combo box.
154  * Adjust the current time value by 12 hours.
155  */
156 void TimeEdit::slotAmPmChanged(int item)
157 {
158  if (mAmPm)
159  {
160  int value = mSpinBox->value();
161  if (item == mPmIndex && value < 720)
162  mSpinBox->setValue(value + 720);
163  else if (item != mPmIndex && value >= 720)
164  mSpinBox->setValue(value - 720);
165  }
166 }
167 
168 /******************************************************************************
169  * Set up the AM/PM combo box to contain the specified items.
170  */
171 void TimeEdit::setAmPmCombo(int am, int pm)
172 {
173  if (am > 0 && mAmIndex < 0)
174  {
175  // Insert "am"
176  mAmIndex = 0;
177  mAmPm->insertItem(KGlobal::locale()->translate("am"), mAmIndex);
178  if (mPmIndex >= 0)
179  mPmIndex = 1;
180  mAmPm->setCurrentItem(mPmIndex >= 0 ? mPmIndex : mAmIndex);
181  }
182  else if (am == 0 && mAmIndex >= 0)
183  {
184  // Remove "am"
185  mAmPm->removeItem(mAmIndex);
186  mAmIndex = -1;
187  if (mPmIndex >= 0)
188  mPmIndex = 0;
189  mAmPm->setCurrentItem(mPmIndex);
190  }
191 
192  if (pm > 0 && mPmIndex < 0)
193  {
194  // Insert "pm"
195  mPmIndex = mAmIndex + 1;
196  mAmPm->insertItem(KGlobal::locale()->translate("pm"), mPmIndex);
197  if (mAmIndex < 0)
198  mAmPm->setCurrentItem(mPmIndex);
199  }
200  else if (pm == 0 && mPmIndex >= 0)
201  {
202  // Remove "pm"
203  mAmPm->removeItem(mPmIndex);
204  mPmIndex = -1;
205  mAmPm->setCurrentItem(mAmIndex);
206  }
207 }