2 Copyright (C) 2003 by Walter Schreppers
3 Copyright (C) 2004 by Cies Breijs
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of version 2 of the GNU General Public
7 License as published by the Free Software Foundation.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 Value::Value(const Value
& n
)
37 int Value::Type() const
42 void Value::setType(int newType
)
44 if (type
== newType
) return; // dont change values when type is not changing
45 else if (newType
== boolValue
)
49 m_string
= i18n("false");
51 else if (newType
== numberValue
)
56 else if (newType
== stringValue
)
61 else if (newType
== emptyValue
) init();
65 bool Value::Bool() const
67 if (type
== boolValue
) return m_bool
;
68 else if (type
== numberValue
) return (m_double
- 0.5 > 0);
72 void Value::setBool(bool b
)
79 m_string
= i18n("true");
84 m_string
= i18n("false");
89 double Value::Number() const
91 if (type
== boolValue
)
96 else if (type
== numberValue
) return m_double
;
97 return 0; // stringValue, emptyValue
100 void Value::setNumber(double d
)
107 bool Value::setNumber(const QString
&s
)
111 double num
= s
.toDouble(&ok
);
115 m_string
.setNum(num
);
122 QString
Value::String() const
124 if (type
== boolValue
)
126 if (m_bool
) return QString( i18n("true") );
127 else return QString( i18n("false") );
129 else if (type
== numberValue
)
135 return m_string
; // stringValue, emptyValue
138 void Value::setString(double d
)
145 void Value::setString(const QString
&s
)
153 Value
& Value::operator=(const Value
& n
)
161 m_double
= n
.Number();
162 m_string
= n
.String();
164 else if (n
.Type() == boolValue
)
168 else if (n
.Type() == numberValue
)
170 setNumber( n
.Number() );
172 else if (n
.Type() == stringValue
)
174 setString( n
.String() );
176 else if (n
.Type() == emptyValue
)
185 Value
& Value::operator=(const QString
& s
)
191 Value
& Value::operator=(double n
)
199 Value
& Value::operator+(const Value
& n
)
201 if (type
== numberValue
&& n
.Type() == numberValue
)
203 m_double
+= n
.Number();
208 m_string
= String() + n
.String();
214 Value
& Value::operator-(const Value
& n
)
216 if (type
== numberValue
&& n
.Type() == numberValue
)
218 m_double
-= n
.Number();
222 kdDebug(0)<<"Value::operator; cannot subtract strings"<<endl
;
228 Value
& Value::operator*(const Value
& n
)
230 if (type
== numberValue
&& n
.Type() == numberValue
)
232 m_double
*= n
.Number();
236 kdDebug(0)<<"Value::operator; cannot multiply strings"<<endl
;
242 Value
& Value::operator/(const Value
& n
)
244 if (type
== numberValue
&& n
.Type() == numberValue
)
246 m_double
/= n
.Number();
250 kdDebug(0)<<"Value::operator; cannot divide strings"<<endl
;
257 bool Value::operator==(const Value
& n
) const
259 if (type
== boolValue
&& n
.Type() == boolValue
) return m_bool
== n
.Bool();
260 if (type
== numberValue
&& n
.Type() == numberValue
) return m_double
== n
.Number();
261 if (type
== stringValue
&& n
.Type() == stringValue
) return m_string
== n
.String();
262 if (type
== emptyValue
&& n
.Type() == emptyValue
) return true;
267 bool Value::operator!=(const Value
& n
) const
269 if (type
== boolValue
&& n
.Type() == boolValue
) return m_bool
!= n
.Bool();
270 if (type
== numberValue
&& n
.Type() == numberValue
) return m_double
!= n
.Number();
271 if (type
== stringValue
&& n
.Type() == stringValue
) return m_string
!= n
.String();
272 // if (type == emptyValue && n.Type() == emptyValue) return false;
277 bool Value::operator<(const Value
& n
) const
279 if (type
== boolValue
&& n
.Type() == boolValue
) return m_bool
< n
.Bool();
280 if (type
== numberValue
&& n
.Type() == numberValue
) return m_double
< n
.Number();
281 if (type
== stringValue
&& n
.Type() == stringValue
) return m_string
.length() < n
.String().length();
282 // if (type == emptyValue && n.Type() == emptyValue) return false;
287 bool Value::operator<=(const Value
& n
) const
289 if (type
== boolValue
&& n
.Type() == boolValue
) return m_bool
<= n
.Bool();
290 if (type
== numberValue
&& n
.Type() == numberValue
) return m_double
<= n
.Number();
291 if (type
== stringValue
&& n
.Type() == stringValue
) return m_string
.length() <= n
.String().length();
292 if (type
== emptyValue
&& n
.Type() == emptyValue
) return true;
297 bool Value::operator>(const Value
& n
) const
299 if (type
== boolValue
&& n
.Type() == boolValue
) return m_bool
> n
.Bool();
300 if (type
== numberValue
&& n
.Type() == numberValue
) return m_double
> n
.Number();
301 if (type
== stringValue
&& n
.Type() == stringValue
) return m_string
.length() > n
.String().length();
302 // if (type == emptyValue && n.Type() == emptyValue) return false;
307 bool Value::operator>=(const Value
& n
) const
309 if (type
== boolValue
&& n
.Type() == boolValue
) return m_bool
>= n
.Bool();
310 if (type
== numberValue
&& n
.Type() == numberValue
) return m_double
>= n
.Number();
311 if (type
== stringValue
&& n
.Type() == stringValue
) return m_string
.length() >= n
.String().length();
312 if (type
== emptyValue
&& n
.Type() == emptyValue
) return true;
322 type
= emptyValue
; // init'ed values are empty by default