15#include <QTextDocument>
38 if (
auto* code =
dynamic_cast<CodeEditor*
>(current))
39 code->zoomBy(in ? 1 : -1);
40 else if (
auto* visualizer =
dynamic_cast<SislVisualizer*
>(current))
49 struct PreparedBinary final : PreparedEditorDocument {
52 struct PreparedArchitecture final : PreparedEditorDocument {
53 std::shared_ptr<ArchitectureLoadPlan> plan;
63 callbacks.connect(
codeEditor->document(), &QTextDocument::modificationChanged, [
this](
bool modified) {
64 if (modified && codeEditor->fileType())
68 [historyChanged](
bool) { historyChanged(); });
70 [historyChanged](
bool) { historyChanged(); });
72 [editStateChanged] { editStateChanged(); });
80std::shared_ptr<PreparedEditorDocument>
83 auto result = std::make_shared<PreparedText>();
84 result->type = document.
getType();
90 const auto* text =
dynamic_cast<const PreparedText*
>(&prepared);
92 throw std::logic_error(
"Invalid prepared code document");
94 codeEditor->setPlainText(QString::fromStdString(text->contents));
100 session.projectContext.upsertDocument({path,
codeEditor->toPlainText().toStdString()});
118 if (
codeEditor->document()->isUndoAvailable())
125 if (
codeEditor->document()->isRedoAvailable())
132 return codeEditor->document()->isUndoAvailable()
137 return codeEditor->document()->isRedoAvailable()
155 if (cursor.hasSelection())
156 cursor.removeSelectedText();
163 const bool hasSelection =
codeEditor->textCursor().hasSelection();
164 return {.canCut = hasSelection,
165 .canCopy = hasSelection,
167 .canDelete = hasSelection};
177 const auto type = SILICON::project::documentTypeForPath(
178 this->session.activeDocumentPath);
180 && SILICON::project::categoryOf(*type)
181 == SILICON::project::DocumentCategory::Binary)
185 [historyChanged](
bool) { historyChanged(); });
187 [historyChanged](
bool) { historyChanged(); });
195std::shared_ptr<PreparedEditorDocument>
198 auto result = std::make_shared<PreparedBinary>();
204 const auto* binary =
dynamic_cast<const PreparedBinary*
>(&prepared);
206 throw std::logic_error(
"Invalid prepared binary document");
207 binaryEditor->setData(QByteArray(binary->contents.data(),
208 static_cast<qsizetype
>(binary->contents.size())));
214 session.projectContext.upsertDocument(
215 {path, std::string(data.constData(),
static_cast<std::size_t
>(data.size()))});
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(); });
275 callbacks.connect(architectureWorkspace.get(), &QTabWidget::currentChanged, [historyChanged, editStateChanged](
int) {
280ArchitectureDocumentEditor::~ArchitectureDocumentEditor() =
default;
286std::shared_ptr<PreparedEditorDocument>
289 auto result = std::make_shared<PreparedArchitecture>();
291 document,
session.projectContext.documents());
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");
306 throw std::invalid_argument(
"Architecture component has no editor");
307 session.projectContext.upsertDocument({path, editor->toPlainText().toStdString()});
308 editor->document()->setModified(
false);
343 const bool hasSelection = editor->textCursor().hasSelection();
344 return {.canCut = hasSelection,
345 .canCopy = hasSelection,
346 .canPaste = editor->canPaste(),
347 .canDelete = hasSelection};
355 if (editor && editor->document()->isUndoAvailable())
366 if (editor && editor->document()->isRedoAvailable())
376 && ((editor && editor->document()->isUndoAvailable())
384 && ((editor && editor->document()->isRedoAvailable())
391 if (
auto* code =
dynamic_cast<CodeEditor*
>(editor))
413 auto cursor = editor->textCursor();
414 if (cursor.hasSelection())
415 cursor.removeSelectedText();
418 editor->setTextCursor(cursor);
DocumentType getType() const noexcept
const std::string & getContents() const noexcept
bool isVisualizerActive() const noexcept
void resetDirtyState() noexcept override
bool canUndo() const override
void flush(const std::string &) override
bool canRedo() const 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
bool isDirty() const override
void deleteSelection() override
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)
bool canUndo() const override
void flush(const std::string &) override
bool canRedo() const override
QWidget * widget() const noexcept override
void zoom(bool in) override
Zooms the document in (true) or out (false) by one step.
~BinaryDocumentEditor() override
bool isDirty() const override
std::shared_ptr< PreparedEditorDocument > prepare(const SILICON::project::Document &) override
void apply(const PreparedEditorDocument &) override
void resetDirtyState() noexcept override
std::unique_ptr< CodeEditor > codeEditor
bool canUndo() const override
CodeDocumentEditor(ProjectSession &session, QWidget *parent, EditorNotifications notifications)
void flush(const std::string &) override
bool canRedo() const 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.
bool isDirty() const override
void deleteSelection() override
std::shared_ptr< PreparedEditorDocument > prepare(const SILICON::project::Document &) override
~CodeDocumentEditor() override
void apply(const PreparedEditorDocument &) override
QUndoStack * projectHistory
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.