karm
ktimewidget.cpp00001 #include <stdlib.h>
00002
00003 #include <qlabel.h>
00004 #include <qlayout.h>
00005 #include <qlineedit.h>
00006 #include <qstring.h>
00007 #include <qvalidator.h>
00008 #include <qwidget.h>
00009
00010 #include <klocale.h>
00011 #include "ktimewidget.h"
00012
00013 enum ValidatorType { HOUR, MINUTE };
00014
00015 class TimeValidator : public QValidator
00016 {
00017 public:
00018 TimeValidator( ValidatorType tp, QWidget *parent=0, const char *name=0)
00019 : QValidator(parent, name)
00020 {
00021 _tp = tp;
00022 }
00023 State validate(QString &str, int &) const
00024 {
00025 if (str.isEmpty())
00026 return Acceptable;
00027
00028 bool ok;
00029 int val = str.toInt( &ok );
00030 if ( ! ok )
00031 return Invalid;
00032
00033 if ( _tp==MINUTE && val >= 60 )
00034 return Invalid;
00035 else
00036 return Acceptable;
00037 }
00038
00039 public:
00040 ValidatorType _tp;
00041 };
00042
00043
00044 class KarmLineEdit : public QLineEdit
00045 {
00046
00047 public:
00048 KarmLineEdit( QWidget* parent, const char* name = 0 )
00049 : QLineEdit( parent, name ) {}
00050
00051 protected:
00052
00053 virtual void keyPressEvent( QKeyEvent *event )
00054 {
00055 QLineEdit::keyPressEvent( event );
00056 if ( text().length() == 2 && !event->text().isEmpty() )
00057 focusNextPrevChild(true);
00058 }
00059 };
00060
00061
00062 KArmTimeWidget::KArmTimeWidget( QWidget* parent, const char* name )
00063 : QWidget(parent, name)
00064 {
00065 QHBoxLayout *layout = new QHBoxLayout(this);
00066
00067 _hourLE = new QLineEdit( this);
00068
00069
00070 _hourLE->setFixedWidth( fontMetrics().maxWidth() * 3
00071 + 2 * _hourLE->frameWidth() + 2);
00072 layout->addWidget(_hourLE);
00073 TimeValidator *validator = new TimeValidator( HOUR, _hourLE,
00074 "Validator for _hourLE");
00075 _hourLE->setValidator( validator );
00076 _hourLE->setAlignment( Qt::AlignRight );
00077
00078
00079 QLabel *hr = new QLabel( i18n( "abbreviation for hours", " hr. " ), this );
00080 layout->addWidget( hr );
00081
00082 _minuteLE = new KarmLineEdit(this);
00083
00084
00085 _minuteLE->setFixedWidth( fontMetrics().maxWidth() * 2
00086 + 2 * _minuteLE->frameWidth() + 2);
00087 layout->addWidget(_minuteLE);
00088 validator = new TimeValidator( MINUTE, _minuteLE, "Validator for _minuteLE");
00089 _minuteLE->setValidator( validator );
00090 _minuteLE->setMaxLength(2);
00091 _minuteLE->setAlignment( Qt::AlignRight );
00092
00093 QLabel *min = new QLabel( i18n( "abbreviation for minutes", " min. " ), this );
00094 layout->addWidget( min );
00095
00096 layout->addStretch(1);
00097 setFocusProxy( _hourLE );
00098 }
00099
00100 void KArmTimeWidget::setTime( int hour, int minute )
00101 {
00102 QString dummy;
00103
00104 dummy.setNum( hour );
00105 _hourLE->setText( dummy );
00106
00107 dummy.setNum( abs(minute) );
00108 if (abs(minute) < 10 ) {
00109 dummy = QString::fromLatin1( "0" ) + dummy;
00110 }
00111 _minuteLE->setText( dummy );
00112 }
00113
00114 long KArmTimeWidget::time() const
00115 {
00116 bool ok;
00117 int h, m;
00118
00119 h = _hourLE->text().toInt( &ok );
00120 m = _minuteLE->text().toInt( &ok );
00121
00122
00123 return h * 60 + ( ( h < 0) ? -1 : 1 ) * m;
00124 }
|