/
EClassTreeBuilder.cpp
145 lines (113 loc) · 3.22 KB
/
EClassTreeBuilder.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
#include "EClassTreeBuilder.h"
#include "itextstream.h"
#include "EClassTree.h"
#include "debugging/ScopedDebugTimer.h"
#include "wxutil/dataview/TreeModel.h"
#include "wxutil/Bitmap.h"
namespace ui
{
namespace
{
const char* ENTITY_ICON = "cmenu_add_entity.png";
const std::string INHERIT_KEY("inherit");
}
EClassTreeBuilder::EClassTreeBuilder(const EClassTreeColumns& columns,
wxEvtHandler* finishedHandler) :
wxThread(wxTHREAD_JOINABLE),
_columns(columns),
_treeStore(new wxutil::TreeModel(_columns)),
_finishedHandler(finishedHandler),
_treePopulator(_treeStore)
{
wxBitmap icon = wxutil::GetLocalBitmap(ENTITY_ICON);
_entityIcon.CopyFromBitmap(icon);
}
EClassTreeBuilder::~EClassTreeBuilder()
{
// We might have a running thread, wait for it
if (IsRunning())
{
Delete();
}
}
wxThread::ExitCode EClassTreeBuilder::Entry()
{
ScopedDebugTimer timer("EClassTreeBuilder::run()");
// Travese the entity classes, this will call visit() for each eclass
GlobalEntityClassManager().forEachEntityClass(*this);
if (TestDestroy()) return static_cast<wxThread::ExitCode>(0);
// Visit the tree populator in order to fill in the column data
_treePopulator.forEachNode(*this);
if (TestDestroy()) return static_cast<wxThread::ExitCode>(0);
// Sort the model before returning it
_treeStore->SortModelByColumn(_columns.name);
if (!TestDestroy())
{
// Send the event to our listener, only if we are not forced to finish
wxQueueEvent(_finishedHandler, new wxutil::TreeModel::PopulationFinishedEvent(_treeStore));
}
return static_cast<wxThread::ExitCode>(0);
}
void EClassTreeBuilder::populate()
{
if (IsRunning()) return;
Run();
}
void EClassTreeBuilder::visit(const IEntityClassPtr& eclass)
{
if (TestDestroy())
{
return;
}
std::string fullPath;
// Prefix mod name
fullPath = eclass->getModName() + "/";
// Prefix inheritance path (recursively)
fullPath += getInheritancePathRecursive(eclass);
// The entityDef name itself
fullPath += eclass->getName();
// Let the VFSTreePopulator do the insertion
_treePopulator.addPath(fullPath);
}
void EClassTreeBuilder::visit(wxutil::TreeModel& /* store */, wxutil::TreeModel::Row& row,
const std::string& path, bool isExplicit)
{
if (TestDestroy()) return;
// Get the display path, everything after rightmost slash
row[_columns.name] = wxVariant(wxDataViewIconText(
path.substr(path.rfind("/") + 1), _entityIcon));
}
std::string EClassTreeBuilder::getInheritancePathRecursive(const IEntityClassPtr& eclass)
{
std::string returnValue;
try
{
EntityClassAttribute attribute = eclass->getAttribute(
INHERIT_KEY, false /* includeInherited*/
);
// Don't use empty "inherit" keys
if (!attribute.getValue().empty())
{
// Get the inherited eclass first and resolve the path
IEntityClassPtr parent = GlobalEntityClassManager().findClass(
attribute.getValue()
);
if (parent != NULL)
{
returnValue += getInheritancePathRecursive(parent);
}
else
{
rError() << "EClassTreeBuilder: Cannot resolve inheritance path for "
<< eclass->getName() << std::endl;
}
returnValue += attribute.getValue() + "/";
}
}
catch (std::runtime_error&)
{
// no inherit key
}
return returnValue;
}
} // namespace ui