| 1 | // Copyright (C) 2025 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qqmltreerow_p.h" |
| 5 | |
| 6 | #include <QtCore/qjsonobject.h> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | using namespace Qt::Literals::StringLiterals; |
| 11 | static const QString TREE_ROWS_PROPERTY_NAME = u"rows"_s; |
| 12 | |
| 13 | QQmlTreeRow::QQmlTreeRow(QQmlTreeRow *parentItem) |
| 14 | : m_parent(parentItem) |
| 15 | { |
| 16 | |
| 17 | } |
| 18 | |
| 19 | QQmlTreeRow::QQmlTreeRow(const QVariant &data, QQmlTreeRow *parentItem) |
| 20 | : m_parent(parentItem) |
| 21 | { |
| 22 | QVariantMap variantMap = data.toJsonObject().toVariantMap(); |
| 23 | unpackVariantMap(dataMap: variantMap); |
| 24 | } |
| 25 | |
| 26 | QQmlTreeRow::QQmlTreeRow(const QVariantMap &data, QQmlTreeRow *parentItem) |
| 27 | : m_parent(parentItem), |
| 28 | dataMap(data) |
| 29 | { |
| 30 | unpackVariantMap(dataMap: data); |
| 31 | } |
| 32 | |
| 33 | void QQmlTreeRow::addChild(QQmlTreeRow *child) |
| 34 | { |
| 35 | m_children.push_back(x: std::unique_ptr<QQmlTreeRow>(child)); |
| 36 | child->setParent(this); |
| 37 | } |
| 38 | |
| 39 | void QQmlTreeRow::unpackVariantMap(const QVariantMap &variantMap) |
| 40 | { |
| 41 | for (auto it = variantMap.begin(); it != variantMap.end(); ++it) { |
| 42 | const QString& key = it.key(); |
| 43 | const QVariant& value = it.value(); |
| 44 | |
| 45 | if ((value.typeId() == QMetaType::Type::QVariantList) && (key == TREE_ROWS_PROPERTY_NAME)) { |
| 46 | const QList<QVariant> variantList = value.toList(); |
| 47 | for (const QVariant &rowAsVariant : variantList) |
| 48 | m_children.push_back(x: std::make_unique<QQmlTreeRow>(args: rowAsVariant, args: this)); |
| 49 | } else { |
| 50 | dataMap.insert(key, value); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void QQmlTreeRow::removeChild(std::vector<std::unique_ptr<QQmlTreeRow>>::const_iterator &child) |
| 56 | { |
| 57 | m_children.erase(position: child); |
| 58 | } |
| 59 | |
| 60 | void QQmlTreeRow::removeChildAt(int i) |
| 61 | { |
| 62 | m_children.erase(position: std::next(x: m_children.begin(), n: i)); |
| 63 | } |
| 64 | |
| 65 | void QQmlTreeRow::setData(const QVariant &data) |
| 66 | { |
| 67 | dataMap.clear(); |
| 68 | const QVariantMap variantMap = data.toJsonObject().toVariantMap(); |
| 69 | unpackVariantMap(variantMap); |
| 70 | } |
| 71 | |
| 72 | void QQmlTreeRow::setData(const QVariantMap &data) |
| 73 | { |
| 74 | dataMap.clear(); |
| 75 | unpackVariantMap(variantMap: data); |
| 76 | } |
| 77 | |
| 78 | void QQmlTreeRow::setField(const QString &key, const QVariant &value) |
| 79 | { |
| 80 | if (dataMap.contains(key)) |
| 81 | dataMap[key] = value; |
| 82 | } |
| 83 | |
| 84 | QVariant QQmlTreeRow::toVariant() const |
| 85 | { |
| 86 | QVariantMap variantMap(dataMap); |
| 87 | |
| 88 | if (!m_children.empty()) { |
| 89 | QVariantList children; |
| 90 | for (const auto &child : m_children) |
| 91 | children.append(t: child->toVariant()); |
| 92 | |
| 93 | variantMap[TREE_ROWS_PROPERTY_NAME] = children; |
| 94 | } |
| 95 | |
| 96 | return variantMap; |
| 97 | } |
| 98 | |
| 99 | int QQmlTreeRow::subTreeSize() const |
| 100 | { |
| 101 | int size = 1; |
| 102 | |
| 103 | for (const auto &child : m_children) |
| 104 | size += child->subTreeSize(); |
| 105 | |
| 106 | return size; |
| 107 | } |
| 108 | |
| 109 | QT_END_NAMESPACE |
| 110 |
