From f4a94aa8ff7c96046ab73c21c57c539ca8a287a3 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Mon, 18 Aug 2014 01:49:55 +0200 Subject: [PATCH] refs #314, new propertyBy methods (nested indexes) for PQs --- src/blackmisc/pqphysicalquantity.cpp | 60 ++++++++++++++++++++++++++++ src/blackmisc/pqphysicalquantity.h | 19 +++++++++ 2 files changed, 79 insertions(+) diff --git a/src/blackmisc/pqphysicalquantity.cpp b/src/blackmisc/pqphysicalquantity.cpp index a284169af..e15a22cdf 100644 --- a/src/blackmisc/pqphysicalquantity.cpp +++ b/src/blackmisc/pqphysicalquantity.cpp @@ -310,6 +310,66 @@ namespace BlackMisc this->m_value = json.value("value").toDouble(); } + /* + * Property + */ + template QVariant CPhysicalQuantity::propertyByIndex(const CPropertyIndex &index) const + { + if (index.isMyself()) { return this->toQVariant(); } + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexValue: + return QVariant(this->m_value); + case IndexUnit: + return this->m_unit.toQVariant(); + case IndexValueRounded0DigitsWithUnit: + return QVariant(this->valueRoundedWithUnit(0)); + case IndexValueRounded1DigitsWithUnit: + return QVariant(this->valueRoundedWithUnit(1)); + case IndexValueRounded2DigitsWithUnit: + return QVariant(this->valueRoundedWithUnit(2)); + case IndexValueRounded3DigitsWithUnit: + return QVariant(this->valueRoundedWithUnit(3)); + case IndexValueRounded6DigitsWithUnit: + return QVariant(this->valueRoundedWithUnit(6)); + default: + return CValueObject::propertyByIndex(index); + } + } + + /* + * Property + */ + template void CPhysicalQuantity::setPropertyByIndex(const QVariant &variant, const CPropertyIndex &index) + { + if (index.isMyself()) + { + this->fromQVariant(variant); + return; + } + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexValue: + this->m_value = variant.toDouble(); + break; + case IndexUnit: + this->m_unit.fromQVariant(variant); + break; + case IndexValueRounded0DigitsWithUnit: + case IndexValueRounded1DigitsWithUnit: + case IndexValueRounded2DigitsWithUnit: + case IndexValueRounded3DigitsWithUnit: + case IndexValueRounded6DigitsWithUnit: + this->parseFromString(variant.toString()); + break; + default: + CValueObject::setPropertyByIndex(variant, index); + break; + } + } + /* * metaTypeId */ diff --git a/src/blackmisc/pqphysicalquantity.h b/src/blackmisc/pqphysicalquantity.h index 7f72cf4ba..c9936582d 100644 --- a/src/blackmisc/pqphysicalquantity.h +++ b/src/blackmisc/pqphysicalquantity.h @@ -14,6 +14,7 @@ #include "blackmisc/pqbase.h" #include "blackmisc/pqunits.h" +#include "blackmisc/propertyindex.h" #include "blackmisc/mathematics.h" #include "blackmisc/pqstring.h" #include @@ -32,6 +33,18 @@ namespace BlackMisc template class CPhysicalQuantity : public BlackMisc::CValueObject { public: + //! Index + enum ColumnIndex + { + IndexUnit = BlackMisc::CPropertyIndex::GlobalIndexCPhysicalQuantity, + IndexValue, + IndexValueRounded0DigitsWithUnit, + IndexValueRounded1DigitsWithUnit, + IndexValueRounded2DigitsWithUnit, + IndexValueRounded3DigitsWithUnit, + IndexValueRounded6DigitsWithUnit + }; + //! Virtual destructor virtual ~CPhysicalQuantity() {} @@ -210,6 +223,12 @@ namespace BlackMisc *this = CPqString::parse(value, CPqString::SeparatorsCLocale); } + //! \copydoc CValueObject::propertyByIndex(int) + virtual QVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const override; + + //! \copydoc CValueObject::setPropertyByIndex(const QVariant, int) + virtual void setPropertyByIndex(const QVariant &variant, const BlackMisc::CPropertyIndex &index) override; + //! Register metadata of unit and quantity static void registerMetadata();