27 #include <tqkeycode.h>
28 #include <tqcombobox.h>
29 #include <tqdatetime.h>
30 #include <tqlineedit.h>
32 #include <tdemessagebox.h>
33 #include <tdeglobal.h>
35 #include <tdelocale.h>
37 #include "ktimeedit.h"
38 #include <tqvalidator.h>
39 #include "ktimeedit.moc"
43 class KOTimeValidator :
public TQValidator
46 KOTimeValidator(TQWidget* parent,
const char* name=0) : TQValidator(parent, name) {}
48 virtual State validate(TQString& str,
int& )
const
50 int length = str.length();
56 TDEGlobal::locale()->readTime(str, TDELocale::WithoutSeconds, &ok);
61 int tm = str.toInt( &ok );
62 if ( ok && ( 0 <= tm ) ) {
63 if ( ( tm < 2400 ) && ( tm%100 < 60 ) )
78 TQString minutes = str.mid(1);
79 int m = minutes.toInt(&ok);
80 if ( ok && m >= 0 && m < 60 )
82 }
else if ( str[str.length()-1] == sep )
84 TQString hours = str.left(length-1);
85 int h = hours.toInt(&ok);
86 if ( ok && h >= 0 && h < 24 )
92 virtual void fixup ( TQString & input )
const {
94 TDEGlobal::locale()->readTime( input, TDELocale::WithoutSeconds, &ok );
97 int tm = input.toInt( &ok );
98 if ( ( 0 <= tm ) && ( tm < 2400 ) && ( tm%100 < 60 ) && ok ) {
99 input = TDEGlobal::locale()->formatTime( TQTime( tm / 100, tm % 100, 0 ) );
109 : TQComboBox( true, parent, name )
111 setInsertionPolicy( NoInsertion );
112 setValidator(
new KOTimeValidator(
this ) );
120 TQTime timeEntry(0,0,0);
122 insertItem(TDEGlobal::locale()->formatTime(timeEntry));
123 timeEntry = timeEntry.addSecs(60*15);
124 }
while (!timeEntry.isNull());
126 insertItem( TDEGlobal::locale()->formatTime( TQTime( 23, 59, 59 ) ) );
129 setFocusPolicy(TQWidget::StrongFocus);
131 connect(
this, TQ_SIGNAL(activated(
int)),
this, TQ_SLOT(active(
int)));
132 connect(
this, TQ_SIGNAL(highlighted(
int)),
this, TQ_SLOT(hilit(
int)));
133 connect(
this, TQ_SIGNAL(textChanged(
const TQString&)),
this,TQ_SLOT(changedText()));
136 KTimeEdit::~KTimeEdit()
143 if ( currentText().isEmpty() )
return false;
154 TQTime time = TDEGlobal::locale()->readTime( currentText(), TDELocale::WithoutSeconds, &ok );
157 int tm = currentText().toInt( &ok );
158 if ( ( 0 <= tm ) && ( tm < 2400 ) && ( tm%100 < 60 ) && ok ) {
159 time.setHMS( tm / 100, tm % 100, 0 );
172 TQSizePolicy
sizePolicy(TQSizePolicy::Fixed,TQSizePolicy::Fixed);
179 if ( mTime != newTime )
181 kdDebug(5300) <<
"KTimeEdit::setTime(): " << TQString(newTime.toString()) << endl;
188 void KTimeEdit::active(
int i)
191 if( i == count() - 1 )
192 mTime = TQTime( 23, 59, 0 );
194 mTime = TQTime(0,0,0).addSecs(i*15*60);
198 void KTimeEdit::hilit(
int )
203 void KTimeEdit::addTime(TQTime qt)
206 mTime = qt.addSecs(mTime.minute()*60+mTime.hour()*3600);
211 void KTimeEdit::subTime(TQTime qt)
218 h = mTime.hour()-qt.hour();
219 m = mTime.minute()-qt.minute();
231 mTime.setHMS(h, m, 0);
236 void KTimeEdit::keyPressEvent(TQKeyEvent *qke)
240 addTime(TQTime(0,1,0));
243 subTime(TQTime(0,1,0));
246 subTime(TQTime(1,0,0));
249 addTime(TQTime(1,0,0));
252 TQComboBox::keyPressEvent(qke);
257 void KTimeEdit::updateText()
260 TQString s = TDEGlobal::locale()->formatTime(mTime);
262 TQLineEdit *line = lineEdit();
263 line->blockSignals(
true);
264 int pos = line->cursorPosition();
268 setCurrentItem((mTime.hour()*4)+((mTime.minute()+7)/15));
271 line->setCursorPosition(pos);
272 line->blockSignals(
false);
279 int cursorPos = lineEdit()->cursorPosition();
280 TQString str = currentText();
281 return validator()->validate( str, cursorPos ) == TQValidator::Acceptable;
284 void KTimeEdit::changedText()