Changeset View
Changeset View
Standalone View
Standalone View
runners/converter/converterrunner.cpp
Show First 20 Lines • Show All 72 Lines • ▼ Show 20 Line(s) | 47 | { | |||
---|---|---|---|---|---|
73 | return result; | 73 | return result; | ||
74 | } | 74 | } | ||
75 | 75 | | |||
76 | bool isNumber(const QChar &ch) | 76 | bool isNumber(const QChar &ch) | ||
77 | { | 77 | { | ||
78 | if (ch.isNumber()) { | 78 | if (ch.isNumber()) { | ||
79 | return true; | 79 | return true; | ||
80 | } | 80 | } | ||
81 | if (QStringLiteral(".,-+").contains(ch)) { | 81 | if (QStringLiteral(".,-+/").contains(ch)) { | ||
82 | return true; | 82 | return true; | ||
83 | } | 83 | } | ||
84 | return false; | 84 | return false; | ||
85 | } | 85 | } | ||
86 | 86 | | |||
87 | QString rest() | 87 | QString rest() | ||
88 | { | 88 | { | ||
89 | return m_s.mid(m_index).simplified(); | 89 | return m_s.mid(m_index).simplified(); | ||
▲ Show 20 Lines • Show All 147 Lines • ▼ Show 20 Line(s) | 236 | if (localCurrency.isValid() && !units.contains(localCurrency)) { | |||
237 | units << localCurrency; | 237 | units << localCurrency; | ||
238 | } | 238 | } | ||
239 | } | 239 | } | ||
240 | } | 240 | } | ||
241 | 241 | | |||
242 | QList<Plasma::QueryMatch> matches; | 242 | QList<Plasma::QueryMatch> matches; | ||
243 | 243 | | |||
244 | QLocale locale; | 244 | QLocale locale; | ||
245 | auto stringToDouble = [&locale](const QStringRef &value, bool *ok) { | ||||
246 | double numberValue = locale.toDouble(value, ok); | ||||
247 | if (!(*ok)) { | ||||
248 | numberValue = value.toDouble(ok); | ||||
249 | } | ||||
250 | return numberValue; | ||||
251 | }; | ||||
252 | | ||||
245 | KUnitConversion::Unit u1 = category.unit(unit1); | 253 | KUnitConversion::Unit u1 = category.unit(unit1); | ||
246 | foreach (const KUnitConversion::Unit& u, units) { | 254 | foreach (const KUnitConversion::Unit& u, units) { | ||
247 | if (u1 == u) { | 255 | if (u1 == u) { | ||
248 | continue; | 256 | continue; | ||
249 | } | 257 | } | ||
250 | 258 | | |||
259 | double numberValue = 0.0; | ||||
260 | | ||||
261 | const auto fractionParts = value.splitRef(QLatin1Char('/'), QString::SkipEmptyParts); | ||||
262 | if (fractionParts.isEmpty() || fractionParts.count() > 2) { | ||||
263 | continue; | ||||
264 | } | ||||
265 | | ||||
266 | if (fractionParts.count() == 2) { | ||||
251 | bool ok; | 267 | bool ok; | ||
252 | double numberValue = locale.toDouble(value, &ok); | 268 | const double numerator = stringToDouble(fractionParts.first(), &ok); | ||
253 | if (!ok) { | 269 | if (!ok) { | ||
254 | numberValue = value.toDouble(&ok); | 270 | continue; | ||
271 | } | ||||
272 | const double denominator = stringToDouble(fractionParts.last(), &ok); | ||||
273 | if (!ok || qFuzzyIsNull(denominator)) { | ||||
274 | continue; | ||||
275 | } | ||||
276 | | ||||
277 | numberValue = numerator / denominator; | ||||
278 | } else if (fractionParts.count() == 1) { | ||||
279 | bool ok; | ||||
280 | numberValue = stringToDouble(fractionParts.first(), &ok); | ||||
255 | if (!ok) { | 281 | if (!ok) { | ||
256 | continue; | 282 | continue; | ||
257 | } | 283 | } | ||
258 | } | 284 | } | ||
285 | | ||||
259 | KUnitConversion::Value v = category.convert(KUnitConversion::Value(numberValue, u1), u); | 286 | KUnitConversion::Value v = category.convert(KUnitConversion::Value(numberValue, u1), u); | ||
260 | 287 | | |||
261 | if (!v.isValid()) { | 288 | if (!v.isValid()) { | ||
262 | continue; | 289 | continue; | ||
263 | } | 290 | } | ||
264 | 291 | | |||
265 | Plasma::QueryMatch match(this); | 292 | Plasma::QueryMatch match(this); | ||
266 | match.setType(Plasma::QueryMatch::InformationalMatch); | 293 | match.setType(Plasma::QueryMatch::InformationalMatch); | ||
Show All 22 Lines |