kstars
timebox.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "timebox.h"
00019 #include "libkdeedu/extdate/extdatetime.h"
00020 #include <qdatetime.h>
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
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
00062 if ( ok != NULL ) *ok = false;
00063
00064
00065
00066 QString entry = text().stripWhiteSpace();
00067
00068
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
00089
00090 if ( !valueFound ) {
00091 QStringList fields;
00092
00093
00094 if ( entry.contains(':') )
00095 fields = QStringList::split( ':', entry );
00096 else fields = QStringList::split( " ", entry );
00097
00098
00099
00100
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
00113
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
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
00144
00145
00146
00147 return qt;
00148 }
00149
00150
00151 ExtDate timeBox::createDate (bool *)
00152 {
00153
00154 QString entry = text().stripWhiteSpace();
00155
00156
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 }