00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <qkeycode.h>
00028 #include <qcombobox.h>
00029 #include <qdatetime.h>
00030 #include <qlineedit.h>
00031
00032 #include <kmessagebox.h>
00033 #include <kglobal.h>
00034 #include <kdebug.h>
00035 #include <klocale.h>
00036
00037 #include "ktimeedit.h"
00038 #include <qvalidator.h>
00039 #include "ktimeedit.moc"
00040
00041
00042
00043 class KOTimeValidator : public QValidator
00044 {
00045 public:
00046 KOTimeValidator(QWidget* parent, const char* name=0) : QValidator(parent, name) {}
00047
00048 virtual State validate(QString& str, int& ) const
00049 {
00050 int length = str.length();
00051
00052 if ( length <= 0 )
00053 return Intermediate;
00054
00055 bool ok = false;
00056 KGlobal::locale()->readTime(str, KLocale::WithoutSeconds, &ok);
00057 if ( ok )
00058 return Acceptable;
00059
00060
00061 int tm = str.toInt( &ok );
00062 if ( ok && ( 0 <= tm ) ) {
00063 if ( ( tm < 2400 ) && ( tm%100 < 60 ) )
00064 return Acceptable;
00065 else
00066 return Intermediate;
00067 }
00068
00069
00070
00071
00072 QChar sep = ':';
00073
00074 if ( str[0] == sep )
00075 {
00076 if ( length == 1 )
00077 return Intermediate;
00078 QString minutes = str.mid(1);
00079 int m = minutes.toInt(&ok);
00080 if ( ok && m >= 0 && m < 60 )
00081 return Intermediate;
00082 } else if ( str[str.length()-1] == sep )
00083 {
00084 QString hours = str.left(length-1);
00085 int h = hours.toInt(&ok);
00086 if ( ok && h >= 0 && h < 24 )
00087 return Intermediate;
00088 }
00089
00090 return Intermediate;
00091 }
00092 virtual void fixup ( QString & input ) const {
00093 bool ok = false;
00094 KGlobal::locale()->readTime( input, KLocale::WithoutSeconds, &ok );
00095 if ( !ok ) {
00096
00097 int tm = input.toInt( &ok );
00098 if ( ( 0 <= tm ) && ( tm < 2400 ) && ( tm%100 < 60 ) && ok ) {
00099 input = KGlobal::locale()->formatTime( QTime( tm / 100, tm % 100, 0 ) );
00100 }
00101 }
00102 }
00103 };
00104
00105
00106
00107
00108 KTimeEdit::KTimeEdit( QWidget *parent, QTime qt, const char *name )
00109 : QComboBox( true, parent, name )
00110 {
00111 setInsertionPolicy( NoInsertion );
00112 setValidator( new KOTimeValidator( this ) );
00113
00114 mTime = qt;
00115
00116
00117
00118
00119
00120 QTime timeEntry(0,0,0);
00121 do {
00122 insertItem(KGlobal::locale()->formatTime(timeEntry));
00123 timeEntry = timeEntry.addSecs(60*15);
00124 } while (!timeEntry.isNull());
00125
00126 insertItem( KGlobal::locale()->formatTime( QTime( 23, 59, 59 ) ) );
00127
00128 updateText();
00129 setFocusPolicy(QWidget::StrongFocus);
00130
00131 connect(this, SIGNAL(activated(int)), this, SLOT(active(int)));
00132 connect(this, SIGNAL(highlighted(int)), this, SLOT(hilit(int)));
00133 connect(this, SIGNAL(textChanged(const QString&)),this,SLOT(changedText()));
00134 }
00135
00136 KTimeEdit::~KTimeEdit()
00137 {
00138 }
00139
00140 bool KTimeEdit::hasTime() const
00141 {
00142
00143 if ( currentText().isEmpty() ) return false;
00144
00145
00146 return true;
00147 }
00148
00149 QTime KTimeEdit::getTime() const
00150 {
00151
00152
00153 bool ok = false;
00154 QTime time = KGlobal::locale()->readTime( currentText(), KLocale::WithoutSeconds, &ok );
00155 if ( !ok ) {
00156
00157 int tm = currentText().toInt( &ok );
00158 if ( ( 0 <= tm ) && ( tm < 2400 ) && ( tm%100 < 60 ) && ok ) {
00159 time.setHMS( tm / 100, tm % 100, 0 );
00160 } else {
00161 ok = false;
00162 }
00163 }
00164 kdDebug(5300) << "KTimeEdit::getTime(): " << time.toString() << endl;
00165 return time;
00166 }
00167
00168 QSizePolicy KTimeEdit::sizePolicy() const
00169 {
00170
00171
00172 QSizePolicy sizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
00173
00174 return sizePolicy;
00175 }
00176
00177 void KTimeEdit::setTime(QTime newTime)
00178 {
00179 if ( mTime != newTime )
00180 {
00181 kdDebug(5300) << "KTimeEdit::setTime(): " << newTime.toString() << endl;
00182
00183 mTime = newTime;
00184 updateText();
00185 }
00186 }
00187
00188 void KTimeEdit::active(int i)
00189 {
00190
00191 if( i == count() - 1 )
00192 mTime = QTime( 23, 59, 0 );
00193 else
00194 mTime = QTime(0,0,0).addSecs(i*15*60);
00195 emit timeChanged(mTime);
00196 }
00197
00198 void KTimeEdit::hilit(int )
00199 {
00200
00201 }
00202
00203 void KTimeEdit::addTime(QTime qt)
00204 {
00205
00206 mTime = qt.addSecs(mTime.minute()*60+mTime.hour()*3600);
00207 updateText();
00208 emit timeChanged(mTime);
00209 }
00210
00211 void KTimeEdit::subTime(QTime qt)
00212 {
00213 int h, m;
00214
00215
00216
00217
00218 h = mTime.hour()-qt.hour();
00219 m = mTime.minute()-qt.minute();
00220
00221 if(m < 0) {
00222 m += 60;
00223 h -= 1;
00224 }
00225
00226 if(h < 0) {
00227 h += 24;
00228 }
00229
00230
00231 mTime.setHMS(h, m, 0);
00232 updateText();
00233 emit timeChanged(mTime);
00234 }
00235
00236 void KTimeEdit::keyPressEvent(QKeyEvent *qke)
00237 {
00238 switch(qke->key()) {
00239 case Key_Down:
00240 addTime(QTime(0,1,0));
00241 break;
00242 case Key_Up:
00243 subTime(QTime(0,1,0));
00244 break;
00245 case Key_Prior:
00246 subTime(QTime(1,0,0));
00247 break;
00248 case Key_Next:
00249 addTime(QTime(1,0,0));
00250 break;
00251 default:
00252 QComboBox::keyPressEvent(qke);
00253 break;
00254 }
00255 }
00256
00257 void KTimeEdit::updateText()
00258 {
00259
00260 QString s = KGlobal::locale()->formatTime(mTime);
00261
00262 QLineEdit *line = lineEdit();
00263 line->blockSignals(true);
00264 int pos = line->cursorPosition();
00265
00266
00267
00268 setCurrentItem((mTime.hour()*4)+((mTime.minute()+7)/15));
00269
00270 line->setText(s);
00271 line->setCursorPosition(pos);
00272 line->blockSignals(false);
00273
00274
00275 }
00276
00277 bool KTimeEdit::inputIsValid() const
00278 {
00279 int cursorPos = lineEdit()->cursorPosition();
00280 QString str = currentText();
00281 return validator()->validate( str, cursorPos ) == QValidator::Acceptable;
00282 }
00283
00284 void KTimeEdit::changedText()
00285 {
00286
00287 if ( inputIsValid() )
00288 {
00289 mTime = getTime();
00290 emit timeChanged(mTime);
00291 }
00292 }