SILICON
Toggle main menu visibility
Loading...
Searching...
No Matches
projectDocumentController.cpp
Go to the documentation of this file.
1
/*
2
Copyright (c) 2026. Giulio Cocconi
3
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
*/
9
10
#include "
projectDocumentController.hpp
"
11
12
#include <algorithm>
13
#include <ranges>
14
#include <stdexcept>
15
#include <utility>
16
17
#include <QSignalBlocker>
18
#include <QWidget>
19
20
#include <
ui/circuit/diagram/scene/diagramScene.hpp
>
21
#include <
ui/circuit/editor/circuitEditor.hpp
>
22
#include <
ui/circuit/editor/componentCatalogOverlay.hpp
>
23
#include <
ui/documents/editorWorkspace.hpp
>
24
#include <
ui/project/projectDocumentPolicy.hpp
>
25
#include <
ui/project/projectSession.hpp
>
26
#include <
ui/project/projectTree.hpp
>
27
#include <
ui/shell/inputDialogUtils.hpp
>
28
29
namespace
SILICON::ui
{
30
using namespace
SILICON::core;
31
32
ProjectDocumentController::ProjectDocumentController
(
33
ProjectSession
&
session
,
EditorWorkspace
&
workspace
,
ProjectTree
& tree,
34
ComponentCatalogOverlay
&
catalog
, QUndoStack& history, QWidget*
dialogParent
)
35
: QObject(
dialogParent
),
36
session
(
session
),
37
workspace
(
workspace
),
38
projectTree
(&tree),
39
catalog
(
catalog
),
40
undoStack
(&history),
41
dialogParent
(
dialogParent
)
42
{
43
}
44
45
const
std::string&
ProjectDocumentController::currentDocumentPath
() const noexcept
46
{
47
return
session
.activeDocumentPath;
48
}
49
50
bool
ProjectDocumentController::activateDocument
(
const
std::string& documentPath)
51
{
52
if
(!
SILICON::project::documentTypeForPath
(documentPath)
53
|| !
session
.projectContext.documents().contains(documentPath))
54
return
false
;
55
56
if
(documentPath ==
session
.activeDocumentPath) {
57
selectDocument
(documentPath);
58
return
true
;
59
}
60
61
return
switchToDocument
(documentPath,
true
);
62
}
63
64
void
ProjectDocumentController::restoreProjectDocuments
(
65
const
std::vector<SILICON::project::Document>& documents,
66
const
std::string& activePath)
67
{
68
const
auto
active =
69
std::ranges::find(documents, activePath, &
SILICON::project::Document::getPath
);
70
if
(active == documents.end())
71
throw
std::runtime_error(
"Project-state command has no active document"
);
72
73
// Circuit payloads resolve their subcircuits against the project store, so the snapshot
74
// has to be published before it is deserialized. Both the store and the active path are
75
// put back when the snapshot cannot be loaded, leaving the workspace on its document.
76
const
auto
previousDocuments =
session
.projectContext.documents().getDocuments();
77
const
auto
previousActive =
session
.activeDocumentPath;
78
79
session
.projectContext.setDocuments(documents);
80
session
.activeDocumentPath = activePath;
81
try
{
82
const
auto
prepared =
workspace
.prepareDocument(*active);
83
workspace
.activateDocument(*prepared);
84
}
catch
(...) {
85
session
.projectContext.setDocuments(previousDocuments);
86
session
.activeDocumentPath = previousActive;
87
throw
;
88
}
89
90
rebuildTree
();
91
selectDocument
(activePath);
92
emit
projectDocumentsChanged
();
93
notifyActiveDocumentActivated
();
94
}
95
96
void
ProjectDocumentController::notifyActiveDocumentActivated
()
97
{
98
const
auto
type =
SILICON::project::documentTypeForPath
(
session
.activeDocumentPath);
99
const
auto
category = type ?
SILICON::project::categoryOf
(*type)
100
:
SILICON::project::DocumentCategory::Code
;
101
emit
activeDocumentChanged
(QString::fromStdString(
session
.activeDocumentPath),
102
category);
103
}
104
105
void
ProjectDocumentController::selectDocument
(
const
std::string& path)
106
{
107
projectTree
->selectDocument(path);
108
}
109
110
void
ProjectDocumentController::reportLoadFailure
(
111
const
SILICON::project::DocumentType
type,
const
std::string& reason)
112
{
113
const
auto
noun =
documentTypeName
(type);
114
SILICON::ui::inputDialog::critical
(
115
dialogParent
, tr(
"%1 Switch Error"
).arg(noun),
116
tr(
"Failed to load the selected %1:\n%2"
)
117
.arg(noun.toLower(), QString::fromStdString(reason)));
118
}
119
120
bool
ProjectDocumentController::switchToDocument
(
const
std::string& path,
121
const
bool
selectInTree)
122
{
123
const
auto
& store =
session
.projectContext.documents();
124
const
auto
* target = store.find(path);
125
if
(!target)
126
return
false
;
127
128
if
(path ==
session
.activeDocumentPath) {
129
if
(selectInTree)
130
selectDocument
(path);
131
return
true
;
132
}
133
134
const
auto
type = target->getType();
135
catalog
.dismiss();
136
137
if
(
workspace
.circuitEditor().scene()->getInteractionMode()
138
!= InteractionMode::NORMAL_MODE)
139
workspace
.circuitEditor().scene()->setInteractionMode(InteractionMode::NORMAL_MODE);
140
141
try
{
142
workspace
.flushActiveDocument();
143
}
catch
(
const
std::exception& e) {
144
const
auto
noun =
documentTypeName
(type);
145
SILICON::ui::inputDialog::warning
(
146
dialogParent
, tr(
"%1 Switch Error"
).arg(noun),
147
tr(
"Failed to save the current document before switching:\n%1"
).arg(e.what()));
148
return
false
;
149
}
150
151
target = store.find(path);
152
if
(!target)
153
return
false
;
154
155
// The incoming document is deserialized while detached from the workspace, so a
156
// malformed payload leaves the current document loaded and the active path untouched.
157
std::shared_ptr<EditorWorkspace::PreparedDocument> prepared;
158
try
{
159
prepared =
workspace
.prepareDocument(*target);
160
}
catch
(
const
std::exception& e) {
161
reportLoadFailure
(type, e.what());
162
return
false
;
163
}
164
165
const
auto
previousPath =
session
.activeDocumentPath;
166
session
.activeDocumentPath = path;
167
try
{
168
workspace
.activateDocument(*prepared);
169
}
catch
(
const
std::exception& e) {
170
session
.activeDocumentPath = previousPath;
171
reportLoadFailure
(type, e.what());
172
return
false
;
173
}
174
175
if
(selectInTree)
176
selectDocument
(path);
177
178
notifyActiveDocumentActivated
();
179
return
true
;
180
}
181
182
void
ProjectDocumentController::removeDocument
(
const
std::string& path)
183
{
184
const
auto
& store =
session
.projectContext.documents();
185
const
auto
* document = store.find(path);
186
if
(!document)
187
return
;
188
189
if
(
session
.activeDocumentPath == path) {
190
const
auto
fallback =
session
.firstCircuitPath(path);
191
if
(!fallback || !
switchToDocument
(*fallback,
true
))
192
return
;
193
}
194
195
session
.projectContext.removeDocument(path);
196
rebuildTree
();
197
selectDocument
(
session
.activeDocumentPath);
198
emit
projectDocumentsChanged
();
199
}
200
201
void
ProjectDocumentController::insertDocument
(
202
SILICON::project::Document
document,
const
std::optional<std::ptrdiff_t> insertAt,
203
const
bool
activate)
204
{
205
const
auto
& store =
session
.projectContext.documents();
206
const
auto
path = document.
getPath
();
207
if
(store.contains(path))
208
return
;
209
210
if
(insertAt)
211
session
.projectContext.insertDocument(
212
std::move(document),
213
static_cast<
std::size_t
>
(std::max<std::ptrdiff_t>(0, *insertAt)));
214
else
215
session
.projectContext.upsertDocument(std::move(document));
216
217
rebuildTree
();
218
emit
projectDocumentsChanged
();
219
if
(activate)
220
switchToDocument
(path,
true
);
221
}
222
223
void
ProjectDocumentController::rebuildTree
()
224
{
225
projectDocumentPolicy::ensureProjectDocuments
(
session
.projectContext);
226
const
auto
project
=
session
.currentProjectInfo.value_or(
227
projectDocumentPolicy::defaultProjectInfo
(
session
.currentFileName));
228
const
auto
& store =
session
.projectContext.documents();
229
projectTree
->rebuild(
project
, store.getDocuments(),
session
.activeDocumentPath);
230
}
231
232
}
// namespace SILICON::ui
circuitEditor.hpp
SILICON::project::Document
Definition
projectDocument.hpp:109
SILICON::project::Document::getPath
const std::string & getPath() const noexcept
Definition
projectDocument.cpp:210
SILICON::ui::ComponentCatalogOverlay
Definition
componentCatalogOverlay.hpp:46
SILICON::ui::EditorWorkspace
Selects and hosts editors while preserving detached document preparation.
Definition
editorWorkspace.hpp:39
SILICON::ui::ProjectDocumentController::catalog
ComponentCatalogOverlay & catalog
Definition
projectDocumentController.hpp:116
SILICON::ui::ProjectDocumentController::session
ProjectSession & session
Definition
projectDocumentController.hpp:113
SILICON::ui::ProjectDocumentController::notifyActiveDocumentActivated
void notifyActiveDocumentActivated()
Publishes the active document to observers.
Definition
projectDocumentController.cpp:96
SILICON::ui::ProjectDocumentController::removeDocument
void removeDocument(const std::string &path)
Definition
projectDocumentController.cpp:182
SILICON::ui::ProjectDocumentController::ProjectDocumentController
ProjectDocumentController(ProjectSession &session, EditorWorkspace &workspace, ProjectTree &tree, ComponentCatalogOverlay &catalog, QUndoStack &undoStack, QWidget *dialogParent)
Definition
projectDocumentController.cpp:32
SILICON::ui::ProjectDocumentController::workspace
EditorWorkspace & workspace
Definition
projectDocumentController.hpp:114
SILICON::ui::ProjectDocumentController::currentDocumentPath
const std::string & currentDocumentPath() const noexcept override
Definition
projectDocumentController.cpp:45
SILICON::ui::ProjectDocumentController::insertDocument
void insertDocument(SILICON::project::Document document, std::optional< std::ptrdiff_t > insertAt, bool activate)
Definition
projectDocumentController.cpp:201
SILICON::ui::ProjectDocumentController::reportLoadFailure
void reportLoadFailure(SILICON::project::DocumentType type, const std::string &reason)
Reports a rejected document activation to the user.
Definition
projectDocumentController.cpp:110
SILICON::ui::ProjectDocumentController::switchToDocument
bool switchToDocument(const std::string &path, bool selectInTree)
Definition
projectDocumentController.cpp:120
SILICON::ui::ProjectDocumentController::projectDocumentsChanged
void projectDocumentsChanged()
Emitted when the set of project documents was added to, changed, or removed.
SILICON::ui::ProjectDocumentController::activeDocumentChanged
void activeDocumentChanged(const QString &path, SILICON::project::DocumentCategory category)
Emitted after a document was loaded and made visible.
SILICON::ui::ProjectDocumentController::projectTree
ProjectTree * projectTree
Definition
projectDocumentController.hpp:115
SILICON::ui::ProjectDocumentController::rebuildTree
void rebuildTree()
Definition
projectDocumentController.cpp:223
SILICON::ui::ProjectDocumentController::selectDocument
void selectDocument(const std::string &path)
Definition
projectDocumentController.cpp:105
SILICON::ui::ProjectDocumentController::activateDocument
bool activateDocument(const std::string &path) override
Definition
projectDocumentController.cpp:50
SILICON::ui::ProjectDocumentController::dialogParent
QWidget * dialogParent
Definition
projectDocumentController.hpp:118
SILICON::ui::ProjectDocumentController::restoreProjectDocuments
void restoreProjectDocuments(const std::vector< SILICON::project::Document > &documents, const std::string &activePath)
Definition
projectDocumentController.cpp:64
SILICON::ui::ProjectDocumentController::undoStack
QUndoStack * undoStack
Definition
projectDocumentController.hpp:117
SILICON::ui::ProjectTree
Project document navigator used by the SILICON editor.
Definition
projectTree.hpp:40
componentCatalogOverlay.hpp
diagramScene.hpp
editorWorkspace.hpp
inputDialogUtils.hpp
SILICON::project
Definition
circuitDependencyGraph.cpp:30
SILICON::project::DocumentCategory::Code
@ Code
Definition
projectDocument.hpp:39
SILICON::project::categoryOf
constexpr DocumentCategory categoryOf(const DocumentType type)
Definition
projectDocument.hpp:81
SILICON::project::documentTypeForPath
std::optional< DocumentType > documentTypeForPath(const std::string_view path)
Definition
projectDocument.cpp:165
SILICON::project::DocumentType
DocumentType
Definition
projectDocument.hpp:37
SILICON::ui::inputDialog::critical
void critical(QWidget *parent, const QString &title, const QString &text)
Definition
inputDialogUtils.cpp:110
SILICON::ui::inputDialog::warning
void warning(QWidget *parent, const QString &title, const QString &text)
Definition
inputDialogUtils.cpp:98
SILICON::ui::projectDocumentPolicy::defaultProjectInfo
SILICON::project::ProjectInfo defaultProjectInfo(const QString &fileName)
Definition
projectDocumentPolicy.cpp:24
SILICON::ui::projectDocumentPolicy::ensureProjectDocuments
void ensureProjectDocuments(SILICON::project::ProjectContext &context)
Definition
projectDocumentPolicy.cpp:32
SILICON::ui
Definition
actionGroups.hpp:14
SILICON::ui::documentTypeName
QString documentTypeName(SILICON::project::DocumentType type)
Definition
uiUtils.hpp:30
projectDocumentController.hpp
projectDocumentPolicy.hpp
projectSession.hpp
projectTree.hpp
SILICON::ui::ProjectSession
Definition
projectSession.hpp:26
src
ui
project
projectDocumentController.cpp
Generated by
1.17.0