/
GraphTreeModel.cpp
183 lines (145 loc) · 4.52 KB
/
GraphTreeModel.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "GraphTreeModel.h"
#include "iselectable.h"
#include "iselection.h"
#include "GraphTreeModelPopulator.h"
#include "debugging/ScenegraphUtils.h"
namespace ui
{
GraphTreeModel::GraphTreeModel() :
_model(new wxutil::TreeModel(_columns)),
_visibleNodesOnly(false)
{}
GraphTreeModel::~GraphTreeModel()
{
// Remove everything before shutting down
clear();
}
void GraphTreeModel::connectToSceneGraph()
{
// Subscribe to the scenegraph to get notified about insertions/deletions
GlobalSceneGraph().addSceneObserver(this);
}
void GraphTreeModel::disconnectFromSceneGraph()
{
GlobalSceneGraph().removeSceneObserver(this);
}
const GraphTreeNode::Ptr& GraphTreeModel::insert(const scene::INodePtr& node)
{
// Insert this iterator below a possible parent iterator
auto parentIter = findParentIter(node);
wxutil::TreeModel::Row row = parentIter ? _model->AddItem(parentIter) : _model->AddItem();
// Create a new GraphTreeNode
auto gtNode = std::make_shared<GraphTreeNode>(node, row.getItem());
// Assign root node member
if (node->getNodeType() == scene::INode::Type::MapRoot)
{
_mapRootNode = gtNode;
}
// Fill in the values
row[_columns.node] = wxVariant(node.get());
row[_columns.name] = node->name();
row.SendItemAdded();
// Insert this iterator into the node map to facilitate lookups
// Return the GraphTreeNode reference
return _nodemap.emplace(node, gtNode).first->second;
}
void GraphTreeModel::erase(const scene::INodePtr& node)
{
auto found = _nodemap.find(node);
if (found != _nodemap.end())
{
// Remove this from the model...
_model->RemoveItem(found->second->getIter());
// ...and from our lookup table
_nodemap.erase(found);
if (_mapRootNode && _mapRootNode->getNode() == node)
{
_mapRootNode.reset();
}
}
}
const GraphTreeNode::Ptr& GraphTreeModel::find(const scene::INodePtr& node) const
{
auto found = _nodemap.find(node);
return found != _nodemap.end() ? found->second : _nullTreeNode;
}
void GraphTreeModel::clear()
{
// Remove everything, wx plus nodemap
_nodemap.clear();
_model->Clear();
_mapRootNode.reset();
}
void GraphTreeModel::refresh()
{
#if defined(__linux__)
_model->Clear();
#else
// Create a new model from scratch and populate it
_model = new wxutil::TreeModel(_columns);
#endif
if (!GlobalSceneGraph().root()) return;
// Instantiate a scenegraph walker and visit every node in the graph
// The walker also clears the graph in its constructor
GraphTreeModelPopulator populator(*this, _visibleNodesOnly);
GlobalSceneGraph().root()->traverse(populator);
// Now sort the model once we have all nodes in the tree
_model->SortModelByColumn(_columns.name);
}
void GraphTreeModel::setConsiderVisibleNodesOnly(bool visibleOnly)
{
_visibleNodesOnly = visibleOnly;
}
void GraphTreeModel::updateSelectionStatus(const NotifySelectionUpdateFunc& notifySelectionChanged)
{
// Don't traverse the entire scenegraph, visit selected nodes only
GlobalSelectionSystem().foreachSelected([&](const scene::INodePtr& node)
{
updateSelectionStatus(node, notifySelectionChanged);
});
}
void GraphTreeModel::updateSelectionStatus(const scene::INodePtr& node,
const NotifySelectionUpdateFunc& notifySelectionChanged)
{
if (auto found = _nodemap.find(node); found != _nodemap.end())
{
notifySelectionChanged(found->second->getIter(), Node_isSelected(node));
}
}
wxDataViewItem GraphTreeModel::findParentIter(const scene::INodePtr& node)
{
switch (node->getNodeType())
{
case scene::INode::Type::MapRoot:
return wxDataViewItem(); // root node is inserted at the root level
case scene::INode::Type::Entity:
if (!_mapRootNode)
{
// Create a new map root node right here
insert(node->getParent());
}
return _mapRootNode->getIter();
default:
rWarning() << "Node type " << getNameForNodeType(node->getNodeType()) << " should not be inserted into the tree" << std::endl;
return wxDataViewItem();
}
}
const GraphTreeModel::TreeColumns& GraphTreeModel::getColumns() const
{
return _columns;
}
wxutil::TreeModel::Ptr GraphTreeModel::getModel()
{
return _model;
}
void GraphTreeModel::onSceneNodeInsert(const scene::INodePtr& node)
{
if (!NodeIsRelevant(node)) return;
insert(node); // wrap to the actual insert() method
}
void GraphTreeModel::onSceneNodeErase(const scene::INodePtr& node)
{
if (!NodeIsRelevant(node)) return;
erase(node); // wrap to the actual erase() method
}
} // namespace