SILICON
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
11
12#include <algorithm>
13#include <ranges>
14#include <stdexcept>
15#include <utility>
16
17#include <QSignalBlocker>
18#include <QWidget>
19
28
29namespace SILICON::ui {
30using namespace SILICON::core;
31
44
45const std::string& ProjectDocumentController::currentDocumentPath() const noexcept
46{
47 return session.activeDocumentPath;
48}
49
50bool ProjectDocumentController::activateDocument(const std::string& documentPath)
51{
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
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
91 selectDocument(activePath);
94}
95
97{
98 const auto type = SILICON::project::documentTypeForPath(session.activeDocumentPath);
99 const auto category = type ? SILICON::project::categoryOf(*type)
101 emit activeDocumentChanged(QString::fromStdString(session.activeDocumentPath),
102 category);
103}
104
105void ProjectDocumentController::selectDocument(const std::string& path)
106{
107 projectTree->selectDocument(path);
108}
109
111 const SILICON::project::DocumentType type, const std::string& reason)
112{
113 const auto noun = documentTypeName(type);
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
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);
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
179 return true;
180}
181
182void 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);
199}
200
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();
219 if (activate)
220 switchToDocument(path, true);
221}
222
224{
226 const auto project = session.currentProjectInfo.value_or(
228 const auto& store = session.projectContext.documents();
229 projectTree->rebuild(project, store.getDocuments(), session.activeDocumentPath);
230}
231
232} // namespace SILICON::ui
const std::string & getPath() const noexcept
Selects and hosts editors while preserving detached document preparation.
void notifyActiveDocumentActivated()
Publishes the active document to observers.
ProjectDocumentController(ProjectSession &session, EditorWorkspace &workspace, ProjectTree &tree, ComponentCatalogOverlay &catalog, QUndoStack &undoStack, QWidget *dialogParent)
const std::string & currentDocumentPath() const noexcept override
void insertDocument(SILICON::project::Document document, std::optional< std::ptrdiff_t > insertAt, bool activate)
void reportLoadFailure(SILICON::project::DocumentType type, const std::string &reason)
Reports a rejected document activation to the user.
bool switchToDocument(const std::string &path, bool selectInTree)
void projectDocumentsChanged()
Emitted when the set of project documents was added to, changed, or removed.
void activeDocumentChanged(const QString &path, SILICON::project::DocumentCategory category)
Emitted after a document was loaded and made visible.
bool activateDocument(const std::string &path) override
void restoreProjectDocuments(const std::vector< SILICON::project::Document > &documents, const std::string &activePath)
Project document navigator used by the SILICON editor.
constexpr DocumentCategory categoryOf(const DocumentType type)
std::optional< DocumentType > documentTypeForPath(const std::string_view path)
void critical(QWidget *parent, const QString &title, const QString &text)
void warning(QWidget *parent, const QString &title, const QString &text)
SILICON::project::ProjectInfo defaultProjectInfo(const QString &fileName)
void ensureProjectDocuments(SILICON::project::ProjectContext &context)
QString documentTypeName(SILICON::project::DocumentType type)
Definition uiUtils.hpp:30