SILICON
Loading...
Searching...
No Matches
architectureController.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 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
19
20#include <format>
21#include <optional>
22#include <string>
23#include <string_view>
24
25#include <QAction>
26#include <QSignalBlocker>
27#include <QTabBar>
28#include <QTabWidget>
29
30#include <sisl/sisl.hpp>
31
33#include <logging/logger.hpp>
41
42namespace SILICON::ui {
43namespace {
44 const SILICON::logging::Logger isaLog("sisl");
45
52 [[nodiscard]] bool isArchitectureDocument(const ProjectSession& session)
53 {
54 return SILICON::project::documentTypeForPath(session.activeDocumentPath)
56 }
57
63 std::optional<sisl::Isa> buildEditedArchitecture(const std::string_view documentPath,
64 const CodeEditor& editor)
65 {
66 const auto result = SILICON::project::buildArchitecture(
68 editor.toPlainText().toStdString());
69 if (result)
70 return result.isa;
71 for (const auto& diagnostic : result.diagnostics) {
72 const auto location =
73 diagnostic.source
74 ? std::format("{}:{}:{}", documentPath, diagnostic.source->line.value_or(0),
75 diagnostic.source->column.value_or(0))
76 : std::string(documentPath);
77 isaLog.error(std::format("{}: [{}] {}", location, sisl::to_string(diagnostic.code),
78 diagnostic.message));
79 }
80 if (result.error)
81 isaLog.error(std::format("{}: {}", documentPath, *result.error));
82 return std::nullopt;
83 }
84} // namespace
85
95
97{
98 connect(actions.build, &QAction::triggered, this,
100 connect(actions.visualize, &QAction::triggered, this,
102}
103
105{
106 architectureEditor = dynamic_cast<ArchitectureDocumentEditor*>(editor);
108 return;
109 auto* architecture = architectureEditor->workspace();
110 architecture->tabBar()->setContextMenuPolicy(Qt::CustomContextMenu);
111 connect(architecture->tabBar(), &QWidget::customContextMenuRequested, &documents,
113 connect(architecture, &QTabWidget::currentChanged, this,
115}
116
118{
119 auto* architecture = architectureEditor ? architectureEditor->workspace() : nullptr;
120 if (!architecture)
121 return;
122 if (index < 0)
123 return;
124
125 if (architecture->isVisualizerTab(index))
126 return;
127
128 const auto active = SILICON::project::documentTypeForPath(session.activeDocumentPath);
129 if (!active
132 return;
133
134 const auto name = SILICON::project::documentSlugForPath(session.activeDocumentPath);
135 if (!name)
136 return;
137
138 const auto type = static_cast<SILICON::project::DocumentType>(
139 architecture->tabBar()->tabData(index).toInt());
140 const auto path = SILICON::project::documentPathForSlug(type, *name);
141 if (documents.switchToDocument(path, false))
142 return;
143
144 // The document was rejected, so the workspace must not keep showing its tab.
145 const QSignalBlocker blocker(architecture);
146 for (int tab = 0; tab < architecture->count(); ++tab) {
147 if (architecture->tabBar()->tabData(tab).toInt() == static_cast<int>(*active)) {
148 architecture->setCurrentIndex(tab);
149 break;
150 }
151 }
152}
153
155{
156 if (!isArchitectureDocument(session))
157 return;
158 const auto* editor =
161 : nullptr;
162 if (!editor)
163 return;
164 const auto isa = buildEditedArchitecture(session.activeDocumentPath, *editor);
165 if (!isa)
166 return;
167 architectureEditor->workspace()->showVisualization(isa->describe());
168}
169
171{
172 if (!isArchitectureDocument(session))
173 return;
174 const auto* editor =
177 : nullptr;
178 if (!editor)
179 return;
180 if (buildEditedArchitecture(session.activeDocumentPath, *editor))
181 isaLog.info(std::format("{}: build succeeded", session.activeDocumentPath));
182}
183
184} // namespace SILICON::ui
void visualizeActiveArchitecture()
Opens the visualizer tab for the active architecture document.
void bindActions(const ArchitectureActions &actions)
Connects the main-window architecture actions to their commands.
void configureWorkspace(DocumentEditor *editor)
void handleTabChanged(int index)
Activates the document owned by a newly selected architecture tab.
ArchitectureController(ProjectSession &session, EditorWorkspace &workspace, ProjectDocumentController &documents, QObject *parent=nullptr)
ArchitectureDocumentEditor * architectureEditor
void buildActiveArchitecture()
Compiles the active architecture document, reporting build diagnostics.
Selects and hosts editors while preserving detached document preparation.
void editorCreated(DocumentEditor *editor)
void showArchitectureTabContextMenu(const QPoint &position)
std::string documentPathForSlug(const DocumentType type, const std::string_view slug)
constexpr DocumentCategory categoryOf(const DocumentType type)
std::optional< std::string > documentSlugForPath(const std::string_view path)
std::optional< DocumentType > documentTypeForPath(const std::string_view path)
ArchitectureBuildResult buildArchitecture(const std::optional< std::string > expectedName, const std::string_view source)