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