From de3cb717060dfe2d3e50776f4d70118dde097fc3 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 10 Jun 2018 02:10:15 +0200 Subject: [PATCH] Ref T275, general improvement in value object added comparePropertyByIndex --- src/blackmisc/geo/coordinategeodetic.cpp | 12 +++++ src/blackmisc/geo/coordinategeodetic.h | 3 ++ src/blackmisc/propertyindexvariantmap.cpp | 30 +++++------ src/blackmisc/propertyindexvariantmap.h | 61 +++++++++++++++-------- src/blackmisc/valueobject.h | 4 +- 5 files changed, 74 insertions(+), 36 deletions(-) diff --git a/src/blackmisc/geo/coordinategeodetic.cpp b/src/blackmisc/geo/coordinategeodetic.cpp index 4b4934f77..67bb3ae52 100644 --- a/src/blackmisc/geo/coordinategeodetic.cpp +++ b/src/blackmisc/geo/coordinategeodetic.cpp @@ -185,6 +185,18 @@ namespace BlackMisc } } + int CCoordinateGeodetic::comparePropertyByIndex(const CPropertyIndex &index, const CCoordinateGeodetic &compareValue) const + { + if (ICoordinateGeodetic::canHandleIndex(index)) + { + return ICoordinateGeodetic::comparePropertyByIndex(index, compareValue); + } + else + { + return CValueObject::comparePropertyByIndex(index, compareValue); + } + } + CCoordinateGeodetic::CCoordinateGeodetic(const std::array &normalVector) { this->setNormalVector(normalVector); diff --git a/src/blackmisc/geo/coordinategeodetic.h b/src/blackmisc/geo/coordinategeodetic.h index 1734ac631..f07588a16 100644 --- a/src/blackmisc/geo/coordinategeodetic.h +++ b/src/blackmisc/geo/coordinategeodetic.h @@ -244,6 +244,9 @@ namespace BlackMisc //! \copydoc Mixin::Index::setPropertyByIndex void setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant); + //! \copydoc Mixin::Index::setPropertyByIndex + int comparePropertyByIndex(const CPropertyIndex &index, const CCoordinateGeodetic &compareValue) const; + //! Switch unit of height CCoordinateGeodetic &switchUnit(const PhysicalQuantities::CLengthUnit &unit); diff --git a/src/blackmisc/propertyindexvariantmap.cpp b/src/blackmisc/propertyindexvariantmap.cpp index f87e6eb74..5a4e78897 100644 --- a/src/blackmisc/propertyindexvariantmap.cpp +++ b/src/blackmisc/propertyindexvariantmap.cpp @@ -37,13 +37,13 @@ namespace BlackMisc bool CPropertyIndexVariantMap::matchesVariant(const CVariant &variant) const { - if (this->isEmpty()) return this->isWildcard(); + if (this->isEmpty()) { return this->isWildcard(); } const auto &map = this->map(); for (auto it = map.begin(); it != map.end(); ++it) { // QVariant cannot be compared directly - CVariant p = variant.propertyByIndex(it.key()); // from value object - CVariant v = it.value(); // from map + const CVariant p = variant.propertyByIndex(it.key()); // from value object + const CVariant v = it.value(); // from map if (p != v) return false; } return true; @@ -51,14 +51,14 @@ namespace BlackMisc QString CPropertyIndexVariantMap::convertToQString(bool i18n) const { - if (this->isEmpty()) return QString("{wildcard: %1}").arg(this->m_wildcard ? "true" : "false"); + if (this->isEmpty()) return QString("{wildcard: %1}").arg(m_wildcard ? "true" : "false"); QString s; - for (const CPropertyIndex &index : makeKeysRange(this->m_values)) + for (const CPropertyIndex &index : makeKeysRange(m_values)) { - CVariant v = this->m_values.value(index); + CVariant v = m_values.value(index); s.isEmpty() ? - s.append("{wildcard: ").append(this->m_wildcard ? "true" : "false").append(" ") : + s.append("{wildcard: ").append(m_wildcard ? "true" : "false").append(" ") : s.append(", "); s.append('{').append(index.toQString(i18n)).append(": "); @@ -73,8 +73,8 @@ namespace BlackMisc void CPropertyIndexVariantMap::marshallToDbus(QDBusArgument &argument) const { - argument << this->m_values.keys(); - argument << this->m_values.values(); + argument << m_values.keys(); + argument << m_values.values(); } void CPropertyIndexVariantMap::unmarshallFromDbus(const QDBusArgument &argument) @@ -90,12 +90,12 @@ namespace BlackMisc newMap.insert(indexes[i], values[i]); } // replace values in one step - this->m_values.swap(newMap); + m_values.swap(newMap); } void CPropertyIndexVariantMap::addValue(const CPropertyIndex &index, const CVariant &value) { - this->m_values.insert(index, value); + m_values.insert(index, value); } void CPropertyIndexVariantMap::addValue(const CPropertyIndex &index, const char *str) @@ -110,19 +110,19 @@ namespace BlackMisc { CPropertyIndex newPi(pi); newPi.prepend(index); - newMap.insert(newPi, this->m_values[pi]); + newMap.insert(newPi, m_values[pi]); } - this->m_values = newMap; + m_values = newMap; } CPropertyIndexList CPropertyIndexVariantMap::indexes() const { - return CSequence(this->m_values.keys()); + return CSequence(m_values.keys()); } int CPropertyIndexVariantMap::size() const { - return this->m_values.size(); + return m_values.size(); } uint CPropertyIndexVariantMap::getValueHash() const diff --git a/src/blackmisc/propertyindexvariantmap.h b/src/blackmisc/propertyindexvariantmap.h index f37d1e360..2767c1a9a 100644 --- a/src/blackmisc/propertyindexvariantmap.h +++ b/src/blackmisc/propertyindexvariantmap.h @@ -58,17 +58,20 @@ namespace BlackMisc //! Update by variant map //! \return number of values changed, with skipEqualValues equal values will not be changed - CPropertyIndexList apply(const BlackMisc::CPropertyIndexVariantMap &indexMap, bool skipEqualValues = false); + CPropertyIndexList apply(const CPropertyIndexVariantMap &indexMap, bool skipEqualValues = false); //! Set property by index void setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant); //! Property by index - CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const; + CVariant propertyByIndex(const CPropertyIndex &index) const; //! Property by index as String QString propertyByIndexAsString(const CPropertyIndex &index, bool i18n = false) const; + //! Compare for index + int comparePropertyByIndex(const CPropertyIndex &index, const Derived &compareValue) const; + //! Is given variant equal to value of property index? bool equalsPropertyByIndex(const CVariant &compareValue, const CPropertyIndex &index) const; @@ -106,12 +109,15 @@ namespace BlackMisc * When a derived class and a base class both inherit from Mixin::Index, * the derived class uses this macro to disambiguate the inherited members. */ + // *INDENT-OFF* # define BLACKMISC_DECLARE_USING_MIXIN_INDEX(DERIVED) \ using ::BlackMisc::Mixin::Index::apply; \ using ::BlackMisc::Mixin::Index::setPropertyByIndex; \ using ::BlackMisc::Mixin::Index::propertyByIndex; \ using ::BlackMisc::Mixin::Index::propertyByIndexAsString; \ + using ::BlackMisc::Mixin::Index::comparePropertyByIndex; \ using ::BlackMisc::Mixin::Index::equalsPropertyByIndex; + // *INDENT-ON* } // Mixin @@ -142,37 +148,37 @@ namespace BlackMisc void addValue(const CPropertyIndex &index, const char *str); //! Add a value as non CVariant - template void addValue(const CPropertyIndex &index, const T &value) { this->m_values.insert(index, CVariant::fromValue(value)); } + template void addValue(const CPropertyIndex &index, const T &value) { m_values.insert(index, CVariant::fromValue(value)); } //! Prepend index to all property indexes void prependIndex(int index); //! Is empty? - bool isEmpty() const { return this->m_values.isEmpty(); } + bool isEmpty() const { return m_values.isEmpty(); } //! Value - CVariant value(const CPropertyIndex &index) const { return this->m_values.value(index); } + CVariant value(const CPropertyIndex &index) const { return m_values.value(index); } //! Set value - void value(const CPropertyIndex &index, const CVariant &value) { this->m_values.value(index, value); } + void value(const CPropertyIndex &index, const CVariant &value) { m_values.value(index, value); } //! Indexes CPropertyIndexList indexes() const; //! Contains index? - bool contains(const CPropertyIndex &index) const { return this->m_values.contains(index); } + bool contains(const CPropertyIndex &index) const { return m_values.contains(index); } //! values - QList values() const { return this->m_values.values(); } + QList values() const { return m_values.values(); } //! Wildcard, only relevant when used in search - bool isWildcard() const { return this->m_wildcard; } + bool isWildcard() const { return m_wildcard; } //! Wildcard, only relevant when used in search - void setWildcard(bool wildcard) { this->m_wildcard = wildcard; } + void setWildcard(bool wildcard) { m_wildcard = wildcard; } //! clear - void clear() { this->m_values.clear(); } + void clear() { m_values.clear(); } //! Number of elements int size() const; @@ -190,7 +196,7 @@ namespace BlackMisc template bool matches(const T &value) const { return matchesVariant(CVariant::from(value)); } //! Map - const QMap &map() const { return this->m_values; } + const QMap &map() const { return m_values; } //! Hash value uint getValueHash() const; @@ -211,7 +217,6 @@ namespace BlackMisc //! \copydoc BlackMisc::Mixin::DBusByMetaClass::unmarshallFromDbus void unmarshallFromDbus(const QDBusArgument &argument); - }; namespace Mixin @@ -229,7 +234,7 @@ namespace BlackMisc const CPropertyIndex index = it.key(); if (skipEqualValues) { - bool equal = derived()->equalsPropertyByIndex(value, index); + const bool equal = derived()->equalsPropertyByIndex(value, index); if (equal) { continue; } } derived()->setPropertyByIndex(index, value); @@ -237,6 +242,7 @@ namespace BlackMisc } return changed; } + template void Index::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant) { @@ -249,13 +255,11 @@ namespace BlackMisc baseSetPropertyByIndex(static_cast *>(derived()), variant, index); } } + template CVariant Index::propertyByIndex(const CPropertyIndex &index) const { - if (index.isMyself()) - { - return myself(); - } + if (index.isMyself()) { return myself(); } const auto i = index.frontCasted(); switch (i) { @@ -265,16 +269,34 @@ namespace BlackMisc default: return basePropertyByIndex(static_cast *>(derived()), index); } } + template QString Index::propertyByIndexAsString(const CPropertyIndex &index, bool i18n) const { return derived()->propertyByIndex(index).toQString(i18n); } + template bool Index::equalsPropertyByIndex(const CVariant &compareValue, const CPropertyIndex &index) const { return derived()->propertyByIndex(index) == compareValue; } + + template + int Index::comparePropertyByIndex(const CPropertyIndex &index, const Derived &compareValue) const + { + if (this == &compareValue) { return 0; } + const auto i = index.frontCasted(); + switch (i) + { + case IndexIcon: + case IndexPixmap: + case IndexString: + default: + break; + } + return derived()->toQString().compare(compareValue.toQString()); + } } // Mixin template @@ -282,8 +304,7 @@ namespace BlackMisc { return m_map.matches(value); } - -} +} // ns Q_DECLARE_METATYPE(BlackMisc::CPropertyIndexVariantMap) diff --git a/src/blackmisc/valueobject.h b/src/blackmisc/valueobject.h index c4e02ab05..cd48e8857 100644 --- a/src/blackmisc/valueobject.h +++ b/src/blackmisc/valueobject.h @@ -37,7 +37,6 @@ namespace BlackMisc { - /*! * Default base class for CValueObject. */ @@ -113,6 +112,9 @@ namespace BlackMisc //! \copydoc BlackMisc::Mixin::Index::propertyByIndex using Mixin::Index::propertyByIndex; + //! \copydoc BlackMisc::Mixin::Index::comparePropertyByIndex + using Mixin::Index::comparePropertyByIndex; + //! \copydoc BlackMisc::Mixin::Index::propertyByIndexAsString using Mixin::Index::propertyByIndexAsString;