]> sigrok.org Git - pulseview.git/blob - pv/views/trace/decodetrace.hpp
DecodeTrace: Perform delayed row initialization once the owner is set
[pulseview.git] / pv / views / trace / decodetrace.hpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef PULSEVIEW_PV_VIEWS_TRACEVIEW_DECODETRACE_HPP
21 #define PULSEVIEW_PV_VIEWS_TRACEVIEW_DECODETRACE_HPP
22
23 #include <config.h>
24 #include "trace.hpp"
25
26 #include <list>
27 #include <map>
28 #include <memory>
29 #include <vector>
30
31 #include <QColor>
32 #include <QCheckBox>
33 #include <QElapsedTimer>
34 #include <QPolygon>
35 #include <QPushButton>
36 #include <QSignalMapper>
37 #include <QTimer>
38
39 #include <pv/binding/decoder.hpp>
40 #include <pv/data/decode/decoder.hpp>
41 #include <pv/data/decode/annotation.hpp>
42 #include <pv/data/decode/row.hpp>
43 #include <pv/data/signalbase.hpp>
44
45 #define DECODETRACE_SHOW_RENDER_TIME 0
46
47 using std::deque;
48 using std::list;
49 using std::map;
50 using std::mutex;
51 using std::pair;
52 using std::shared_ptr;
53 using std::vector;
54
55 using pv::data::SignalBase;
56 using pv::data::decode::Annotation;
57 using pv::data::decode::Decoder;
58 using pv::data::decode::Row;
59
60 struct srd_channel;
61 struct srd_decoder;
62
63 namespace pv {
64
65 class Session;
66
67 namespace data {
68 struct DecodeChannel;
69 class DecodeSignal;
70
71 namespace decode {
72 class Decoder;
73 class Row;
74 }
75 }  // namespace data
76
77 namespace widgets {
78 class DecoderGroupBox;
79 }
80
81 namespace views {
82 namespace trace {
83
84 class ContainerWidget;
85
86 struct DecodeTraceRow {
87         // When adding a field, make sure it's initialized properly in
88         // DecodeTrace::update_rows()
89
90         Row* decode_row;
91         unsigned int height, expanded_height, title_width, animation_step;
92         bool exists, currently_visible, has_hidden_classes;
93         bool expand_marker_highlighted, expanding, expanded, collapsing;
94         QPolygon expand_marker_shape;
95         float anim_height, anim_shape;
96
97         ContainerWidget* container;
98         QWidget* header_container;
99         QWidget* selector_container;
100         QCheckBox* row_visibility_checkbox;
101         vector<QCheckBox*> selectors;
102
103         QColor row_color;
104         map<uint32_t, QColor> ann_class_color;
105         map<uint32_t, QColor> ann_class_dark_color;
106 };
107
108 class ContainerWidget : public QWidget
109 {
110         Q_OBJECT
111
112 public:
113         ContainerWidget(QWidget *parent = nullptr);
114
115         virtual void resizeEvent(QResizeEvent* event);
116
117 Q_SIGNALS:
118         void widgetResized(QWidget* sender);
119 };
120
121 class DecodeTrace : public Trace
122 {
123         Q_OBJECT
124
125 private:
126         static const QColor ErrorBgColor;
127         static const QColor NoDecodeColor;
128         static const QColor ExpandMarkerWarnColor;
129         static const QColor ExpandMarkerHiddenColor;
130         static const uint8_t ExpansionAreaHeaderAlpha;
131         static const uint8_t ExpansionAreaAlpha;
132
133         static const int ArrowSize;
134         static const double EndCapWidth;
135         static const int RowTitleMargin;
136         static const int DrawPadding;
137
138         static const int MaxTraceUpdateRate;
139         static const int AnimationDurationInTicks;
140         static const int HiddenRowHideDelay;
141
142 public:
143         DecodeTrace(pv::Session &session, shared_ptr<SignalBase> signalbase,
144                 int index);
145
146         ~DecodeTrace();
147
148         bool enabled() const;
149
150         shared_ptr<SignalBase> base() const;
151
152         /**
153          * Sets the owner this trace in the view trace hierachy.
154          * @param The new owner of the trace.
155          */
156         virtual void set_owner(TraceTreeItemOwner *owner);
157
158         /**
159          * Computes the vertical extents of the contents of this row item.
160          * @return A pair containing the minimum and maximum y-values.
161          */
162         pair<int, int> v_extents() const;
163
164         /**
165          * Paints the background layer of the trace with a QPainter
166          * @param p the QPainter to paint into.
167          * @param pp the painting parameters object to paint with..
168          */
169         void paint_back(QPainter &p, ViewItemPaintParams &pp);
170
171         /**
172          * Paints the mid-layer of the trace with a QPainter
173          * @param p the QPainter to paint into.
174          * @param pp the painting parameters object to paint with.
175          */
176         void paint_mid(QPainter &p, ViewItemPaintParams &pp);
177
178         /**
179          * Paints the foreground layer of the trace with a QPainter
180          * @param p the QPainter to paint into.
181          * @param pp the painting parameters object to paint with.
182          */
183         void paint_fore(QPainter &p, ViewItemPaintParams &pp);
184
185         void populate_popup_form(QWidget *parent, QFormLayout *form);
186
187         QMenu* create_header_context_menu(QWidget *parent);
188
189         virtual QMenu* create_view_context_menu(QWidget *parent, QPoint &click_pos);
190
191         virtual void delete_pressed();
192
193         virtual void hover_point_changed(const QPoint &hp);
194
195         virtual void mouse_left_press_event(const QMouseEvent* event);
196
197 private:
198         void draw_annotations(deque<const Annotation*>& annotations, QPainter &p,
199                 const ViewItemPaintParams &pp, int y, const DecodeTraceRow& row);
200
201         void draw_annotation(const Annotation* a, QPainter &p,
202                 const ViewItemPaintParams &pp, int y, const DecodeTraceRow& row) const;
203
204         void draw_annotation_block(qreal start, qreal end, Annotation::Class ann_class,
205                 bool use_ann_format, QPainter &p, int y, const DecodeTraceRow& row) const;
206
207         void draw_instant(const Annotation* a, QPainter &p, qreal x, int y) const;
208
209         void draw_range(const Annotation* a, QPainter &p, qreal start, qreal end,
210                 int y, const ViewItemPaintParams &pp, int row_title_width) const;
211
212         void draw_error(QPainter &p, const QString &message, const ViewItemPaintParams &pp);
213
214         void draw_unresolved_period(QPainter &p, int left, int right) const;
215
216         pair<double, double> get_pixels_offset_samples_per_pixel() const;
217
218         /**
219          * Determines the start and end sample for a given pixel range.
220          * @param x_start the X coordinate of the start sample in the view
221          * @param x_end the X coordinate of the end sample in the view
222          * @return Returns a pair containing the start sample and the end
223          *      sample that correspond to the start and end coordinates.
224          */
225         pair<uint64_t, uint64_t> get_view_sample_range(int x_start, int x_end) const;
226
227         QColor get_row_color(int row_index) const;
228         QColor get_annotation_color(QColor row_color, int annotation_index) const;
229
230         unsigned int get_row_y(const DecodeTraceRow* row) const;
231
232         DecodeTraceRow* get_row_at_point(const QPoint &point);
233
234         const QString get_annotation_at_point(const QPoint &point);
235
236         void update_stack_button();
237
238         void create_decoder_form(int index, shared_ptr<Decoder> &dec,
239                 QWidget *parent, QFormLayout *form);
240
241         QComboBox* create_channel_selector(QWidget *parent,
242                 const data::decode::DecodeChannel *ch);
243         QComboBox* create_channel_selector_init_state(QWidget *parent,
244                 const data::decode::DecodeChannel *ch);
245
246         void export_annotations(deque<const Annotation*>& annotations) const;
247
248         void initialize_row_widgets(DecodeTraceRow* r, unsigned int row_id);
249         void update_rows();
250
251         /**
252          * Sets row r to expanded state without forcing an update of the view
253          */
254         void set_row_expanded(DecodeTraceRow* r);
255
256         /**
257          * Sets row r to collapsed state without forcing an update of the view
258          */
259         void set_row_collapsed(DecodeTraceRow* r);
260
261         void update_expanded_rows();
262
263 private Q_SLOTS:
264         void on_setting_changed(const QString &key, const QVariant &value);
265
266         void on_new_annotations();
267         void on_delayed_trace_update();
268         void on_decode_reset();
269         void on_decode_finished();
270         void on_pause_decode();
271
272         void on_delete();
273
274         void on_channel_selected(int);
275
276         void on_channels_updated();
277
278         void on_init_state_changed(int);
279
280         void on_stack_decoder(srd_decoder *decoder);
281
282         void on_delete_decoder(int index);
283
284         void on_show_hide_decoder(int index);
285         void on_show_hide_row(int row_id);
286         void on_show_hide_class(QWidget* sender);
287         void on_show_all_classes();
288         void on_hide_all_classes();
289         void on_row_container_resized(QWidget* sender);
290
291         void on_copy_annotation_to_clipboard();
292
293         void on_export_row();
294         void on_export_all_rows();
295         void on_export_row_with_cursor();
296         void on_export_all_rows_with_cursor();
297         void on_export_row_from_here();
298         void on_export_all_rows_from_here();
299
300         void on_animation_timer();
301         void on_hide_hidden_rows();
302
303 private:
304         pv::Session &session_;
305         shared_ptr<data::DecodeSignal> decode_signal_;
306
307         deque<DecodeTraceRow> rows_;
308         mutable mutex row_modification_mutex_;
309
310         map<QComboBox*, uint16_t> channel_id_map_;  // channel selector -> decode channel ID
311         map<QComboBox*, uint16_t> init_state_map_;  // init state selector -> decode channel ID
312         list< shared_ptr<pv::binding::Decoder> > bindings_;
313
314         const Row* selected_row_;
315         pair<uint64_t, uint64_t> selected_sample_range_;
316
317         vector<pv::widgets::DecoderGroupBox*> decoder_forms_;
318         QPushButton* stack_button_;
319
320         unsigned int default_row_height_, annotation_height_;
321         unsigned int visible_rows_, max_visible_rows_;
322
323         int min_useful_label_width_;
324         bool always_show_all_rows_, show_hidden_rows_;
325
326         QSignalMapper delete_mapper_, show_hide_mapper_;
327         QSignalMapper row_show_hide_mapper_, class_show_hide_mapper_;
328
329         QTimer delayed_trace_updater_, animation_timer_, delayed_hidden_row_hider_;
330
331         QPolygon default_marker_shape_;
332
333 #if DECODETRACE_SHOW_RENDER_TIME
334         QElapsedTimer render_time_;
335 #endif
336 };
337
338 } // namespace trace
339 } // namespace views
340 } // namespace pv
341
342 #endif // PULSEVIEW_PV_VIEWS_TRACEVIEW_DECODETRACE_HPP