5 #include <tqlineedit.h>
7 #include <tqvalidator.h>
10 #include <tdelocale.h>
11 #include <tdeglobal.h>
12 #include "ktimewidget.h"
14 enum ValidatorType { HOUR, MINUTE };
16 class TimeValidator :
public TQValidator
19 TimeValidator( ValidatorType tp, TQWidget *parent=0,
const char *name=0)
20 : TQValidator(parent, name)
24 State validate(TQString &str,
int &)
const
30 int val = str.toInt( &ok );
34 if ( _tp==MINUTE && val >= 60 )
45 class KarmLineEdit :
public TQLineEdit
49 KarmLineEdit( TQWidget* parent,
const char* name = 0 )
50 : TQLineEdit( parent, name ) {}
54 virtual void keyPressEvent( TQKeyEvent *event )
56 TQLineEdit::keyPressEvent( event );
57 if ( text().length() == 2 && !event->text().isEmpty() )
58 focusNextPrevChild(
true);
63 KArmTimeWidget::KArmTimeWidget( TQWidget* parent,
const char* name )
64 : TQWidget(parent, name)
66 TQHBoxLayout *layout =
new TQHBoxLayout(
this);
68 _hourLE =
new TQLineEdit(
this);
71 _hourLE->setFixedWidth( fontMetrics().maxWidth() * 3
72 + 2 * _hourLE->frameWidth() + 2);
73 layout->addWidget(_hourLE);
74 TimeValidator *validator =
new TimeValidator( HOUR, _hourLE,
75 "Validator for _hourLE");
76 _hourLE->setValidator( validator );
77 _hourLE->setAlignment( TQt::AlignRight );
80 TQLabel *hr =
new TQLabel( i18n(
"abbreviation for hours",
" hr. " ),
this );
81 layout->addWidget( hr );
83 _minuteLE =
new KarmLineEdit(
this);
86 _minuteLE->setFixedWidth( fontMetrics().maxWidth() * 2
87 + 2 * _minuteLE->frameWidth() + 2);
88 layout->addWidget(_minuteLE);
89 validator =
new TimeValidator( MINUTE, _minuteLE,
"Validator for _minuteLE");
90 _minuteLE->setValidator( validator );
91 _minuteLE->setMaxLength(2);
92 _minuteLE->setAlignment( TQt::AlignRight );
94 TQLabel *min =
new TQLabel( i18n(
"abbreviation for minutes",
" min. " ),
this );
95 layout->addWidget( min );
97 layout->addStretch(1);
98 setFocusProxy( _hourLE );
101 void KArmTimeWidget::setTime(
long minutes )
104 long hourpart = labs(minutes) / 60;
105 long minutepart = labs(minutes) % 60;
107 dummy.setNum( hourpart );
109 dummy = TDEGlobal::locale()->negativeSign() + dummy;
110 _hourLE->setText( dummy );
112 dummy.setNum( minutepart );
113 if (minutepart < 10 ) {
114 dummy = TQString::fromLatin1(
"0" ) + dummy;
116 _minuteLE->setText( dummy );
119 long KArmTimeWidget::time()
const
124 h = abs(_hourLE->text().toInt( &ok ));
125 m = _minuteLE->text().toInt( &ok );
126 isNegative = _hourLE->text().startsWith(TDEGlobal::locale()->negativeSign());
128 return (h * 60 + m) * ((isNegative) ? -1 : 1);