karm Library API Documentation

ktimewidget.cpp

00001 #include <stdlib.h> // abs() 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> // i18n 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 ( str.contains('-') != 0 ) 00034 return Invalid; 00035 00036 if ( _tp==MINUTE && val >= 60 ) 00037 return Invalid; 00038 else 00039 return Acceptable; 00040 } 00041 00042 public: 00043 ValidatorType _tp; 00044 }; 00045 00046 00047 class KarmLineEdit : public QLineEdit 00048 { 00049 00050 public: 00051 KarmLineEdit( QWidget* parent, const char* name = 0 ) 00052 : QLineEdit( parent, name ) {} 00053 00054 protected: 00055 00056 virtual void keyPressEvent( QKeyEvent *event ) 00057 { 00058 QLineEdit::keyPressEvent( event ); 00059 if ( text().length() == 2 && !event->text().isEmpty() ) 00060 focusNextPrevChild(true); 00061 } 00062 }; 00063 00064 00065 KArmTimeWidget::KArmTimeWidget( QWidget* parent, const char* name ) 00066 : QWidget(parent, name) 00067 { 00068 QHBoxLayout *layout = new QHBoxLayout(this); 00069 00070 _hourLE = new QLineEdit( this); 00071 // 9999 hours > 1 year! 00072 // 999 hours = 41 days (That should be enough ...) 00073 _hourLE->setFixedWidth( fontMetrics().maxWidth() * 3 00074 + 2 * _hourLE->frameWidth() + 2); 00075 layout->addWidget(_hourLE); 00076 TimeValidator *validator = new TimeValidator( HOUR, _hourLE, 00077 "Validator for _hourLE"); 00078 _hourLE->setValidator( validator ); 00079 _hourLE->setAlignment( Qt::AlignRight ); 00080 00081 00082 QLabel *hr = new QLabel( i18n( "abbreviation for hours", " hr. " ), this ); 00083 layout->addWidget( hr ); 00084 00085 _minuteLE = new KarmLineEdit(this); 00086 00087 // Minutes lineedit: Make room for 2 digits 00088 _minuteLE->setFixedWidth( fontMetrics().maxWidth() * 2 00089 + 2 * _minuteLE->frameWidth() + 2); 00090 layout->addWidget(_minuteLE); 00091 validator = new TimeValidator( MINUTE, _minuteLE, "Validator for _minuteLE"); 00092 _minuteLE->setValidator( validator ); 00093 _minuteLE->setMaxLength(2); 00094 _minuteLE->setAlignment( Qt::AlignRight ); 00095 00096 QLabel *min = new QLabel( i18n( "abbreviation for minutes", " min. " ), this ); 00097 layout->addWidget( min ); 00098 00099 layout->addStretch(1); 00100 setFocusProxy( _hourLE ); 00101 } 00102 00103 void KArmTimeWidget::setTime( int hour, int minute ) 00104 { 00105 QString dummy; 00106 00107 dummy.setNum( hour ); 00108 _hourLE->setText( dummy ); 00109 00110 dummy.setNum( abs(minute) ); 00111 if (abs(minute) < 10 ) { 00112 dummy = QString::fromLatin1( "0" ) + dummy; 00113 } 00114 _minuteLE->setText( dummy ); 00115 } 00116 00117 long KArmTimeWidget::time() const 00118 { 00119 bool ok; 00120 int h, m; 00121 00122 h = _hourLE->text().toInt( &ok ); 00123 m = _minuteLE->text().toInt( &ok ); 00124 00125 // if h is negative, we have to *subtract* m 00126 return h * 60 + ( ( h < 0) ? -1 : 1 ) * m; 00127 }
KDE Logo
This file is part of the documentation for karm Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Oct 1 15:19:01 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003