• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

kalarm/lib

timespinbox.cpp

Go to the documentation of this file.
00001 /*
00002  *  timespinbox.cpp  -  time spinbox widget
00003  *  Program:  kalarm
00004  *  Copyright © 2001-2008 by David Jarvie <djarvie@kde.org>
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 <QLineEdit>
00024 #include <klocale.h>
00025 
00026 #include "timespinbox.moc"
00027 
00028 
00029 /*=============================================================================
00030 = Class TimeSpinBox
00031 = This is a spin box displaying a time in the format hh:mm, with a pair of
00032 = spin buttons for each of the hour and minute values.
00033 = It can operate in 3 modes:
00034 =  1) a time of day using the 24-hour clock.
00035 =  2) a time of day using the 12-hour clock. The value is held as 0:00 - 23:59,
00036 =     but is displayed as 12:00 - 11:59. This is for use in a TimeEdit widget.
00037 =  3) a length of time, not restricted to the length of a day.
00038 =============================================================================*/
00039 
00040 /******************************************************************************
00041  * Construct a wrapping 00:00 - 23:59, or 12:00 - 11:59 time spin box.
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);   // keep buttons the same way round even if right-to-left language
00053     setShiftSteps(5, 360);    // shift-left button increments 5 min / 6 hours
00054     setSelectOnStep(false);
00055     setAlignment(Qt::AlignHCenter);
00056     connect(this, SIGNAL(valueChanged(int)), SLOT(slotValueChanged(int)));
00057 }
00058 
00059 /******************************************************************************
00060  * Construct a non-wrapping time spin box.
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);   // keep buttons the same way round even if right-to-left language
00070     setShiftSteps(5, 300);    // shift-left button increments 5 min / 5 hours
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;      // convert 0:nn to 12:nn
00091         else if (v >= 780)
00092             v -= 720;      // convert 13 - 23 hours to 1 - 11
00093     }
00094     QString s;
00095     s.sprintf((wrapping() ? "%02d:%02d" : "%d:%02d"), v/60, v%60);
00096     return s;
00097 }
00098 
00099 /******************************************************************************
00100  * Convert the user-entered text to a value in minutes.
00101  * The allowed formats are:
00102  *    [hour]:[minute], where minute must be non-blank, or
00103  *    hhmm, 4 digits, where hour < 24.
00104  * Reply = 0 if error.
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         // [h]:m format for any time value
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;     // error
00129                     else if (h == 12)
00130                         h = 0;       // convert 12:nn to 0:nn
00131                     if (mPm)
00132                         h += 12;     // convert to PM
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         // hhmm format for time of day
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;    // error
00153                 else if (h == 12)
00154                     h = 0;      // convert 12:nn to 0:nn
00155                 if (mPm)
00156                     h += 12;    // convert to PM
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  * Set the spin box as valid or invalid.
00169  * If newly invalid, the value is displayed as asterisks.
00170  * If newly valid, the value is set to the minimum value.
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 * Set the spin box's minimum value.
00193 */
00194 void TimeSpinBox::setMinimum(int minutes)
00195 {
00196     mMinimumValue = minutes;
00197     SpinBox2::setMinimum(mMinimumValue - (mInvalid ? 1 : 0));
00198 }
00199 
00200 /******************************************************************************
00201  * Set the spin box's value.
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  * Step the spin box value.
00227  * If it was invalid, set it valid and set the value to the minimum.
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  * Validate the time spin box input.
00263  * The entered time must either be 4 digits, or it must contain a colon, but
00264  * hours may be blank.
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         // The hhmm form of entry is only allowed for time-of-day, i.e. <= 2359
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;    // error;
00314             else if (hr == 12)
00315                 hr = 0;      // convert 12:nn to 0:nn
00316             if (mPm)
00317                 hr += 12;    // convert to PM
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 }

kalarm/lib

Skip menu "kalarm/lib"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal