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