From 9b031bb58285f20e0493da38f0da67f586bf89f6 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 15 Apr 2020 05:28:34 +0200 Subject: [PATCH] [PQ] Allow to provide a default unit for parsing, e.g. "0" -> "0ft" --- src/blackmisc/pq/physicalquantity.cpp | 17 ++++++++++++++++- src/blackmisc/pq/physicalquantity.h | 9 ++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/blackmisc/pq/physicalquantity.cpp b/src/blackmisc/pq/physicalquantity.cpp index 776f07323..565e4dc53 100644 --- a/src/blackmisc/pq/physicalquantity.cpp +++ b/src/blackmisc/pq/physicalquantity.cpp @@ -21,6 +21,7 @@ #include "blackmisc/pq/acceleration.h" #include "blackmisc/propertyindexvariantmap.h" #include "blackmisc/comparefunctions.h" +#include "blackmisc/stringutils.h" #include "blackmisc/dictionary.h" #include "blackmisc/variant.h" #include "blackmisc/verify.h" @@ -419,7 +420,7 @@ namespace BlackMisc // there is no double qHash // also unit and rounding has to be considered hashs << qHash(this->valueRoundedWithUnit(MU::defaultUnit())); - return BlackMisc::calculateHash(hashs, "PQ"); + return calculateHash(hashs, "PQ"); } template @@ -449,6 +450,20 @@ namespace BlackMisc *this = CPqString::parse(value, mode); } + template + void CPhysicalQuantity::parseFromString(const QString &value, CPqString::SeparatorMode mode, const MU &defaultUnitIfMissing) + { + if (is09OnlyString(value)) + { + const QString v = value + defaultUnitIfMissing.getName(); + this->parseFromString(v, mode); + } + else + { + this->parseFromString(value, mode); + } + } + template void CPhysicalQuantity::parseFromString(const QString &value) { diff --git a/src/blackmisc/pq/physicalquantity.h b/src/blackmisc/pq/physicalquantity.h index 1a2b0e658..b7b8d1380 100644 --- a/src/blackmisc/pq/physicalquantity.h +++ b/src/blackmisc/pq/physicalquantity.h @@ -47,7 +47,7 @@ namespace BlackMisc */ template class CPhysicalQuantity : public Mixin::DBusOperators>, - public Mixin::DataStreamOperators>, + public Mixin::DataStreamOperators>, public Mixin::JsonOperators>, public Mixin::Index, public Mixin::MetaType, @@ -255,11 +255,14 @@ namespace BlackMisc //! \copydoc BlackMisc::Mixin::String::toQString QString convertToQString(bool i18n = false) const; + //! Parse value from string + void parseFromString(const QString &value); + //! Parse to string, with specified separator void parseFromString(const QString &value, CPqString::SeparatorMode mode); - //! Parse value from string - void parseFromString(const QString &value); + //! Parse to string, with specified separator + void parseFromString(const QString &value, CPqString::SeparatorMode mode, const MU &defaultUnitIfMissing); //! Compare with other PQ int compare(const PQ &other) const { return compareImpl(*this->derived(), other); }