kalarm/lib
timeedit.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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(QWidget* parent, const char* name)
00032 : QHBox(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, SIGNAL(valueChanged(int)), SLOT(slotValueChanged(int)));
00042 if (use12hour)
00043 {
00044 mAmPm = new ComboBox(this);
00045 setAmPmCombo(1, 1);
00046 mAmPm->setFixedSize(mAmPm->sizeHint());
00047 connect(mAmPm, SIGNAL(highlighted(int)), 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
00074
00075
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
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);
00126 mSpinBox->setMinValue(minutes);
00127 }
00128
00129 void TimeEdit::setMaxValue(int minutes)
00130 {
00131 if (mAmPm)
00132 setAmPmCombo(-1, (minutes < 720 ? 0 : 1));
00133 mSpinBox->setMaxValue(minutes);
00134 }
00135
00136
00137
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
00154
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
00170
00171 void TimeEdit::setAmPmCombo(int am, int pm)
00172 {
00173 if (am > 0 && mAmIndex < 0)
00174 {
00175
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
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
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
00203 mAmPm->removeItem(mPmIndex);
00204 mPmIndex = -1;
00205 mAmPm->setCurrentItem(mAmIndex);
00206 }
00207 }
|