kalarm/lib
timespinbox.cpp
Go to the documentation of this file.00001
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 <QLineEdit>
00024 #include <klocale.h>
00025
00026 #include "timespinbox.moc"
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 TimeSpinBox::TimeSpinBox(bool use24hour, QWidget* parent)
00044 : SpinBox2(0, 1439, 60, parent),
00045 mMinimumValue(0),
00046 m12Hour(!use24hour),
00047 mPm(false),
00048 mInvalid(false),
00049 mEnteredSetValue(false)
00050 {
00051 setWrapping(true);
00052 setReverseWithLayout(false);
00053 setShiftSteps(5, 360);
00054 setSelectOnStep(false);
00055 setAlignment(Qt::AlignHCenter);
00056 connect(this, SIGNAL(valueChanged(int)), SLOT(slotValueChanged(int)));
00057 }
00058
00059
00060
00061
00062 TimeSpinBox::TimeSpinBox(int minMinute, int maxMinute, QWidget* parent)
00063 : SpinBox2(minMinute, maxMinute, 60, parent),
00064 mMinimumValue(minMinute),
00065 m12Hour(false),
00066 mInvalid(false),
00067 mEnteredSetValue(false)
00068 {
00069 setReverseWithLayout(false);
00070 setShiftSteps(5, 300);
00071 setSelectOnStep(false);
00072 setAlignment(Qt::AlignRight);
00073 }
00074
00075 QString TimeSpinBox::shiftWhatsThis()
00076 {
00077 return i18nc("@info:whatsthis", "Press the Shift key while clicking the spin buttons to adjust the time by a larger step (6 hours / 5 minutes).");
00078 }
00079
00080 QTime TimeSpinBox::time() const
00081 {
00082 return QTime(value() / 60, value() % 60);
00083 }
00084
00085 QString TimeSpinBox::textFromValue(int v) const
00086 {
00087 if (m12Hour)
00088 {
00089 if (v < 60)
00090 v += 720;
00091 else if (v >= 780)
00092 v -= 720;
00093 }
00094 QString s;
00095 s.sprintf((wrapping() ? "%02d:%02d" : "%d:%02d"), v/60, v%60);
00096 return s;
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106 int TimeSpinBox::valueFromText(const QString&) const
00107 {
00108 QString text = cleanText();
00109 int colon = text.indexOf(QLatin1Char(':'));
00110 if (colon >= 0)
00111 {
00112
00113 QString hour = text.left(colon).trimmed();
00114 QString minute = text.mid(colon + 1).trimmed();
00115 if (!minute.isEmpty())
00116 {
00117 bool okmin;
00118 bool okhour = true;
00119 int m = minute.toUInt(&okmin);
00120 int h = 0;
00121 if (!hour.isEmpty())
00122 h = hour.toUInt(&okhour);
00123 if (okhour && okmin && m < 60)
00124 {
00125 if (m12Hour)
00126 {
00127 if (h == 0 || h > 12)
00128 h = 100;
00129 else if (h == 12)
00130 h = 0;
00131 if (mPm)
00132 h += 12;
00133 }
00134 int t = h * 60 + m;
00135 if (t >= mMinimumValue && t <= maximum())
00136 return t;
00137 }
00138 }
00139 }
00140 else if (text.length() == 4)
00141 {
00142
00143 bool okn;
00144 int mins = text.toUInt(&okn);
00145 if (okn)
00146 {
00147 int m = mins % 100;
00148 int h = mins / 100;
00149 if (m12Hour)
00150 {
00151 if (h == 0 || h > 12)
00152 h = 100;
00153 else if (h == 12)
00154 h = 0;
00155 if (mPm)
00156 h += 12;
00157 }
00158 int t = h * 60 + m;
00159 if (h < 24 && m < 60 && t >= mMinimumValue && t <= maximum())
00160 return t;
00161 }
00162
00163 }
00164 return 0;
00165 }
00166
00167
00168
00169
00170
00171
00172 void TimeSpinBox::setValid(bool valid)
00173 {
00174 if (valid && mInvalid)
00175 {
00176 mInvalid = false;
00177 if (value() < mMinimumValue)
00178 SpinBox2::setValue(mMinimumValue);
00179 setSpecialValueText(QString());
00180 SpinBox2::setMinimum(mMinimumValue);
00181 }
00182 else if (!valid && !mInvalid)
00183 {
00184 mInvalid = true;
00185 SpinBox2::setMinimum(mMinimumValue - 1);
00186 setSpecialValueText(QLatin1String("**:**"));
00187 SpinBox2::setValue(mMinimumValue - 1);
00188 }
00189 }
00190
00191
00192
00193
00194 void TimeSpinBox::setMinimum(int minutes)
00195 {
00196 mMinimumValue = minutes;
00197 SpinBox2::setMinimum(mMinimumValue - (mInvalid ? 1 : 0));
00198 }
00199
00200
00201
00202
00203 void TimeSpinBox::setValue(int minutes)
00204 {
00205 if (!mEnteredSetValue)
00206 {
00207 mEnteredSetValue = true;
00208 mPm = (minutes >= 720);
00209 if (minutes > maximum())
00210 setValid(false);
00211 else
00212 {
00213 if (mInvalid)
00214 {
00215 mInvalid = false;
00216 setSpecialValueText(QString());
00217 SpinBox2::setMinimum(mMinimumValue);
00218 }
00219 SpinBox2::setValue(minutes);
00220 mEnteredSetValue = false;
00221 }
00222 }
00223 }
00224
00225
00226
00227
00228
00229 void TimeSpinBox::stepBy(int increment)
00230 {
00231 if (mInvalid)
00232 setValid(true);
00233 else
00234 SpinBox2::stepBy(increment);
00235 }
00236
00237 bool TimeSpinBox::isValid() const
00238 {
00239 return value() >= mMinimumValue;
00240 }
00241
00242 void TimeSpinBox::slotValueChanged(int value)
00243 {
00244 mPm = (value >= 720);
00245 }
00246
00247 QSize TimeSpinBox::sizeHint() const
00248 {
00249 QSize sz = SpinBox2::sizeHint();
00250 QFontMetrics fm(font());
00251 return QSize(sz.width() + fm.width(":"), sz.height());
00252 }
00253
00254 QSize TimeSpinBox::minimumSizeHint() const
00255 {
00256 QSize sz = SpinBox2::minimumSizeHint();
00257 QFontMetrics fm(font());
00258 return QSize(sz.width() + fm.width(":"), sz.height());
00259 }
00260
00261
00262
00263
00264
00265
00266 QValidator::State TimeSpinBox::validate(QString& text, int&) const
00267 {
00268 QString cleanText = text.trimmed();
00269 if (cleanText.isEmpty())
00270 return QValidator::Intermediate;
00271 QValidator::State state = QValidator::Acceptable;
00272 int maxMinute = maximum();
00273 QString hour;
00274 bool ok;
00275 int hr = 0;
00276 int mn = 0;
00277 int colon = cleanText.indexOf(QLatin1Char(':'));
00278 if (colon >= 0)
00279 {
00280 QString minute = cleanText.mid(colon + 1);
00281 if (minute.isEmpty())
00282 state = QValidator::Intermediate;
00283 else if ((mn = minute.toUInt(&ok)) >= 60 || !ok)
00284 return QValidator::Invalid;
00285
00286 hour = cleanText.left(colon);
00287 }
00288 else if (maxMinute >= 1440)
00289 {
00290
00291 hour = cleanText;
00292 state = QValidator::Intermediate;
00293 }
00294 else
00295 {
00296 if (cleanText.length() > 4)
00297 return QValidator::Invalid;
00298 if (cleanText.length() < 4)
00299 state = QValidator::Intermediate;
00300 hour = cleanText.left(2);
00301 QString minute = cleanText.mid(2);
00302 if (!minute.isEmpty()
00303 && ((mn = minute.toUInt(&ok)) >= 60 || !ok))
00304 return QValidator::Invalid;
00305 }
00306
00307 if (!hour.isEmpty())
00308 {
00309 hr = hour.toUInt(&ok);
00310 if (m12Hour)
00311 {
00312 if (hr == 0 || hr > 12)
00313 hr = 100;
00314 else if (hr == 12)
00315 hr = 0;
00316 if (mPm)
00317 hr += 12;
00318 }
00319 if (!ok || hr > maxMinute/60)
00320 return QValidator::Invalid;
00321 }
00322 if (state == QValidator::Acceptable)
00323 {
00324 int t = hr * 60 + mn;
00325 if (t < minimum() || t > maxMinute)
00326 return QValidator::Invalid;
00327 }
00328 return state;
00329 }