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