SILICON
Loading...
Searching...
No Matches
windowActions.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 "windowActions.hpp"
19
20#include <algorithm>
21#include <initializer_list>
22#include <optional>
23#include <ranges>
24#include <vector>
25
26#include <QAction>
27#include <QMainWindow>
28#include <QMenu>
29#include <QMenuBar>
30#include <QToolBar>
31
36#include <ui/shell/icons.hpp>
37
38namespace SILICON::ui {
39using namespace SILICON::core;
40
46
47void WindowActions::setActionsEnabled(std::initializer_list<QAction*> actions,
48 const bool enabled)
49{
50 for (QAction* action : actions) {
51 if (action)
52 action->setEnabled(enabled);
53 }
54}
55
57{
58 if (!edit.undo || !edit.redo)
59 return;
60 edit.undo->setEnabled(workspace.canUndoActiveDocument());
61 edit.redo->setEnabled(workspace.canRedoActiveDocument());
62}
63
69
71{
72 connect(edit.undo, &QAction::triggered, &workspace,
74 connect(edit.redo, &QAction::triggered, &workspace,
76 connect(edit.cut, &QAction::triggered, &workspace, &EditorWorkspace::cutActiveDocument);
77 connect(edit.copy, &QAction::triggered, &workspace,
79 connect(edit.paste, &QAction::triggered, &workspace,
81 connect(edit.remove, &QAction::triggered, &workspace,
83
90
92}
93
95{
96 fileMenu = window.menuBar()->addMenu(tr("&File"));
97 fileMenu->addAction(project.newProject);
98 auto* newMenu = fileMenu->addMenu(Icon("file"), tr("New &Document"));
99 newMenu->addAction(documents.newCircuit);
100 newMenu->addAction(documents.newCodeFile);
101 newMenu->addAction(documents.newArchitecture);
102 newMenu->addAction(documents.newBinaryFile);
103 fileMenu->addAction(project.open);
104 fileMenu->addAction(project.save);
105 fileMenu->addAction(project.exportImage);
106 fileMenu->addAction(waveform.toggleTrace);
107 fileMenu->addSeparator();
108 fileMenu->addAction(project.exit);
109
110 editMenu = window.menuBar()->addMenu(tr("&Edit"));
111 editMenu->addAction(edit.undo);
112 editMenu->addAction(edit.redo);
113 editMenu->addSeparator();
114 editMenu->addAction(edit.cut);
115 editMenu->addAction(edit.copy);
116 editMenu->addAction(edit.paste);
117 editMenu->addAction(circuit.rotate);
118 editMenu->addAction(circuit.autoPlace);
119 editMenu->addAction(circuit.centerView);
120 editMenu->addAction(edit.remove);
121 editMenu->addSeparator();
122 editMenu->addAction(project.settings);
123
124 helpMenu = window.menuBar()->addMenu(tr("&Help"));
125 helpMenu->addAction(project.about);
126}
127
129{
130 toolBar = new QToolBar(&window);
131 toolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
132 toolBar->setFloatable(false);
133
134 toolBar->addAction(project.newProject);
135 toolBar->addAction(project.open);
136 toolBar->addAction(project.save);
137
138 diagramToolsSeparator = toolBar->addSeparator();
139
140 toolBar->addAction(circuit.setNormalMode);
141 toolBar->addAction(circuit.setPanMode);
142 toolBar->addAction(circuit.setWireCreationMode);
143 toolBar->addAction(circuit.setSimulationMode);
144 toolBar->addAction(waveform.toggleTrace);
145
146 documentToolsSeparator = toolBar->addSeparator();
147 toolBar->addAction(circuit.openComponentCatalog);
148 toolBar->addAction(circuit.editSubcircuitShape);
149 toolBar->addAction(documents.codeConversion);
150 toolBar->addAction(architecture.build);
151 toolBar->addAction(architecture.visualize);
152
153 window.addToolBar(toolBar);
154}
155
157{
158 if (!circuit.editSubcircuitShape)
159 return;
161 circuit.editSubcircuitShape->setVisible(active);
162 circuit.editSubcircuitShape->setEnabled(active);
164}
165
166std::optional<SILICON::project::DocumentType>
168{
169 return SILICON::project::documentTypeForPath(session.activeDocumentPath);
170}
171
173{
174 if (!documents.codeConversion)
175 return;
176 const auto* document =
177 session.projectContext.documents().find(session.activeDocumentPath);
178 const auto converters = document ? documentConvertersFor(document->getType())
179 : std::vector<const DocumentConverter*>{};
180 const auto available =
181 std::ranges::count_if(converters, [](const DocumentConverter* converter) {
182 return converter->available;
183 });
184 documents.codeConversion->setVisible(!converters.empty());
185 documents.codeConversion->setEnabled(available != 0);
186 documents.codeConversion->setToolTip(
187 !converters.empty() && available == 0
188 ? QString::fromUtf8(
189 converters.front()->unavailableReason.data(),
190 static_cast<qsizetype>(converters.front()->unavailableReason.size()))
191 : QString());
192 if (converters.size() == 1) {
193 documents.codeConversion->setText(
194 tr("Convert to %1").arg(documentTypeName(converters.front()->target)));
195 } else if (!converters.empty()) {
196 documents.codeConversion->setText(tr("Convert..."));
197 }
198 const auto type = activeDocumentType();
199 const bool nonGraphical = type
203 {circuit.setNormalMode, circuit.setPanMode, circuit.setWireCreationMode,
204 circuit.setSimulationMode, waveform.toggleTrace, circuit.openComponentCatalog,
205 circuit.setComponentPlacingMode, circuit.autoPlace, circuit.centerView},
206 !nonGraphical);
207 circuit.selectAll->setEnabled(type
210
212}
213
215{
216 if (!toolBar)
217 return;
218
219 const auto type = activeDocumentType();
220 const bool diagram = type
223
224 const bool catalogVisible = diagram && circuit.openComponentCatalog->isEnabled();
225 const bool shapeVisible = circuit.editSubcircuitShape->isVisible()
226 && circuit.editSubcircuitShape->isEnabled();
227 const bool conversionVisible =
228 documents.codeConversion->isVisible() && documents.codeConversion->isEnabled();
229
230 // QWidget visibility is not authoritative for toolbar actions: Qt may recreate or
231 // show the widget again after the shared QAction changes state. Remove unavailable
232 // actions from this toolbar and re-add the active group in its canonical order.
233 for (auto* action :
234 {diagramToolsSeparator, circuit.setNormalMode, circuit.setPanMode,
235 circuit.setWireCreationMode, circuit.setSimulationMode, waveform.toggleTrace,
236 documentToolsSeparator, circuit.openComponentCatalog, circuit.editSubcircuitShape,
237 documents.codeConversion, architecture.build, architecture.visualize})
238 toolBar->removeAction(action);
239
240 if (diagram) {
241 toolBar->addAction(diagramToolsSeparator);
242 for (auto* action :
243 {circuit.setNormalMode, circuit.setPanMode, circuit.setWireCreationMode,
244 circuit.setSimulationMode, waveform.toggleTrace}) {
245 if (action->isEnabled())
246 toolBar->addAction(action);
247 }
248 }
249
250 const bool buildVisible = type == SILICON::project::DocumentType::Sisl;
251 if (catalogVisible || shapeVisible || conversionVisible || buildVisible) {
253 if (catalogVisible)
254 toolBar->addAction(circuit.openComponentCatalog);
255 if (shapeVisible)
256 toolBar->addAction(circuit.editSubcircuitShape);
257 if (conversionVisible)
258 toolBar->addAction(documents.codeConversion);
259 if (buildVisible) {
260 toolBar->addAction(architecture.build);
261 toolBar->addAction(architecture.visualize);
262 }
263 }
264}
265
267{
268 project.newProject =
269 makeAction(&window, Icon("file"), tr("&New Project"), tr("Create a new project"));
270 documents.newCircuit =
272 tr("Circuit"), tr("Create a new circuit document"));
273 documents.newCodeFile =
275 tr("Code File..."), tr("Create an empty source-code document"));
276 documents.newArchitecture = makeAction(&window, Icon("cpu"), tr("ISA Architecture..."),
277 tr("Create a SISL instruction format"));
278 documents.newBinaryFile =
280 tr("Binary File..."), tr("Create a fixed-size raw binary document"));
281 project.open = makeAction(&window, Icon("open"), tr("&Open..."),
282 tr("Open an existing silicon file"));
283 project.save =
284 makeAction(&window, Icon("save"), tr("&Save"), tr("Save the project to disk"));
285 project.exportImage = makeAction(&window, Icon("export"), tr("&Export..."),
286 tr("Export the circuit as an image"));
287 project.exit =
288 makeAction(&window, Icon("xmark"), tr("E&xit"), tr("Exit the application"));
289 edit.cut = makeAction(&window, Icon("cut"), tr("Cu&t"),
290 tr("Cut the current selection's contents to the clipboard"));
291 edit.copy = makeAction(&window, Icon("copy"), tr("&Copy"));
292 edit.paste =
293 makeAction(&window, Icon("paste"), tr("&Paste"),
294 tr("Paste the clipboard's contents into the current selection"));
295 circuit.selectAll = makeAction(&window, tr("Select All"));
296 window.addAction(circuit.selectAll);
297 circuit.rotate = makeAction(&window, Icon("rotate"), tr("&Rotate"));
298 circuit.autoPlace = makeAction(&window, Icon("rearrange"), tr("&Auto place"),
299 tr("Automatically place components and reroute wires"));
300 circuit.centerView =
301 makeAction(&window, tr("&Center view"), tr("Center the view on the circuit"));
302 edit.remove = makeAction(&window, Icon("delete"), tr("&Delete"),
303 tr("Delete selected components"));
304 project.about = makeAction(&window, Icon("info"), tr("&About"),
305 tr("Show the application's about box"));
306 project.settings = makeAction(&window, Icon("settings"), tr("&Settings..."),
307 tr("Edit application settings"));
308
309 edit.undo =
310 makeAction(&window, Icon("undo"), tr("&Undo"), tr("Undo the last operation"));
311 edit.redo =
312 makeAction(&window, Icon("redo"), tr("&Redo"), tr("Redo the last operation"));
313
314 setActionsEnabled({circuit.rotate, edit.cut, edit.copy, edit.remove}, false);
315
316 circuit.setNormalMode = new QAction(Icon("mouse-pointer"), "", &window);
317 circuit.setPanMode = new QAction(Icon("pan"), "", &window);
318 circuit.setWireCreationMode = new QAction(Icon("link"), "", &window);
319 circuit.setSimulationMode = new QAction(Icon("play"), "", &window);
320 waveform.toggleTrace =
321 makeAction(&window, Icon("chart"), tr("Trace"), tr("Show waveform viewer"));
322 waveform.toggleTrace->setCheckable(true);
323 circuit.cancelInteraction =
324 makeAction(&window, QString(), tr("Cancel the current interaction"));
325
326 circuit.openComponentCatalog =
327 makeAction(&window, Icon("plus"), "", tr("Open the component catalog"));
328 circuit.editSubcircuitShape =
329 makeAction(&window, Icon("circuit-board"), tr("Edit Shape"),
330 tr("Edit the active circuit shape"));
331 circuit.editSubcircuitShape->setVisible(false);
332 circuit.editSubcircuitShape->setEnabled(false);
333 documents.codeConversion = makeAction(&window, Icon("code"), tr("Code"));
334 documents.codeConversion->setVisible(false);
335 documents.codeConversion->setEnabled(false);
336 architecture.build = makeAction(&window, Icon("build"), tr("Build"),
337 tr("Compile the current SISL instruction format"));
338 architecture.visualize = makeAction(&window, Icon("diagram"), tr("Visualize"),
339 tr("Visualize SISL instruction formats"));
340 circuit.setComponentPlacingMode =
341 makeAction(&window, Icon("plus"), "", tr("Open quick component search"));
342
344}
345
347{
348 if (!circuit.rotate || !edit.cut || !edit.copy || !edit.paste || !edit.remove)
349 return;
350
351 const auto state = workspace.activeEditState();
352 circuit.rotate->setEnabled(state.canRotate);
353 edit.cut->setEnabled(state.canCut);
354 edit.copy->setEnabled(state.canCopy);
355 edit.paste->setEnabled(state.canPaste);
356 edit.remove->setEnabled(state.canDelete);
357}
358
359} // namespace SILICON::ui
Selects and hosts editors while preserving detached document preparation.
void editStateChanged()
Emitted when the active editor's editing availability may have changed.
void historyStateChanged()
Emitted when the active editor's undo/redo availability may have changed.
WaveformActions waveform
Commands owned by the waveform viewer.
std::optional< SILICON::project::DocumentType > activeDocumentType() const noexcept
QToolBar * toolBar
Main toolbar containing edit, mode, and simulation actions.
WindowActions(QMainWindow &window, ProjectSession &session, EditorWorkspace &workspace)
ArchitectureActions architecture
Commands owned by the architecture subsystem.
DocumentActions documents
Commands owned by the project document workflow.
CircuitActions circuit
Commands owned by the circuit editor.
ProjectActions project
Commands owned by the project and application shell.
EditActions edit
Commands owned by the active editor, whatever its kind.
static void setActionsEnabled(std::initializer_list< QAction * > actions, bool enabled)
constexpr DocumentCategory categoryOf(const DocumentType type)
std::optional< DocumentType > documentTypeForPath(const std::string_view path)
QAction * makeAction(QObject *parent, const QIcon &icon, const QString &text, const QString &statusTip)
Definition uiUtils.cpp:38
QIcon categoryIcon(const SILICON::project::DocumentType type)
Definition uiUtils.cpp:26
std::vector< const DocumentConverter * > documentConvertersFor(const DocumentType source)
QString documentTypeName(SILICON::project::DocumentType type)
Definition uiUtils.hpp:30