forked from mrkite/minutor
/
entityidentifier.cpp
171 lines (147 loc) · 4.84 KB
/
entityidentifier.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
/** Copyright (c) 2014, Mc_Etlam */
#include <QDebug>
#include <assert.h>
#include "./entityidentifier.h"
#include "./json.h"
EntityInfo::EntityInfo(QString name, QString category, QColor brushColor,
QColor penColor) : name(name), category(category),
brushColor(brushColor), penColor(penColor) {}
// dummy for entities not found in *_entity.json
static EntityInfo entityDummy("Name unknown", "Others", // Name Category
Qt::black, Qt::black); // Black circle with Black border
EntityIdentifier::EntityIdentifier() {}
EntityIdentifier::~EntityIdentifier() {}
EntityIdentifier& EntityIdentifier::Instance() {
static EntityIdentifier singleton;
return singleton;
}
void EntityIdentifier::enableDefinitions(int packID) {
if (packID < 0) return;
for (auto it = packs.begin(); it != packs.end(); ++it) {
if (it->packID == packID) {
it->enabled = true;
return;
}
}
}
void EntityIdentifier::disableDefinitions(int packID) {
if (packID < 0) return;
for (auto it = packs.begin(); it != packs.end(); ++it) {
if (it->packID == packID)
it->enabled = false;
}
}
int EntityIdentifier::addDefinitions(JSONArray *defs, int packID) {
if (packID == -1) {
// find largest used packID
for (auto it = packs.constBegin(); it != packs.constEnd(); ++it) {
if (it->packID > packID)
packID = it->packID;
}
packID++; // use one higher than largest found
packs.append(TpackInfo(packID));
}
int len = defs->length();
for (int i = 0; i < len; i++)
parseCategoryDefinition(dynamic_cast<JSONObject *>(defs->at(i)), packID);
return packID;
}
EntityIdentifier::TentityMap& EntityIdentifier::getMapForPackID(int packID) {
for (auto it = packs.begin(); it != packs.end(); ++it) {
if (it->packID == packID)
return it->map;
}
return dummyMap;
}
void EntityIdentifier::parseCategoryDefinition(JSONObject *data, int packID) {
QString category;
if (data->has("category"))
category = data->at("category")->asString();
else
category = "Unknown";
QColor catcolor;
if (data->has("catcolor")) {
QString colorname = data->at("catcolor")->asString();
catcolor.setNamedColor(colorname);
assert(catcolor.isValid());
} else { // use hashed by name instead
quint32 hue = qHash(category);
catcolor.setHsv(hue % 360, 255, 255);
}
addCategory(qMakePair(category, catcolor));
if (data->has("entity")) {
JSONArray *entities = dynamic_cast<JSONArray *>(data->at("entity"));
int len = entities->length();
for (int e = 0; e < len; e++)
parseEntityDefinition(dynamic_cast<JSONObject *>(entities->at(e)),
category, catcolor, packID);
}
}
void EntityIdentifier::parseEntityDefinition(JSONObject *entity,
QString const &category,
QColor catcolor, int packID) {
QString id;
if (entity->has("id"))
id = entity->at("id")->asString();
else
id = "Unknown";
if (entity->has("catcolor")) {
QString colorname = entity->at("catcolor")->asString();
catcolor.setNamedColor(colorname);
assert(catcolor.isValid());
}
QColor color;
if (entity->has("color")) {
QString colorname = entity->at("color")->asString();
color.setNamedColor(colorname);
assert(color.isValid());
} else { // use hashed by name instead
quint32 hue = qHash(id);
color.setHsv(hue % 360, 255, 255);
}
QString name = id;
if (entity->has("name")) {
name = entity->at("name")->asString();
}
// enter entity into manager
TentityMap& map = getMapForPackID(packID);
map.insert(id, EntityInfo(name, category, catcolor, color));
}
bool EntityIdentifier::addCategory(QPair<QString, QColor> cat) {
for (auto it = categories.constBegin(); it != categories.constEnd(); ++it) {
if (it->first == cat.first)
return false;
}
categories.append(cat);
return true;
}
int EntityIdentifier::getNumCategories() const {
return categories.size();
}
EntityIdentifier::TcatList const &EntityIdentifier::getCategoryList() const {
return categories;
}
QColor EntityIdentifier::getCategoryColor(QString name) const {
for (auto it = categories.constBegin(); it != categories.constEnd(); ++it) {
if (it->first == name)
return it->second;
}
return Qt::black; // dummy
}
EntityInfo const &EntityIdentifier::getEntityInfo(QString id) const {
for (auto it = packs.constBegin(); it != packs.constEnd(); ++it) {
if (it->enabled) {
TentityMap::const_iterator info = it->map.find(id);
if (info != it->map.end()) { // found it
return info.value();
}
}
}
return entityDummy;
}
QColor EntityIdentifier::getBrushColor(QString id) const {
return getEntityInfo(id).brushColor;
}
QColor EntityIdentifier::getPenColor(QString id) const {
return getEntityInfo(id).penColor;
}