korganizer Library API Documentation

ktimeedit.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 1999 Preston Brown, Ian Dawes
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
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qkeycode.h>
00025 #include <qcombobox.h>
00026 #include <qdatetime.h>
00027 
00028 #include <kmessagebox.h>
00029 #include <kglobal.h>
00030 #include <kdebug.h>
00031 #include <klocale.h>
00032 
00033 #include "ktimeedit.h"
00034 #include "ktimeedit.moc"
00035 
00036 KTimeEdit::KTimeEdit(QWidget *parent, QTime qt, const char *name)
00037   : QComboBox(TRUE, parent, name)
00038 {
00039   setInsertionPolicy(NoInsertion);
00040 
00041   mTime = qt;
00042 
00043   mNoTimeString = i18n("No Time");
00044 //  insertItem( mNoTimeString );
00045 
00046   // Fill combo box with selection of times in localized format.
00047   QTime timeEntry(0,0,0);
00048   do {
00049     insertItem(KGlobal::locale()->formatTime(timeEntry));
00050     timeEntry = timeEntry.addSecs(60*15);
00051   } while (!timeEntry.isNull());
00052   // Add end of day.
00053   insertItem( KGlobal::locale()->formatTime( QTime( 23, 59, 59 ) ) );
00054 
00055   updateSelection();
00056   setFocusPolicy(QWidget::StrongFocus);
00057 
00058   connect(this, SIGNAL(activated(int)), this, SLOT(activ(int)));
00059   connect(this, SIGNAL(highlighted(int)), this, SLOT(hilit(int)));
00060   connect(this,SIGNAL(textChanged(const QString&)),this,SLOT(changedText()));
00061 }
00062 
00063 KTimeEdit::~KTimeEdit()
00064 {
00065 }
00066 
00067 bool KTimeEdit::hasTime()
00068 {
00069   if ( currentText().isEmpty() ) return false;
00070   if ( currentText() == mNoTimeString ) return false;
00071 
00072   return true;
00073 }
00074 
00075 QTime KTimeEdit::getTime()
00076 {
00077 //  kdDebug() << "KTimeEdit::getTime()" << endl;
00078   QTime time = KGlobal::locale()->readTime(currentText());
00079 //  kdDebug() << "KTimeEdit::getTime(): " << time.toString() << endl;
00080   return time;
00081 }
00082 
00083 QSizePolicy  KTimeEdit::sizePolicy() const
00084 {
00085   // Set size policy to Fixed, because edit cannot contain more text than the
00086   // string representing the time. It doesn't make sense to provide more space.
00087   QSizePolicy sizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
00088 
00089   return sizePolicy;
00090 }
00091 
00092 void KTimeEdit::setTime(QTime newTime)
00093 {
00094 //  kdDebug() << "KTimeEdit::setTime(): " << newTime.toString() << endl;
00095 
00096   mTime = newTime;
00097   updateSelection();
00098 }
00099 
00100 void KTimeEdit::activ(int i)
00101 {
00102   mTime = QTime(0,0,0).addSecs(i*15*60);
00103   //emit timeChanged(mTime);
00104 }
00105 
00106 void KTimeEdit::hilit(int )
00107 {
00108   // we don't currently need to do anything here.
00109 }
00110 
00111 void KTimeEdit::addTime(QTime qt)
00112 {
00113   // Calculate the new time.
00114   mTime = qt.addSecs(mTime.minute()*60+mTime.hour()*3600);
00115   //emit timeChanged(mTime);
00116   updateSelection();
00117 }
00118 
00119 void KTimeEdit::subTime(QTime qt)
00120 {
00121   int h, m;
00122 
00123   // Note that we cannot use the same method for determining the new
00124   // time as we did in addTime, because QTime does not handle adding
00125   // negative seconds well at all.
00126   h = mTime.hour()-qt.hour();
00127   m = mTime.minute()-qt.minute();
00128 
00129   if(m < 0) {
00130     m += 60;
00131     h -= 1;
00132   }
00133 
00134   if(h < 0) {
00135     h += 24;
00136   }
00137 
00138   // store the newly calculated time.
00139   mTime.setHMS(h, m, 0);
00140   //emit timeChanged(mTime);
00141   updateSelection();
00142 }
00143 
00144 void KTimeEdit::keyPressEvent(QKeyEvent *qke)
00145 {
00146   switch(qke->key()) {
00147   case Key_Enter:
00148   case Key_Return:
00149     mTime = getTime();
00150     emit timeChanged(mTime);
00151 //    validateEntry();
00152     break;
00153   case Key_Down:
00154     addTime(QTime(0,15,0));
00155     break;
00156   case Key_Up:
00157     subTime(QTime(0,15,0));
00158     break;
00159   default:
00160     QComboBox::keyPressEvent(qke);
00161     break;
00162   } // switch
00163 }
00164 
00165 void KTimeEdit::validateEntry()
00166 {
00167 // Disabled because it does not make anything useful. Should probably try to fix
00168 // up invalid time input.
00169 /*
00170   QTime t = KGlobal::locale()->readTime(currentText());
00171 
00172   if (!t.isValid()) {
00173 //    KMessageBox::sorry(this,"You must specify a valid time");
00174     current_display_valid = false;
00175   } else {
00176     mTime = t;
00177     current_display_valid = true;
00178   }
00179 */
00180 }
00181 
00182 void KTimeEdit::updateSelection()
00183 {
00184   QString s = KGlobal::locale()->formatTime(mTime);
00185   setEditText(s);
00186 
00187   if (!mTime.minute() % 15) {
00188     setCurrentItem((mTime.hour()*4)+(mTime.minute()/15));
00189   }
00190 }
00191 
00192 bool KTimeEdit::inputIsValid()
00193 {
00194 //  if ( !hasTime() ) return true;
00195 
00196   QTime t = KGlobal::locale()->readTime(currentText());
00197 
00198   return t.isValid();
00199 }
00200 
00201 void KTimeEdit::changedText()
00202 {
00203 //  kdDebug() << "KTimeEdit::changedText()" << endl;
00204   mTime = getTime();
00205   emit timeChanged(mTime);
00206 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:32 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001