1// Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB).
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 "entityvisitor_p.h"
5#include <Qt3DRender/private/managers_p.h>
6#include <Qt3DRender/private/nodemanagers_p.h>
7
8QT_USE_NAMESPACE
9using namespace Qt3DRender::Render;
10
11EntityVisitor::EntityVisitor(NodeManagers *manager)
12 : m_manager(manager)
13 , m_pruneDisabled(false)
14{
15
16}
17
18EntityVisitor::~EntityVisitor() = default;
19
20/*!
21 * \internal
22 *
23 * Override in derived class to do work on the current entity
24 *
25 * Return value (Continue, Prune, Stop) will affect traversal
26 */
27EntityVisitor::Operation EntityVisitor::visit(Entity *entity) {
28 // return false to stop traversal
29 if (!entity)
30 return Stop;
31 return Continue;
32}
33
34/*!
35 * \internal
36 *
37 * If true, disabled entities and all their children will be ignored
38 * during traversal
39 *
40 */
41bool EntityVisitor::pruneDisabled() const
42{
43 return m_pruneDisabled;
44}
45
46void EntityVisitor::setPruneDisabled(bool pruneDisabled)
47{
48 m_pruneDisabled = pruneDisabled;
49}
50
51/*!
52 * \internal
53 *
54 * Call on the root of the tree that should be traversed.
55 * Returns false if any visit resulted in Stop
56 */
57bool EntityVisitor::apply(Entity *root) {
58 if (!root)
59 return false;
60 if (m_pruneDisabled && !root->isEnabled())
61 return true;
62
63 const auto op = visit(entity: root);
64 if (op == Stop)
65 return false;
66 if (op == Prune)
67 return true;
68
69 const auto &childrenHandles = root->childrenHandles();
70 for (const HEntity &handle : childrenHandles) {
71 Entity *child = m_manager->renderNodesManager()->data(handle);
72 if (child != nullptr && !apply(root: child))
73 return false;
74 }
75
76 return true;
77}
78

source code of qt3d/src/render/backend/entityvisitor.cpp