-
Notifications
You must be signed in to change notification settings - Fork 2
/
ApplicationContext.cpp
259 lines (213 loc) · 7.98 KB
/
ApplicationContext.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "ApplicationContext.h"
#include "Application.h"
#include "ComponentFactory.h"
#include "Document.h"
#include "gui/MainWindow.h"
#include "Project.h"
#include <QFile>
#include <QtDebug>
using namespace q2d;
using namespace constants;
using MainWindow = q2d::gui::MainWindow;
ApplicationContext::ApplicationContext(Application* parent)
: QObject(parent) {
Q_CHECK_PTR(parent);
m_application = parent;
m_componentFactory = new ComponentFactory(this);
// create the main window
m_mainWindow = new gui::MainWindow(this);
m_mainWindow->setupSignalsAndSlots();
m_mainWindow->show();
this->setupSignalsAndSlots();
QStandardItemModel* componentHierarchy = this->m_componentFactory->componentHierarchy();
Q_CHECK_PTR(componentHierarchy);
emit this->signal_componentModelChanged(componentHierarchy);
}
ApplicationContext::~ApplicationContext() {
// TODO save project
// TODO uninitialize and close mainWindow
}
bool
ApplicationContext::hasCurrentProject() {
return m_currentProject != nullptr;
}
Project*
ApplicationContext::getCurrentProject() {
return this->m_currentProject;
}
gui::MainWindow*
ApplicationContext::getMainWindow() {
return this->m_mainWindow;
}
ComponentFactory*
ApplicationContext::componentFactory() {
return this->m_componentFactory;
}
void
ApplicationContext::setupSignalsAndSlots() {
Q_CHECK_PTR(this->m_mainWindow);
// FIXME enforce proper connection setup
// caller established connection
// ApplicationContext -> MainWindow
connect(this, &ApplicationContext::signal_projectNameChanged,
m_mainWindow, &MainWindow::slot_updateProjectName);
connect(this, &ApplicationContext::signal_canSaveProjects,
m_mainWindow, &MainWindow::slot_enableProjectSaving);
connect(this, &ApplicationContext::signal_canAddDocuments,
m_mainWindow, &MainWindow::slot_enableDocumentMenus);
connect(this, &ApplicationContext::signal_documentModelChanged,
m_mainWindow, &MainWindow::slot_setDocumentModel);
connect(this, &ApplicationContext::signal_componentModelChanged,
m_mainWindow, &MainWindow::slot_setComponentModel);
connect(this, &ApplicationContext::signal_showDocument,
m_mainWindow, static_cast<void (MainWindow::*)(Document*)>
(&MainWindow::slot_openDocumentTab)); // ship around overloaded function
connect(this, &ApplicationContext::signal_quantorSolutionAvailable,
m_mainWindow, &MainWindow::slot_displayQuantorResult);
connect(this, &ApplicationContext::signal_error,
m_mainWindow, &MainWindow::slot_displayErrorMessage);
// saving signal has to be set up by project
// ApplicationContext -> ComponentFactory
connect(this, &ApplicationContext::signal_clearComponentTypes,
m_componentFactory, &ComponentFactory::slot_clearHierarchy);
connect(this, &ApplicationContext::signal_createComponentCategory,
m_componentFactory, &ComponentFactory::slot_addCategory);
connect(this, &ApplicationContext::signal_saveLibraryRequested,
m_componentFactory, &ComponentFactory::slot_saveHierarchy);
connect(this, &ApplicationContext::signal_loadLibraryRequested,
m_componentFactory, &ComponentFactory::slot_loadHierarchy);
// MainWindow -> ComponentFactory
connect(m_mainWindow, &MainWindow::signal_loadType,
m_componentFactory, &ComponentFactory::slot_loadType);
connect(m_mainWindow, &MainWindow::signal_clearComponentTypes,
m_componentFactory, &ComponentFactory::slot_clearHierarchy);
// ApplicationContext -> Application
connect(this, &ApplicationContext::signal_triggerQuantor,
m_application, &Application::signal_quantorTriggered);
// Application -> ApplicationContext
connect(m_application, &Application::signal_quantorSolutionAvailable,
this, &ApplicationContext::signal_quantorSolutionAvailable);
}
/**
* @brief ApplicationContext::createProject creates a new Project - instance
* and makes it the current project.
*
* Callers have to make sure:
* <ul>
* <li>The projects name is valid</li>
* <li>There is no current project</li>
* </ul>
*
* @param name is the name of the new project
*/
void
ApplicationContext::createProject(QString name) {
Q_ASSERT(!name.isEmpty());
Q_ASSERT(!this->hasCurrentProject());
Project* newProject = new Project(name, this);
Q_CHECK_PTR(newProject);
m_currentProject = newProject;
newProject->setupSignalsAndSlots();
// inform other objects about changes
emit this->signal_projectNameChanged(name);
emit this->signal_canSaveProjects(true);
// enable document menus
emit this->signal_canAddDocuments(true);
emit this->signal_documentModelChanged(newProject->getDocuments());
}
void
ApplicationContext::unloadProject() {
// TODO save (if wanted) before unloading
delete m_currentProject;
m_currentProject = nullptr;
emit this->signal_clearComponentTypes();
emit this->signal_canAddDocuments(false);
emit this->signal_projectNameChanged(QString());
emit this->signal_documentModelChanged(nullptr);
}
/**
* @brief ApplicationContext::slot_newDocument forwards the request of creating
* a new document to the project.
*/
void
ApplicationContext::slot_newDocument(QString name) {
Q_CHECK_PTR(this->m_currentProject);
emit this->signal_createDocument(name);
}
/**
* @brief ApplicationContext::createProject is triggered upon the UIs request
* to create a new project.
* It is up to the UI to generate or request a name from the user.
*
* @param name
* <ul>
* <li> Must not be empty </li>
* </ul>
*
*/
void
ApplicationContext::slot_newProject(QString name) {
if (this->hasCurrentProject()) {
this->unloadProject();
}
// create new empty project
this->createProject(name);
}
// TODO is this still useful?
/**
* @brief ApplicationContext::slot_projectNameChanged
* Propagates forward a change in the current projects name.
*
* @param newName
*/
void
ApplicationContext::slot_projectNameChanged(QString newName) {
emit this->signal_projectNameChanged(newName);
}
/**
* @brief ApplicationContext::slot_loadProject initiates loading a project from a given path.
* The projects name will be inferred from the name of the project folder.
* @param projectDirPath is the path to the directory the project resides in
*/
void
ApplicationContext::slot_loadProject(QString projectDirPath) {
qDebug() << "Loading project with path " << projectDirPath;
// check, if all necessary files exist
QFile componentTreeFile(projectDirPath + FILE_COMPONENT_TREE);
Q_ASSERT(componentTreeFile.exists());
QString projectName = projectDirPath.split("/").last();
// extract project name from path
// TODO in case of failure emit a signal
// that makes the main window show a warning message
// unload current Project, if needed
if (m_currentProject != nullptr) {
this->unloadProject();
}
Q_ASSERT(m_currentProject == nullptr);
this->createProject(projectName);
// load component hierarchy
if (!componentTreeFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "On loading component hierarchy: Could not open file "
<< componentTreeFile.fileName();
return;
}
QJsonDocument hierarchyJson = QJsonDocument::fromJson(componentTreeFile.readAll());
componentTreeFile.close();
m_componentFactory->importHierarchy(hierarchyJson);
// load documents
// find all files with appropriate extension
QDir projectDir(projectDirPath);
projectDir.setFilter(QDir::Files | QDir::Readable);
projectDir.setNameFilters(QStringList("*" + EXTENSION_DOCFILE));
QStringList docFilePaths = projectDir.entryList();
for (QString currentPath : docFilePaths) {
m_currentProject->loadDocument(projectDirPath + "/" + currentPath);
}
}
void
ApplicationContext::slot_unloadProject() {
if (!this->hasCurrentProject()) {
return;
}
this->unloadProject();
}