-
Notifications
You must be signed in to change notification settings - Fork 2
/
ComponentFactory.cpp
245 lines (199 loc) · 7.03 KB
/
ComponentFactory.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "ComponentFactory.h"
#include "Application.h"
#include "ApplicationContext.h"
#include "Constants.h"
#include "Document.h"
#include "Enumerations.h"
#include "JsonHelpers.h"
#include "Util.h"
#include "gui/ComponentGraphicsItem.h"
#include "gui/Schematic.h"
#include "gui/PortGraphicsItem.h"
#include "gui/WireGraphicsItem.h"
#include "metamodel/Category.h"
#include "metamodel/ConfigurationBitDescriptor.h"
#include "metamodel/ComponentDescriptor.h"
#include "metamodel/PortDescriptor.h"
#include "model/Component.h"
#include "model/Conductor.h"
#include "model/Model.h"
#include "model/Port.h"
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonObject>
#include <QtDebug>
using namespace q2d;
using namespace q2d::constants;
using namespace q2d::metamodel;
ComponentFactory::ComponentFactory(ApplicationContext* parent)
: QObject(parent) {}
/**
* @brief ComponentFactory::addCategory creates a new category and adds it to
* the component hierarchy.
* If the parent category is null, the item will be sorted directly below the
* root.
*
* @param name
* @param parent the parent category; May be null
*/
Category*
ComponentFactory::slot_addCategory(QString name, Category* parent) {
// TODO ensure update signals are fired upon adding a category
Q_ASSERT(!name.isEmpty ());
qDebug() << "Adding category" << name << "with parent" << parent;
Category* newCategory = new Category(name, parent);
if (parent == nullptr) {
this->m_componentHierarchy.invisibleRootItem()->appendRow(newCategory);
} else {
parent->addSubItem(newCategory);
}
return newCategory;
}
/**
* @brief ComponentFactory::slot_loadType creates a new component type
* and adds it to the component hierarchy.
* If the parent category is null, the item will be sorted directly below the
* root.
*
* @param filePath is the absolute path of the file that contains the component description as JSON
* @param parent the parent category; May be null
*/
void
ComponentFactory::slot_loadType(QString filePath, Category* parent) {
// logging prefix
const QString logPrefix = this->objectName() + "::slot_loadType(" + filePath + ", "
+ util::ptrToString(parent) + ")";
QJsonDocument jsonDocument = json::readJsonFile(filePath);
// validity check
if (jsonDocument.isNull()) {
qWarning() << logPrefix << "json document seems to be invalid";
return;
}
ComponentDescriptor* newType = json::toComponentDescriptor(jsonDocument, filePath, parent);
Q_CHECK_PTR(newType);
// TODO later disallow items w/o category
if (parent != nullptr) {
parent->addSubItem(newType);
} else {
this->m_componentHierarchy.invisibleRootItem()->appendRow(newType);
}
}
/**
* @brief ComponentFactory::getCategoryForIndex attempts to get the ComponentCategory
* for the given index.
*
* If the index is invalid or the indexed element is not a ComponentCategory, a nullptr will be returned.
*
* @param index
* @return ; May return null
*/
Category*
ComponentFactory::categoryForIndex(const QModelIndex &index) {
if (!index.isValid()) {
return nullptr;
}
QStandardItem* item = this->m_componentHierarchy.itemFromIndex(index);
return dynamic_cast<Category*>(item);
}
/**
* @brief ComponentFactory::getTypeForIndex attempts to get the ComponentType
* for the given index.
*
* If the index is invalid or the indexed element is not a ComponentType, a nullptr will be returned.
*
* @param index
* @return ; May return null
*/
ComponentDescriptor*
ComponentFactory::componentDescriptor(const QModelIndex &index) {
if (!index.isValid()) {
return nullptr;
}
QStandardItem* item = this->m_componentHierarchy.itemFromIndex(index);
return static_cast<ComponentDescriptor*>(item);
}
ComponentDescriptor*
ComponentFactory::componentDescriptor(const QString hierarchyName) {
QStringList hierarchy = hierarchyName.split(HIERARCHY_SEPERATOR,
QString::SkipEmptyParts);
// traverse the hierarchy and find the component
// TODO use a more effective approach here, this is really a hack
QStandardItem* currentItem = this->m_componentHierarchy.invisibleRootItem();
while (!hierarchy.isEmpty()) {
// iterate over the current hierarcy level
for (int index = 0; index < currentItem->rowCount(); ++index) {
QStandardItem* child = currentItem->child(index);
if (child->text() == hierarchy.first()) {
currentItem = child;
hierarchy.removeFirst(); // down to the next level
break;
}
if (index == currentItem->rowCount() - 1) {
// found nothing in the last element searched
qDebug() << hierarchyName << "not found in component hierarchy!";
return nullptr;
}
}
}
return dynamic_cast<ComponentDescriptor*>(currentItem);
}
QStandardItemModel*
ComponentFactory::componentHierarchy() {
return &(this->m_componentHierarchy);
}
// FIXME move this and all helpers to JsonHelpers
void
ComponentFactory::importHierarchy(QJsonDocument source) {
Q_ASSERT(!source.isNull());
Q_ASSERT(source.isArray());
QJsonArray hierarchy = source.array();
for (int index = 0; index < hierarchy.count(); ++index) {
jsonToEntry(hierarchy[index].toObject(), nullptr);
}
}
void
ComponentFactory::jsonToCategoryEntry(QJsonObject json, Category* parent) {
QString name = json.value(JSON_HIERARCHY_CATEGORY_NAME).toString();
Category* result = this->slot_addCategory(name, parent);
QJsonArray children = json.value(JSON_HIERARCHY_CHILD).toArray();
for (int index = 0; index < children.count(); ++index) {
jsonToEntry(children[index].toObject(), result);
}
}
void
ComponentFactory::jsonToTypeEntry(QJsonObject json, Category* parent) {
QString sourcePath = json.value(JSON_HIERARCHY_SOURCE).toString();
Q_ASSERT(!sourcePath.isEmpty());
this->slot_loadType(sourcePath, parent);
}
void
ComponentFactory::jsonToEntry(QJsonObject json, Category* parent) {
QString type = json.value(JSON_HIERARCHY_TYPE).toString();
if (type == JSON_HIERARCHY_TYPE_COMPONENT) {
jsonToTypeEntry(json, parent);
} else if (type == JSON_HIERARCHY_TYPE_CATEGORY) {
jsonToCategoryEntry(json, parent);
} else {
Q_ASSERT(false);
}
}
void
ComponentFactory::slot_clearHierarchy() {
m_componentHierarchy.clear();
}
void
ComponentFactory::slot_saveHierarchy(QString filePath){
json::writeJsonFile(filePath, json::exportComponentHierarchy(&m_componentHierarchy));
}
void
ComponentFactory::slot_loadHierarchy(QString filePath){
// logging prefix
const QString logPrefix = QString("%1::slot_loadHierarchy(%2)").arg(this->objectName()).arg(filePath);
QJsonDocument jsonDocument = json::readJsonFile(filePath);
// validity check
if (jsonDocument.isNull()) {
qWarning() << logPrefix << "json document seems to be invalid";
return;
}
this->importHierarchy(jsonDocument);
}