diff --git a/src/blackmisc/blackmiscfreefunctions.h b/src/blackmisc/blackmiscfreefunctions.h index ac837f54b..f8dd4c680 100644 --- a/src/blackmisc/blackmiscfreefunctions.h +++ b/src/blackmisc/blackmiscfreefunctions.h @@ -111,11 +111,18 @@ namespace BlackMisc //! Checked version from QVariant template void setFromQVariant(T *value, const QVariant &variant) { - Q_ASSERT(variant.canConvert()); if (variant.canConvert()) { (*value) = variant.value(); } + else + { + const QString m = QString("Target type: %1 Variant type: %2 %3 %4"). + arg(qMetaTypeId()). + arg(static_cast(variant.type())).arg(variant.userType()).arg(variant.typeName()); + Q_ASSERT_X(false, "setFromQVariant", m.toLocal8Bit().constData()); + Q_UNUSED(m); + } } /*! diff --git a/src/blackmisc/propertyindex.cpp b/src/blackmisc/propertyindex.cpp index 9268d3e13..ceac5e172 100644 --- a/src/blackmisc/propertyindex.cpp +++ b/src/blackmisc/propertyindex.cpp @@ -13,88 +13,82 @@ namespace BlackMisc { - /* - * Non nested index - */ CPropertyIndex::CPropertyIndex(int singleProperty) { Q_ASSERT(singleProperty >= static_cast(GlobalIndexCValueObject)); - this->m_indexes.append(singleProperty); - this->listToString(); + this->setIndexStringByList({singleProperty}); } - /* - * Construct from initializer list - */ - CPropertyIndex::CPropertyIndex(std::initializer_list il) : - m_indexes(il) + CPropertyIndex::CPropertyIndex(std::initializer_list il) { - this->listToString(); + this->setIndexStringByList(il); } - /* - * Construct from QList - */ - CPropertyIndex::CPropertyIndex(const QList &indexes) : - m_indexes(indexes) + CPropertyIndex::CPropertyIndex(const QList &indexes) { - this->listToString(); + this->setIndexStringByList(indexes); } - /* - * From string - */ CPropertyIndex::CPropertyIndex(const QString &indexes) : m_indexString(indexes) { this->parseFromString(indexes); } - /* - * Current property index, front element removed - */ CPropertyIndex CPropertyIndex::copyFrontRemoved() const { - Q_ASSERT(!this->m_indexes.isEmpty()); - if (this->m_indexes.isEmpty()) return CPropertyIndex(); - QList c = this->m_indexes; - c.removeAt(0); - CPropertyIndex pi(c); + Q_ASSERT(!this->isEmpty()); + if (this->isEmpty()) { return CPropertyIndex(); } + QList l = this->indexList(); + l.removeAt(0); + CPropertyIndex pi(l); return pi; } - /* - * Nested index - */ bool CPropertyIndex::isNested() const { - return this->m_indexes.size() > 1; + return this->m_indexString.contains(';'); + } + + bool CPropertyIndex::isMyself() const + { + return this->m_indexString.isEmpty(); + } + + bool CPropertyIndex::isEmpty() const + { + return this->m_indexString.isEmpty(); } - /* - * Convert to string - */ QString CPropertyIndex::convertToQString(bool i18n) const { Q_UNUSED(i18n); return this->m_indexString; } - /* - * Parse from string - */ void CPropertyIndex::parseFromString(const QString &indexes) { - this->m_indexString = indexes.simplified().replace(" ", ";").replace(",", ";"); - this->stringToList(); + 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; + } + } } - /* - * To string - */ - void CPropertyIndex::listToString() + void CPropertyIndex::setIndexStringByList(const QList &list) { QString l; - foreach(int i, this->m_indexes) + for (const int &i : list) { Q_ASSERT(i >= static_cast(GlobalIndexCValueObject)); if (!l.isEmpty()) { l.append(";"); } @@ -103,23 +97,21 @@ namespace BlackMisc this->m_indexString = l; } - /* - * To int list - */ - void CPropertyIndex::stringToList() + QList CPropertyIndex::indexList() const { - this->m_indexes.clear(); - if (this->m_indexString.isEmpty()) { return; } + QList list; + if (this->m_indexString.isEmpty()) { return list; } QStringList indexes = this->m_indexString.split(';'); foreach(QString index, indexes) { - if (index.isEmpty()) continue; + if (index.isEmpty()) { continue; } bool ok; int i = index.toInt(&ok); Q_ASSERT(ok); Q_ASSERT(i >= static_cast(GlobalIndexCValueObject)); - this->m_indexes.append(i); + list.append(i); } + return list; } } // namespace diff --git a/src/blackmisc/propertyindex.h b/src/blackmisc/propertyindex.h index e683c9847..f57796e02 100644 --- a/src/blackmisc/propertyindex.h +++ b/src/blackmisc/propertyindex.h @@ -81,16 +81,20 @@ namespace BlackMisc bool isNested() const; //! Myself index, used with nesting - bool isMyself() const { return this->m_indexes.isEmpty(); } + bool isMyself() const; //! Empty? - bool isEmpty() const { return this->m_indexes.isEmpty(); } + bool isEmpty() const; + + //! Index list + QList indexList() const; //! First element casted to given type, usually then PropertIndex enum template CastType frontCasted() const { - Q_ASSERT(!this->m_indexes.isEmpty()); - int f = this->m_indexes.isEmpty() ? 0 : this->m_indexes.first(); + QList l = indexList(); + Q_ASSERT(!l.isEmpty()); + int f = l.isEmpty() ? 0 : l.first(); return static_cast(f); } @@ -98,8 +102,9 @@ namespace BlackMisc template bool equalsPropertyIndexEnum(EnumType ev) { static_assert(std::is_enum::value, "Argument must be an enum"); - if (this->m_indexes.size() != 1) { return false; } - return static_cast(ev) == m_indexes.first(); + QList l = indexList(); + if (l.size() != 1) { return false; } + return static_cast(ev) == l.first(); } protected: @@ -111,14 +116,10 @@ namespace BlackMisc private: BLACK_ENABLE_TUPLE_CONVERSION(CPropertyIndex) - QList m_indexes; 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 listToString(); - - //! String to list - void stringToList(); + void setIndexStringByList(const QList &list); }; } //namespace