moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kturtle / src / value.cpp
blobaadcdb4b6b43c122f130a8b6a1300d578ae77336
1 /*
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.
19 #include <kdebug.h>
20 #include <klocale.h>
22 #include "value.h"
25 Value::Value()
27 init();
30 Value::Value(const Value& n)
32 *this = n;
37 int Value::Type() const
39 return type;
42 void Value::setType(int newType)
44 if (type == newType) return; // dont change values when type is not changing
45 else if (newType == boolValue)
47 init();
48 type = newType;
49 m_string = i18n("false");
51 else if (newType == numberValue)
53 init();
54 type = newType;
56 else if (newType == stringValue)
58 init();
59 type = newType;
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);
69 return false;
72 void Value::setBool(bool b)
74 type = boolValue;
75 m_bool = b;
76 if (m_bool)
78 m_double = 1;
79 m_string = i18n("true");
81 else
83 m_double = 0;
84 m_string = i18n("false");
89 double Value::Number() const
91 if (type == boolValue)
93 if (m_bool) return 1;
94 else return 0;
96 else if (type == numberValue) return m_double;
97 return 0; // stringValue, emptyValue
100 void Value::setNumber(double d)
102 type = numberValue;
103 m_double = d;
104 m_string.setNum(d);
107 bool Value::setNumber(const QString &s)
109 type = numberValue;
110 bool ok = true;
111 double num = s.toDouble(&ok);
112 if (ok)
114 m_double = num;
115 m_string.setNum(num);
116 return true;
118 return false;
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)
131 QString s;
132 s.setNum(m_double);
133 return s;
135 return m_string; // stringValue, emptyValue
138 void Value::setString(double d)
140 type = stringValue;
141 m_double = d;
142 m_string.setNum(d);
145 void Value::setString(const QString &s)
147 type = stringValue;
148 m_string = s;
153 Value& Value::operator=(const Value& n)
155 if (this != &n)
157 if (type == n.type)
159 type = n.Type();
160 m_bool = n.Bool();
161 m_double = n.Number();
162 m_string = n.String();
164 else if (n.Type() == boolValue)
166 setBool( n.Bool() );
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)
178 init();
181 return *this;
185 Value& Value::operator=(const QString& s)
187 setString(s);
188 return *this;
191 Value& Value::operator=(double n)
193 setNumber(n);
194 return *this;
199 Value& Value::operator+(const Value& n)
201 if (type == numberValue && n.Type() == numberValue)
203 m_double += n.Number();
205 else
207 type = stringValue;
208 m_string = String() + n.String();
210 return *this;
214 Value& Value::operator-(const Value& n)
216 if (type == numberValue && n.Type() == numberValue)
218 m_double -= n.Number();
220 else
222 kdDebug(0)<<"Value::operator; cannot subtract strings"<<endl;
224 return *this;
228 Value& Value::operator*(const Value& n)
230 if (type == numberValue && n.Type() == numberValue)
232 m_double *= n.Number();
234 else
236 kdDebug(0)<<"Value::operator; cannot multiply strings"<<endl;
238 return *this;
242 Value& Value::operator/(const Value& n)
244 if (type == numberValue && n.Type() == numberValue)
246 m_double /= n.Number();
248 else
250 kdDebug(0)<<"Value::operator; cannot divide strings"<<endl;
252 return *this;
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;
263 return false;
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;
273 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;
283 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;
293 return false;
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;
303 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;
313 return false;
318 // private
320 void Value::init()
322 type = emptyValue; // init'ed values are empty by default
323 m_bool = false;
324 m_double = 0;
325 m_string = "";