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

kstars

timebox.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           timebox.cpp  -  description
00003                              -------------------
00004     begin                : Sun Jan 20 2002
00005     copyright            : (C) 2002 by Pablo de Vicente
00006     email                : vicente@oan.es
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "timebox.h"
00019 #include "libkdeedu/extdate/extdatetime.h"
00020 #include <qdatetime.h>  //needed for QTime
00021 #include <qregexp.h>
00022 #include <kglobal.h>
00023 #include <klocale.h>
00024 #include <stdlib.h>
00025 #include <kdebug.h>
00026 
00027 timeBox::timeBox(QWidget *parent, const char *name, bool tt) : QLineEdit(parent,name) 
00028 { 
00029 
00030     if (tt) { 
00031         setMaxLength(9);
00032         setMaximumWidth(90);
00033     }
00034     else {
00035         setMaxLength(12);
00036         setMaximumWidth(120);
00037     }
00038 
00039     timet = tt;
00040 }
00041 
00042 void timeBox::showTime (QTime t)
00043 {
00044     setEntry( t.toString("hh:mm:ss") );
00045 }
00046 
00047 void timeBox::showDate (ExtDate t)
00048 {
00049     setEntry( t.toString() );
00050 
00051 }
00052 
00053 QTime timeBox::createTime ( bool *ok )
00054 {
00055 //  QString entry;
00056     int h = 0, m = 0, is = 0;
00057     double s = 0.0;
00058     QTime qt;
00059     bool valueFound = false, badEntry = false , checkValue = false;
00060 
00061 //Initialize bool for result
00062     if ( ok != NULL ) *ok = false;
00063 
00064 //  QString errMsg = i18n( "Unable to parse %1 entry. Specify a %1 value as a simple integer, a floating-point number, or a triplet of values using colons or spaces as separators." );
00065 
00066     QString entry = text().stripWhiteSpace();
00067 
00068     //Try simplest cases: integer or double representation
00069 
00070     h = entry.toInt( &checkValue );
00071     if ( checkValue ) {
00072         qt = QTime( h, 0, 0 );
00073         valueFound = true;
00074         if ( ok != NULL ) *ok = true;
00075         return qt;
00076     } else {
00077         double x = entry.toDouble( &checkValue );
00078         if ( checkValue ) {
00079             int seconds = int(x * 3600);
00080             QTime qt(0,0,0);
00081             qt.addSecs(seconds);
00082             valueFound = true;
00083             if ( ok != NULL ) *ok = true;
00084             return qt;
00085         }
00086     }
00087 
00088     //no success yet...try assuming multiple fields
00089 
00090     if ( !valueFound ) { 
00091         QStringList fields;
00092         
00093         //check for colon-delimiters or space-delimiters
00094         if ( entry.contains(':') ) 
00095             fields = QStringList::split( ':', entry );
00096         else fields = QStringList::split( " ", entry ); 
00097 
00098         // If two fields we will add a third one, and then parse with 
00099         // the 3-field code block. If field[1] is a double, convert 
00100         // it to integer mins, and convert the remainder to secs
00101          
00102         if ( fields.count() == 2 ) {
00103             double mx = fields[1].toDouble( &checkValue );
00104             if ( checkValue ) {
00105                 fields[1] = QString("%1").arg( int(mx) );
00106                 fields.append( QString("%1").arg( int( 60.0*(mx - int(mx)) ) ) );
00107             } else {
00108                 fields.append( QString( "0" ) );
00109             }
00110         }
00111         
00112         // Three fields space-delimited ( h/d m s ); 
00113         // ignore all after 3rd field
00114 
00115         if ( fields.count() >= 3 ) {
00116             fields[0].replace( QRegExp("h"), "" );
00117             fields[1].replace( QRegExp("m"), "" );
00118             fields[2].replace( QRegExp("s"), "" );
00119         }
00120         //See if first two fields parse as integers.
00121         //
00122         h = fields[0].toInt( &checkValue );
00123         if ( !checkValue ) badEntry = true;
00124         m = fields[1].toInt( &checkValue );
00125         if ( !checkValue ) badEntry = true;
00126         s = fields[2].toDouble( &checkValue );
00127         if ( !checkValue ) badEntry = true;
00128 
00129         if ( !badEntry ) {
00130             valueFound = true;
00131             is = (int)s;
00132 
00133             if ( ok != NULL ) *ok = true;
00134 
00135             QTime qt(h,m,is);
00136             return qt;
00137 
00138         } else {
00139             if ( ok != NULL ) *ok = false;
00140         }
00141     }
00142 
00143 //   if ( !valueFound )
00144 //      KMessageBox::sorry( 0, errMsg.arg( "Angle" ), i18n( "Could Not Set Value" ) );
00145 
00146 
00147     return qt;
00148 }
00149 
00150 
00151 ExtDate timeBox::createDate (bool */*ok*/)
00152 {
00153     
00154     QString entry = text().stripWhiteSpace();
00155 
00156     // if entry is an empty string or invalid date use current date
00157 
00158     ExtDate date = ExtDate().fromString(entry);
00159 
00160     if ( !date.isValid() ) {
00161         kdDebug() << k_funcinfo << "Invalid date" << endl;
00162         showDate(ExtDate::currentDate());
00163         entry = text().stripWhiteSpace();
00164         return ExtDate::currentDate();
00165     } else {
00166         return date;
00167     }
00168 }
00169 
00170 timeBox::~timeBox(){
00171 }

kstars

Skip menu "kstars"
  • Main Page
  • Modules
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
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