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