/* Copyright (C) 2013 * swift project Community / Contributors * * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, * including this file, may be copied, modified, propagated, or distributed except according to the terms * contained in the LICENSE file. */ #include "pqstring.h" #include "tuple.h" #include "pqallquantities.h" namespace BlackMisc { namespace PhysicalQuantities { /* * Convert to string */ QString CPqString::convertToQString(bool /** i18n **/) const { return this->m_string; } /* * is a */ bool CPqString::isA(int metaTypeId) const { if (metaTypeId == qMetaTypeId()) { return true; } return this->CValueObject::isA(metaTypeId); } /* * Meta id */ int CPqString::getMetaTypeId() const { return qMetaTypeId(); } /* * Compare */ int CPqString::compareImpl(const CValueObject &otherBase) const { const auto &other = static_cast(otherBase); return compare(TupleConverter::toTuple(*this), TupleConverter::toTuple(other)); } /* * Marshall to DBus */ void CPqString::marshallToDbus(QDBusArgument &argument) const { argument << TupleConverter::toTuple(*this); } /* * Unmarshall from DBus */ void CPqString::unmarshallFromDbus(const QDBusArgument &argument) { argument >> TupleConverter::toTuple(*this); } /* * Equal? */ bool CPqString::operator ==(const CPqString &other) const { if (this == &other) return true; return TupleConverter::toTuple(*this) == TupleConverter::toTuple(other); } /* * Unequal? */ bool CPqString::operator !=(const CPqString &other) const { return !((*this) == other); } /* * Hash */ uint CPqString::getValueHash() const { return qHash(TupleConverter::toTuple(*this)); } /* * Register metadata */ void CPqString::registerMetadata() { qRegisterMetaType(); qDBusRegisterMetaType(); } /* * Parse */ QVariant CPqString::parseToVariant(const QString &value, SeparatorMode mode) { static QRegExp rx("([-+]?[0-9]*[\\.,]?[0-9]+)\\s*(\\D*)$"); QVariant v; // fine tuning of the string QString vs = value.trimmed().simplified(); // check if (vs.isEmpty()) return v; if (rx.indexIn(value) < 0) return v; // not a valid number QString unit = rx.cap(2).trimmed(); QString number = QString(value).replace(unit, ""); unit = unit.trimmed(); // trim after replace, not before if (unit.isEmpty() || number.isEmpty()) return v; bool success; double numberD; switch (mode) { case SeparatorsLocale: numberD = QLocale::system().toDouble(number, &success); break; case SeparatorsCLocale: numberD = number.toDouble(&success); break; case SeparatorsBestGuess: numberD = number.toDouble(&success); if (!success) numberD = QLocale::system().toDouble(number, &success); break; default: qFatal("Wrong mode"); break; } if (!success) return v; if (CMeasurementUnit::isValidUnitSymbol(unit)) { CAcceleration pq(numberD, CMeasurementUnit::unitFromSymbol(unit, false)); return pq.toQVariant(); } if (CMeasurementUnit::isValidUnitSymbol(unit)) { CAngle pq(numberD, CMeasurementUnit::unitFromSymbol(unit, false)); return pq.toQVariant(); } if (CMeasurementUnit::isValidUnitSymbol(unit)) { CFrequency pq(numberD, CMeasurementUnit::unitFromSymbol(unit, false)); return pq.toQVariant(); } if (CMeasurementUnit::isValidUnitSymbol(unit)) { CLength pq(numberD, CMeasurementUnit::unitFromSymbol(unit, false)); return pq.toQVariant(); } if (CMeasurementUnit::isValidUnitSymbol(unit)) { CMass pq(numberD, CMeasurementUnit::unitFromSymbol(unit, false)); return pq.toQVariant(); } if (CMeasurementUnit::isValidUnitSymbol(unit)) { CPressure pq(numberD, CMeasurementUnit::unitFromSymbol(unit, false)); return pq.toQVariant(); } if (CMeasurementUnit::isValidUnitSymbol(unit)) { CSpeed pq(numberD, CMeasurementUnit::unitFromSymbol(unit, false)); return pq.toQVariant(); } if (CMeasurementUnit::isValidUnitSymbol(unit)) { CTime pq(numberD, CMeasurementUnit::unitFromSymbol(unit, false)); return pq.toQVariant(); } if (CMeasurementUnit::isValidUnitSymbol(unit)) { CTemperature pq(numberD, CMeasurementUnit::unitFromSymbol(unit, false)); return pq.toQVariant(); } return v; } } // namespace } // namespace