summaryrefslogtreecommitdiffstats
path: root/src/quick3d/quick3d/items/quick3dnode.cpp
blob: 09abcf2ae9b393c91532964b2d40ad115d4ef825 (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
111
112
113
114
115
116
117
118
119
120
121
// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "quick3dnode_p.h"

#include <QtCore/QDebug>

QT_BEGIN_NAMESPACE

namespace Qt3DCore {
namespace Quick {

/*!
    \qmltype Node
    \inqmlmodule Qt3D.Core
    \since 5.5

    \brief A base QML type that other types inherit. It cannot be directly
    created.
*/

Quick3DNode::Quick3DNode(QObject *parent)
    : QObject(parent)
{
}

/*!
    \qmlproperty list<QtQml::QtObject> Qt3D.Core::Node::data
    \qmldefault
*/

QQmlListProperty<QObject> Quick3DNode::data()
{
    using qt_size_type = qsizetype;
    using ListContentType = QObject;
    auto appendFunction = [](QQmlListProperty<ListContentType> *list, ListContentType *obj) {
        if (!obj)
            return;

        Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
        self->childAppended(0, obj);
    };
    auto countFunction = [](QQmlListProperty<ListContentType> *list) -> qt_size_type {
        Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
        return self->parentNode()->children().size();
    };
    auto atFunction = [](QQmlListProperty<ListContentType> *list, qt_size_type index) -> ListContentType * {
        Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
        return self->parentNode()->children().at(index);
    };
    auto clearFunction = [](QQmlListProperty<ListContentType> *list) {
        Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
        for (QObject *const child : self->parentNode()->children())
            self->childRemoved(0, child);
    };

    return QQmlListProperty<ListContentType>(this, nullptr, appendFunction, countFunction, atFunction, clearFunction);
}

/*!
    \qmlproperty list<Node> Qt3D.Core::Node::childNodes
    \readonly
*/

QQmlListProperty<QNode> Quick3DNode::childNodes()
{
    using qt_size_type = qsizetype;
    using ListContentType = QNode;
    auto appendFunction = [](QQmlListProperty<ListContentType> *list, ListContentType *obj) {
        if (!obj)
            return;

        Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
        Q_ASSERT(!self->parentNode()->children().contains(obj));

        self->childAppended(0, obj);
    };
    auto countFunction = [](QQmlListProperty<ListContentType> *list) -> qt_size_type {
        Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
        return self->parentNode()->children().size();
    };
    auto atFunction = [](QQmlListProperty<ListContentType> *list, qt_size_type index) -> ListContentType * {
        Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
        return qobject_cast<QNode *>(self->parentNode()->children().at(index));
    };
    auto clearFunction = [](QQmlListProperty<ListContentType> *list) {
        Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
        for (QObject *const child : self->parentNode()->children())
            self->childRemoved(0, child);
    };

    return QQmlListProperty<ListContentType>(this, nullptr, appendFunction, countFunction, atFunction, clearFunction);
}

void Quick3DNode::childAppended(int, QObject *obj)
{
    QNode *parentNode = this->parentNode();
    if (obj->parent() == parentNode)
        obj->setParent(nullptr);
    // Set after otherwise addChild might not work
    if (QNode *n = qobject_cast<QNode *>(obj))
        n->setParent(parentNode);
    else
        obj->setParent(parentNode);
}

void Quick3DNode::childRemoved(int, QObject *obj)
{
    if (QNode *n = qobject_cast<QNode *>(obj))
        n->setParent(Q_NODE_NULLPTR);
    else
        obj->setParent(nullptr);
}

} // namespace Quick
} // namespace Qt3DCore

QT_END_NAMESPACE

#include "moc_quick3dnode_p.cpp"
#include "moc_qt3dquickforeign_p.cpp"