SILICON
Loading...
Searching...
No Matches
documentEditors.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 "documentEditors.hpp"
11
12#include <stdexcept>
13
14#include <QTabWidget>
15#include <QTextDocument>
16#include <QUndoStack>
17
23
24namespace SILICON::ui {
26{
27 codeEditor->zoomBy(in ? 1 : -1);
28}
29
31{
32 binaryEditor->zoomBy(in ? 1 : -1);
33}
34
36{
37 auto* current = architectureWorkspace->currentWidget();
38 if (auto* code = dynamic_cast<CodeEditor*>(current))
39 code->zoomBy(in ? 1 : -1);
40 else if (auto* visualizer = dynamic_cast<SislVisualizer*>(current))
41 visualizer->zoom(in);
42}
43
44namespace {
45 struct PreparedText final : PreparedEditorDocument {
47 std::string contents;
48 };
49 struct PreparedBinary final : PreparedEditorDocument {
50 std::string contents;
51 };
52 struct PreparedArchitecture final : PreparedEditorDocument {
53 std::shared_ptr<ArchitectureLoadPlan> plan;
54 };
55} // namespace
56
58 EditorNotifications notifications)
59 : session(session), codeEditor(std::make_unique<CodeEditor>(parent))
60{
61 const auto& historyChanged = notifications.historyChanged;
62 const auto& editStateChanged = notifications.editStateChanged;
63 callbacks.connect(codeEditor->document(), &QTextDocument::modificationChanged, [this](bool modified) {
64 if (modified && codeEditor->fileType())
65 dirty = true;
66 });
67 callbacks.connect(codeEditor.get(), &QPlainTextEdit::undoAvailable,
68 [historyChanged](bool) { historyChanged(); });
69 callbacks.connect(codeEditor.get(), &QPlainTextEdit::redoAvailable,
70 [historyChanged](bool) { historyChanged(); });
71 callbacks.connect(codeEditor.get(), &QPlainTextEdit::selectionChanged,
72 [editStateChanged] { editStateChanged(); });
73}
75
76QWidget* CodeDocumentEditor::widget() const noexcept
77{
78 return codeEditor.get();
79}
80std::shared_ptr<PreparedEditorDocument>
82{
83 auto result = std::make_shared<PreparedText>();
84 result->type = document.getType();
85 result->contents = document.getContents();
86 return result;
87}
89{
90 const auto* text = dynamic_cast<const PreparedText*>(&prepared);
91 if (!text)
92 throw std::logic_error("Invalid prepared code document");
93 codeEditor->setFileType(text->type);
94 codeEditor->setPlainText(QString::fromStdString(text->contents));
95 codeEditor->document()->setModified(false);
96 codeEditor->setFocus();
97}
98void CodeDocumentEditor::flush(const std::string& path)
99{
100 session.projectContext.upsertDocument({path, codeEditor->toPlainText().toStdString()});
101 codeEditor->document()->setModified(false);
102}
104{
105 return dirty || codeEditor->document()->isModified();
106}
108{
109 dirty = false;
110}
112{
113 codeEditor->clearFileType();
115}
117{
118 if (codeEditor->document()->isUndoAvailable())
119 codeEditor->undo();
120 else if (projectHistory)
121 projectHistory->undo();
122}
124{
125 if (codeEditor->document()->isRedoAvailable())
126 codeEditor->redo();
127 else if (projectHistory)
128 projectHistory->redo();
129}
131{
132 return codeEditor->document()->isUndoAvailable()
133 || (projectHistory && projectHistory->canUndo());
134}
136{
137 return codeEditor->document()->isRedoAvailable()
138 || (projectHistory && projectHistory->canRedo());
139}
141{
142 codeEditor->copy();
143}
145{
146 codeEditor->cut();
147}
149{
150 codeEditor->paste();
151}
153{
154 auto cursor = codeEditor->textCursor();
155 if (cursor.hasSelection())
156 cursor.removeSelectedText();
157 else
158 cursor.deleteChar();
159 codeEditor->setTextCursor(cursor);
160}
162{
163 const bool hasSelection = codeEditor->textCursor().hasSelection();
164 return {.canCut = hasSelection,
165 .canCopy = hasSelection,
166 .canPaste = codeEditor->canPaste(),
167 .canDelete = hasSelection};
168}
169
171 EditorNotifications notifications)
172 : session(session), binaryEditor(std::make_unique<BinaryEditor>(parent))
173{
174 const auto& historyChanged = notifications.historyChanged;
175 callbacks.connect(binaryEditor->history(), &QUndoStack::cleanChanged,
176 [this](bool clean) {
177 const auto type = SILICON::project::documentTypeForPath(
178 this->session.activeDocumentPath);
179 if (!clean && type
180 && SILICON::project::categoryOf(*type)
181 == SILICON::project::DocumentCategory::Binary)
182 dirty = true;
183 });
184 callbacks.connect(binaryEditor->history(), &QUndoStack::canUndoChanged,
185 [historyChanged](bool) { historyChanged(); });
186 callbacks.connect(binaryEditor->history(), &QUndoStack::canRedoChanged,
187 [historyChanged](bool) { historyChanged(); });
188}
190
191QWidget* BinaryDocumentEditor::widget() const noexcept
192{
193 return binaryEditor.get();
194}
195std::shared_ptr<PreparedEditorDocument>
197{
198 auto result = std::make_shared<PreparedBinary>();
199 result->contents = document.getContents();
200 return result;
201}
203{
204 const auto* binary = dynamic_cast<const PreparedBinary*>(&prepared);
205 if (!binary)
206 throw std::logic_error("Invalid prepared binary document");
207 binaryEditor->setData(QByteArray(binary->contents.data(),
208 static_cast<qsizetype>(binary->contents.size())));
209 binaryEditor->setFocus();
210}
211void BinaryDocumentEditor::flush(const std::string& path)
212{
213 const auto& data = binaryEditor->data();
214 session.projectContext.upsertDocument(
215 {path, std::string(data.constData(), static_cast<std::size_t>(data.size()))});
216 binaryEditor->setModified(false);
217}
219{
220 return dirty || binaryEditor->isModified();
221}
223{
224 dirty = false;
225}
227{
228 binaryEditor->setData({});
230}
232{
233 if (binaryEditor->history()->canUndo())
234 binaryEditor->history()->undo();
235 else if (projectHistory)
236 projectHistory->undo();
237}
239{
240 if (binaryEditor->history()->canRedo())
241 binaryEditor->history()->redo();
242 else if (projectHistory)
243 projectHistory->redo();
244}
246{
247 return binaryEditor->history()->canUndo()
248 || (projectHistory && projectHistory->canUndo());
249}
251{
252 return binaryEditor->history()->canRedo()
253 || (projectHistory && projectHistory->canRedo());
254}
255
257 QWidget* parent,
258 EditorNotifications notifications)
259 : session(session), architectureWorkspace(std::make_unique<ArchitectureWorkspace>(parent))
260{
261 const auto& historyChanged = notifications.historyChanged;
262 const auto& editStateChanged = notifications.editStateChanged;
263 callbacks.connect(architectureWorkspace.get(), &ArchitectureWorkspace::documentModified, [this] { dirty = true; });
264 for (const auto& [type, editor] : architectureWorkspace->editors()) {
265 callbacks.connect(editor, &QPlainTextEdit::undoAvailable,
266 [historyChanged](bool) { historyChanged(); });
267 callbacks.connect(editor, &QPlainTextEdit::redoAvailable,
268 [historyChanged](bool) { historyChanged(); });
269 callbacks.connect(editor, &QPlainTextEdit::selectionChanged,
270 [editStateChanged] { editStateChanged(); });
271 }
272 // Selecting a tab swaps the editor owning the architecture document, and the visualizer
273 // tab replaces editing altogether, so a tab change alters both what can be undone and
274 // which editing commands apply.
275 callbacks.connect(architectureWorkspace.get(), &QTabWidget::currentChanged, [historyChanged, editStateChanged](int) {
276 historyChanged();
277 editStateChanged();
278 });
279}
280ArchitectureDocumentEditor::~ArchitectureDocumentEditor() = default;
281
282QWidget* ArchitectureDocumentEditor::widget() const noexcept
283{
284 return architectureWorkspace.get();
285}
286std::shared_ptr<PreparedEditorDocument>
288{
289 auto result = std::make_shared<PreparedArchitecture>();
290 result->plan = architectureWorkspace->prepareLoadPlan(
291 document, session.projectContext.documents());
292 return result;
293}
295{
296 const auto* architecture = dynamic_cast<const PreparedArchitecture*>(&prepared);
297 if (!architecture || !architecture->plan)
298 throw std::logic_error("Prepared architecture document has no load plan");
299 architectureWorkspace->applyLoadPlan(*architecture->plan);
300}
301void ArchitectureDocumentEditor::flush(const std::string& path)
302{
303 const auto type = SILICON::project::documentTypeForPath(path);
304 auto* editor = type ? architectureWorkspace->editor(*type) : nullptr;
305 if (!editor)
306 throw std::invalid_argument("Architecture component has no editor");
307 session.projectContext.upsertDocument({path, editor->toPlainText().toStdString()});
308 editor->document()->setModified(false);
309}
311{
312 return dirty || architectureWorkspace->hasModifiedEditors();
313}
315{
316 dirty = false;
317}
319{
320 architectureWorkspace->resetLoadedArchitecture();
322}
329{
330 return architectureWorkspace->isVisualizerActive();
331}
333{
334 // The visualizer replaces the source editors, so no editing command applies to it.
335 if (isVisualizerActive())
336 return {};
337
338 const auto type = SILICON::project::documentTypeForPath(session.activeDocumentPath);
339 auto* editor = type ? activeCodeEditor(*type) : nullptr;
340 if (!editor)
341 return {};
342
343 const bool hasSelection = editor->textCursor().hasSelection();
344 return {.canCut = hasSelection,
345 .canCopy = hasSelection,
346 .canPaste = editor->canPaste(),
347 .canDelete = hasSelection};
348}
350{
351 if (isVisualizerActive())
352 return;
353 const auto type = SILICON::project::documentTypeForPath(session.activeDocumentPath);
354 auto* editor = type ? activeCodeEditor(*type) : nullptr;
355 if (editor && editor->document()->isUndoAvailable())
356 editor->undo();
357 else if (projectHistory)
358 projectHistory->undo();
359}
361{
362 if (isVisualizerActive())
363 return;
364 const auto type = SILICON::project::documentTypeForPath(session.activeDocumentPath);
365 auto* editor = type ? activeCodeEditor(*type) : nullptr;
366 if (editor && editor->document()->isRedoAvailable())
367 editor->redo();
368 else if (projectHistory)
369 projectHistory->redo();
370}
372{
373 const auto type = SILICON::project::documentTypeForPath(session.activeDocumentPath);
374 auto* editor = type ? activeCodeEditor(*type) : nullptr;
375 return !isVisualizerActive()
376 && ((editor && editor->document()->isUndoAvailable())
377 || (projectHistory && projectHistory->canUndo()));
378}
380{
381 const auto type = SILICON::project::documentTypeForPath(session.activeDocumentPath);
382 auto* editor = type ? activeCodeEditor(*type) : nullptr;
383 return !isVisualizerActive()
384 && ((editor && editor->document()->isRedoAvailable())
385 || (projectHistory && projectHistory->canRedo()));
386}
388{
389 if (!isVisualizerActive())
390 if (auto* editor = architectureWorkspace->currentWidget())
391 if (auto* code = dynamic_cast<CodeEditor*>(editor))
392 code->copy();
393}
395{
396 if (!isVisualizerActive())
397 if (auto* code = dynamic_cast<CodeEditor*>(architectureWorkspace->currentWidget()))
398 code->cut();
399}
401{
402 if (!isVisualizerActive())
403 if (auto* code = dynamic_cast<CodeEditor*>(architectureWorkspace->currentWidget()))
404 code->paste();
405}
407{
408 if (isVisualizerActive())
409 return;
410 auto* editor = dynamic_cast<CodeEditor*>(architectureWorkspace->currentWidget());
411 if (!editor)
412 return;
413 auto cursor = editor->textCursor();
414 if (cursor.hasSelection())
415 cursor.removeSelectedText();
416 else
417 cursor.deleteChar();
418 editor->setTextCursor(cursor);
419}
420
421} // namespace SILICON::ui
DocumentType getType() const noexcept
const std::string & getContents() const noexcept
void flush(const std::string &) override
CodeEditor * activeCodeEditor(SILICON::project::DocumentType type) const noexcept
ArchitectureDocumentEditor(ProjectSession &session, QWidget *parent, EditorNotifications notifications)
QWidget * widget() const noexcept override
void zoom(bool in) override
Zooms the document in (true) or out (false) by one step.
EditorEditState editState() const override
Reports which generic editing commands the editor currently accepts.
std::unique_ptr< ArchitectureWorkspace > architectureWorkspace
std::shared_ptr< PreparedEditorDocument > prepare(const SILICON::project::Document &) override
void apply(const PreparedEditorDocument &) override
std::unique_ptr< BinaryEditor > binaryEditor
void resetDirtyState() noexcept override
BinaryDocumentEditor(ProjectSession &session, QWidget *parent, EditorNotifications notifications)
void flush(const std::string &) override
QWidget * widget() const noexcept override
void zoom(bool in) override
Zooms the document in (true) or out (false) by one step.
std::shared_ptr< PreparedEditorDocument > prepare(const SILICON::project::Document &) override
void apply(const PreparedEditorDocument &) override
void resetDirtyState() noexcept override
std::unique_ptr< CodeEditor > codeEditor
CodeDocumentEditor(ProjectSession &session, QWidget *parent, EditorNotifications notifications)
void flush(const std::string &) override
QWidget * widget() const noexcept override
void zoom(bool in) override
Zooms the document in (true) or out (false) by one step.
EditorEditState editState() const override
Reports which generic editing commands the editor currently accepts.
std::shared_ptr< PreparedEditorDocument > prepare(const SILICON::project::Document &) override
void apply(const PreparedEditorDocument &) override
std::optional< DocumentType > documentTypeForPath(const std::string_view path)
std::function< void()> historyChanged
The undo/redo availability of the hosted editor may have changed.
std::function< void()> editStateChanged
The generic editing availability of the hosted editor may have changed.