SILICON
Toggle main menu visibility
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
18
#include <
ui/documents/architecture/architectureWorkspace.hpp
>
19
#include <
ui/documents/documentEditors.hpp
>
20
#include <
ui/documents/editorWorkspace.hpp
>
21
#include <
ui/project/projectDocumentController.hpp
>
22
#include <
ui/project/projectSession.hpp
>
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
38
#include <
core/isaArchitecture.hpp
>
39
#include <
logging/logger.hpp
>
40
#include <
ui/documents/architecture/sislVisualizer.hpp
>
41
#include <
ui/documents/code/codeEditor.hpp
>
42
#include <
ui/documents/code/codeFilePresentation.hpp
>
43
#include <
ui/shell/icons.hpp
>
44
#include <
ui/shell/inputDialogUtils.hpp
>
45
46
namespace
SILICON::ui
{
47
void
ProjectDocumentController::createArchitecture
()
48
{
49
SILICON::ui::inputDialog::getText
(
50
dialogParent
, tr(
"New ISA Architecture"
), tr(
"Architecture name"
),
51
QStringLiteral(
"NewArch"
), [
this
](
const
QString& requestedName) {
52
const
auto
name = requestedName.trimmed().toStdString();
53
if
(!
SILICON::project::isValidArchitectureName
(name)) {
54
SILICON::ui::inputDialog::warning
(
55
dialogParent
, tr(
"New ISA Architecture"
),
56
tr(
"Enter a valid SISL architecture identifier."
));
57
return
;
58
}
59
const
auto
path =
SILICON::project::documentPathForSlug
(
60
SILICON::project::DocumentType::Sisl
, name);
61
if
(
session
.projectContext.documents().contains(path)) {
62
SILICON::ui::inputDialog::warning
(
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
74
void
ProjectDocumentController::showArchitectureTabContextMenu
(
const
QPoint& position)
75
{
76
const
auto
active =
SILICON::project::documentTypeForPath
(
session
.activeDocumentPath);
77
if
(!active
78
||
SILICON::project::categoryOf
(*active)
79
!=
SILICON::project::DocumentCategory::Architecture
)
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
102
void
ProjectDocumentController::deleteArchitectureComponent
(
const
std::string& path)
103
{
104
if
(!
session
.projectContext.documents().contains(path))
105
return
;
106
SILICON::ui::inputDialog::question
(
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
117
void
ProjectDocumentController::renameArchitecture
(
const
std::string& name)
118
{
119
const
auto
title = tr(
"Rename ISA Architecture"
);
120
SILICON::ui::inputDialog::getText
(
121
dialogParent
, title, tr(
"Name"
), QString::fromStdString(name),
122
[
this
, name, title](
const
QString& requestedName) {
123
const
auto
newName = requestedName.trimmed().toStdString();
124
if
(!
SILICON::project::isValidArchitectureName
(newName)) {
125
SILICON::ui::inputDialog::warning
(
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)) {
135
SILICON::ui::inputDialog::warning
(
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);
154
document =
SILICON::project::Document
(
155
newPrefix + document.getPath().substr(oldPrefix.size()),
156
std::move(contents));
157
}
158
SILICON::project::ProjectContext
validated;
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) {
167
SILICON::ui::inputDialog::warning
(
dialogParent
, title, error.what());
168
}
169
},
170
this
);
171
}
172
173
void
ProjectDocumentController::deleteArchitecture
(
const
std::string& name)
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
}
181
SILICON::ui::inputDialog::question
(
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
architectureWorkspace.hpp
SILICON::project::Document
Definition
projectDocument.hpp:109
SILICON::project::ProjectContext
Definition
projectContext.hpp:30
SILICON::project::ProjectContext::setDocuments
void setDocuments(std::vector< Document > documents)
Definition
projectContext.cpp:106
SILICON::ui::ArchitectureDocumentEditor
Definition
documentEditors.hpp:81
SILICON::ui::ArchitectureDocumentEditor::workspace
ArchitectureWorkspace * workspace() const noexcept
Definition
documentEditors.hpp:87
SILICON::ui::Icon
Definition
icons.hpp:39
SILICON::ui::ProjectDocumentController::session
ProjectSession & session
Definition
projectDocumentController.hpp:113
SILICON::ui::ProjectDocumentController::pushCreateDocumentCommand
void pushCreateDocumentCommand(SILICON::project::Document document, const QString &commandText)
Definition
projectDocumentActions.cpp:55
SILICON::ui::ProjectDocumentController::createArchitecture
void createArchitecture()
Definition
projectArchitectureActions.cpp:47
SILICON::ui::ProjectDocumentController::workspace
EditorWorkspace & workspace
Definition
projectDocumentController.hpp:114
SILICON::ui::ProjectDocumentController::showArchitectureTabContextMenu
void showArchitectureTabContextMenu(const QPoint &position)
Definition
projectArchitectureActions.cpp:74
SILICON::ui::ProjectDocumentController::pushDocumentSnapshotCommand
void pushDocumentSnapshotCommand(QString commandText, std::vector< SILICON::project::Document > before, std::string beforeActive, std::vector< SILICON::project::Document > after, std::string afterActive)
Definition
projectDocumentActions.cpp:263
SILICON::ui::ProjectDocumentController::renameArchitecture
void renameArchitecture(const std::string &name)
Definition
projectArchitectureActions.cpp:117
SILICON::ui::ProjectDocumentController::deleteArchitectureComponent
void deleteArchitectureComponent(const std::string &path)
Definition
projectArchitectureActions.cpp:102
SILICON::ui::ProjectDocumentController::dialogParent
QWidget * dialogParent
Definition
projectDocumentController.hpp:118
SILICON::ui::ProjectDocumentController::deleteArchitecture
void deleteArchitecture(const std::string &name)
Definition
projectArchitectureActions.cpp:173
SILICON::ui::ProjectDocumentController::removeProjectDocuments
void removeProjectDocuments(const std::vector< std::string > &paths, const QString &commandText)
Definition
projectDocumentActions.cpp:334
codeEditor.hpp
codeFilePresentation.hpp
documentEditors.hpp
editorWorkspace.hpp
icons.hpp
inputDialogUtils.hpp
isaArchitecture.hpp
logger.hpp
SILICON::project::documentPathForSlug
std::string documentPathForSlug(const DocumentType type, const std::string_view slug)
Definition
projectDocument.cpp:191
SILICON::project::DocumentCategory::Architecture
@ Architecture
Definition
projectDocument.hpp:39
SILICON::project::documentFileName
std::string documentFileName(const Document &document)
Definition
projectDocument.cpp:256
SILICON::project::categoryOf
constexpr DocumentCategory categoryOf(const DocumentType type)
Definition
projectDocument.hpp:81
SILICON::project::renameArchitectureDeclaration
std::string renameArchitectureDeclaration(const std::string_view source, const std::string_view oldName, const std::string_view newName)
Definition
isaArchitecture.cpp:115
SILICON::project::documentSlugForPath
std::optional< std::string > documentSlugForPath(const std::string_view path)
Definition
projectDocument.cpp:174
SILICON::project::documentTypeForPath
std::optional< DocumentType > documentTypeForPath(const std::string_view path)
Definition
projectDocument.cpp:165
SILICON::project::DocumentType
DocumentType
Definition
projectDocument.hpp:37
SILICON::project::DocumentType::Sisl
@ Sisl
Definition
projectDocument.hpp:37
SILICON::project::isValidArchitectureName
bool isValidArchitectureName(const std::string_view name)
Definition
isaArchitecture.cpp:44
SILICON::project::architectureDirectory
std::string architectureDirectory(const std::string_view name)
Definition
isaArchitecture.cpp:136
SILICON::ui::inputDialog::question
void question(QWidget *parent, const QString &title, const QString &text, AcceptedCallback accepted, QObject *context)
Definition
inputDialogUtils.cpp:122
SILICON::ui::inputDialog::warning
void warning(QWidget *parent, const QString &title, const QString &text)
Definition
inputDialogUtils.cpp:98
SILICON::ui::inputDialog::getText
void getText(QWidget *parent, const QString &title, const QString &label, const QString &defaultText, TextCallback callback, QObject *context)
Definition
inputDialogUtils.cpp:43
SILICON::ui
Definition
actionGroups.hpp:14
projectDocumentController.hpp
projectSession.hpp
sislVisualizer.hpp
src
ui
project
projectArchitectureActions.cpp
Generated by
1.17.0