timeedit.cpp
00001 /* 00002 * timeedit.cpp - time-of-day edit widget, with AM/PM shown depending on locale 00003 * Program: kalarm 00004 * Copyright (C) 2004 by David Jarvie <software@astrojar.org.uk> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kalarm.h" 00022 00023 #include <tdeglobal.h> 00024 #include <tdelocale.h> 00025 00026 #include "combobox.h" 00027 #include "timespinbox.h" 00028 #include "timeedit.moc" 00029 00030 00031 TimeEdit::TimeEdit(TQWidget* parent, const char* name) 00032 : TQHBox(parent, name), 00033 mAmPm(0), 00034 mAmIndex(-1), 00035 mPmIndex(-1), 00036 mReadOnly(false) 00037 { 00038 bool use12hour = TDEGlobal::locale()->use12Clock(); 00039 mSpinBox = new TimeSpinBox(!use12hour, this); 00040 mSpinBox->setFixedSize(mSpinBox->sizeHint()); 00041 connect(mSpinBox, TQT_SIGNAL(valueChanged(int)), TQT_SLOT(slotValueChanged(int))); 00042 if (use12hour) 00043 { 00044 mAmPm = new ComboBox(this); 00045 setAmPmCombo(1, 1); // add "am" and "pm" options to the combo box 00046 mAmPm->setFixedSize(mAmPm->sizeHint()); 00047 connect(mAmPm, TQT_SIGNAL(highlighted(int)), TQT_SLOT(slotAmPmChanged(int))); 00048 } 00049 } 00050 00051 void TimeEdit::setReadOnly(bool ro) 00052 { 00053 if (ro != mReadOnly) 00054 { 00055 mReadOnly = ro; 00056 mSpinBox->setReadOnly(ro); 00057 if (mAmPm) 00058 mAmPm->setReadOnly(ro); 00059 } 00060 } 00061 00062 int TimeEdit::value() const 00063 { 00064 return mSpinBox->value(); 00065 } 00066 00067 bool TimeEdit::isValid() const 00068 { 00069 return mSpinBox->isValid(); 00070 } 00071 00072 /****************************************************************************** 00073 * Set the edit value as valid or invalid. 00074 * If newly invalid, the value is displayed as asterisks. 00075 * If newly valid, the value is set to the minimum value. 00076 */ 00077 void TimeEdit::setValid(bool valid) 00078 { 00079 bool oldValid = mSpinBox->isValid(); 00080 if ((valid && !oldValid) || (!valid && oldValid)) 00081 { 00082 mSpinBox->setValid(valid); 00083 if (mAmPm) 00084 mAmPm->setCurrentItem(0); 00085 } 00086 } 00087 00088 /****************************************************************************** 00089 * Set the widget's value. 00090 */ 00091 void TimeEdit::setValue(int minutes) 00092 { 00093 if (mAmPm) 00094 { 00095 int i = (minutes >= 720) ? mPmIndex : mAmIndex; 00096 mAmPm->setCurrentItem(i >= 0 ? i : 0); 00097 } 00098 mSpinBox->setValue(minutes); 00099 } 00100 00101 bool TimeEdit::wrapping() const 00102 { 00103 return mSpinBox->wrapping(); 00104 } 00105 00106 void TimeEdit::setWrapping(bool on) 00107 { 00108 mSpinBox->setWrapping(on); 00109 } 00110 00111 int TimeEdit::minValue() const 00112 { 00113 return mSpinBox->minValue(); 00114 } 00115 00116 int TimeEdit::maxValue() const 00117 { 00118 return mSpinBox->maxValue(); 00119 } 00120 00121 void TimeEdit::setMinValue(int minutes) 00122 { 00123 if (mAmPm) 00124 setAmPmCombo((minutes < 720 ? 1 : 0), -1); // insert/remove "am" in combo box 00125 mSpinBox->setMinValue(minutes); 00126 } 00127 00128 void TimeEdit::setMaxValue(int minutes) 00129 { 00130 if (mAmPm) 00131 setAmPmCombo(-1, (minutes < 720 ? 0 : 1)); // insert/remove "pm" in combo box 00132 mSpinBox->setMaxValue(minutes); 00133 } 00134 00135 /****************************************************************************** 00136 * Called when the spin box value has changed. 00137 */ 00138 void TimeEdit::slotValueChanged(int value) 00139 { 00140 if (mAmPm) 00141 { 00142 bool pm = (mAmPm->currentItem() == mPmIndex); 00143 if (pm && value < 720) 00144 mAmPm->setCurrentItem(mAmIndex); 00145 else if (!pm && value >= 720) 00146 mAmPm->setCurrentItem(mPmIndex); 00147 } 00148 emit valueChanged(value); 00149 } 00150 00151 /****************************************************************************** 00152 * Called when a new selection has been made by the user in the AM/PM combo box. 00153 * Adjust the current time value by 12 hours. 00154 */ 00155 void TimeEdit::slotAmPmChanged(int item) 00156 { 00157 if (mAmPm) 00158 { 00159 int value = mSpinBox->value(); 00160 if (item == mPmIndex && value < 720) 00161 mSpinBox->setValue(value + 720); 00162 else if (item != mPmIndex && value >= 720) 00163 mSpinBox->setValue(value - 720); 00164 } 00165 } 00166 00167 /****************************************************************************** 00168 * Set up the AM/PM combo box to contain the specified items. 00169 */ 00170 void TimeEdit::setAmPmCombo(int am, int pm) 00171 { 00172 if (am > 0 && mAmIndex < 0) 00173 { 00174 // Insert "am" 00175 mAmIndex = 0; 00176 mAmPm->insertItem(TDEGlobal::locale()->translate("am"), mAmIndex); 00177 if (mPmIndex >= 0) 00178 mPmIndex = 1; 00179 mAmPm->setCurrentItem(mPmIndex >= 0 ? mPmIndex : mAmIndex); 00180 } 00181 else if (am == 0 && mAmIndex >= 0) 00182 { 00183 // Remove "am" 00184 mAmPm->removeItem(mAmIndex); 00185 mAmIndex = -1; 00186 if (mPmIndex >= 0) 00187 mPmIndex = 0; 00188 mAmPm->setCurrentItem(mPmIndex); 00189 } 00190 00191 if (pm > 0 && mPmIndex < 0) 00192 { 00193 // Insert "pm" 00194 mPmIndex = mAmIndex + 1; 00195 mAmPm->insertItem(TDEGlobal::locale()->translate("pm"), mPmIndex); 00196 if (mAmIndex < 0) 00197 mAmPm->setCurrentItem(mPmIndex); 00198 } 00199 else if (pm == 0 && mPmIndex >= 0) 00200 { 00201 // Remove "pm" 00202 mAmPm->removeItem(mPmIndex); 00203 mPmIndex = -1; 00204 mAmPm->setCurrentItem(mAmIndex); 00205 } 00206 }