]> sigrok.org Git - pulseview.git/blame - pv/view/view.hpp
View: Determine time unit and use it in other classes
[pulseview.git] / pv / view / view.hpp
CommitLineData
adb4b10c 1/*
b3f22de0 2 * This file is part of the PulseView project.
adb4b10c
JH
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
7a01bd36
JH
21#ifndef PULSEVIEW_PV_VIEW_VIEW_HPP
22#define PULSEVIEW_PV_VIEW_VIEW_HPP
adb4b10c
JH
23
24#include <stdint.h>
25
8914fe79 26#include <list>
f9abf97e 27#include <memory>
1bc6525b 28#include <set>
448a72cf 29#include <unordered_map>
38eeddea
JH
30#include <vector>
31
adb4b10c 32#include <QAbstractScrollArea>
2e04f9bd 33#include <QSizeF>
32218d3e 34#include <QTimer>
adb4b10c 35
2acdb232 36#include <pv/data/signaldata.hpp>
ef454ad5 37#include <pv/util.hpp>
1bc6525b 38
2acdb232 39#include "cursorpair.hpp"
8914fe79 40#include "flag.hpp"
2acdb232 41#include "rowitemowner.hpp"
f76af637 42
cf124e47
JH
43namespace sigrok {
44class ChannelGroup;
45}
46
51e77110
JH
47namespace pv {
48
2b81ae46 49class Session;
adb4b10c 50
cdf7bea7
JH
51namespace view {
52
84a0d458 53class CursorHeader;
1d8dca91 54class Header;
ccdd3ef5 55class Ruler;
cf124e47 56class Trace;
cdf7bea7
JH
57class Viewport;
58
eae6e30a 59class View : public QAbstractScrollArea, public RowItemOwner {
adb4b10c
JH
60 Q_OBJECT
61
32218d3e
JH
62private:
63 enum StickyEvents {
76fea660
JH
64 RowItemHExtentsChanged = 1,
65 RowItemVExtentsChanged = 2
32218d3e
JH
66 };
67
adb4b10c
JH
68private:
69 static const double MaxScale;
70 static const double MinScale;
71
f25770e2 72 static const int MaxScrollValue;
c7b03d9d 73 static const int MaxViewAutoUpdateRate;
f25770e2 74
361c560e
JH
75 static const int ScaleUnits[3];
76
adb4b10c 77public:
2b81ae46 78 explicit View(Session &session, QWidget *parent = 0);
adb4b10c 79
2b81ae46
JH
80 Session& session();
81 const Session& session() const;
1d19ef83 82
eae6e30a
JH
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
2ae445ba
SA
93 Viewport* viewport();
94
95 const Viewport* viewport() const;
96
2496bf45
JH
97 /**
98 * Gets a list of time markers.
99 */
100 std::vector< std::shared_ptr<TimeItem> > time_items() const;
101
e2f5223b
JH
102 /**
103 * Returns the view time scale in seconds per pixel.
104 */
adb4b10c 105 double scale() const;
e2f5223b
JH
106
107 /**
108 * Returns the time offset of the left edge of the view in
109 * seconds.
110 */
adb4b10c 111 double offset() const;
f8400017
JH
112
113 /**
114 * Returns the vertical scroll offset.
115 */
7ff0145f 116 int owner_visual_v_offset() const;
adb4b10c 117
4d476647
JH
118 /**
119 * Sets the visual v-offset.
120 */
121 void set_v_offset(int offset);
122
361c560e
JH
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
ef454ad5
SA
133 /**
134 * Returns the unit of time currently used.
135 */
136 util::TimeUnit time_unit() const;
137
3e769a37
JH
138 /**
139 * Returns the number of nested parents that this row item owner has.
140 */
141 unsigned int depth() const;
142
adb4b10c 143 void zoom(double steps);
17c0f398 144 void zoom(double steps, int offset);
adb4b10c 145
ce11b2ea 146 void zoom_fit(bool gui_state);
ca46b534 147
d1e7d82c
JH
148 void zoom_one_to_one();
149
e2f5223b
JH
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 */
adb4b10c
JH
155 void set_scale_offset(double scale, double offset);
156
f9abf97e 157 std::set< std::shared_ptr<pv::data::SignalData> >
1bc6525b
JH
158 get_visible_data() const;
159
160 std::pair<double, double> get_time_extents() const;
161
c7b03d9d
SA
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
f76af637
JH
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
b4d91e56
JH
178 /**
179 * Moves the cursors to a convenient position in the view.
180 */
181 void centre_cursors();
182
f76af637
JH
183 /**
184 * Returns a reference to the pair of cursors.
185 */
5c5ce757 186 std::shared_ptr<CursorPair> cursors() const;
58864c5c 187
8914fe79
JH
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
cbd80f64
JH
203 const QPoint& hover_point() const;
204
9cef9567 205 void update_viewport();
7ff0145f
JH
206
207 void restack_all_row_items();
9cef9567 208
e9213170 209Q_SIGNALS:
cbd80f64
JH
210 void hover_point_changed();
211
8b454527
JH
212 void selection_changed();
213
e0fc5810
JH
214 void scale_offset_changed();
215
c7b03d9d
SA
216 void sticky_scrolling_changed(bool state);
217
ce11b2ea
SA
218 void always_zoom_to_fit_changed(bool state);
219
adb4b10c 220private:
f25770e2 221 void get_scroll_layout(double &length, double &offset) const;
3925091a
JH
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 */
d1e7d82c
JH
229 void set_zoom(double scale, int offset);
230
361c560e
JH
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
adb4b10c
JH
237 void update_scroll();
238
d7c0ca4a
JH
239 void update_layout();
240
eae6e30a
JH
241 /**
242 * Satisifies RowItem functionality.
243 * @param p the QPainter to paint into.
b3f44329 244 * @param rect the rectangle of the header area.
eae6e30a
JH
245 * @param hover true if the label is being hovered over by the mouse.
246 */
b3f44329 247 void paint_label(QPainter &p, const QRect &rect, bool hover);
eae6e30a
JH
248
249 /**
250 * Computes the outline rectangle of a label.
b3f44329 251 * @param rect the rectangle of the header area.
eae6e30a
JH
252 * @return Returns the rectangle of the signal label.
253 */
b3f44329 254 QRectF label_rect(const QRectF &rect);
eae6e30a 255
cf124e47
JH
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(
448a72cf 263 const std::vector< std::shared_ptr<sigrok::Channel> > &channels,
cf124e47 264 const std::unordered_map<std::shared_ptr<sigrok::Channel>,
448a72cf 265 std::shared_ptr<Signal> > &signal_map,
cf124e47 266 std::set< std::shared_ptr<Trace> > &add_list);
448a72cf 267
ef454ad5
SA
268 void determine_time_unit();
269
adb4b10c 270private:
cbd80f64
JH
271 bool eventFilter(QObject *object, QEvent *event);
272
adb4b10c
JH
273 bool viewportEvent(QEvent *e);
274
275 void resizeEvent(QResizeEvent *e);
276
32218d3e 277public:
6e2c3c85 278 void row_item_appearance_changed(bool label, bool content);
98cfe4e8 279 void time_item_appearance_changed(bool label, bool content);
32218d3e
JH
280
281 void extents_changed(bool horz, bool vert);
282
e9213170 283private Q_SLOTS:
cbd80f64 284
b16907d3 285 void h_scroll_value_changed(int value);
f8400017 286 void v_scroll_value_changed();
adb4b10c 287
69dd2b03 288 void signals_changed();
adb4b10c
JH
289 void data_updated();
290
c7b03d9d
SA
291 void perform_delayed_view_update();
292
32218d3e 293 void process_sticky_events();
d7c0ca4a 294
33c62f44
JH
295 void on_hover_point_changed();
296
adb4b10c 297private:
2b81ae46 298 Session &session_;
adb4b10c 299
8dbbc7f0
JH
300 Viewport *viewport_;
301 Ruler *ruler_;
8dbbc7f0 302 Header *header_;
adb4b10c 303
e2f5223b 304 /// The view time scale in seconds per pixel.
8dbbc7f0 305 double scale_;
e2f5223b
JH
306
307 /// The view time offset in seconds.
8dbbc7f0 308 double offset_;
adb4b10c 309
8dbbc7f0 310 bool updating_scroll_;
c7b03d9d 311 bool sticky_scrolling_;
ce11b2ea 312 bool always_zoom_to_fit_;
c7b03d9d 313 QTimer delayed_view_updater_;
cbd80f64 314
361c560e
JH
315 double tick_period_;
316 unsigned int tick_prefix_;
ef454ad5 317 util::TimeUnit time_unit_;
361c560e 318
8dbbc7f0 319 bool show_cursors_;
5c5ce757 320 std::shared_ptr<CursorPair> cursors_;
f76af637 321
8914fe79
JH
322 std::list< std::shared_ptr<Flag> > flags_;
323 char next_flag_text_;
324
8dbbc7f0 325 QPoint hover_point_;
32218d3e 326
8dbbc7f0
JH
327 unsigned int sticky_events_;
328 QTimer lazy_event_handler_;
adb4b10c
JH
329};
330
cdf7bea7
JH
331} // namespace view
332} // namespace pv
333
7a01bd36 334#endif // PULSEVIEW_PV_VIEW_VIEW_HPP