• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

ktimetracker

ktimewidget.cpp

Go to the documentation of this file.
00001 /*
00002  *     Copyright (C) 2000 by Jesper Perderson <blackie@kde.org>
00003  *                   2007 the ktimetracker developers
00004  *
00005  *   This program is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   This program is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License along
00016  *   with this program; if not, write to the
00017  *      Free Software Foundation, Inc.
00018  *      51 Franklin Street, Fifth Floor
00019  *      Boston, MA  02110-1301  USA.
00020  *
00021  */
00022 
00023 #include "ktimewidget.h"
00024 
00025 #include <stdlib.h>             // abs()
00026 
00027 #include <QHBoxLayout>
00028 #include <QKeyEvent>
00029 #include <QLabel>
00030 #include <QString>
00031 #include <QValidator>
00032 #include <QWidget>
00033 #include <kdebug.h>
00034 #include <KLineEdit>
00035 #include <KLocale>
00036 
00037 enum ValidatorType { HOUR, MINUTE };
00038 
00039 class TimeValidator : public QValidator
00040 {
00041   public:
00042     TimeValidator( ValidatorType tp, QWidget *parent=0, const char *name=0)
00043       : QValidator(parent)
00044     {
00045       if ( name ) { setObjectName( name ); }
00046       _tp = tp;
00047     }
00048     State validate(QString &str, int &) const
00049     {
00050       if (str.isEmpty())
00051         return Acceptable;
00052 
00053       bool ok;
00054       str.toInt( &ok );
00055       if ( ! ok )
00056         return Invalid;
00057       else
00058         return Acceptable;
00059     }
00060 
00061   public:
00062     ValidatorType _tp;
00063 };
00064 
00065 
00066 class KarmLineEdit : public KLineEdit
00067 {
00068 
00069   public:
00070     KarmLineEdit( QWidget* parent, const char* name = 0 )
00071       : KLineEdit( parent ) { setObjectName( name ); }
00072 
00073 protected:
00074 
00075   virtual void keyPressEvent( QKeyEvent *event )
00076   {
00077     KLineEdit::keyPressEvent( event );
00078     if ( text().length() == 2 && !event->text().isEmpty() )
00079       focusNextPrevChild(true);
00080   }
00081 };
00082 
00083 
00084 KArmTimeWidget::KArmTimeWidget( QWidget* parent, const char* name )
00085   : QWidget(parent)
00086 {
00087   setObjectName( name );
00088   QHBoxLayout *layout = new QHBoxLayout(this);
00089   layout->setMargin( 0 );
00090   layout->setSpacing( 0 );
00091 
00092   _hourLE = new KLineEdit( this);
00093   // 9999 hours > 1 year!
00094   // 999 hours = 41 days  (That should be enough ...)
00095 
00096   _hourLE->setFixedWidth( fontMetrics().maxWidth() * 3
00097                           + 2  + 2);
00098   layout->addWidget(_hourLE);
00099   TimeValidator *validator = new TimeValidator( HOUR, _hourLE,
00100                                                 "Validator for _hourLE");
00101   _hourLE->setValidator( validator );
00102   _hourLE->setAlignment( Qt::AlignRight );
00103 
00104 
00105   QLabel *hr = new QLabel( i18nc( "abbreviation for hours", " hr. " ), this );
00106   layout->addWidget( hr );
00107 
00108   _minuteLE = new KarmLineEdit(this);
00109 
00110   // Minutes lineedit: Make room for 2 digits
00111 
00112   _minuteLE->setFixedWidth( fontMetrics().maxWidth() * 2
00113                             + 2  + 2);
00114   layout->addWidget(_minuteLE);
00115   validator = new TimeValidator( MINUTE, _minuteLE, "Validator for _minuteLE");
00116   _minuteLE->setValidator( validator );
00117   _minuteLE->setMaxLength(2);
00118   _minuteLE->setAlignment( Qt::AlignRight );
00119 
00120   QLabel *min = new QLabel( i18nc( "abbreviation for minutes", " min. " ), this );
00121   layout->addWidget( min );
00122 
00123   layout->addStretch(1);
00124   setFocusProxy( _hourLE );
00125 }
00126 
00127 void KArmTimeWidget::setTime( int hour, int minute )
00128 {
00129   kDebug(5970) << "Entering function( "<< hour << ", " << minute << " )";
00130   QString dummy;
00131 
00132   dummy.setNum( hour + (minute/60));
00133   _hourLE->setText( dummy );
00134 
00135   dummy.setNum( abs(minute%60) );
00136   if (abs(minute) < 10 ) 
00137   {
00138     dummy = QString::fromLatin1( "0" ) + dummy;
00139   }
00140   _minuteLE->setText( dummy );
00141 }
00142 
00143 long KArmTimeWidget::time() const
00144 {
00145   bool ok;
00146   int h, m;
00147 
00148   h = _hourLE->text().toInt( &ok );
00149   m = _minuteLE->text().toInt( &ok );
00150 
00151   // if h is negative, we have to *subtract* m
00152   return h * 60 + ( ( h < 0) ? -1 : 1 ) * m;
00153 }

ktimetracker

Skip menu "ktimetracker"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal