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