SILICON
Loading...
Searching...
No Matches
architectureWorkspace.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 <memory>
14#include <ranges>
15#include <stdexcept>
16#include <utility>
17#include <vector>
18
19#include <QSignalBlocker>
20#include <QTabBar>
21#include <QTextDocument>
22
27
28namespace SILICON::ui {
29
47
48ArchitectureWorkspace::ArchitectureWorkspace(QWidget* parent) : QTabWidget(parent)
49{
50 visualizer = new SislVisualizer(this);
51 for (const auto& component : codeFilePresentations()) {
52 if (!component.architectureTabName)
53 continue;
54 auto* codeEditor = new CodeEditor(this);
55 codeEditor->setFileType(component.type);
56 codeEditors.emplace(component.type, codeEditor);
57 callbacks.connect(codeEditor->document(), &QTextDocument::modificationChanged,
58 [this](bool modified) {
59 if (modified)
60 emit documentModified();
61 });
62 callbacks.connect(codeEditor, &QPlainTextEdit::textChanged, [this] {
63 const int index = indexOf(visualizer);
64 if (index >= 0) {
65 visualizer->hide();
66 removeTab(index);
67 }
68 });
69 }
70}
71
74{
75 const auto it = codeEditors.find(type);
76 return it == codeEditors.end() ? nullptr : it->second;
77}
78
80{
81 return std::ranges::any_of(
82 codeEditors, [](const auto& entry) { return entry.second->document()->isModified(); });
83}
84
86{
87 return currentWidget() == visualizer;
88}
89
90bool ArchitectureWorkspace::isVisualizerTab(int index) const noexcept
91{
92 return widget(index) == visualizer;
93}
94
100
101std::shared_ptr<ArchitectureLoadPlan>
104{
105 const auto name = SILICON::project::documentSlugForPath(document.getPath());
106 if (!name)
107 throw std::invalid_argument("Architecture document has no architecture name");
108
109 // The plan resolves every file of the architecture up front, so the tabs are only
110 // rebuilt once the whole architecture is known to be loadable.
111 auto plan = std::make_shared<ArchitectureLoadPlan>();
112 plan->name = *name;
113 plan->activeType = document.getType();
114
115 for (const auto& component : codeFilePresentations()) {
116 if (!component.architectureTabName)
117 continue;
118 const auto path = SILICON::project::documentPathForSlug(component.type, *name);
119 const auto* file = store.find(path);
120 if (!file)
121 continue;
122 if (!editor(component.type))
123 throw std::logic_error("Registered architecture component has no editor");
124 plan->files.push_back({component.type, component.architectureTabName,
125 QString::fromStdString(file->getContents())});
126 }
127
128 if (std::ranges::none_of(plan->files, [&plan](const auto& file) {
129 return file.type == plan->activeType;
130 }))
131 throw std::invalid_argument("Architecture component is not registered");
132
133 return plan;
134}
135
137{
138 const QSignalBlocker blocker(this);
139 visualizer->hide();
140 clear();
141 const bool newArchitecture = loadedArchitectureName != plan.name;
142 int activeTab = -1;
143 for (const auto& file : plan.files) {
144 auto* codeEditor = editor(file.type);
145 if (!codeEditor)
146 throw std::logic_error("Registered architecture component has no editor");
147 if (newArchitecture || codeEditor->toPlainText() != file.contents)
148 codeEditor->setPlainText(file.contents);
149 codeEditor->document()->setModified(false);
150 const int tab = addTab(codeEditor, tr(file.tabName));
151 tabBar()->setTabData(tab, static_cast<int>(file.type));
152 if (file.type == plan.activeType)
153 activeTab = tab;
154 }
156 setCurrentIndex(activeTab);
157 if (auto* active = editor(plan.activeType))
158 active->setFocus();
159}
160
161void ArchitectureWorkspace::showVisualization(sisl::IsaDescription description)
162{
163 visualizer->setDescription(std::move(description));
164 if (indexOf(visualizer) < 0)
165 addTab(visualizer, tr("Visualizer"));
166 setCurrentWidget(visualizer);
167}
168
169} // namespace SILICON::ui
const Document * find(std::string_view documentPath) const noexcept
DocumentType getType() const noexcept
const std::string & getPath() const noexcept
Architecture files resolved before the editor tabs are rebuilt.
SILICON::project::DocumentType activeType
File that should be focused once the tabs exist.
std::string name
Architecture identifier shared by every file of the plan.
std::vector< File > files
Architecture files to show as tabs, in presentation order.
std::map< SILICON::project::DocumentType, CodeEditor * > codeEditors
void loadDocument(const SILICON::project::Document &document, const SILICON::project::DocumentStore &store)
ArchitectureWorkspace(QWidget *parent=nullptr)
CodeEditor * editor(SILICON::project::DocumentType type) const noexcept
bool isVisualizerTab(int index) const noexcept
void applyLoadPlan(const ArchitectureLoadPlan &plan)
Rebuilds the architecture tabs from a prepared plan.
std::shared_ptr< ArchitectureLoadPlan > prepareLoadPlan(const SILICON::project::Document &document, const SILICON::project::DocumentStore &store)
Resolves the architecture tabs of document without touching the tabs.
void showVisualization(sisl::IsaDescription description)
std::string documentPathForSlug(const DocumentType type, const std::string_view slug)
std::optional< std::string > documentSlugForPath(const std::string_view path)
std::span< const CodeFilePresentation > codeFilePresentations()
One architecture file with its tab label and stored contents.