]> sigrok.org Git - pulseview.git/blob - pv/view/view.hpp
95a02084dca1d95cafa28949b24dccd5441d4930
[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 "rowitemowner.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
59 class View : public QAbstractScrollArea, public RowItemOwner {
60         Q_OBJECT
61
62 private:
63         enum StickyEvents {
64                 RowItemHExtentsChanged = 1,
65                 RowItemVExtentsChanged = 2
66         };
67
68 private:
69         static const double MaxScale;
70         static const double MinScale;
71
72         static const int MaxScrollValue;
73         static const int MaxViewAutoUpdateRate;
74
75         static const int ScaleUnits[3];
76
77 public:
78         explicit View(Session &session, QWidget *parent = 0);
79
80         Session& session();
81         const Session& session() const;
82
83         /**
84          * Returns the view of the owner.
85          */
86         virtual pv::view::View* view();
87
88         /**
89          * Returns the view of the owner.
90          */
91         virtual const pv::view::View* view() const;
92
93         Viewport* viewport();
94
95         const Viewport* viewport() const;
96
97         /**
98          * Gets a list of time markers.
99          */
100         std::vector< std::shared_ptr<TimeItem> > time_items() const;
101
102         /**
103          * Returns the view time scale in seconds per pixel.
104          */
105         double scale() const;
106
107         /**
108          * Returns the time offset of the left edge of the view in
109          * seconds.
110          */
111         double offset() const;
112
113         /**
114          * Returns the vertical scroll offset.
115          */
116         int owner_visual_v_offset() const;
117
118         /**
119          * Sets the visual v-offset.
120          */
121         void set_v_offset(int offset);
122
123         /**
124          * Returns the SI prefix to apply to the graticule time markings.
125          */
126         unsigned int tick_prefix() const;
127
128         /**
129          * Returns period of the graticule time markings.
130          */
131         double tick_period() const;
132
133         /**
134          * Returns the unit of time currently used.
135          */
136         util::TimeUnit time_unit() const;
137
138         /**
139          * Returns the number of nested parents that this row item owner has.
140          */
141         unsigned int depth() const;
142
143         void zoom(double steps);
144         void zoom(double steps, int offset);
145
146         void zoom_fit(bool gui_state);
147
148         void zoom_one_to_one();
149
150         /**
151          * Sets the scale and offset.
152          * @param scale The new view scale in seconds per pixel.
153          * @param offset The view time offset in seconds.
154          */
155         void set_scale_offset(double scale, double offset);
156
157         std::set< std::shared_ptr<pv::data::SignalData> >
158                 get_visible_data() const;
159
160         std::pair<double, double> get_time_extents() const;
161
162         /**
163          * Enables or disables sticky scrolling, i.e. the view always shows
164          * the most recent samples when capturing data.
165          */
166         void enable_sticky_scrolling(bool state);
167
168         /**
169          * Returns true if cursors are displayed. false otherwise.
170          */
171         bool cursors_shown() const;
172
173         /**
174          * Shows or hides the cursors.
175          */
176         void show_cursors(bool show = true);
177
178         /**
179          * Moves the cursors to a convenient position in the view.
180          */
181         void centre_cursors();
182
183         /**
184          * Returns a reference to the pair of cursors.
185          */
186         std::shared_ptr<CursorPair> cursors() const;
187
188         /**
189          * Adds a new flag at a specified time.
190          */
191         void add_flag(double time);
192
193         /**
194          * Removes a flag from the list.
195          */
196         void remove_flag(std::shared_ptr<Flag> flag);
197
198         /**
199          * Gets the list of flags.
200          */
201         std::vector< std::shared_ptr<Flag> > flags() const;
202
203         const QPoint& hover_point() const;
204
205         void update_viewport();
206
207         void restack_all_row_items();
208
209 Q_SIGNALS:
210         void hover_point_changed();
211
212         void selection_changed();
213
214         void scale_offset_changed();
215
216         void sticky_scrolling_changed(bool state);
217
218         void always_zoom_to_fit_changed(bool state);
219
220 private:
221         void get_scroll_layout(double &length, double &offset) const;
222
223         /**
224          * Simultaneously sets the zoom and offset.
225          * @param scale The scale to set the view to in seconds per pixel. This
226          * value is clamped between MinScale and MaxScale.
227          * @param offset The offset of the left edge of the view in seconds.
228          */
229         void set_zoom(double scale, int offset);
230
231         /**
232          * Find a tick spacing and number formatting that does not cause
233          * the values to collide.
234          */
235         void calculate_tick_spacing();
236
237         void update_scroll();
238
239         void update_layout();
240
241         /**
242          * Satisifies RowItem functionality.
243          * @param p the QPainter to paint into.
244          * @param rect the rectangle of the header area.
245          * @param hover true if the label is being hovered over by the mouse.
246          */
247         void paint_label(QPainter &p, const QRect &rect, bool hover);
248
249         /**
250          * Computes the outline rectangle of a label.
251          * @param rect the rectangle of the header area.
252          * @return Returns the rectangle of the signal label.
253          */
254         QRectF label_rect(const QRectF &rect);
255
256         RowItemOwner* find_prevalent_trace_group(
257                 const std::shared_ptr<sigrok::ChannelGroup> &group,
258                 const std::unordered_map<std::shared_ptr<sigrok::Channel>,
259                         std::shared_ptr<Signal> > &signal_map);
260
261         static std::vector< std::shared_ptr<Trace> >
262                 extract_new_traces_for_channels(
263                 const std::vector< std::shared_ptr<sigrok::Channel> > &channels,
264                 const std::unordered_map<std::shared_ptr<sigrok::Channel>,
265                         std::shared_ptr<Signal> > &signal_map,
266                 std::set< std::shared_ptr<Trace> > &add_list);
267
268         void determine_time_unit();
269
270 private:
271         bool eventFilter(QObject *object, QEvent *event);
272
273         bool viewportEvent(QEvent *e);
274
275         void resizeEvent(QResizeEvent *e);
276
277 public:
278         void row_item_appearance_changed(bool label, bool content);
279         void time_item_appearance_changed(bool label, bool content);
280
281         void extents_changed(bool horz, bool vert);
282
283 private Q_SLOTS:
284
285         void h_scroll_value_changed(int value);
286         void v_scroll_value_changed();
287
288         void signals_changed();
289         void capture_state_updated(int state);
290         void data_updated();
291
292         void perform_delayed_view_update();
293
294         void process_sticky_events();
295
296         void on_hover_point_changed();
297
298 private:
299         Session &session_;
300
301         Viewport *viewport_;
302         Ruler *ruler_;
303         Header *header_;
304
305         /// The view time scale in seconds per pixel.
306         double scale_;
307
308         /// The view time offset in seconds.
309         double offset_;
310
311         bool updating_scroll_;
312         bool sticky_scrolling_;
313         bool always_zoom_to_fit_;
314         QTimer delayed_view_updater_;
315
316         double tick_period_;
317         unsigned int tick_prefix_;
318         util::TimeUnit time_unit_;
319
320         bool show_cursors_;
321         std::shared_ptr<CursorPair> cursors_;
322
323         std::list< std::shared_ptr<Flag> > flags_;
324         char next_flag_text_;
325
326         QPoint hover_point_;
327
328         unsigned int sticky_events_;
329         QTimer lazy_event_handler_;
330 };
331
332 } // namespace view
333 } // namespace pv
334
335 #endif // PULSEVIEW_PV_VIEW_VIEW_HPP