Debian Codegraph @

avogadro/rendering/groupnode.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "groupnode.h"
7 #include "visitor.h"
8
9 namespace Avogadro::Rendering {
10
11 GroupNode::GroupNode(GroupNode* parent_)
12 {
13 if (parent_)
14 parent_->addChild(this);
15 }
16
17 GroupNode::~GroupNode()
18 {
19 // Like all good parents, we destroy our children before we go...
20 clear(ALL);
21 }
22
23 void GroupNode::accept(Visitor& visitor)
24 {
25 visitor.visit(*this);
26 for (auto& it : m_children) {
27 it.node->accept(visitor);
28 }
29 }
30
31 void GroupNode::addChild(Node* node, NodeType ui)
32 {
33 if (!node || node == this)
34 return;
35 if (!hasChild(node)) {
36 node->setParent(this);
37 m_children.emplace_back(node, ui);
38 }
39 }
40
41 bool GroupNode::hasChild(Node* node) const
42 {
43 if (!node)
44 return false;
45 for (auto it : m_children) {
46 if (it.node == node) {
47 return true;
48 }
49 }
50 return false;
51 }
52
53 bool GroupNode::removeChild(Node* node)
54 {
55 if (!node)
56 return false;
57 for (auto it = m_children.begin(); it != m_children.end(); ++it) {
58 if (it->node == node) {
59 it->node->setParent(nullptr);
60 m_children.erase(it);
61 return true;
62 }
63 }
64 return false;
65 }
66
67 Node* GroupNode::child(size_t index)
68 {
69 if (index >= m_children.size())
70 return nullptr;
71 else {
72 // this is only used in test so we can us a O(n) function
73 auto it = m_children.begin();
74 it = std::next(it, index);
75 return it->node;
76 }
77 }
78
79 void GroupNode::clear(NodeType ui)
80 {
81 // Like all good parents, we destroy our children before we go...
82 for (auto it = m_children.begin(); it != m_children.end();) {
83 auto itNext = std::next(it);
84 if (it->ui == ui || ui == ALL) {
85 auto groupNode = it->node->cast<GroupNode>();
86 if (groupNode != nullptr && ui != ALL) {
87 switch (ui) {
88 case UI:
89 groupNode->clearUI();
90 break;
91 default:
92 case GEOMETRY:
93 groupNode->clear();
94 break;
95 }
96 // like a good parent, kill your son if you don't have nieces
97 if (groupNode->childCount() == 0) {
98 delete it->node;
99 m_children.erase(it);
100 }
101 } else {
102 delete it->node;
103 m_children.erase(it);
104 }
105 }
106 it = itNext;
107 }
108 }
109
110 void GroupNode::clear()
111 {
112 clear(GEOMETRY);
113 }
114
115 void GroupNode::clearUI()
116 {
117 clear(UI);
118 }
119
120 } // namespace Avogadro::Rendering