]> sigrok.org Git - pulseview.git/blob - pv/views/trace/view.hpp
Refactor channel/signal handling
[pulseview.git] / pv / views / trace / 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 <mutex>
27 #include <set>
28 #include <vector>
29
30 #include <QAbstractScrollArea>
31 #include <QShortcut>
32 #include <QSizeF>
33 #include <QSplitter>
34
35 #include <pv/globalsettings.hpp>
36 #include <pv/util.hpp>
37 #include <pv/data/signaldata.hpp>
38 #include <pv/views/viewbase.hpp>
39
40 #include "cursorpair.hpp"
41 #include "flag.hpp"
42 #include "trace.hpp"
43 #include "tracetreeitemowner.hpp"
44
45 using std::list;
46 using std::map;
47 using std::set;
48 using std::shared_ptr;
49 using std::vector;
50
51 namespace sigrok {
52 class ChannelGroup;
53 }
54
55 namespace pv {
56
57 class Session;
58
59 namespace data {
60 class Logic;
61 }
62
63 namespace views {
64
65 namespace trace {
66
67 class DecodeTrace;
68 class Header;
69 class Ruler;
70 class Signal;
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, public GlobalSettingsInterface
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         static const int ViewScrollMargin;
99
100         static const int ScaleUnits[3];
101
102 public:
103         explicit View(Session &session, bool is_main_view=false, QMainWindow *parent = nullptr);
104
105         ~View();
106
107         virtual ViewType get_type() const;
108
109         /**
110          * Resets the view to its default state after construction. It does however
111          * not reset the signal bases or any other connections with the session.
112          */
113         virtual void reset_view_state();
114
115         Session& session();  // This method is needed for TraceTreeItemOwner, not ViewBase
116         const Session& session() const;  // This method is needed for TraceTreeItemOwner, not ViewBase
117
118         /**
119          * Returns the signals contained in this view.
120          */
121         vector< shared_ptr<Signal> > signals() const;
122
123         shared_ptr<Signal> get_signal_by_signalbase(shared_ptr<data::SignalBase> base) const;
124
125         virtual void clear_signalbases();
126         virtual void add_signalbase(const shared_ptr<data::SignalBase> signalbase);
127         virtual void remove_signalbase(const shared_ptr<data::SignalBase> signalbase);
128
129 #ifdef ENABLE_DECODE
130         virtual void clear_decode_signals();
131
132         virtual void add_decode_signal(shared_ptr<data::DecodeSignal> signal);
133
134         virtual void remove_decode_signal(shared_ptr<data::DecodeSignal> signal);
135 #endif
136
137         shared_ptr<Signal> get_signal_under_mouse_cursor() const;
138
139         /**
140          * Returns the view of the owner.
141          */
142         virtual View* view();
143
144         /**
145          * Returns the view of the owner.
146          */
147         virtual const View* view() const;
148
149         Viewport* viewport();
150         const Viewport* viewport() const;
151
152         QAbstractScrollArea* scrollarea() const;
153
154         const Ruler* ruler() const;
155
156         virtual void save_settings(QSettings &settings) const;
157         virtual void restore_settings(QSettings &settings);
158
159         /**
160          * Gets a list of time markers.
161          */
162         vector< shared_ptr<TimeItem> > time_items() const;
163
164         /**
165          * Returns the view time scale in seconds per pixel.
166          */
167         double scale() const;
168
169         /**
170          * Returns the internal view version of the time offset of the left edge
171          * of the view in seconds.
172          */
173         const pv::util::Timestamp& offset() const;
174
175         /**
176          * Returns the ruler version of the time offset of the left edge
177          * of the view in seconds.
178          */
179         const pv::util::Timestamp& ruler_offset() const;
180
181         void set_zero_position(const pv::util::Timestamp& position);
182
183         void reset_zero_position();
184
185         pv::util::Timestamp zero_offset() const;
186
187         /**
188          * Returns the vertical scroll offset.
189          */
190         int owner_visual_v_offset() const;
191
192         /**
193          * Sets the visual v-offset.
194          */
195         void set_v_offset(int offset);
196
197         /**
198          * Sets the visual h-offset.
199          */
200         void set_h_offset(int offset);
201
202         /**
203          * Gets the length of the horizontal scrollbar.
204          */
205         int get_h_scrollbar_maximum() const;
206
207         /**
208          * Returns the SI prefix to apply to the graticule time markings.
209          */
210         pv::util::SIPrefix tick_prefix() const;
211
212         /**
213          * Returns the number of fractional digits shown for the time markings.
214          */
215         unsigned int tick_precision() const;
216
217         /**
218          * Returns period of the graticule time markings.
219          */
220         const pv::util::Timestamp& tick_period() const;
221
222         /**
223          * Returns number of minor division ticks per time marking.
224          */
225         unsigned int minor_tick_count() const;
226
227         /**
228          * Returns the unit of time currently used.
229          */
230         util::TimeUnit time_unit() const;
231
232         /**
233          * Returns the number of nested parents that this row item owner has.
234          */
235         unsigned int depth() const;
236
237         /**
238          * Returns the currently displayed segment, starting at 0.
239          */
240         uint32_t current_segment() const;
241
242         /**
243          * Returns whether the currently shown segment can be influenced
244          * (selected) or not.
245          */
246         bool segment_is_selectable() const;
247
248         Trace::SegmentDisplayMode segment_display_mode() const;
249         void set_segment_display_mode(Trace::SegmentDisplayMode mode);
250
251         void zoom(double steps);
252         void zoom(double steps, int offset);
253
254         void zoom_fit(bool gui_state);
255
256         virtual void focus_on_range(uint64_t start_sample, uint64_t end_sample);
257
258         /**
259          * Sets the scale and offset.
260          * @param scale The new view scale in seconds per pixel.
261          * @param offset The view time offset in seconds.
262          */
263         void set_scale_offset(double scale, const pv::util::Timestamp& offset);
264
265         vector< shared_ptr<pv::data::SignalData> > get_visible_data() const;
266
267         pair<pv::util::Timestamp, pv::util::Timestamp> get_time_extents() const;
268
269         /**
270          * Returns true if the trace background should be drawn with a colored background.
271          */
272         bool colored_bg() const;
273
274         /**
275          * Returns true if cursors are displayed, false otherwise.
276          */
277         bool cursors_shown() const;
278
279         /**
280          * Shows or hides the cursors.
281          */
282         void show_cursors(bool show = true);
283
284         /**
285          * Sets the cursors to the given offsets.
286          * You still have to call show_cursors() separately.
287          */
288         void set_cursors(pv::util::Timestamp& first, pv::util::Timestamp& second);
289
290         /**
291          * Moves the cursors to a convenient position in the view.
292          * You still have to call show_cursors() separately.
293          */
294         void center_cursors();
295
296         /**
297          * Returns a reference to the pair of cursors.
298          */
299         shared_ptr<CursorPair> cursors() const;
300
301         /**
302          * Adds a new flag at a specified time.
303          */
304         shared_ptr<Flag> add_flag(const pv::util::Timestamp& time);
305
306         /**
307          * Removes a flag from the list.
308          */
309         void remove_flag(shared_ptr<Flag> flag);
310
311         /**
312          * Gets the list of flags.
313          */
314         vector< shared_ptr<Flag> > flags() const;
315
316         const QPoint& hover_point() const;
317         const QWidget* hover_widget() const;
318
319         /**
320          * Determines the closest level change (i.e. edge) to a given point, which
321          * is useful for e.g. the "snap to edge" functionality.
322          *
323          * @param p The current position of the mouse cursor
324          * @return The sample number of the nearest level change or -1 if none
325          */
326         int64_t get_nearest_level_change(const QPoint &p);
327
328         void restack_all_trace_tree_items();
329
330         int header_width() const;
331
332         void on_setting_changed(const QString &key, const QVariant &value);
333
334 Q_SIGNALS:
335         void hover_point_changed(const QWidget* widget, const QPoint &hp);
336
337         void selection_changed();
338
339         /// Emitted when the offset changed.
340         void offset_changed();
341
342         /// Emitted when the scale changed.
343         void scale_changed();
344
345         void sticky_scrolling_changed(bool state);
346
347         void always_zoom_to_fit_changed(bool state);
348
349         /// Emitted when the tick_prefix changed.
350         void tick_prefix_changed();
351
352         /// Emitted when the tick_precision changed.
353         void tick_precision_changed();
354
355         /// Emitted when the tick_period changed.
356         void tick_period_changed();
357
358         /// Emitted when the time_unit changed.
359         void time_unit_changed();
360
361         /// Emitted when the currently selected segment changed
362         void segment_changed(int segment_id);
363
364         /// Emitted when the multi-segment display mode changed
365         /// @param mode is a value of Trace::SegmentDisplayMode
366         void segment_display_mode_changed(int mode, bool segment_selectable);
367
368         /// Emitted when the cursors are shown/hidden
369         void cursor_state_changed(bool show);
370
371 public Q_SLOTS:
372         void trigger_event(int segment_id, util::Timestamp location);
373
374 private:
375         void get_scroll_layout(double &length, pv::util::Timestamp &offset) const;
376
377         /**
378          * Simultaneously sets the zoom and offset.
379          * @param scale The scale to set the view to in seconds per pixel. This
380          * value is clamped between MinScale and MaxScale.
381          * @param offset The offset of the left edge of the view in seconds.
382          */
383         void set_zoom(double scale, int offset);
384
385         /**
386          * Find a tick spacing and number formatting that does not cause
387          * the values to collide.
388          */
389         void calculate_tick_spacing();
390
391         void adjust_top_margin();
392
393         void update_scroll();
394
395         void reset_scroll();
396
397         void set_scroll_default();
398
399         void determine_if_header_was_shrunk();
400
401         void resize_header_to_fit();
402
403         void update_layout();
404
405         TraceTreeItemOwner* find_prevalent_trace_group(
406                 const shared_ptr<sigrok::ChannelGroup> &group,
407                 const map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > &signal_map);
408
409         static vector< shared_ptr<Trace> >
410                 extract_new_traces_for_channels(
411                 const vector< shared_ptr<sigrok::Channel> > &channels,
412                 const map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > &signal_map,
413                 set< shared_ptr<Trace> > &add_list);
414
415         void determine_time_unit();
416
417         bool eventFilter(QObject *object, QEvent *event);
418
419         virtual void contextMenuEvent(QContextMenuEvent *event);
420
421         void resizeEvent(QResizeEvent *event);
422
423         void update_hover_point();
424
425 public:
426         void row_item_appearance_changed(bool label, bool content);
427         void time_item_appearance_changed(bool label, bool content);
428
429         void extents_changed(bool horz, bool vert);
430
431 private Q_SLOTS:
432         void on_signal_name_changed();
433         void on_splitter_moved();
434
435         void on_zoom_in_shortcut_triggered();
436         void on_zoom_out_shortcut_triggered();
437         void on_scroll_to_start_shortcut_triggered();
438         void on_scroll_to_end_shortcut_triggered();
439
440         void h_scroll_value_changed(int value);
441         void v_scroll_value_changed();
442
443         void on_grab_ruler(int ruler_id);
444
445         void signals_changed();
446         void capture_state_updated(int state);
447
448         void on_new_segment(int new_segment_id);
449         void on_segment_completed(int new_segment_id);
450         void on_segment_changed(int segment);
451
452         void on_settingViewTriggerIsZeroTime_changed(const QVariant new_value);
453
454         virtual void perform_delayed_view_update();
455
456         void process_sticky_events();
457
458         /**
459          * Sets the 'offset_' and ruler_offset_ members and emits the 'offset_changed'
460          * signal if needed.
461          */
462         void set_offset(const pv::util::Timestamp& offset, bool force_update = false);
463
464         /**
465          * Sets the 'scale_' member and emits the 'scale_changed'
466          * signal if needed.
467          */
468         void set_scale(double scale);
469
470         /**
471          * Sets the 'tick_prefix_' member and emits the 'tick_prefix_changed'
472          * signal if needed.
473          */
474         void set_tick_prefix(pv::util::SIPrefix tick_prefix);
475
476         /**
477          * Sets the 'tick_precision_' member and emits the 'tick_precision_changed'
478          * signal if needed.
479          */
480         void set_tick_precision(unsigned tick_precision);
481
482         /**
483          * Sets the 'tick_period_' member and emits the 'tick_period_changed'
484          * signal if needed.
485          */
486         void set_tick_period(const pv::util::Timestamp& tick_period);
487
488         /**
489          * Sets the 'time_unit' member and emits the 'time_unit_changed'
490          * signal if needed.
491          */
492         void set_time_unit(pv::util::TimeUnit time_unit);
493
494         /**
495          * Sets the current segment with the first segment starting at 0.
496          */
497         void set_current_segment(uint32_t segment_id);
498
499 private:
500         CustomScrollArea *scrollarea_;
501         Viewport *viewport_;
502         Ruler *ruler_;
503         Header *header_;
504         QSplitter *splitter_;
505
506         QShortcut *zoom_in_shortcut_, *zoom_in_shortcut_2_;
507         QShortcut *zoom_out_shortcut_, *zoom_out_shortcut_2_;
508         QShortcut *home_shortcut_, *end_shortcut_;
509         QShortcut *grab_ruler_left_shortcut_, *grab_ruler_right_shortcut_;
510         QShortcut *cancel_grab_shortcut_;
511
512         mutable mutex signal_mutex_;
513         vector< shared_ptr<Signal> > signals_;
514
515 #ifdef ENABLE_DECODE
516         vector< shared_ptr<DecodeTrace> > decode_traces_;
517 #endif
518
519         Trace::SegmentDisplayMode segment_display_mode_;
520
521         /// Signals whether the user can change the currently shown segment.
522         bool segment_selectable_;
523
524         /// The view time scale in seconds per pixel.
525         double scale_;
526
527         /// The internal view version of the time offset in seconds.
528         pv::util::Timestamp offset_;
529         /// The ruler version of the time offset in seconds.
530         pv::util::Timestamp ruler_offset_;
531         /// The offset of the zero point in seconds.
532         pv::util::Timestamp zero_offset_;
533         /// Shows whether the user set a custom zero offset that we should keep
534         bool custom_zero_offset_set_;
535
536         bool updating_scroll_;
537         bool settings_restored_;
538         bool header_was_shrunk_;
539
540         bool sticky_scrolling_;
541         bool colored_bg_;
542         bool always_zoom_to_fit_;
543
544         pv::util::Timestamp tick_period_;
545         pv::util::SIPrefix tick_prefix_;
546         unsigned int minor_tick_count_;
547         unsigned int tick_precision_;
548         util::TimeUnit time_unit_;
549
550         bool show_cursors_;
551         shared_ptr<CursorPair> cursors_;
552
553         list< shared_ptr<Flag> > flags_;
554         char next_flag_text_;
555
556         vector< shared_ptr<TriggerMarker> > trigger_markers_;
557
558         QWidget* hover_widget_;
559         TimeMarker* grabbed_widget_;
560         QPoint hover_point_;
561         shared_ptr<Signal> signal_under_mouse_cursor_;
562         uint16_t snap_distance_;
563
564         unsigned int sticky_events_;
565         QTimer lazy_event_handler_;
566
567         // This is true when the defaults couldn't be set due to insufficient info
568         bool scroll_needs_defaults_;
569
570         // A nonzero value indicates the v offset to restore. See View::resizeEvent()
571         int saved_v_offset_;
572
573         // These are used to determine whether the view was altered after acq started
574         double scale_at_acq_start_;
575         pv::util::Timestamp offset_at_acq_start_;
576
577         // Used to suppress performing a "zoom to fit" when the session stops. This
578         // is needed when the view's settings are restored before acquisition ends.
579         // In that case we want to keep the restored settings, not have a "zoom to fit"
580         // mess them up.
581         bool suppress_zoom_to_fit_after_acq_;
582 };
583
584 } // namespace trace
585 } // namespace views
586 } // namespace pv
587
588 #endif // PULSEVIEW_PV_VIEWS_TRACEVIEW_VIEW_HPP