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