File indexing completed on 2024-12-01 06:36:21
0001 /* 0002 SPDX-FileCopyrightText: 2003-2006 Cies Breijs <cies AT kde DOT nl> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "value.h" 0008 0009 #include <QDebug> 0010 //#include <QLocale> 0011 0012 #include "translator.h" // for the boolean (true and false) to string translation 0013 0014 0015 Value::Value() 0016 { 0017 init(); 0018 } 0019 0020 Value::Value(Value* n) : 0021 m_type(n->type()), 0022 m_bool(n->boolean()), 0023 m_double(n->number()), 0024 m_string(n->string()) 0025 { 0026 } 0027 0028 0029 void Value::setType(int newType) // maybe someday we have to do some type casting logic here 0030 { 0031 if (m_type == newType) { 0032 return; // don't change values when type is not changing 0033 } else { 0034 switch (newType) 0035 { 0036 case Value::Bool: 0037 init(); 0038 m_type = newType; 0039 break; 0040 0041 case Value::Number: 0042 init(); 0043 m_type = newType; 0044 break; 0045 0046 case Value::String: 0047 init(); 0048 m_type = newType; 0049 break; 0050 0051 case Value::Empty: 0052 init(); 0053 break; 0054 } 0055 } 0056 } 0057 0058 0059 bool Value::boolean() const 0060 { 0061 switch (m_type) { 0062 case Value::Bool: 0063 return m_bool; 0064 case Value::Empty: 0065 return false; 0066 case Value::Number: 0067 return (m_double==0 ? false : true); 0068 } 0069 return true; // numbers and strings 0070 } 0071 0072 void Value::setBool(bool b) 0073 { 0074 m_type = Value::Bool; 0075 m_bool = b; 0076 } 0077 0078 0079 double Value::number() const 0080 { 0081 switch (m_type) { 0082 case Value::Bool: 0083 return (m_bool ? 1 : 0); 0084 0085 case Value::Number: 0086 return m_double; 0087 0088 case Value::String: 0089 bool ok = true; 0090 double num = m_string.toDouble(&ok); 0091 if (ok) return num; 0092 } 0093 return 0; // Value::String, Value::Empty 0094 } 0095 0096 void Value::setNumber(double d) 0097 { 0098 m_type = Value::Number; 0099 m_double = d; 0100 } 0101 0102 bool Value::setNumber(const QString &s) 0103 { 0104 m_type = Value::Number; 0105 bool ok = true; 0106 double num = s.toDouble(&ok); 0107 if (ok) { 0108 m_double = num; 0109 return true; 0110 } 0111 m_double = 0; 0112 return false; 0113 } 0114 0115 0116 QString Value::string() const 0117 { 0118 if (m_type == Value::Bool) { 0119 if (m_bool) 0120 return QString(Translator::instance()->default2localized(QStringLiteral("true"))); 0121 else 0122 return QString(Translator::instance()->default2localized(QStringLiteral("false"))); 0123 } else if (m_type == Value::Number) { 0124 QString s; 0125 s.setNum(m_double); 0126 return s; 0127 } 0128 return m_string; // Value::String, Value::Empty 0129 } 0130 0131 void Value::setString(double d) 0132 { 0133 m_type = Value::String; 0134 m_double = d; 0135 m_string.setNum(d); 0136 } 0137 0138 void Value::setString(const QString &s) 0139 { 0140 m_type = Value::String; 0141 m_string = s; 0142 } 0143 0144 0145 0146 Value& Value::operator=(Value* n) 0147 { 0148 switch (n->type()) { 0149 case Value::Bool: 0150 setBool(n->boolean()); 0151 break; 0152 0153 case Value::Number: 0154 setNumber(n->number()); 0155 break; 0156 0157 case Value::String: 0158 setString(n->string()); 0159 break; 0160 0161 case Value::Empty: 0162 init(); 0163 break; 0164 0165 default: 0166 init(); 0167 break; 0168 } 0169 return *this; 0170 } 0171 0172 0173 Value& Value::operator=(const QString& s) 0174 { 0175 setString(s); 0176 return *this; 0177 } 0178 0179 Value& Value::operator=(double n) 0180 { 0181 setNumber(n); 0182 return *this; 0183 } 0184 0185 0186 0187 Value& Value::operator+(Value* n) 0188 { 0189 if (m_type == Value::Number && n->type() == Value::Number) { 0190 m_double += n->number(); 0191 } else { 0192 m_type = Value::String; 0193 m_string = string() + n->string(); 0194 } 0195 return *this; 0196 } 0197 0198 0199 Value& Value::operator-(Value* n) 0200 { 0201 if (m_type == Value::Number && n->type() == Value::Number) { 0202 m_double -= n->number(); 0203 } else { 0204 //qDebug() << "cannot subtract strings" << endl; 0205 } 0206 return *this; 0207 } 0208 0209 0210 Value& Value::operator*(Value* n) 0211 { 0212 if (m_type == Value::Number && n->type() == Value::Number) { 0213 m_double *= n->number(); 0214 } else { 0215 //qDebug() << "cannot multiply strings" << endl; 0216 } 0217 return *this; 0218 } 0219 0220 0221 Value& Value::operator/(Value* n) 0222 { 0223 if (m_type == Value::Number && n->type() == Value::Number) { 0224 m_double /= n->number(); 0225 } else { 0226 //qDebug() << "cannot divide strings" << endl; 0227 } 0228 return *this; 0229 } 0230 0231 0232 0233 bool Value::operator==(Value* n) const 0234 { 0235 if (m_type == Value::Bool && n->type() == Value::Bool) return m_bool == n->boolean(); 0236 if (m_type == Value::Number && n->type() == Value::Number) return m_double == n->number(); 0237 if (m_type == Value::String && n->type() == Value::String) return m_string == n->string(); 0238 if (m_type == Value::Empty && n->type() == Value::Empty) return true; 0239 return false; 0240 } 0241 0242 0243 bool Value::operator!=(Value* n) const 0244 { 0245 if (m_type == Value::Bool && n->type() == Value::Bool) return m_bool != n->boolean(); 0246 if (m_type == Value::Number && n->type() == Value::Number) return m_double != n->number(); 0247 if (m_type == Value::String && n->type() == Value::String) return m_string != n->string(); 0248 // if (m_type == Value::Empty && n->type() == Value::Empty) return false; 0249 return false; 0250 } 0251 0252 0253 bool Value::operator<(Value* n) const 0254 { 0255 if (m_type == Value::Bool && n->type() == Value::Bool) return m_bool < n->boolean(); 0256 if (m_type == Value::Number && n->type() == Value::Number) return m_double < n->number(); 0257 if (m_type == Value::String && n->type() == Value::String) return m_string.length() < n->string().length(); 0258 // if (m_type == Value::Empty && n->type() == Value::Empty) return false; 0259 return false; 0260 } 0261 0262 0263 bool Value::operator<=(Value* n) const 0264 { 0265 if (m_type == Value::Bool && n->type() == Value::Bool) return m_bool <= n->boolean(); 0266 if (m_type == Value::Number && n->type() == Value::Number) return m_double <= n->number(); 0267 if (m_type == Value::String && n->type() == Value::String) return m_string.length() <= n->string().length(); 0268 if (m_type == Value::Empty && n->type() == Value::Empty) return true; 0269 return false; 0270 } 0271 0272 0273 bool Value::operator>(Value* n) const 0274 { 0275 if (m_type == Value::Bool && n->type() == Value::Bool) return m_bool > n->boolean(); 0276 if (m_type == Value::Number && n->type() == Value::Number) return m_double > n->number(); 0277 if (m_type == Value::String && n->type() == Value::String) return m_string.length() > n->string().length(); 0278 // if (m_type == Value::Empty && n->type() == Value::Empty) return false; 0279 return false; 0280 } 0281 0282 0283 bool Value::operator>=(Value* n) const 0284 { 0285 if (m_type == Value::Bool && n->type() == Value::Bool) return m_bool >= n->boolean(); 0286 if (m_type == Value::Number && n->type() == Value::Number) return m_double >= n->number(); 0287 if (m_type == Value::String && n->type() == Value::String) return m_string.length() >= n->string().length(); 0288 if (m_type == Value::Empty && n->type() == Value::Empty) return true; 0289 return false; 0290 } 0291 0292 0293 0294 // private 0295 0296 void Value::init() 0297 { 0298 m_type = Value::Empty; // init'ed values are empty by default 0299 m_bool = false; 0300 m_double = 0; 0301 m_string = QLatin1String(""); 0302 } 0303 0304