diff --git a/src/blackmisc/propertyindex.cpp b/src/blackmisc/propertyindex.cpp index 6ca4cd8ec..e3a4ef945 100644 --- a/src/blackmisc/propertyindex.cpp +++ b/src/blackmisc/propertyindex.cpp @@ -8,140 +8,121 @@ */ #include "blackmisc/propertyindex.h" - +#include "blackmisc/verify.h" #include #include namespace BlackMisc { - CPropertyIndex::CPropertyIndex(int singleProperty) + CPropertyIndex::CPropertyIndex(int singleProperty) : m_indexes { singleProperty } { Q_ASSERT(singleProperty >= static_cast(GlobalIndexCValueObject)); - this->setIndexStringByList({singleProperty}); } - CPropertyIndex::CPropertyIndex(std::initializer_list il) - { - this->setIndexStringByList(il); - } + CPropertyIndex::CPropertyIndex(std::initializer_list il) : m_indexes(il) + {} - CPropertyIndex::CPropertyIndex(const QList &indexes) - { - this->setIndexStringByList(indexes); - } + CPropertyIndex::CPropertyIndex(const QVector &indexes) : m_indexes(indexes) + {} - CPropertyIndex::CPropertyIndex(const QString &indexes) : m_indexString(indexes) + CPropertyIndex::CPropertyIndex(const QList &indexes) : m_indexes(indexes.toVector()) + {} + + CPropertyIndex::CPropertyIndex(const QString &indexes) { this->parseFromString(indexes); } CPropertyIndex CPropertyIndex::copyFrontRemoved() const { - Q_ASSERT_X(!this->isEmpty(), Q_FUNC_INFO, "Empty index"); + BLACK_VERIFY_X(!this->isEmpty(), Q_FUNC_INFO, "Empty index"); if (this->isEmpty()) { return CPropertyIndex(); } - const int p = this->m_indexString.indexOf(';'); - if (p < 0) { return CPropertyIndex(); } - return CPropertyIndex(this->m_indexString.mid(p + 1)); + CPropertyIndex copy = *this; + copy.m_indexes.pop_front(); + return copy; } bool CPropertyIndex::isNested() const { - return this->m_indexString.contains(';'); + return m_indexes.size() > 1; } bool CPropertyIndex::isMyself() const { - return this->m_indexString.isEmpty(); + return m_indexes.isEmpty(); } bool CPropertyIndex::isEmpty() const { - return this->m_indexString.isEmpty(); + return m_indexes.isEmpty(); } QString CPropertyIndex::convertToQString(bool i18n) const { Q_UNUSED(i18n); - return this->m_indexString; + QString s; + for (const int i : m_indexes) + { + Q_ASSERT(i >= static_cast(GlobalIndexCValueObject)); + if (!s.isEmpty()) { s.append(";"); } + s.append(QString::number(i)); + } + return s; } void CPropertyIndex::parseFromString(const QString &indexes) { - if (indexes.isEmpty()) - { - this->m_indexString.clear(); - } - else - { - QString is(indexes.simplified().replace(" ", ";").replace(",", ";")); - if (is.endsWith(';')) - { - this->m_indexString = is.left(is.length() - 1); - } - else - { - this->m_indexString = is; - } - } - } - - void CPropertyIndex::setIndexStringByList(const QList &list) - { - QString l; - for (const int &i : list) - { - Q_ASSERT(i >= static_cast(GlobalIndexCValueObject)); - if (!l.isEmpty()) { l.append(";"); } - l.append(QString::number(i)); - } - this->m_indexString = l; - } - - QList CPropertyIndex::indexList() const - { - QList list; - if (this->m_indexString.isEmpty()) { return list; } - QStringList indexes = this->m_indexString.split(';'); - for (const QString &index : indexes) + m_indexes.clear(); + if (indexes.isEmpty()) { return; } + for (const QString &index : indexes.split(';')) { if (index.isEmpty()) { continue; } bool ok; int i = index.toInt(&ok); Q_ASSERT(ok); Q_ASSERT(i >= static_cast(GlobalIndexCValueObject)); - list.append(i); + m_indexes.append(i); } - return list; + } + + QJsonObject CPropertyIndex::toJson() const + { + QJsonObject json; + json.insert(QStringLiteral("indexes"), this->convertToQString()); + return json; + } + + void CPropertyIndex::convertFromJson(const QJsonObject &json) + { + const QJsonValue value = json.value(QLatin1String("indexes")); + if (!value.isString()) { throw CJsonException("'indexes' missing or not a string"); } + this->parseFromString(value.toString()); + } + + QVector CPropertyIndex::indexVector() const + { + return m_indexes; + } + + QList CPropertyIndex::indexList() const + { + return m_indexes.toList(); } void CPropertyIndex::prepend(int newLeftIndex) { - QList newList = indexList(); - newList.prepend(newLeftIndex); - this->setIndexStringByList(newList); + m_indexes.push_front(newLeftIndex); } bool CPropertyIndex::contains(int index) const { - return this->indexList().contains(index); + return m_indexes.contains(index); } int CPropertyIndex::frontToInt() const { Q_ASSERT_X(!this->isEmpty(), Q_FUNC_INFO, "No index"); - int f = -1; - bool ok; - const int p = this->m_indexString.indexOf(';'); - if (p < 0) - { - f = this->m_indexString.toInt(&ok); - } - else - { - f = this->m_indexString.leftRef(p).toInt(&ok); - } - Q_ASSERT_X(ok && f >= 0, Q_FUNC_INFO, "Invalid index"); - return f; + return m_indexes.front(); } bool CPropertyIndex::startsWith(int index) const diff --git a/src/blackmisc/propertyindex.h b/src/blackmisc/propertyindex.h index bf1babbcf..45fb63ff5 100644 --- a/src/blackmisc/propertyindex.h +++ b/src/blackmisc/propertyindex.h @@ -63,7 +63,7 @@ namespace BlackMisc public Mixin::MetaType, public Mixin::HashByMetaClass, public Mixin::DBusByMetaClass, - public Mixin::JsonByMetaClass, + public Mixin::JsonOperators, public Mixin::EqualsByMetaClass, public Mixin::LessThanByMetaClass, public Mixin::CompareByMetaClass, @@ -170,7 +170,10 @@ namespace BlackMisc //! Initializer list constructor CPropertyIndex(std::initializer_list il); - //! Construct from a base class object. + //! Construct from a vector of indexes. + CPropertyIndex(const QVector &indexes); + + //! Construct from a list of indexes. CPropertyIndex(const QList &indexes); //! From string @@ -188,6 +191,9 @@ namespace BlackMisc //! Empty? bool isEmpty() const; + //! Index vector + QVector indexVector() const; + //! Index list QList indexList() const; @@ -237,19 +243,22 @@ namespace BlackMisc //! \copydoc BlackMisc::Mixin::String::toQString QString convertToQString(bool i18n = false) const; + //! \copydoc BlackMisc::CValueObject::toJson + QJsonObject toJson() const; + + //! \copydoc BlackMisc::CValueObject::convertFromJson + void convertFromJson(const QJsonObject &json); + protected: //! Parse indexes from string void parseFromString(const QString &indexes); private: - QString m_indexString; //! I use a little trick here, the string is used with the tupel system, as it provides all operators, hash .. - - //! Convert list to string - void setIndexStringByList(const QList &list); + QVector m_indexes; BLACK_METACLASS( CPropertyIndex, - BLACK_METAMEMBER(indexString) + BLACK_METAMEMBER(indexes) ); }; } //namespace