karm

ktimewidget.cpp
1#include <stdlib.h> // abs()
2
3#include <tqlabel.h>
4#include <tqlayout.h>
5#include <tqlineedit.h>
6#include <tqstring.h>
7#include <tqvalidator.h>
8#include <tqwidget.h>
9
10#include <tdelocale.h> // i18n
11#include <tdeglobal.h>
12#include "ktimewidget.h"
13
14enum ValidatorType { HOUR, MINUTE };
15
16class TimeValidator : public TQValidator
17{
18 public:
19 TimeValidator( ValidatorType tp, TQWidget *parent=0, const char *name=0)
20 : TQValidator(parent, name)
21 {
22 _tp = tp;
23 }
24 State validate(TQString &str, int &) const
25 {
26 if (str.isEmpty())
27 return Acceptable;
28
29 bool ok;
30 int val = str.toInt( &ok );
31 if ( ! ok )
32 return Invalid;
33
34 if ( _tp==MINUTE && val >= 60 )
35 return Invalid;
36 else
37 return Acceptable;
38 }
39
40 public:
41 ValidatorType _tp;
42};
43
44
45class KarmLineEdit : public TQLineEdit
46{
47
48 public:
49 KarmLineEdit( TQWidget* parent, const char* name = 0 )
50 : TQLineEdit( parent, name ) {}
51
52protected:
53
54 virtual void keyPressEvent( TQKeyEvent *event )
55 {
56 TQLineEdit::keyPressEvent( event );
57 if ( text().length() == 2 && !event->text().isEmpty() )
58 focusNextPrevChild(true);
59 }
60};
61
62
63KArmTimeWidget::KArmTimeWidget( TQWidget* parent, const char* name )
64 : TQWidget(parent, name)
65{
66 TQHBoxLayout *layout = new TQHBoxLayout(this);
67
68 _hourLE = new TQLineEdit( this);
69 // 9999 hours > 1 year!
70 // 999 hours = 41 days (That should be enough ...)
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 );
78
79
80 TQLabel *hr = new TQLabel( i18n( "abbreviation for hours", " hr. " ), this );
81 layout->addWidget( hr );
82
83 _minuteLE = new KarmLineEdit(this);
84
85 // Minutes lineedit: Make room for 2 digits
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 );
93
94 TQLabel *min = new TQLabel( i18n( "abbreviation for minutes", " min. " ), this );
95 layout->addWidget( min );
96
97 layout->addStretch(1);
98 setFocusProxy( _hourLE );
99}
100
101void KArmTimeWidget::setTime( long minutes )
102{
103 TQString dummy;
104 long hourpart = labs(minutes) / 60;
105 long minutepart = labs(minutes) % 60;
106
107 dummy.setNum( hourpart );
108 if (minutes < 0)
109 dummy = TDEGlobal::locale()->negativeSign() + dummy;
110 _hourLE->setText( dummy );
111
112 dummy.setNum( minutepart );
113 if (minutepart < 10 ) {
114 dummy = TQString::fromLatin1( "0" ) + dummy;
115 }
116 _minuteLE->setText( dummy );
117}
118
119long KArmTimeWidget::time() const
120{
121 bool ok, isNegative;
122 int h, m;
123
124 h = abs(_hourLE->text().toInt( &ok ));
125 m = _minuteLE->text().toInt( &ok );
126 isNegative = _hourLE->text().startsWith(TDEGlobal::locale()->negativeSign());
127
128 return (h * 60 + m) * ((isNegative) ? -1 : 1);
129}