SILICON
Loading...
Searching...
No Matches
editorWorkspace.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#include "editorWorkspace.hpp"
10
11#include <stdexcept>
12
13#include <QApplication>
14#include <QClipboard>
15#include <QStackedWidget>
16#include <QUndoStack>
17#include <QVBoxLayout>
18
22
23namespace SILICON::ui {
25public:
27 std::shared_ptr<PreparedEditorDocument> payload)
28 : editor(&editor), payload(std::move(payload))
29 {
30 }
32 std::shared_ptr<PreparedEditorDocument> payload;
33};
34
36 : QWidget(parent), session(session), stack(new QStackedWidget(this))
37{
38 auto* layout = new QVBoxLayout(this);
39 layout->setContentsMargins(0, 0, 0, 0);
40 layout->addWidget(stack);
43 [this](DocumentEditor* editor) { editor->setProjectHistory(projectHistory); });
44 // The clipboard is global while the editors answering editState() are local, so it is
45 // observed exactly once here. The active editor still decides what its contents mean.
46 callbacks.connect(QApplication::clipboard(), &QClipboard::dataChanged,
47 [this] { emit editStateChanged(); });
48}
50
52{
53 return {callbacks.guard([this] { emit historyStateChanged(); }),
54 callbacks.guard([this] { emit editStateChanged(); })};
55}
56
58{
59 if (!circuit) {
60 circuit = std::make_unique<CircuitEditor>(session, stack, notifications());
61 stack->addWidget(circuit->widget());
62 emit editorCreated(circuit.get());
63 }
64 return *circuit;
65}
67{
68 if (!code) {
69 code = std::make_unique<CodeDocumentEditor>(session, stack, notifications());
70 stack->addWidget(code->widget());
71 emit editorCreated(code.get());
72 }
73 return *code;
74}
76{
77 if (!binary) {
78 binary = std::make_unique<BinaryDocumentEditor>(session, stack, notifications());
79 stack->addWidget(binary->widget());
80 emit editorCreated(binary.get());
81 }
82 return *binary;
83}
85{
86 if (!architecture) {
88 std::make_unique<ArchitectureDocumentEditor>(session, stack, notifications());
89 stack->addWidget(architecture->widget());
90 emit editorCreated(architecture.get());
91 }
92 return *architecture;
93}
95{
97 switch (SILICON::project::categoryOf(type)) {
98 case DocumentCategory::Diagram: return circuitEditor();
99 case DocumentCategory::Code: return ensureCodeEditor();
100 case DocumentCategory::Binary: return ensureBinaryEditor();
101 case DocumentCategory::Architecture: return ensureArchitectureEditor();
102 }
103 throw std::logic_error("Unsupported project document type");
104}
105void EditorWorkspace::setProjectHistory(QUndoStack* history) noexcept
106{
107 if (projectHistory == history)
108 return;
109 if (projectHistory)
110 disconnect(projectHistory, nullptr, callbacks.context(), nullptr);
111
112 projectHistory = history;
113 if (projectHistory) {
114 // Every editor falls back to the project history when its own document has nothing
115 // left to undo, so its state has to be part of the workspace notifications.
116 callbacks.connect(projectHistory, &QUndoStack::canUndoChanged,
117 [this] { emit historyStateChanged(); });
118 callbacks.connect(projectHistory, &QUndoStack::canRedoChanged,
119 [this] { emit historyStateChanged(); });
120 }
121 if (circuit)
122 circuit->setProjectHistory(projectHistory);
123 if (code)
124 code->setProjectHistory(projectHistory);
125 if (binary)
126 binary->setProjectHistory(projectHistory);
127 if (architecture)
128 architecture->setProjectHistory(projectHistory);
129}
135{
136 return (circuit && circuit->isDirty()) || (code && code->isDirty())
137 || (binary && binary->isDirty()) || (architecture && architecture->isDirty());
138}
148{
149 if (circuit)
150 circuit->resetDirtyState();
151 if (code)
152 code->resetDirtyState();
153 if (binary)
154 binary->resetDirtyState();
155 if (architecture)
156 architecture->resetDirtyState();
157}
159{
160 if (architecture)
161 architecture->reset();
162 if (code)
163 code->reset();
164 if (binary)
165 binary->reset();
167 stack->setCurrentWidget(circuit->widget());
169 emit activeEditorChanged();
170}
182{
183 if (session.activeDocumentPath.empty()) {
184 const auto fallback = session.firstCircuitPath();
185 if (!fallback)
186 throw std::runtime_error("Project has no circuit document");
187 session.activeDocumentPath = *fallback;
188 }
189 const auto* document =
190 session.projectContext.documents().find(session.activeDocumentPath);
191 if (!document)
192 throw std::runtime_error("Active project document is missing");
194 throw std::logic_error("No active editor");
195 activeDocumentEditor->flush(session.activeDocumentPath);
196}
197std::shared_ptr<EditorWorkspace::PreparedDocument>
199{
200 auto& editor = editorFor(document.getType());
201 return std::make_shared<PreparedDocument>(editor, editor.prepare(document));
202}
204{
205 if (!prepared.editor || !prepared.payload)
206 throw std::logic_error("Prepared document has no editor payload");
207 prepared.editor->apply(*prepared.payload);
208 stack->setCurrentWidget(prepared.editor->widget());
209 activeDocumentEditor = prepared.editor;
210 emit activeEditorChanged();
211}
241} // namespace SILICON::ui
DocumentType getType() const noexcept
void setProjectHistory(QUndoStack *history) noexcept
virtual QWidget * widget() const noexcept=0
virtual void apply(const PreparedEditorDocument &prepared)=0
PreparedDocument(DocumentEditor &editor, std::shared_ptr< PreparedEditorDocument > payload)
std::shared_ptr< PreparedEditorDocument > payload
void setProjectHistory(QUndoStack *history) noexcept
Applies the project-wide history shared by every hosted editor.
std::unique_ptr< BinaryDocumentEditor > binary
std::unique_ptr< ArchitectureDocumentEditor > architecture
DocumentEditor & editorFor(SILICON::project::DocumentType type)
void activateDocument(const PreparedDocument &prepared)
std::unique_ptr< CodeDocumentEditor > code
ArchitectureDocumentEditor & ensureArchitectureEditor()
void editorCreated(DocumentEditor *editor)
EditorNotifications notifications()
void editStateChanged()
Emitted when the active editor's editing availability may have changed.
std::unique_ptr< CircuitEditor > circuit
std::shared_ptr< PreparedDocument > prepareDocument(const SILICON::project::Document &document)
CodeDocumentEditor & ensureCodeEditor()
void historyStateChanged()
Emitted when the active editor's undo/redo availability may have changed.
EditorWorkspace(ProjectSession &session, QWidget *parent=nullptr)
void loadDocument(const SILICON::project::Document &document)
EditorEditState activeEditState() const
Which generic editing commands the active editor currently accepts.
BinaryDocumentEditor & ensureBinaryEditor()
constexpr DocumentCategory categoryOf(const DocumentType type)