]> sigrok.org Git - pulseview.git/blob - pv/views/trace/decodetrace.hpp
662562c371984ade791a71aecbf6dfc8b46b6e72
[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 "trace.hpp"
24
25 #include <list>
26 #include <map>
27 #include <memory>
28 #include <vector>
29
30 #include <QColor>
31 #include <QComboBox>
32 #include <QPolygon>
33 #include <QPushButton>
34 #include <QSignalMapper>
35 #include <QTimer>
36
37 #include <pv/binding/decoder.hpp>
38 #include <pv/data/decode/decoder.hpp>
39 #include <pv/data/decode/annotation.hpp>
40 #include <pv/data/decode/row.hpp>
41 #include <pv/data/signalbase.hpp>
42
43 using std::deque;
44 using std::list;
45 using std::map;
46 using std::mutex;
47 using std::pair;
48 using std::shared_ptr;
49 using std::vector;
50
51 struct srd_channel;
52 struct srd_decoder;
53
54 namespace pv {
55
56 class Session;
57
58 namespace data {
59 struct DecodeChannel;
60 class DecodeSignal;
61
62 namespace decode {
63 class Decoder;
64 }
65 }  // namespace data
66
67 namespace widgets {
68 class DecoderGroupBox;
69 }
70
71 namespace views {
72 namespace trace {
73
74 struct RowData {
75         // When adding a field, make sure it's initialized properly in
76         // DecodeTrace::update_rows()
77
78         data::decode::Row decode_row;
79         unsigned int height, expanded_height, title_width, animation_step;
80         bool exists, currently_visible;
81         bool expand_marker_highlighted, expanding, expanded, collapsing;
82         QPolygon expand_marker_shape;
83         float anim_height, anim_shape;
84
85         QWidget* container;
86 };
87
88 class DecodeTrace : public Trace
89 {
90         Q_OBJECT
91
92 private:
93         static const QColor ErrorBgColor;
94         static const QColor NoDecodeColor;
95
96         static const int ArrowSize;
97         static const double EndCapWidth;
98         static const int RowTitleMargin;
99         static const int DrawPadding;
100
101         static const int MaxTraceUpdateRate;
102         static const unsigned int AnimationDurationInTicks;
103
104 public:
105         DecodeTrace(pv::Session &session, shared_ptr<data::SignalBase> signalbase,
106                 int index);
107
108         ~DecodeTrace();
109
110         bool enabled() const;
111
112         shared_ptr<data::SignalBase> base() const;
113
114         /**
115          * Computes the vertical extents of the contents of this row item.
116          * @return A pair containing the minimum and maximum y-values.
117          */
118         pair<int, int> v_extents() const;
119
120         /**
121          * Paints the background layer of the trace with a QPainter
122          * @param p the QPainter to paint into.
123          * @param pp the painting parameters object to paint with..
124          */
125         void paint_back(QPainter &p, ViewItemPaintParams &pp);
126
127         /**
128          * Paints the mid-layer of the trace with a QPainter
129          * @param p the QPainter to paint into.
130          * @param pp the painting parameters object to paint with.
131          */
132         void paint_mid(QPainter &p, ViewItemPaintParams &pp);
133
134         /**
135          * Paints the foreground layer of the trace with a QPainter
136          * @param p the QPainter to paint into.
137          * @param pp the painting parameters object to paint with.
138          */
139         void paint_fore(QPainter &p, ViewItemPaintParams &pp);
140
141         void populate_popup_form(QWidget *parent, QFormLayout *form);
142
143         QMenu* create_header_context_menu(QWidget *parent);
144
145         virtual QMenu* create_view_context_menu(QWidget *parent, QPoint &click_pos);
146
147         virtual void delete_pressed();
148
149         virtual void hover_point_changed(const QPoint &hp);
150
151         virtual void mouse_left_press_event(const QMouseEvent* event);
152
153 private:
154         void draw_annotations(vector<pv::data::decode::Annotation> annotations,
155                 QPainter &p, int h, const ViewItemPaintParams &pp, int y,
156                 QColor row_color, int row_title_width);
157
158         void draw_annotation(const pv::data::decode::Annotation &a, QPainter &p,
159                 int h, const ViewItemPaintParams &pp, int y,
160                 QColor row_color, int row_title_width) const;
161
162         void draw_annotation_block(qreal start, qreal end,
163                 pv::data::decode::Annotation::Class ann_class, bool use_ann_format,
164                 QPainter &p, int h, int y, QColor row_color) const;
165
166         void draw_instant(const pv::data::decode::Annotation &a, QPainter &p,
167                 int h, qreal x, int y) const;
168
169         void draw_range(const pv::data::decode::Annotation &a, QPainter &p,
170                 int h, qreal start, qreal end, int y, const ViewItemPaintParams &pp,
171                 int row_title_width) const;
172
173         void draw_error(QPainter &p, const QString &message,
174                 const ViewItemPaintParams &pp);
175
176         void draw_unresolved_period(QPainter &p, int h, int left,
177                 int right) const;
178
179         pair<double, double> get_pixels_offset_samples_per_pixel() const;
180
181         /**
182          * Determines the start and end sample for a given pixel range.
183          * @param x_start the X coordinate of the start sample in the view
184          * @param x_end the X coordinate of the end sample in the view
185          * @return Returns a pair containing the start sample and the end
186          *      sample that correspond to the start and end coordinates.
187          */
188         pair<uint64_t, uint64_t> get_view_sample_range(int x_start, int x_end) const;
189
190         QColor get_row_color(int row_index) const;
191         QColor get_annotation_color(QColor row_color, int annotation_index) const;
192
193         unsigned int get_row_y(const RowData* row) const;
194
195         RowData* get_row_at_point(const QPoint &point);
196
197         const QString get_annotation_at_point(const QPoint &point);
198
199         void update_stack_button();
200
201         void create_decoder_form(int index,
202                 shared_ptr<pv::data::decode::Decoder> &dec,
203                 QWidget *parent, QFormLayout *form);
204
205         QComboBox* create_channel_selector(QWidget *parent,
206                 const data::decode::DecodeChannel *ch);
207         QComboBox* create_channel_selector_init_state(QWidget *parent,
208                 const data::decode::DecodeChannel *ch);
209
210         void export_annotations(vector<data::decode::Annotation> *annotations) const;
211
212         void update_rows();
213
214 private Q_SLOTS:
215         void on_setting_changed(const QString &key, const QVariant &value);
216
217         void on_new_annotations();
218         void on_delayed_trace_update();
219         void on_decode_reset();
220         void on_decode_finished();
221         void on_pause_decode();
222
223         void on_delete();
224
225         void on_channel_selected(int);
226
227         void on_channels_updated();
228
229         void on_init_state_changed(int);
230
231         void on_stack_decoder(srd_decoder *decoder);
232
233         void on_delete_decoder(int index);
234
235         void on_show_hide_decoder(int index);
236
237         void on_copy_annotation_to_clipboard();
238
239         void on_export_row();
240         void on_export_all_rows();
241         void on_export_row_with_cursor();
242         void on_export_all_rows_with_cursor();
243         void on_export_row_from_here();
244         void on_export_all_rows_from_here();
245
246         void on_animation_timer();
247
248 private:
249         pv::Session &session_;
250         shared_ptr<data::DecodeSignal> decode_signal_;
251
252         deque<RowData> rows_;
253         mutable mutex row_modification_mutex_;
254
255         map<QComboBox*, uint16_t> channel_id_map_;  // channel selector -> decode channel ID
256         map<QComboBox*, uint16_t> init_state_map_;  // init state selector -> decode channel ID
257         list< shared_ptr<pv::binding::Decoder> > bindings_;
258
259         const data::decode::Row* selected_row_;
260         pair<uint64_t, uint64_t> selected_sample_range_;
261
262         vector<pv::widgets::DecoderGroupBox*> decoder_forms_;
263         QPushButton* stack_button_;
264
265         unsigned int default_row_height_, annotation_height_;
266         unsigned int visible_rows_, max_visible_rows_;
267
268         int min_useful_label_width_;
269         bool always_show_all_rows_;
270
271         QSignalMapper delete_mapper_, show_hide_mapper_;
272
273         QTimer delayed_trace_updater_, animation_timer_;
274
275         QPolygon default_marker_shape_;
276 };
277
278 } // namespace trace
279 } // namespace views
280 } // namespace pv
281
282 #endif // PULSEVIEW_PV_VIEWS_TRACEVIEW_DECODETRACE_HPP