SILICON
Loading...
Searching...
No Matches
waveformViewer.hpp
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#pragma once
19
20#include <QScrollArea>
21#include <QSplitter>
22#include <QStringList>
23#include <QTimer>
24#include <QWidget>
26#include <ui/lifetime.hpp>
28#include <vector>
29
30class QAction;
31class QLineEdit;
32class QMouseEvent;
33
34namespace SILICON::ui::waveform {
35using namespace SILICON::waveform;
36
37class SignalListWidget : public QWidget {
38 Q_OBJECT
39public:
40 explicit SignalListWidget(QWidget* parent = nullptr);
41
42 void setTrace(const QStringList& signalNames, int inputCount);
43 void setValues(const QStringList& signalValues);
44 void setSelectedSignalIndex(int signalIndex);
45
46signals:
47 void signalSelected(int signalIndex);
48 void signalContextMenuRequested(int signalIndex, QPoint globalPosition);
49
50protected:
51 void paintEvent(QPaintEvent* event) override;
52 void mousePressEvent(QMouseEvent* event) override;
53
54private:
55 QStringList names;
56 QStringList values;
59
60 [[nodiscard]] int signalAreaHeight() const;
61 [[nodiscard]] int valueColumnX() const;
62 [[nodiscard]] int yForSignalRow(int row) const;
63 [[nodiscard]] int signalRowAt(QPoint position) const;
64};
65
66class Canvas : public QWidget {
67 Q_OBJECT
68public:
71
72 explicit Canvas(QWidget* parent = nullptr);
73
79 void setTrace(const QStringList& names, const std::vector<Sample>& samples);
80
87 void setTrace(const QStringList& names, const std::vector<Sample>& samples,
88 int inputCount);
89
91 void setSignalFormats(const std::vector<SILICON::core::BusValueFormat>& formats);
92
97 void setPixelsPerTick(double value);
98
103 void setSelectedSampleIndex(int sampleIndex);
104
109 void setSelectedSignalIndex(int signalIndex);
110
115 void setEditMode(bool enabled);
116
121 void setEditDuration(quint64 duration);
122
129 void setEditSelection(int signalIndex, quint64 startTime, quint64 endTime);
130
135 void setEditSelectionVisible(bool visible);
136
137signals:
142 void timestampHovered(int sampleIndex);
143
148 void signalSelected(int signalIndex);
149
155 void signalContextMenuRequested(int signalIndex, QPoint globalPosition);
156
163 void editIntervalChanged(int signalIndex, quint64 startTime, quint64 endTime);
164
171 void editIntervalSelected(int signalIndex, quint64 startTime, quint64 endTime);
172
173protected:
174 void paintEvent(QPaintEvent* event) override;
175 void mouseMoveEvent(QMouseEvent* event) override;
176 void mousePressEvent(QMouseEvent* event) override;
177 void mouseReleaseEvent(QMouseEvent* event) override;
178
179private:
181 QStringList signalNames;
182
189 const std::vector<Sample>* traceSamples = nullptr;
190 double pixelsPerTick = 12.0;
194 bool editMode = false;
195 quint64 editDuration = 20;
201 quint64 editDragStartTime = 0;
202 quint64 editDragEndTime = 0;
203 std::vector<SILICON::core::BusValueFormat> signalFormats;
204
205 [[nodiscard]] quint64 endTime() const;
206 [[nodiscard]] int xForTime(quint64 time) const;
208 [[nodiscard]] quint64 timeForX(int x) const;
209 [[nodiscard]] int yForSignalRow(int row) const;
210 [[nodiscard]] int signalRowAt(QPoint position) const;
211 [[nodiscard]] core::BusValue sampleValueAt(int sampleIndex, int row) const;
212 [[nodiscard]] int yForScalarValue(int row, const core::BusValue& value) const;
214 void updateCanvasSize();
215
216 void drawScalar(QPainter& painter, int row, int x0, int x1,
217 const core::BusValue& value) const;
218 void drawScalarTransition(QPainter& painter, int row, int x,
219 const core::BusValue& previousValue,
220 const core::BusValue& nextValue) const;
221 void drawBus(QPainter& painter, int row, int x0, int x1,
222 const core::BusValue& value) const;
223};
224
225class Viewer : public QWidget {
226 Q_OBJECT
227public:
228 explicit Viewer(QWidget* parent = nullptr);
229
230protected:
232 bool eventFilter(QObject* watched, QEvent* event) override;
233
234public slots:
241 void resetTrace(const QStringList& signalNames, int inputCount,
242 const QList<int>& signalWidths);
243
248 void
249 appendSnapshots(const QList<QPair<qulonglong, std::vector<core::BusValue>>>& snapshots);
250
257 void setEditMode(bool enabled);
258
259signals:
264 void editModeChanged(bool enabled);
265
271 void editTraceCommitted(qulonglong duration, std::vector<Sample> inputSnapshots);
272
273private:
276 QScrollArea* labelScrollArea;
277 QScrollArea* scrollArea;
278 QAction* editAct;
279 QLineEdit* durationEdit;
280 QLineEdit* startEdit;
281 QLineEdit* endEdit;
282
286 double pixelsPerTick = 12.0;
287 std::vector<SILICON::core::BusValueFormat> signalFormats;
288 bool editMode = false;
289 quint64 editDuration = 20;
290 quint64 selectedEditStart = 0;
291 quint64 selectedEditEnd = 1;
294 bool keepScrolledToEnd = false;
295
297 QTimer* refreshTimer = nullptr;
298
302
304 void refreshSignalList();
305
307 void refreshCanvas();
308
310 void refreshViews();
311
319 void applyEditInterval(int signalIndex, quint64 startTime, quint64 endTime,
320 const QString& rawValue);
321
328 void promptEditIntervalValue(int signalIndex, quint64 startTime, quint64 endTime);
329
332
338 void setEditIntervalFields(quint64 startTime, quint64 endTime);
339
342
344 void updateDurationField();
345
347 void updateEditControls();
349 void scheduleRefresh();
351 [[nodiscard]] int visibleSignalCount() const;
353 [[nodiscard]] QStringList visibleNames() const;
354 [[nodiscard]] QStringList displayedValues() const;
355 [[nodiscard]] QString displayValue(int signalIndex, const core::BusValue& value) const;
356 void setSelectedSignalIndex(int signalIndex);
357 void showSignalFormatMenu(int signalIndex, QPoint globalPosition);
358 void saveTrace();
359};
360
361} // namespace SILICON::ui::waveform
int signalRowAt(QPoint position) const
std::vector< SILICON::core::BusValueFormat > signalFormats
void setPixelsPerTick(double value)
Changes horizontal waveform scale.
void signalContextMenuRequested(int signalIndex, QPoint globalPosition)
Requests the display-format context menu for a signal row.
void drawScalarTransition(QPainter &painter, int row, int x, const core::BusValue &previousValue, const core::BusValue &nextValue) const
int xForTime(quint64 time) const
void mouseReleaseEvent(QMouseEvent *event) override
void drawBus(QPainter &painter, int row, int x0, int x1, const core::BusValue &value) const
void setSelectedSignalIndex(int signalIndex)
Highlights one signal row, or clears selection for an invalid index.
void editIntervalChanged(int signalIndex, quint64 startTime, quint64 endTime)
Emitted while an edit-mode interval is being adjusted.
void setEditMode(bool enabled)
Enables or disables waveform interval editing gestures.
void mouseMoveEvent(QMouseEvent *event) override
Canvas(QWidget *parent=nullptr)
core::BusValue sampleValueAt(int sampleIndex, int row) const
void drawScalar(QPainter &painter, int row, int x0, int x1, const core::BusValue &value) const
void setSignalFormats(const std::vector< SILICON::core::BusValueFormat > &formats)
Sets per-signal display formats used for bus labels.
void setEditSelection(int signalIndex, quint64 startTime, quint64 endTime)
Sets the edit interval highlighted on the canvas.
void setTrace(const QStringList &names, const std::vector< Sample > &samples)
Displays a trace using the existing input-group count.
void mousePressEvent(QMouseEvent *event) override
void setEditSelectionVisible(bool visible)
Controls whether the precise edit interval highlight is visible.
int yForScalarValue(int row, const core::BusValue &value) const
SILICON::waveform::Sample Sample
One complete set of signal values at a simulation timestamp.
void timestampHovered(int sampleIndex)
Emitted when the pointer is nearest to a trace timestamp.
void setEditDuration(quint64 duration)
Sets the editable waveform time span.
void paintEvent(QPaintEvent *event) override
void signalSelected(int signalIndex)
Emitted when a signal row is selected.
void updateCanvasSize()
Recomputes the scrollable canvas dimensions from trace extent and zoom.
void editIntervalSelected(int signalIndex, quint64 startTime, quint64 endTime)
Emitted after an edit-mode drag chooses a signal/time interval.
QStringList signalNames
Signal labels rendered by the canvas.
quint64 timeForX(int x) const
Converts an x-coordinate into a clamped simulation timestamp.
const std::vector< Sample > * traceSamples
Non-owning view of Viewer's sample storage.
void setSelectedSampleIndex(int sampleIndex)
Highlights one sample timestamp, or clears selection for an invalid index.
void signalContextMenuRequested(int signalIndex, QPoint globalPosition)
void setValues(const QStringList &signalValues)
void mousePressEvent(QMouseEvent *event) override
void paintEvent(QPaintEvent *event) override
void setTrace(const QStringList &signalNames, int inputCount)
std::vector< SILICON::core::BusValueFormat > signalFormats
void updateEditControls()
Synchronizes edit controls with the current mode and duration.
void setEditIntervalFields(quint64 startTime, quint64 endTime)
Updates the precise interval fields from an edit gesture.
bool keepScrolledToEnd
Preserves tail-following behavior across a deferred refresh.
void scheduleRefresh()
Schedules a coalesced waveform refresh if one is not already pending.
void promptEditIntervalValue(int signalIndex, quint64 startTime, quint64 endTime)
Prompts for and validates a value for an edit-mode interval.
void updateDurationField()
Synchronizes the duration field with edit mode or the latest trace time.
void refreshViews()
Refreshes both synchronized waveform views.
int visibleSignalCount() const
Returns the number of signals currently visible in the viewer.
void setSelectedSignalIndex(int signalIndex)
void editModeChanged(bool enabled)
Emitted when waveform edit mode changes.
void setEditMode(bool enabled)
Enables input-waveform editing mode.
void refreshCanvas()
Pushes current trace state into the waveform canvas.
bool eventFilter(QObject *watched, QEvent *event) override
Tracks focus changes for precise interval edit fields.
QTimer * refreshTimer
Coalesces rapid snapshot arrivals into at most one refresh per frame.
QStringList displayedValues() const
Viewer(QWidget *parent=nullptr)
void appendSnapshots(const QList< QPair< qulonglong, std::vector< core::BusValue > > > &snapshots)
Appends a batch of snapshots with a single deferred UI refresh.
QString displayValue(int signalIndex, const core::BusValue &value) const
void promptSelectedEditIntervalValue()
Prompts for a value using the current selected signal and text interval.
void refreshSignalList()
Repaints the signal list while preserving scroll position.
void resetTrace(const QStringList &signalNames, int inputCount, const QList< int > &signalWidths)
Replaces signal metadata and clears the current trace.
void applyEditInterval(int signalIndex, quint64 startTime, quint64 endTime, const QString &rawValue)
Applies a raw bit value to one input signal over a time interval.
void showSignalFormatMenu(int signalIndex, QPoint globalPosition)
QStringList visibleNames() const
Returns the signal names currently visible in the viewer.
void commitEditIntervalFields()
Parses and clamps the precise interval text fields.
void editTraceCommitted(qulonglong duration, std::vector< Sample > inputSnapshots)
Emitted when edit mode is left and the edited input waveform should run.
std::vector< State > BusValue
Definition wire.hpp:120