-
Notifications
You must be signed in to change notification settings - Fork 2
/
Document.cpp
156 lines (129 loc) · 4.11 KB
/
Document.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
#include "Document.h"
#include "factories/DocumentEntryFactory.h"
#include "gui/ComponentGraphicsItem.h"
#include "gui/Schematic.h"
#include "gui/SchematicElement.h"
#include "gui/PortGraphicsItem.h"
#include "gui/WireGraphicsItem.h"
#include "metamodel/ComponentDescriptor.h"
#include "metamodel/PortDescriptor.h"
#include "model/Node.h"
#include "ApplicationContext.h"
#include "ComponentFactory.h"
#include "Constants.h"
#include "JsonHelpers.h"
#include "Project.h"
#include <QFile>
#include <QGraphicsEllipseItem>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QtDebug>
using namespace q2d;
using namespace q2d::constants;
using namespace q2d::core;
using namespace q2d::factories;
using namespace q2d::metamodel;
Document::Document(QString name, Project* parent) :
QObject(parent),
QStandardItem(name),
Identifiable(name) {
Q_CHECK_PTR(parent);
Q_ASSERT(this->text() == name);
// obtain the component factory
ApplicationContext* context =
dynamic_cast<ApplicationContext*>(parent->parent());
Q_CHECK_PTR(context);
this->m_componentFactory = context->componentFactory();
this->setData(QVariant::fromValue(new model::Model(this)),
DocumentRole::MODEL);
this->setData(QVariant::fromValue(new gui::Schematic(this)),
DocumentRole::SCHEMATIC);
}
/**
* @brief Document::getSchematicView is a getter for convenience
* @return
*/
gui::Schematic*
Document::schematic() {
return this->data(DocumentRole::SCHEMATIC).value<gui::Schematic*>();
}
/**
* @brief Document::getDescribedModel is a convenience function
* @return
*/
model::Model*
Document::model() const {
return this->data(DocumentRole::MODEL).value<model::Model*>();
}
ComponentFactory*
Document::componentFactory() const {
return m_componentFactory;
}
/**
* @brief Document::addComponent instantiates a new component from a ComponentType,
* given by its path in the component hierarchy.
* The new component will be placed at the given position in the schematic.
* Also all component ports will be initialized accordingly.
*
* @param typeId is the full id path of the ComponentType
* in the component hierarchy.
* @param position of the component at which it is placed in the schematic.
*/
void
Document::addComponent(QString typeId, QPoint position) {
metamodel::ComponentDescriptor* type = m_componentFactory->componentDescriptor(typeId);
Q_CHECK_PTR(type);
DocumentEntryFactory::instantiateComponent(this, type, position);
}
void
Document::addInputPort(QString id, QPointF pos) {
DocumentEntryFactory::instantiateInputPort(this, pos, id);
}
void
Document::addOutputPort(QString id, QPointF pos) {
DocumentEntryFactory::instantiateOutputPort(this, pos, id);
}
/**
* @brief Document::entry searches for an document entry given by an id string.
* @param id
* @return The entry identified by id or a nullptr if there is no such entry.
*/
DocumentEntry*
Document::entryForFullId(const QString fullId) const {
Q_ASSERT(!fullId.isEmpty());
for (DocumentEntry * entry : m_entries) {
if (entry->fullId() == fullId) {
return entry;
}
}
return nullptr;
}
QList<DocumentEntry*>
Document::entries() const {
return m_entries;
}
void
Document::addWire(QString senderNodeId, QString receiverNodeId) {
DocumentEntry* sender = this->entryForFullId(senderNodeId);
DocumentEntry* receiver = this->entryForFullId(receiverNodeId);
DocumentEntryFactory::instantiateWire(this, sender, receiver);
}
void
Document::addEntry(DocumentEntry* entry) {
Q_CHECK_PTR(entry);
qDebug() << "Added document entry" << entry->fullId();
m_entries.append(entry);
}
void
Document::save(QDir saveDir) {
Q_ASSERT(saveDir.exists());
qDebug() << "Saving Document" << this->text();
// create the JSON document
// the name is implicitly in the file name
QJsonDocument jsonDocument = QJsonDocument();
jsonDocument.setObject(json::fromDocument(this));
QString path = saveDir.absolutePath() + "/"
+ this->text() + EXTENSION_DOCFILE;
json::writeJsonFile(path, jsonDocument);
}