/* Copyright (C) 2013 * swift project Community / Contributors * * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, * including this file, may be copied, modified, propagated, or distributed except according to the terms * contained in the LICENSE file. */ #include "blackmisc/propertyindex.h" #include "blackmisc/verify.h" #include #include namespace BlackMisc { CPropertyIndex::CPropertyIndex(int singleProperty) : m_indexes { singleProperty } { Q_ASSERT(singleProperty >= static_cast(GlobalIndexCValueObject)); } CPropertyIndex::CPropertyIndex(std::initializer_list il) : m_indexes(il) {} CPropertyIndex::CPropertyIndex(const QVector &indexes) : m_indexes(indexes) {} CPropertyIndex::CPropertyIndex(const QList &indexes) : m_indexes(indexes.toVector()) {} CPropertyIndex::CPropertyIndex(const QString &indexes) { this->parseFromString(indexes); } CPropertyIndex CPropertyIndex::copyFrontRemoved() const { BLACK_VERIFY_X(!this->isEmpty(), Q_FUNC_INFO, "Empty index"); if (this->isEmpty()) { return CPropertyIndex(); } CPropertyIndex copy = *this; copy.m_indexes.pop_front(); return copy; } bool CPropertyIndex::isNested() const { return m_indexes.size() > 1; } bool CPropertyIndex::isMyself() const { return m_indexes.isEmpty(); } bool CPropertyIndex::isEmpty() const { return m_indexes.isEmpty(); } QString CPropertyIndex::convertToQString(bool i18n) const { Q_UNUSED(i18n); 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) { 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)); m_indexes.append(i); } } 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) { m_indexes.push_front(newLeftIndex); } bool CPropertyIndex::contains(int index) const { return m_indexes.contains(index); } int CPropertyIndex::frontToInt() const { Q_ASSERT_X(!this->isEmpty(), Q_FUNC_INFO, "No index"); return m_indexes.front(); } bool CPropertyIndex::startsWith(int index) const { if (this->isEmpty()) { return false; } return this->frontToInt() == index; } } // namespace