SILICON
Loading...
Searching...
No Matches
siliconWindow.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 "siliconWindow.hpp"
20
21#ifdef __EMSCRIPTEN__
22 #include <emscripten/emscripten.h>
23#endif
24
25#include <algorithm>
26#include <ranges>
27
28#include <QAction>
29#include <QApplication>
30#include <QCloseEvent>
31#include <QContextMenuEvent>
32#include <QDockWidget>
33#include <QEvent>
34#include <QHBoxLayout>
35#include <QMenu>
36#include <QMouseEvent>
37#include <QObject>
38#include <QPointF>
39#include <QResizeEvent>
40#include <QStatusBar>
41#include <QToolBar>
42#include <QUndoStack>
43#include <QWidget>
44
45#include <logging/logger.hpp>
46
59#include <ui/shell/icons.hpp>
62#include <ui/shell/uiUtils.hpp>
65
66namespace SILICON::ui {
67using namespace SILICON::core;
68
70
73public:
74 explicit WindowLifetime(QMainWindow& window) : window(window) {}
76 {
77 // Non-closable docks accept close after the main window has been hidden.
78 window.hide();
79 for (const auto& dock : docks) {
80 if (dock)
81 dock->close();
82 }
83 // Qt releases toolbar drag state in mouseReleaseEvent(), not its destructor.
84 if (toolBar) {
85 QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPointF(), QPointF(),
86 Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
87 QApplication::sendEvent(toolBar, &releaseEvent);
88 }
89 }
90
91 QPointer<QDockWidget> docks[3];
92 QPointer<QToolBar> toolBar;
93
94private:
95 QMainWindow& window;
96};
97
99{
100 windowLifetime = std::make_unique<WindowLifetime>(*this);
101 // --- Layout Setup --------------------------------------------------------------------
102 const auto centralWidget = new QWidget();
103 setCentralWidget(centralWidget);
104
105 const auto layout = new QHBoxLayout();
106 layout->setContentsMargins(5, 5, 5, 5);
107 centralWidget->setLayout(layout);
108
109 documentDock = new QDockWidget(this);
110 propertyDock = new QDockWidget(this);
111 logDock = new QDockWidget(this);
112 windowLifetime->docks[0] = documentDock;
113 windowLifetime->docks[1] = propertyDock;
114 windowLifetime->docks[2] = logDock;
115
116 addDockWidget(Qt::LeftDockWidgetArea, documentDock);
117 addDockWidget(Qt::LeftDockWidgetArea, propertyDock);
118 addDockWidget(Qt::BottomDockWidgetArea, logDock);
119
120 propertyDock->setFeatures(QDockWidget::DockWidgetMovable);
121 documentDock->setFeatures(QDockWidget::DockWidgetMovable);
122 logDock->setFeatures(QDockWidget::DockWidgetMovable);
123 logDock->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea);
124
125 propertyDock->setWindowTitle("Properties");
126 documentDock->setWindowTitle("Project");
127 logDock->setWindowTitle("Logs");
128
129 splitDockWidget(documentDock, propertyDock, Qt::Vertical);
130
131 workspace = std::make_unique<EditorWorkspace>(projectSession, centralWidget);
132 circuitEditor = &workspace->circuitEditor();
133
135 [this] { updateStatus(); });
136 updateStatus();
137
138 layout->addWidget(workspace.get());
140 circuitEditor->scene(), projectSession.projectContext.documents(),
141 &projectSession.circuitResolver, circuitEditor->view()->viewport());
142 viewportFilter.reset(circuitEditor->view()->viewport(), this);
145 undoStack = std::make_unique<QUndoStack>(this);
146 // The circuit scene, its document editor and the rest of the shell share one history:
147 // the workspace hands it to every editor it hosts.
148 workspace->setProjectHistory(undoStack.get());
149 circuitEditor->scene()->setUndoStack(undoStack.get());
150 interactionController = std::make_unique<DiagramInteractionController>(
152 documentController = std::make_unique<ProjectDocumentController>(projectSession, *workspace, *projectTree,
154 architectureController = std::make_unique<ArchitectureController>(projectSession, *workspace, *documentController, this);
155 circuitEditor->scene()->setDocumentNavigator(documentController.get());
157 [this](const QString&, SILICON::project::DocumentCategory) {
158 refreshActiveDocumentUi();
159 });
161 [this] { refreshProjectUi(); });
162
163 aboutDialog = new AboutDialog("SILICON", this);
164
165 fileController = std::make_unique<ProjectFileController>(projectSession, *workspace,
168 [this] { refreshProjectUi(); });
169 propertyPanel = std::make_unique<PropertyPanel>(propertyDock, circuitEditor->scene(), projectTree, undoStack.get(),
170 projectSession.currentProjectInfo, projectSession.currentFileName,
171 [this] { documentController->rebuildTree(); });
172
173 actionSet = std::make_unique<WindowActions>(*this, projectSession, *workspace);
174 actionSet->createActions();
175 callbacks.connect(circuitEditor->scene(), &DiagramScene::selectionChanged,
176 [this] { selectionChanged(); });
177 waveformController = std::make_unique<WaveformController>(projectSession, *circuitEditor->scene(),
178 *documentController, this);
179 wireActions();
180 actionSet->applyStoredSettings();
181 actionSet->createMenus();
182 actionSet->createToolBar();
183 windowLifetime->toolBar = actionSet->toolBar;
184
185 const auto zoomToolBar = new QToolBar(this);
186 zoomToolBar->setFloatable(false);
187 zoomToolBar->setMovable(false);
188 zoomToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
189 const auto zoomOutAct = zoomToolBar->addAction(Icon("minus"), tr("Zoom Out"));
190 const auto zoomInAct = zoomToolBar->addAction(Icon("plus"), tr("Zoom In"));
191 callbacks.connect(zoomOutAct, &QAction::triggered,
192 [this] { workspace->zoomActiveDocument(false); });
193 callbacks.connect(zoomInAct, &QAction::triggered,
194 [this] { workspace->zoomActiveDocument(true); });
195 statusBar()->addPermanentWidget(zoomToolBar);
196
200 graphicalLogStream = std::make_unique<GraphicalLogStream>(this);
201 logDock->setWidget(logSideView);
202 logDock->setMinimumHeight(logSideView->minimumSizeHint().height());
203 logDock->resize(width(), logSideView->sizeHint().height());
204
206 &LogSideView::appendLine, Qt::QueuedConnection);
207 graphicalLogStream->attachToBoostLog();
208
209 resizeDocks({documentDock, propertyDock}, {320, 260}, Qt::Vertical);
210 resizeDocks({logDock}, {180}, Qt::Vertical);
211
212 setWindowTitle(tr("SILICON"));
213 setMinimumSize(160, 160);
214
215 fileController->resetProjectState();
216 documentController->rebuildTree();
218
219 uiLog.info("Qt logging sideview initialized");
220}
221
223{
224 connect(actionSet->project.newProject, &QAction::triggered, fileController.get(),
226 connect(actionSet->project.open, &QAction::triggered, fileController.get(),
228 callbacks.connect(actionSet->project.save, &QAction::triggered,
229 [this] { fileController->save(); });
230 callbacks.connect(actionSet->project.exportImage, &QAction::triggered,
231 [this] { exportImage(); });
232 callbacks.connect(actionSet->project.exit, &QAction::triggered, [this] { close(); });
233 connect(actionSet->project.settings, &QAction::triggered, actionSet.get(),
235 callbacks.connect(actionSet->project.about, &QAction::triggered, [this] { about(); });
236
237 connect(actionSet->documents.newCircuit, &QAction::triggered, documentController.get(),
239 connect(actionSet->documents.newCodeFile, &QAction::triggered, documentController.get(),
241 connect(actionSet->documents.newArchitecture, &QAction::triggered, documentController.get(),
243 connect(actionSet->documents.newBinaryFile, &QAction::triggered, documentController.get(),
245 connect(actionSet->documents.codeConversion, &QAction::triggered, documentController.get(),
247
248 // Editor subsystems own the commands they implement, so they wire their own actions.
249 interactionController->bindActions(actionSet->circuit);
250 architectureController->bindActions(actionSet->architecture);
251 waveformController->bindActions(actionSet->waveform);
252
253 // These two shortcuts are only meaningful mid-interaction, so they are installed on the
254 // window to stay reachable from anywhere rather than from a focused child widget.
255 addAction(actionSet->circuit.setComponentPlacingMode);
256 addAction(actionSet->circuit.cancelInteraction);
257}
258
260{
261 aboutDialog->show();
262}
263
264#ifndef QT_NO_CONTEXTMENU
265void SiliconWindow::contextMenuEvent(QContextMenuEvent* event)
266{
267 #ifdef __EMSCRIPTEN__
268 auto* menu = new QMenu(this);
269 menu->setAttribute(Qt::WA_DeleteOnClose);
270 menu->addAction(actionSet->edit.cut);
271 menu->addAction(actionSet->edit.copy);
272 menu->addAction(actionSet->edit.paste);
273 menu->addAction(actionSet->circuit.rotate);
274 menu->addAction(actionSet->edit.remove);
275 menu->popup(event->globalPos());
276 #else
277 QMenu menu(this);
278 menu.addAction(actionSet->edit.cut);
279 menu.addAction(actionSet->edit.copy);
280 menu.addAction(actionSet->edit.paste);
281 menu.addAction(actionSet->circuit.rotate);
282 menu.addAction(actionSet->edit.remove);
283 menu.exec(event->globalPos());
284 #endif
285 event->accept();
286}
287#endif // QT_NO_CONTEXTMENU
288
289bool SiliconWindow::eventFilter(QObject* watched, QEvent* event)
290{
291 if (watched == viewportFilter.target() && event->type() == QEvent::Resize) {
293 }
294
295 return QMainWindow::eventFilter(watched, event);
296}
297
298void SiliconWindow::resizeEvent(QResizeEvent* event)
299{
300 QMainWindow::resizeEvent(event);
302
303 const int currentWidth = event->size().width();
304 const int currentHeight = event->size().height();
305
306 const int minWidth = std::min(288, currentWidth / 4);
307 const int maxWidth = currentWidth / 2;
308
309 const int minHeight = currentHeight / 3;
310
311 auto configureSizeConstraints = [minWidth, maxWidth, minHeight](QDockWidget* widget) {
312 widget->setMinimumWidth(minWidth);
313 widget->setMaximumWidth(maxWidth);
314 widget->setMinimumHeight(minHeight);
315 };
316
317 configureSizeConstraints(documentDock);
318 configureSizeConstraints(propertyDock);
319
320 logDock->setMinimumWidth(160);
321 logDock->setMaximumWidth(QWIDGETSIZE_MAX);
322 logDock->setMinimumHeight(120);
323 logDock->setMaximumHeight(std::max(160, currentHeight / 3));
324}
325
326void SiliconWindow::closeEvent(QCloseEvent* event)
327{
328 if (workspace->hasUnsavedChanges() && !closeAfterSaveConfirmation) {
329 event->ignore();
330 fileController->confirmSaveIfDirty(callbacks.guard([this] {
331 closeAfterSaveConfirmation = true;
332 close();
333 }));
334 return;
335 }
336
338 QMainWindow::closeEvent(event);
339}
340
342{
343 if (!componentCatalogOverlay || !circuitEditor->view())
344 return;
345
346 componentCatalogOverlay->setGeometry(circuitEditor->view()->viewport()->rect());
347}
348
350{
351 propertyPanel->refresh();
352}
353
355{
356 actionSet->updateSubcircuitShapeAction();
357 actionSet->updateEditActions();
358 actionSet->updateHistoryActions();
360}
361
366
368
370{
372 documentDock->setWidget(projectTree);
373
374 callbacks.connect(projectTree, &QTreeWidget::itemSelectionChanged,
375 [this] { projectTreeSelectionChanged(); });
376 callbacks.connect(projectTree, &QTreeWidget::customContextMenuRequested,
377 [this](const QPoint& position) { showProjectTreeContextMenu(position); });
378}
379
381{
382 const QSignalBlocker blocker(circuitEditor->scene());
383 circuitEditor->scene()->clearSelection();
384
385 if (const auto selection =
386 projectTree ? projectTree->selectedDocument() : std::nullopt) {
387 documentController->switchToDocument(selection->path, false);
388 return;
389 }
390
391 actionSet->setActionsEnabled({actionSet->circuit.rotate, actionSet->edit.cut,
392 actionSet->edit.copy, actionSet->edit.remove},
393 false);
395}
396
398{
399 if (!projectTree)
400 return;
401
402 projectTree->clearSelection();
403 if (auto* item = projectTree->itemAt(position)) {
404 projectTree->setCurrentItem(item);
405 item->setSelected(true);
406 } else {
407 projectTree->setCurrentItem(nullptr);
408 }
409
410#ifdef __EMSCRIPTEN__
411 auto* menu = new QMenu(this);
412 menu->setAttribute(Qt::WA_DeleteOnClose);
413#else
414 QMenu stackMenu(this);
415 auto* menu = &stackMenu;
416#endif
417
418 auto* newMenu = menu->addMenu(Icon("file"), tr("New"));
419 newMenu->addAction(actionSet->documents.newCircuit);
420 newMenu->addAction(actionSet->documents.newCodeFile);
421 newMenu->addAction(actionSet->documents.newArchitecture);
422 newMenu->addAction(actionSet->documents.newBinaryFile);
423
424 menu->addSeparator();
425 menu->addAction(Icon("import"), tr("Import Document..."), documentController.get(),
427
428 if (const auto selection = projectTree->selectedDocument()) {
429 const bool architecture = ProjectTree::itemKind(projectTree->selectedProjectItem())
431 menu->addAction(Icon("export"), tr("Export Document..."), documentController.get(),
433 menu->addSeparator();
434 menu->addAction(
435 Icon("pencil"),
436 architecture ? tr("Rename ISA Architecture...") : tr("Rename Document..."),
438 auto* deleteAction = menu->addAction(
439 Icon("delete"),
440 architecture ? tr("Delete ISA Architecture")
441 : tr("Delete %1").arg(documentTypeName(selection->type)),
443 const auto circuitCount = std::ranges::count_if(
444 projectSession.projectContext.documents().getDocuments(),
445 [](const auto& document) {
446 return document.getType() == SILICON::project::DocumentType::Circuit;
447 });
448 deleteAction->setEnabled(selection->type != SILICON::project::DocumentType::Circuit
449 || circuitCount > 1);
450 }
451
452#ifdef __EMSCRIPTEN__
453 menu->popup(projectTree->viewport()->mapToGlobal(position));
454#else
455 menu->exec(projectTree->viewport()->mapToGlobal(position));
456#endif
457}
458
459} // namespace SILICON::ui
void modeChanged(InteractionMode mode)
Emitted when the interaction mode changes.
void lineReceived(QString line)
void appendLine(const QString &line)
void projectDocumentsChanged()
Emitted when the set of project documents was added to, changed, or removed.
void activeDocumentChanged(const QString &path, SILICON::project::DocumentCategory category)
Emitted after a document was loaded and made visible.
Project document navigator used by the SILICON editor.
static ProjectTreeItemKind itemKind(const QTreeWidgetItem *item)
void updateStatus() const
Refreshes the status bar text for the current scene mode.
void updateComponentCatalogGeometry()
Repositions and resizes the component catalog overlay.
void refreshProjectUi()
Refreshes the property dock after a project-level change.
SiliconWindow()
Constructs and initializes the SILICON editor window.
std::unique_ptr< EditorWorkspace > workspace
void contextMenuEvent(QContextMenuEvent *event) override
Opens the diagram context menu for mode and editing actions.
QDockWidget * propertyDock
Dock containing the current property editor.
std::unique_ptr< ProjectFileController > fileController
bool closeAfterSaveConfirmation
Allows the close event triggered after an accepted dirty-file prompt.
void initializeProjectTree()
Creates and wires the project tree widget shown in the project dock.
void closeEvent(QCloseEvent *event) override
void about() const
Shows the application about dialog.
bool eventFilter(QObject *watched, QEvent *event) override
Handles viewport events needed by floating child widgets.
std::unique_ptr< WindowLifetime > windowLifetime
QDockWidget * logDock
Dock containing application log output.
void projectTreeSelectionChanged()
Handles user selection changes in the project circuit tree.
void showProjectTreeContextMenu(const QPoint &position)
Shows the context menu for project-tree circuit actions.
void resizeEvent(QResizeEvent *event) override
Keeps floating overlays aligned when the main window is resized.
ComponentCatalogOverlay * componentCatalogOverlay
ProjectTree * projectTree
Borrowed handles into the workspace and Qt widget hierarchy.
std::unique_ptr< ProjectDocumentController > documentController
std::unique_ptr< WindowActions > actionSet
std::unique_ptr< PropertyPanel > propertyPanel
ScopedEventFilter viewportFilter
std::unique_ptr< DiagramInteractionController > interactionController
std::unique_ptr< WaveformController > waveformController
QDockWidget * documentDock
Dock containing the project document tree.
std::unique_ptr< QUndoStack > undoStack
void updatePropertyDock()
Rebuilds the property dock for the current selection or active circuit.
~SiliconWindow() override
Releases scoped callbacks and UI owners in dependency order.
void refreshActiveDocumentUi()
Refreshes actions and docks that depend on the active document.
std::unique_ptr< ArchitectureController > architectureController
std::unique_ptr< GraphicalLogStream > graphicalLogStream
WindowLifetime(QMainWindow &window)
QPointer< QDockWidget > docks[3]
QPointer< QToolBar > toolBar
const SILICON::logging::Logger uiLog("ui")
QString documentTypeName(SILICON::project::DocumentType type)
Definition uiUtils.hpp:30