]> sigrok.org Git - pulseview.git/blob - pv/view/view.hpp
1184213150d21449423f97156f9bafa9fbe01f81
[pulseview.git] / pv / view / view.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_VIEW_HPP
21 #define PULSEVIEW_PV_VIEWS_TRACEVIEW_VIEW_HPP
22
23 #include <cstdint>
24 #include <list>
25 #include <memory>
26 #include <set>
27 #include <unordered_map>
28 #include <vector>
29
30 #include <QAbstractScrollArea>
31 #include <QSizeF>
32 #include <QSplitter>
33
34 #include <pv/data/signaldata.hpp>
35 #include <pv/util.hpp>
36 #include <pv/views/viewbase.hpp>
37
38 #include "cursorpair.hpp"
39 #include "flag.hpp"
40 #include "tracetreeitemowner.hpp"
41
42 using std::list;
43 using std::unordered_map;
44 using std::unordered_set;
45 using std::set;
46 using std::shared_ptr;
47 using std::vector;
48
49 namespace sigrok {
50 class ChannelGroup;
51 }
52
53 namespace pv {
54
55 class Session;
56
57 namespace data {
58 class Logic;
59 }
60
61 namespace views {
62
63 namespace TraceView {
64
65 class CursorHeader;
66 class DecodeTrace;
67 class Header;
68 class Ruler;
69 class Signal;
70 class Trace;
71 class Viewport;
72 class TriggerMarker;
73
74 class CustomScrollArea : public QAbstractScrollArea
75 {
76         Q_OBJECT
77
78 public:
79         CustomScrollArea(QWidget *parent = nullptr);
80         bool viewportEvent(QEvent *event);
81 };
82
83 class View : public ViewBase, public TraceTreeItemOwner
84 {
85         Q_OBJECT
86
87 private:
88         enum StickyEvents {
89                 TraceTreeItemHExtentsChanged = 1,
90                 TraceTreeItemVExtentsChanged = 2
91         };
92
93 private:
94         static const pv::util::Timestamp MaxScale;
95         static const pv::util::Timestamp MinScale;
96
97         static const int MaxScrollValue;
98
99         static const int ScaleUnits[3];
100
101 public:
102         explicit View(Session &session, bool is_main_view=false, QWidget *parent = nullptr);
103
104         Session& session();
105         const Session& session() const;
106
107         /**
108          * Returns the signals contained in this view.
109          */
110         unordered_set< shared_ptr<Signal> > signals() const;
111
112         virtual void clear_signals();
113
114         void add_signal(const shared_ptr<Signal> signal);
115
116 #ifdef ENABLE_DECODE
117         virtual void clear_decode_signals();
118
119         virtual void add_decode_signal(shared_ptr<data::SignalBase> signalbase);
120
121         virtual void remove_decode_signal(shared_ptr<data::SignalBase> signalbase);
122 #endif
123
124         /**
125          * Returns the view of the owner.
126          */
127         virtual View* view();
128
129         /**
130          * Returns the view of the owner.
131          */
132         virtual const View* view() const;
133
134         Viewport* viewport();
135
136         const Viewport* viewport() const;
137
138         virtual void save_settings(QSettings &settings) const;
139
140         virtual void restore_settings(QSettings &settings);
141
142         /**
143          * Gets a list of time markers.
144          */
145         vector< shared_ptr<TimeItem> > time_items() const;
146
147         /**
148          * Returns the view time scale in seconds per pixel.
149          */
150         double scale() const;
151
152         /**
153          * Returns the time offset of the left edge of the view in
154          * seconds.
155          */
156         const pv::util::Timestamp& offset() const;
157
158         /**
159          * Returns the vertical scroll offset.
160          */
161         int owner_visual_v_offset() const;
162
163         /**
164          * Sets the visual v-offset.
165          */
166         void set_v_offset(int offset);
167
168         /**
169          * Returns the SI prefix to apply to the graticule time markings.
170          */
171         pv::util::SIPrefix tick_prefix() const;
172
173         /**
174          * Returns the number of fractional digits shown for the time markings.
175          */
176         unsigned int tick_precision() const;
177
178         /**
179          * Returns period of the graticule time markings.
180          */
181         const pv::util::Timestamp& tick_period() const;
182
183         /**
184          * Returns the unit of time currently used.
185          */
186         util::TimeUnit time_unit() const;
187
188         /**
189          * Returns the number of nested parents that this row item owner has.
190          */
191         unsigned int depth() const;
192
193         void zoom(double steps);
194         void zoom(double steps, int offset);
195
196         void zoom_fit(bool gui_state);
197
198         void zoom_one_to_one();
199
200         /**
201          * Sets the scale and offset.
202          * @param scale The new view scale in seconds per pixel.
203          * @param offset The view time offset in seconds.
204          */
205         void set_scale_offset(double scale, const pv::util::Timestamp& offset);
206
207         set< shared_ptr<pv::data::SignalData> > get_visible_data() const;
208
209         pair<pv::util::Timestamp, pv::util::Timestamp> get_time_extents() const;
210
211         /**
212          * Enables or disables coloured trace backgrounds. If they're not
213          * coloured then they will use alternating colors.
214          */
215         void enable_coloured_bg(bool state);
216
217         /**
218          * Returns true if the trace background should be drawn with a coloured background.
219          */
220         bool coloured_bg() const;
221
222         /**
223          * Enable or disable showing sampling points.
224          */
225         void enable_show_sampling_points(bool state);
226
227         /**
228          * Enable or disable showing the analog minor grid.
229          */
230         void enable_show_analog_minor_grid(bool state);
231
232         /**
233          * Returns true if cursors are displayed. false otherwise.
234          */
235         bool cursors_shown() const;
236
237         /**
238          * Shows or hides the cursors.
239          */
240         void show_cursors(bool show = true);
241
242         /**
243          * Moves the cursors to a convenient position in the view.
244          */
245         void centre_cursors();
246
247         /**
248          * Returns a reference to the pair of cursors.
249          */
250         shared_ptr<CursorPair> cursors() const;
251
252         /**
253          * Adds a new flag at a specified time.
254          */
255         void add_flag(const pv::util::Timestamp& time);
256
257         /**
258          * Removes a flag from the list.
259          */
260         void remove_flag(shared_ptr<Flag> flag);
261
262         /**
263          * Gets the list of flags.
264          */
265         vector< shared_ptr<Flag> > flags() const;
266
267         const QPoint& hover_point() const;
268
269         void restack_all_trace_tree_items();
270
271 Q_SIGNALS:
272         void hover_point_changed();
273
274         void selection_changed();
275
276         /// Emitted when the offset changed.
277         void offset_changed();
278
279         /// Emitted when the scale changed.
280         void scale_changed();
281
282         void sticky_scrolling_changed(bool state);
283
284         void always_zoom_to_fit_changed(bool state);
285
286         /// Emitted when the tick_prefix changed.
287         void tick_prefix_changed();
288
289         /// Emitted when the tick_precision changed.
290         void tick_precision_changed();
291
292         /// Emitted when the tick_period changed.
293         void tick_period_changed();
294
295         /// Emitted when the time_unit changed.
296         void time_unit_changed();
297
298 public Q_SLOTS:
299         void trigger_event(util::Timestamp location);
300
301 private:
302         void get_scroll_layout(double &length, pv::util::Timestamp &offset) const;
303
304         /**
305          * Simultaneously sets the zoom and offset.
306          * @param scale The scale to set the view to in seconds per pixel. This
307          * value is clamped between MinScale and MaxScale.
308          * @param offset The offset of the left edge of the view in seconds.
309          */
310         void set_zoom(double scale, int offset);
311
312         /**
313          * Find a tick spacing and number formatting that does not cause
314          * the values to collide.
315          */
316         void calculate_tick_spacing();
317
318         void adjust_top_margin();
319
320         void update_scroll();
321
322         void reset_scroll();
323
324         void set_scroll_default();
325
326         bool header_fully_visible() const;
327
328         void update_layout();
329
330         TraceTreeItemOwner* find_prevalent_trace_group(
331                 const shared_ptr<sigrok::ChannelGroup> &group,
332                 const unordered_map<shared_ptr<data::SignalBase>,
333                         shared_ptr<Signal> > &signal_map);
334
335         static vector< shared_ptr<Trace> >
336                 extract_new_traces_for_channels(
337                 const vector< shared_ptr<sigrok::Channel> > &channels,
338                 const unordered_map<shared_ptr<data::SignalBase>,
339                         shared_ptr<Signal> > &signal_map,
340                 set< shared_ptr<Trace> > &add_list);
341
342         void determine_time_unit();
343
344         bool eventFilter(QObject *object, QEvent *event);
345
346         void resizeEvent(QResizeEvent *event);
347
348 public:
349         void row_item_appearance_changed(bool label, bool content);
350         void time_item_appearance_changed(bool label, bool content);
351
352         void extents_changed(bool horz, bool vert);
353
354 private Q_SLOTS:
355
356         void on_splitter_moved();
357         void h_scroll_value_changed(int value);
358         void v_scroll_value_changed();
359
360         void signals_changed();
361         void capture_state_updated(int state);
362
363         virtual void perform_delayed_view_update();
364
365         void process_sticky_events();
366
367         void on_hover_point_changed();
368
369         /**
370          * Sets the 'offset_' member and emits the 'offset_changed'
371          * signal if needed.
372          */
373         void set_offset(const pv::util::Timestamp& offset);
374
375         /**
376          * Sets the 'scale_' member and emits the 'scale_changed'
377          * signal if needed.
378          */
379         void set_scale(double scale);
380
381         /**
382          * Sets the 'tick_prefix_' member and emits the 'tick_prefix_changed'
383          * signal if needed.
384          */
385         void set_tick_prefix(pv::util::SIPrefix tick_prefix);
386
387         /**
388          * Sets the 'tick_precision_' member and emits the 'tick_precision_changed'
389          * signal if needed.
390          */
391         void set_tick_precision(unsigned tick_precision);
392
393         /**
394          * Sets the 'tick_period_' member and emits the 'tick_period_changed'
395          * signal if needed.
396          */
397         void set_tick_period(const pv::util::Timestamp& tick_period);
398
399         /**
400          * Sets the 'time_unit' member and emits the 'time_unit_changed'
401          * signal if needed.
402          */
403         void set_time_unit(pv::util::TimeUnit time_unit);
404
405 private:
406         CustomScrollArea *scrollarea_;
407         Viewport *viewport_;
408         Ruler *ruler_;
409         Header *header_;
410         QSplitter *splitter_;
411
412         unordered_set< shared_ptr<Signal> > signals_;
413
414 #ifdef ENABLE_DECODE
415         vector< shared_ptr<DecodeTrace> > decode_traces_;
416 #endif
417
418         /// The view time scale in seconds per pixel.
419         double scale_;
420
421         /// The view time offset in seconds.
422         pv::util::Timestamp offset_;
423
424         bool updating_scroll_;
425         bool sticky_scrolling_;
426         bool coloured_bg_;
427         bool always_zoom_to_fit_;
428
429         pv::util::Timestamp tick_period_;
430         pv::util::SIPrefix tick_prefix_;
431         unsigned int tick_precision_;
432         util::TimeUnit time_unit_;
433
434         bool show_cursors_;
435         shared_ptr<CursorPair> cursors_;
436
437         list< shared_ptr<Flag> > flags_;
438         char next_flag_text_;
439
440         vector< shared_ptr<TriggerMarker> > trigger_markers_;
441
442         QPoint hover_point_;
443
444         unsigned int sticky_events_;
445         QTimer lazy_event_handler_;
446
447         // This is true when the defaults couldn't be set due to insufficient info
448         bool scroll_needs_defaults_;
449
450         // A nonzero value indicates the v offset to restore. See View::resizeEvent()
451         int saved_v_offset_;
452 };
453
454 } // namespace TraceView
455 } // namespace views
456 } // namespace pv
457
458 #endif // PULSEVIEW_PV_VIEWS_TRACEVIEW_VIEW_HPP