SILICON
Loading...
Searching...
No Matches
waveformController.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
11
12#include <QAction>
13#include <QDialog>
14#include <QSignalBlocker>
15#include <QVBoxLayout>
16
21
22namespace SILICON::ui {
23
24namespace {
25
26 [[nodiscard]] bool isDiagramDocument(const ProjectSession& session)
27 {
28 const auto type = SILICON::project::documentTypeForPath(session.activeDocumentPath);
29 return type
32 }
33
34} // namespace
35
38 QWidget* window)
39 : QObject(window), scene(scene), activeIsDiagram(isDiagramDocument(session))
40{
41 activeDocumentPath = QString::fromStdString(session.activeDocumentPath);
42
43 this->window = std::make_unique<QDialog>(window);
44 this->window->setWindowTitle(tr("Waveform"));
45 this->window->setModal(false);
46 this->window->resize(900, 420);
47
48 auto* layout = new QVBoxLayout(this->window.get());
49 layout->setContentsMargins(0, 0, 0, 0);
50 this->viewer = new waveform::Viewer(this->window.get());
51 layout->addWidget(this->viewer);
52
53 callbacks.connect(this->window.get(), &QDialog::finished, [this] {
54 setTraceActionChecked(false);
55 this->viewer->setEditMode(false);
56 });
57 connect(&this->scene, &DiagramScene::waveformTraceReset, this->viewer,
59 connect(&this->scene, &DiagramScene::waveformTraceSnapshots, this->viewer,
62 [this](bool enabled) { this->scene.setIoInteractionsEnabled(!enabled); });
63 connect(this->viewer, &waveform::Viewer::editTraceCommitted, &this->scene,
65
66 // Traces belong to the circuit that produced them, so the viewer follows the document
67 // controller instead of inspecting the project session on its own.
69 [this](const QString& path, SILICON::project::DocumentCategory category) {
70 handleActiveDocumentChanged(path, category);
71 });
72}
73
75{
76 toggleAction = actions.toggleTrace;
77 callbacks.connect(toggleAction, &QAction::toggled,
78 [this](bool enabled) { toggle(enabled); });
79}
80
82
83void WaveformController::toggle(const bool enabled)
84{
85 if (enabled && !activeIsDiagram) {
87 return;
88 }
89
90 window->setVisible(enabled);
91 if (!enabled)
92 return;
93
94 scene.setInteractionMode(DiagramScene::InteractionMode::SIMULATION_MODE);
95 window->raise();
96 window->activateWindow();
97}
98
100 const QString& path, const SILICON::project::DocumentCategory category)
101{
102 const bool switchedDocument = path != activeDocumentPath;
103 activeDocumentPath = path;
105
106 if (!activeIsDiagram) {
107 // Waveforms only exist for diagram documents: a code, binary, or architecture
108 // document has no scene to trace.
109 closeViewer();
110 return;
111 }
112
113 // Another circuit is now shown, so the previous circuit's samples would be misleading.
114 if (switchedDocument)
115 viewer->resetTrace({}, 0, {});
116}
117
119{
120 if (!toggleAction)
121 return;
122 const QSignalBlocker blocker(toggleAction);
123 toggleAction->setChecked(checked);
124}
125
127{
128 // Closing emits finished(), which already unchecks the action and leaves edit mode.
129 if (window->isVisible())
130 window->close();
131
133 viewer->setEditMode(false);
134 scene.setInteractionMode(DiagramScene::InteractionMode::NORMAL_MODE);
135}
136
137} // namespace SILICON::ui
Graphics scene for the circuit diagram editor.
void setIoInteractionsEnabled(bool enabled)
Enables or disables user-triggered IO interactions in simulation mode.
void waveformTraceReset(QStringList signalNames, int inputCount, QList< int > signalWidths)
Emitted when interactive waveform signals should be rebuilt.
void simulateEditedWaveform(qulonglong duration, std::vector< Sample > inputSnapshots)
Simulates the circuit using an edited input waveform.
void waveformTraceSnapshots(QList< QPair< qulonglong, std::vector< BusValue > > > snapshots)
Emitted when a completed simulation job produced multiple waveform states.
void activeDocumentChanged(const QString &path, SILICON::project::DocumentCategory category)
Emitted after a document was loaded and made visible.
void bindActions(const WaveformActions &actions)
Connects the main-window waveform actions to their viewer commands.
void handleActiveDocumentChanged(const QString &path, SILICON::project::DocumentCategory category)
Keeps the viewer bound to the diagram that is now active.
void closeViewer()
Hides the viewer and leaves the scene in a non-simulating state.
std::unique_ptr< QDialog > window
WaveformController(ProjectSession &session, DiagramScene &scene, ProjectDocumentController &documents, QWidget *window)
void editModeChanged(bool enabled)
Emitted when waveform edit mode changes.
void appendSnapshots(const QList< QPair< qulonglong, std::vector< core::BusValue > > > &snapshots)
Appends a batch of snapshots with a single deferred UI refresh.
void resetTrace(const QStringList &signalNames, int inputCount, const QList< int > &signalWidths)
Replaces signal metadata and clears the current trace.
void editTraceCommitted(qulonglong duration, std::vector< Sample > inputSnapshots)
Emitted when edit mode is left and the edited input waveform should run.
constexpr DocumentCategory categoryOf(const DocumentType type)
std::optional< DocumentType > documentTypeForPath(const std::string_view path)