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