ktimewidget.cpp
00001 #include <stdlib.h> // abs() 00002 00003 #include <tqlabel.h> 00004 #include <tqlayout.h> 00005 #include <tqlineedit.h> 00006 #include <tqstring.h> 00007 #include <tqvalidator.h> 00008 #include <tqwidget.h> 00009 00010 #include <tdelocale.h> // i18n 00011 #include <tdeglobal.h> 00012 #include "ktimewidget.h" 00013 00014 enum ValidatorType { HOUR, MINUTE }; 00015 00016 class TimeValidator : public TQValidator 00017 { 00018 public: 00019 TimeValidator( ValidatorType tp, TQWidget *parent=0, const char *name=0) 00020 : TQValidator(TQT_TQOBJECT(parent), name) 00021 { 00022 _tp = tp; 00023 } 00024 State validate(TQString &str, int &) const 00025 { 00026 if (str.isEmpty()) 00027 return Acceptable; 00028 00029 bool ok; 00030 int val = str.toInt( &ok ); 00031 if ( ! ok ) 00032 return Invalid; 00033 00034 if ( _tp==MINUTE && val >= 60 ) 00035 return Invalid; 00036 else 00037 return Acceptable; 00038 } 00039 00040 public: 00041 ValidatorType _tp; 00042 }; 00043 00044 00045 class KarmLineEdit : public TQLineEdit 00046 { 00047 00048 public: 00049 KarmLineEdit( TQWidget* parent, const char* name = 0 ) 00050 : TQLineEdit( parent, name ) {} 00051 00052 protected: 00053 00054 virtual void keyPressEvent( TQKeyEvent *event ) 00055 { 00056 TQLineEdit::keyPressEvent( event ); 00057 if ( text().length() == 2 && !event->text().isEmpty() ) 00058 focusNextPrevChild(true); 00059 } 00060 }; 00061 00062 00063 KArmTimeWidget::KArmTimeWidget( TQWidget* parent, const char* name ) 00064 : TQWidget(parent, name) 00065 { 00066 TQHBoxLayout *layout = new TQHBoxLayout(this); 00067 00068 _hourLE = new TQLineEdit( this); 00069 // 9999 hours > 1 year! 00070 // 999 hours = 41 days (That should be enough ...) 00071 _hourLE->setFixedWidth( fontMetrics().maxWidth() * 3 00072 + 2 * _hourLE->frameWidth() + 2); 00073 layout->addWidget(_hourLE); 00074 TimeValidator *validator = new TimeValidator( HOUR, _hourLE, 00075 "Validator for _hourLE"); 00076 _hourLE->setValidator( validator ); 00077 _hourLE->setAlignment( TQt::AlignRight ); 00078 00079 00080 TQLabel *hr = new TQLabel( i18n( "abbreviation for hours", " hr. " ), this ); 00081 layout->addWidget( hr ); 00082 00083 _minuteLE = new KarmLineEdit(this); 00084 00085 // Minutes lineedit: Make room for 2 digits 00086 _minuteLE->setFixedWidth( fontMetrics().maxWidth() * 2 00087 + 2 * _minuteLE->frameWidth() + 2); 00088 layout->addWidget(_minuteLE); 00089 validator = new TimeValidator( MINUTE, _minuteLE, "Validator for _minuteLE"); 00090 _minuteLE->setValidator( validator ); 00091 _minuteLE->setMaxLength(2); 00092 _minuteLE->setAlignment( TQt::AlignRight ); 00093 00094 TQLabel *min = new TQLabel( i18n( "abbreviation for minutes", " min. " ), this ); 00095 layout->addWidget( min ); 00096 00097 layout->addStretch(1); 00098 setFocusProxy( _hourLE ); 00099 } 00100 00101 void KArmTimeWidget::setTime( long minutes ) 00102 { 00103 TQString dummy; 00104 long hourpart = labs(minutes) / 60; 00105 long minutepart = labs(minutes) % 60; 00106 00107 dummy.setNum( hourpart ); 00108 if (minutes < 0) 00109 dummy = TDEGlobal::locale()->negativeSign() + dummy; 00110 _hourLE->setText( dummy ); 00111 00112 dummy.setNum( minutepart ); 00113 if (minutepart < 10 ) { 00114 dummy = TQString::fromLatin1( "0" ) + dummy; 00115 } 00116 _minuteLE->setText( dummy ); 00117 } 00118 00119 long KArmTimeWidget::time() const 00120 { 00121 bool ok, isNegative; 00122 int h, m; 00123 00124 h = abs(_hourLE->text().toInt( &ok )); 00125 m = _minuteLE->text().toInt( &ok ); 00126 isNegative = _hourLE->text().startsWith(TDEGlobal::locale()->negativeSign()); 00127 00128 return (h * 60 + m) * ((isNegative) ? -1 : 1); 00129 }