moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / timebox.cpp
blob9aa513a3e3f86e1c8ae2fc82896bc9db0ab31c99
1 /***************************************************************************
2 timebox.cpp - description
3 -------------------
4 begin : Sun Jan 20 2002
5 copyright : (C) 2002 by Pablo de Vicente
6 email : vicente@oan.es
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include "timebox.h"
19 #include "libkdeedu/extdate/extdatetime.h"
20 #include <qdatetime.h> //needed for QTime
21 #include <qregexp.h>
22 #include <kglobal.h>
23 #include <klocale.h>
24 #include <stdlib.h>
25 #include <kdebug.h>
27 timeBox::timeBox(QWidget *parent, const char *name, bool tt) : QLineEdit(parent,name)
30 if (tt) {
31 setMaxLength(9);
32 setMaximumWidth(90);
34 else {
35 setMaxLength(12);
36 setMaximumWidth(120);
39 timet = tt;
42 void timeBox::showTime (QTime t)
44 setEntry( t.toString("hh:mm:ss") );
47 void timeBox::showDate (ExtDate t)
49 setEntry( t.toString() );
53 QTime timeBox::createTime ( bool *ok )
55 // QString entry;
56 int h = 0, m = 0, is = 0;
57 double s = 0.0;
58 QTime qt;
59 bool valueFound = false, badEntry = false , checkValue = false;
61 //Initialize bool for result
62 if ( ok != NULL ) *ok = false;
64 // 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." );
66 QString entry = text().stripWhiteSpace();
68 //Try simplest cases: integer or double representation
70 h = entry.toInt( &checkValue );
71 if ( checkValue ) {
72 qt = QTime( h, 0, 0 );
73 valueFound = true;
74 if ( ok != NULL ) *ok = true;
75 return qt;
76 } else {
77 double x = entry.toDouble( &checkValue );
78 if ( checkValue ) {
79 int seconds = int(x * 3600);
80 QTime qt(0,0,0);
81 qt.addSecs(seconds);
82 valueFound = true;
83 if ( ok != NULL ) *ok = true;
84 return qt;
88 //no success yet...try assuming multiple fields
90 if ( !valueFound ) {
91 QStringList fields;
93 //check for colon-delimiters or space-delimiters
94 if ( entry.contains(':') )
95 fields = QStringList::split( ':', entry );
96 else fields = QStringList::split( " ", entry );
98 // If two fields we will add a third one, and then parse with
99 // the 3-field code block. If field[1] is a double, convert
100 // it to integer mins, and convert the remainder to secs
102 if ( fields.count() == 2 ) {
103 double mx = fields[1].toDouble( &checkValue );
104 if ( checkValue ) {
105 fields[1] = QString("%1").arg( int(mx) );
106 fields.append( QString("%1").arg( int( 60.0*(mx - int(mx)) ) ) );
107 } else {
108 fields.append( QString( "0" ) );
112 // Three fields space-delimited ( h/d m s );
113 // ignore all after 3rd field
115 if ( fields.count() >= 3 ) {
116 fields[0].replace( QRegExp("h"), "" );
117 fields[1].replace( QRegExp("m"), "" );
118 fields[2].replace( QRegExp("s"), "" );
120 //See if first two fields parse as integers.
122 h = fields[0].toInt( &checkValue );
123 if ( !checkValue ) badEntry = true;
124 m = fields[1].toInt( &checkValue );
125 if ( !checkValue ) badEntry = true;
126 s = fields[2].toDouble( &checkValue );
127 if ( !checkValue ) badEntry = true;
129 if ( !badEntry ) {
130 valueFound = true;
131 is = (int)s;
133 if ( ok != NULL ) *ok = true;
135 QTime qt(h,m,is);
136 return qt;
138 } else {
139 if ( ok != NULL ) *ok = false;
143 // if ( !valueFound )
144 // KMessageBox::sorry( 0, errMsg.arg( "Angle" ), i18n( "Could Not Set Value" ) );
147 return qt;
151 ExtDate timeBox::createDate (bool */*ok*/)
154 QString entry = text().stripWhiteSpace();
156 // if entry is an empty string or invalid date use current date
158 ExtDate date = ExtDate().fromString(entry);
160 if ( !date.isValid() ) {
161 kdDebug() << k_funcinfo << "Invalid date" << endl;
162 showDate(ExtDate::currentDate());
163 entry = text().stripWhiteSpace();
164 return ExtDate::currentDate();
165 } else {
166 return date;
170 timeBox::~timeBox(){