SILICON
Loading...
Searching...
No Matches
projectArchitectureActions.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
23
24#include <format>
25#include <ranges>
26#include <stdexcept>
27#include <utility>
28
29#include <QMenu>
30#include <QSignalBlocker>
31#include <QStackedWidget>
32#include <QTabBar>
33#include <QTabWidget>
34#include <QTextDocument>
35
36#include <sisl/sisl.hpp>
37
39#include <logging/logger.hpp>
43#include <ui/shell/icons.hpp>
45
46namespace SILICON::ui {
48{
50 dialogParent, tr("New ISA Architecture"), tr("Architecture name"),
51 QStringLiteral("NewArch"), [this](const QString& requestedName) {
52 const auto name = requestedName.trimmed().toStdString();
55 dialogParent, tr("New ISA Architecture"),
56 tr("Enter a valid SISL architecture identifier."));
57 return;
58 }
61 if (session.projectContext.documents().contains(path)) {
63 dialogParent, tr("New ISA Architecture"),
64 tr("An architecture named '%1' already exists.")
65 .arg(QString::fromStdString(name)));
66 return;
67 }
68 this->pushCreateDocumentCommand({path, std::format("arch {} = {{}};\n", name)},
69 tr("Create ISA Architecture"));
70 },
71 this);
72}
73
75{
76 const auto active = SILICON::project::documentTypeForPath(session.activeDocumentPath);
77 if (!active
80 return;
81 auto* editor = dynamic_cast<ArchitectureDocumentEditor*>(workspace.activeEditor());
82 if (!editor)
83 return;
84 auto* architecture = editor->workspace();
85 const auto index = architecture->tabBar()->tabAt(position);
86 if (index < 0)
87 return;
88 if (architecture->isVisualizerTab(index))
89 return;
90 const auto name = SILICON::project::documentSlugForPath(session.activeDocumentPath);
91 if (!name)
92 return;
93 const auto type = static_cast<SILICON::project::DocumentType>(
94 architecture->tabBar()->tabData(index).toInt());
95 const auto path = SILICON::project::documentPathForSlug(type, *name);
96 QMenu menu(dialogParent);
97 menu.addAction(Icon("delete"), tr("Delete File"), this,
98 [this, path] { deleteArchitectureComponent(path); });
99 menu.exec(architecture->tabBar()->mapToGlobal(position));
100}
101
103{
104 if (!session.projectContext.documents().contains(path))
105 return;
107 dialogParent, tr("Delete Architecture File"),
108 tr("Delete %1? If it is the last file, the architecture will also be removed.")
109 .arg(QString::fromStdString(SILICON::project::documentFileName(
110 *session.projectContext.documents().find(path)))),
111 [this, path] {
112 this->removeProjectDocuments({path}, tr("Delete Architecture File"));
113 },
114 this);
115}
116
118{
119 const auto title = tr("Rename ISA Architecture");
121 dialogParent, title, tr("Name"), QString::fromStdString(name),
122 [this, name, title](const QString& requestedName) {
123 const auto newName = requestedName.trimmed().toStdString();
126 dialogParent, title, tr("Enter a valid SISL architecture identifier."));
127 return;
128 }
129 if (newName == name)
130 return;
131 const auto oldPrefix = SILICON::project::architectureDirectory(name);
132 const auto newPrefix = SILICON::project::architectureDirectory(newName);
133 for (const auto& document : session.projectContext.documents().getDocuments()) {
134 if (document.getPath().starts_with(newPrefix)) {
136 dialogParent, title,
137 tr("An architecture named '%1' already exists.")
138 .arg(QString::fromStdString(newName)));
139 return;
140 }
141 }
142
143 try {
144 workspace.flushActiveDocument();
145 const auto before = session.projectContext.documents().getDocuments();
146 auto after = before;
147 for (auto& document : after) {
148 if (!document.getPath().starts_with(oldPrefix))
149 continue;
150 auto contents = document.getContents();
151 if (document.getType() == SILICON::project::DocumentType::Sisl)
152 contents = SILICON::project::renameArchitectureDeclaration(contents, name,
153 newName);
155 newPrefix + document.getPath().substr(oldPrefix.size()),
156 std::move(contents));
157 }
159 validated.setDocuments(after);
160 const auto beforeActive = session.activeDocumentPath;
161 const auto afterActive = beforeActive.starts_with(oldPrefix)
162 ? newPrefix + beforeActive.substr(oldPrefix.size())
163 : beforeActive;
164 this->pushDocumentSnapshotCommand(title, before, beforeActive, std::move(after),
165 afterActive);
166 } catch (const std::exception& error) {
168 }
169 },
170 this);
171}
172
174{
175 const auto prefix = SILICON::project::architectureDirectory(name);
176 std::vector<std::string> paths;
177 for (const auto& document : session.projectContext.documents().getDocuments()) {
178 if (document.getPath().starts_with(prefix))
179 paths.push_back(document.getPath());
180 }
182 dialogParent, tr("Delete ISA Architecture"),
183 tr("Delete ISA architecture \"%1\" and all its files?")
184 .arg(QString::fromStdString(name)),
185 [this, paths] {
186 this->removeProjectDocuments(paths, tr("Delete ISA Architecture"));
187 },
188 this);
189}
190
191} // namespace SILICON::ui
void setDocuments(std::vector< Document > documents)
ArchitectureWorkspace * workspace() const noexcept
void pushCreateDocumentCommand(SILICON::project::Document document, const QString &commandText)
void showArchitectureTabContextMenu(const QPoint &position)
void pushDocumentSnapshotCommand(QString commandText, std::vector< SILICON::project::Document > before, std::string beforeActive, std::vector< SILICON::project::Document > after, std::string afterActive)
void deleteArchitectureComponent(const std::string &path)
void removeProjectDocuments(const std::vector< std::string > &paths, const QString &commandText)
std::string documentPathForSlug(const DocumentType type, const std::string_view slug)
std::string documentFileName(const Document &document)
constexpr DocumentCategory categoryOf(const DocumentType type)
std::string renameArchitectureDeclaration(const std::string_view source, const std::string_view oldName, const std::string_view newName)
std::optional< std::string > documentSlugForPath(const std::string_view path)
std::optional< DocumentType > documentTypeForPath(const std::string_view path)
bool isValidArchitectureName(const std::string_view name)
std::string architectureDirectory(const std::string_view name)
void question(QWidget *parent, const QString &title, const QString &text, AcceptedCallback accepted, QObject *context)
void warning(QWidget *parent, const QString &title, const QString &text)
void getText(QWidget *parent, const QString &title, const QString &label, const QString &defaultText, TextCallback callback, QObject *context)