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 <kglobal.h> 00024 #include <klocale.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 = KGlobal::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 00081 || !valid && oldValid) 00082 { 00083 mSpinBox->setValid(valid); 00084 if (mAmPm) 00085 mAmPm->setCurrentItem(0); 00086 } 00087 } 00088 00089 /****************************************************************************** 00090 * Set the widget's value. 00091 */ 00092 void TimeEdit::setValue(int minutes) 00093 { 00094 if (mAmPm) 00095 { 00096 int i = (minutes >= 720) ? mPmIndex : mAmIndex; 00097 mAmPm->setCurrentItem(i >= 0 ? i : 0); 00098 } 00099 mSpinBox->setValue(minutes); 00100 } 00101 00102 bool TimeEdit::wrapping() const 00103 { 00104 return mSpinBox->wrapping(); 00105 } 00106 00107 void TimeEdit::setWrapping(bool on) 00108 { 00109 mSpinBox->setWrapping(on); 00110 } 00111 00112 int TimeEdit::minValue() const 00113 { 00114 return mSpinBox->minValue(); 00115 } 00116 00117 int TimeEdit::maxValue() const 00118 { 00119 return mSpinBox->maxValue(); 00120 } 00121 00122 void TimeEdit::setMinValue(int minutes) 00123 { 00124 if (mAmPm) 00125 setAmPmCombo((minutes < 720 ? 1 : 0), -1); // insert/remove "am" in combo box 00126 mSpinBox->setMinValue(minutes); 00127 } 00128 00129 void TimeEdit::setMaxValue(int minutes) 00130 { 00131 if (mAmPm) 00132 setAmPmCombo(-1, (minutes < 720 ? 0 : 1)); // insert/remove "pm" in combo box 00133 mSpinBox->setMaxValue(minutes); 00134 } 00135 00136 /****************************************************************************** 00137 * Called when the spin box value has changed. 00138 */ 00139 void TimeEdit::slotValueChanged(int value) 00140 { 00141 if (mAmPm) 00142 { 00143 bool pm = (mAmPm->currentItem() == mPmIndex); 00144 if (pm && value < 720) 00145 mAmPm->setCurrentItem(mAmIndex); 00146 else if (!pm && value >= 720) 00147 mAmPm->setCurrentItem(mPmIndex); 00148 } 00149 emit valueChanged(value); 00150 } 00151 00152 /****************************************************************************** 00153 * Called when a new selection has been made by the user in the AM/PM combo box. 00154 * Adjust the current time value by 12 hours. 00155 */ 00156 void TimeEdit::slotAmPmChanged(int item) 00157 { 00158 if (mAmPm) 00159 { 00160 int value = mSpinBox->value(); 00161 if (item == mPmIndex && value < 720) 00162 mSpinBox->setValue(value + 720); 00163 else if (item != mPmIndex && value >= 720) 00164 mSpinBox->setValue(value - 720); 00165 } 00166 } 00167 00168 /****************************************************************************** 00169 * Set up the AM/PM combo box to contain the specified items. 00170 */ 00171 void TimeEdit::setAmPmCombo(int am, int pm) 00172 { 00173 if (am > 0 && mAmIndex < 0) 00174 { 00175 // Insert "am" 00176 mAmIndex = 0; 00177 mAmPm->insertItem(KGlobal::locale()->translate("am"), mAmIndex); 00178 if (mPmIndex >= 0) 00179 mPmIndex = 1; 00180 mAmPm->setCurrentItem(mPmIndex >= 0 ? mPmIndex : mAmIndex); 00181 } 00182 else if (am == 0 && mAmIndex >= 0) 00183 { 00184 // Remove "am" 00185 mAmPm->removeItem(mAmIndex); 00186 mAmIndex = -1; 00187 if (mPmIndex >= 0) 00188 mPmIndex = 0; 00189 mAmPm->setCurrentItem(mPmIndex); 00190 } 00191 00192 if (pm > 0 && mPmIndex < 0) 00193 { 00194 // Insert "pm" 00195 mPmIndex = mAmIndex + 1; 00196 mAmPm->insertItem(KGlobal::locale()->translate("pm"), mPmIndex); 00197 if (mAmIndex < 0) 00198 mAmPm->setCurrentItem(mPmIndex); 00199 } 00200 else if (pm == 0 && mPmIndex >= 0) 00201 { 00202 // Remove "pm" 00203 mAmPm->removeItem(mPmIndex); 00204 mPmIndex = -1; 00205 mAmPm->setCurrentItem(mAmIndex); 00206 } 00207 }