1 /***************************************************************************
2 timebox.cpp - description
4 begin : Sun Jan 20 2002
5 copyright : (C) 2002 by Pablo de Vicente
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
19 #include "libkdeedu/extdate/extdatetime.h"
20 #include <qdatetime.h> //needed for QTime
27 timeBox::timeBox(QWidget
*parent
, const char *name
, bool tt
) : QLineEdit(parent
,name
)
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
)
56 int h
= 0, m
= 0, is
= 0;
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
);
72 qt
= QTime( h
, 0, 0 );
74 if ( ok
!= NULL
) *ok
= true;
77 double x
= entry
.toDouble( &checkValue
);
79 int seconds
= int(x
* 3600);
83 if ( ok
!= NULL
) *ok
= true;
88 //no success yet...try assuming multiple 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
);
105 fields
[1] = QString("%1").arg( int(mx
) );
106 fields
.append( QString("%1").arg( int( 60.0*(mx
- int(mx
)) ) ) );
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;
133 if ( ok
!= NULL
) *ok
= true;
139 if ( ok
!= NULL
) *ok
= false;
143 // if ( !valueFound )
144 // KMessageBox::sorry( 0, errMsg.arg( "Angle" ), i18n( "Could Not Set Value" ) );
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();