aboutsummaryrefslogtreecommitdiffstats
path: root/src/labs/models/qqmltreerow.cpp
blob: 811d184e9d54523763520db616cfd1f8a1c1b374 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
// Qt-Security score:significant reason:default

#include "qqmltreerow_p.h"

#include <QtCore/qjsonobject.h>

QT_BEGIN_NAMESPACE

using namespace Qt::Literals::StringLiterals;
static const QString TREE_ROWS_PROPERTY_NAME = u"rows"_s;

QQmlTreeRow::QQmlTreeRow(QQmlTreeRow *parentItem)
    : m_parent(parentItem)
{

}

QQmlTreeRow::QQmlTreeRow(const QVariant &data, QQmlTreeRow *parentItem)
    : m_parent(parentItem)
{
    QVariantMap variantMap = data.toJsonObject().toVariantMap();
    unpackVariantMap(variantMap);
}

QQmlTreeRow::QQmlTreeRow(const QVariantMap &data, QQmlTreeRow *parentItem)
    : m_parent(parentItem),
      dataMap(data)
{
    unpackVariantMap(data);
}

void QQmlTreeRow::addChild(QQmlTreeRow *child)
{
    m_children.push_back(std::unique_ptr<QQmlTreeRow>(child));
    child->setParent(this);
}

void QQmlTreeRow::unpackVariantMap(const QVariantMap &variantMap)
{
    for (auto it = variantMap.begin(); it != variantMap.end(); ++it) {
        const QString& key = it.key();
        const QVariant& value = it.value();

        if ((value.typeId() == QMetaType::Type::QVariantList) && (key == TREE_ROWS_PROPERTY_NAME)) {
            const QList<QVariant> variantList = value.toList();
            for (const QVariant &rowAsVariant : variantList)
                m_children.push_back(std::make_unique<QQmlTreeRow>(rowAsVariant, this));
        } else {
            dataMap.insert(key, value);
        }
    }
}

void QQmlTreeRow::removeChild(std::vector<std::unique_ptr<QQmlTreeRow>>::const_iterator &child)
{
    m_children.erase(child);
}

void QQmlTreeRow::removeChildAt(int i)
{
    m_children.erase(std::next(m_children.begin(), i));
}

void QQmlTreeRow::setData(const QVariant &data)
{
    dataMap.clear();
    const QVariantMap variantMap = data.toJsonObject().toVariantMap();
    unpackVariantMap(variantMap);
}

void QQmlTreeRow::setData(const QVariantMap &data)
{
    dataMap.clear();
    unpackVariantMap(data);
}

void QQmlTreeRow::setField(const QString &key, const QVariant &value)
{
    if (dataMap.contains(key))
        dataMap[key] = value;
}

QVariant QQmlTreeRow::toVariant() const
{
    QVariantMap variantMap(dataMap);

    if (!m_children.empty()) {
        QVariantList children;
        for (const auto &child : m_children)
            children.append(child->toVariant());

        variantMap[TREE_ROWS_PROPERTY_NAME] = children;
    }

    return variantMap;
}

int QQmlTreeRow::subTreeSize() const
{
    int size = 1;

    for (const auto &child : m_children)
        size += child->subTreeSize();

    return size;
}

QT_END_NAMESPACE