]>
Commit | Line | Data |
---|---|---|
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 | ||
21 | #include <assert.h> | |
f25770e2 | 22 | #include <limits.h> |
adb4b10c JH |
23 | #include <math.h> |
24 | ||
25 | #include <boost/foreach.hpp> | |
26 | ||
27 | #include <QEvent> | |
cbd80f64 | 28 | #include <QMouseEvent> |
adb4b10c JH |
29 | #include <QScrollBar> |
30 | ||
1d8dca91 | 31 | #include "header.h" |
ccdd3ef5 | 32 | #include "ruler.h" |
2e575351 | 33 | #include "signal.h" |
cdf7bea7 JH |
34 | #include "view.h" |
35 | #include "viewport.h" | |
adb4b10c | 36 | |
1b1ec774 JH |
37 | #include "pv/sigsession.h" |
38 | #include "pv/data/logic.h" | |
39 | #include "pv/data/logicsnapshot.h" | |
adb4b10c JH |
40 | |
41 | using namespace boost; | |
42 | using namespace std; | |
43 | ||
cdf7bea7 JH |
44 | namespace pv { |
45 | namespace view { | |
adb4b10c | 46 | |
cdf7bea7 JH |
47 | const double View::MaxScale = 1e9; |
48 | const double View::MinScale = 1e-15; | |
adb4b10c | 49 | |
cdf7bea7 JH |
50 | const int View::LabelMarginWidth = 70; |
51 | const int View::RulerHeight = 30; | |
52 | ||
f25770e2 JH |
53 | const int View::MaxScrollValue = INT_MAX / 2; |
54 | ||
8d44d030 JH |
55 | const int View::SignalHeight = 30; |
56 | const int View::SignalMargin = 10; | |
49153683 | 57 | const int View::SignalSnapGridSize = 10; |
1d8dca91 | 58 | |
f76af637 JH |
59 | const QColor View::CursorAreaColour(220, 231, 243); |
60 | ||
2e04f9bd JH |
61 | const QSizeF View::LabelPadding(4, 0); |
62 | ||
cdf7bea7 | 63 | View::View(SigSession &session, QWidget *parent) : |
adb4b10c JH |
64 | QAbstractScrollArea(parent), |
65 | _session(session), | |
cdf7bea7 | 66 | _viewport(new Viewport(*this)), |
ccdd3ef5 | 67 | _ruler(new Ruler(*this)), |
1d8dca91 | 68 | _header(new Header(*this)), |
adb4b10c JH |
69 | _data_length(0), |
70 | _scale(1e-6), | |
71 | _offset(0), | |
cbd80f64 | 72 | _v_offset(0), |
528bd8a1 | 73 | _updating_scroll(false), |
f76af637 | 74 | _show_cursors(false), |
b42d25c4 | 75 | _cursors(*this), |
cbd80f64 | 76 | _hover_point(-1, -1) |
adb4b10c | 77 | { |
b16907d3 JH |
78 | connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), |
79 | this, SLOT(h_scroll_value_changed(int))); | |
adb4b10c JH |
80 | connect(verticalScrollBar(), SIGNAL(valueChanged(int)), |
81 | this, SLOT(v_scroll_value_changed(int))); | |
69dd2b03 JH |
82 | |
83 | connect(&_session, SIGNAL(signals_changed()), | |
84 | this, SLOT(signals_changed())); | |
adb4b10c JH |
85 | connect(&_session, SIGNAL(data_updated()), |
86 | this, SLOT(data_updated())); | |
1d8dca91 | 87 | |
b42d25c4 | 88 | connect(&_cursors.first(), SIGNAL(time_changed()), |
ca4ec3ea | 89 | this, SLOT(marker_time_changed())); |
b42d25c4 | 90 | connect(&_cursors.second(), SIGNAL(time_changed()), |
ca4ec3ea JH |
91 | this, SLOT(marker_time_changed())); |
92 | ||
54401bbb | 93 | connect(_header, SIGNAL(signals_moved()), |
07204819 | 94 | this, SLOT(on_signals_moved())); |
54401bbb | 95 | |
ccdd3ef5 | 96 | setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0); |
adb4b10c | 97 | setViewport(_viewport); |
cbd80f64 JH |
98 | |
99 | _viewport->installEventFilter(this); | |
100 | _ruler->installEventFilter(this); | |
101 | _header->installEventFilter(this); | |
adb4b10c JH |
102 | } |
103 | ||
1d19ef83 JH |
104 | SigSession& View::session() |
105 | { | |
106 | return _session; | |
107 | } | |
108 | ||
cdf7bea7 | 109 | double View::scale() const |
adb4b10c JH |
110 | { |
111 | return _scale; | |
112 | } | |
113 | ||
cdf7bea7 | 114 | double View::offset() const |
adb4b10c JH |
115 | { |
116 | return _offset; | |
117 | } | |
118 | ||
cdf7bea7 | 119 | int View::v_offset() const |
adb4b10c JH |
120 | { |
121 | return _v_offset; | |
122 | } | |
123 | ||
cdf7bea7 | 124 | void View::zoom(double steps) |
adb4b10c JH |
125 | { |
126 | zoom(steps, (width() - LabelMarginWidth) / 2); | |
127 | } | |
128 | ||
17c0f398 JH |
129 | void View::zoom(double steps, int offset) |
130 | { | |
131 | const double cursor_offset = _offset + _scale * offset; | |
132 | _scale *= pow(3.0/2.0, -steps); | |
133 | _scale = max(min(_scale, MaxScale), MinScale); | |
134 | _offset = cursor_offset - _scale * offset; | |
135 | ||
136 | _ruler->update(); | |
137 | _viewport->update(); | |
138 | update_scroll(); | |
139 | } | |
140 | ||
141 | ||
cdf7bea7 | 142 | void View::set_scale_offset(double scale, double offset) |
adb4b10c JH |
143 | { |
144 | _scale = scale; | |
145 | _offset = offset; | |
ccdd3ef5 | 146 | |
adb4b10c | 147 | update_scroll(); |
ccdd3ef5 | 148 | _ruler->update(); |
adb4b10c JH |
149 | _viewport->update(); |
150 | } | |
151 | ||
f76af637 JH |
152 | bool View::cursors_shown() const |
153 | { | |
154 | return _show_cursors; | |
155 | } | |
156 | ||
157 | void View::show_cursors(bool show) | |
158 | { | |
159 | _show_cursors = show; | |
160 | _ruler->update(); | |
161 | _viewport->update(); | |
162 | } | |
163 | ||
b4d91e56 JH |
164 | void View::centre_cursors() |
165 | { | |
166 | const double time_width = _scale * _viewport->width(); | |
167 | _cursors.first().set_time(_offset + time_width * 0.4); | |
168 | _cursors.second().set_time(_offset + time_width * 0.6); | |
169 | _ruler->update(); | |
170 | _viewport->update(); | |
171 | } | |
172 | ||
b42d25c4 | 173 | CursorPair& View::cursors() |
f76af637 JH |
174 | { |
175 | return _cursors; | |
176 | } | |
177 | ||
cbd80f64 JH |
178 | const QPoint& View::hover_point() const |
179 | { | |
180 | return _hover_point; | |
181 | } | |
182 | ||
4b192962 JH |
183 | void View::normalize_layout() |
184 | { | |
3868e5fa | 185 | const vector< shared_ptr<Signal> > sigs(_session.get_signals()); |
4b192962 JH |
186 | |
187 | int v_min = INT_MAX; | |
188 | BOOST_FOREACH(const shared_ptr<Signal> s, sigs) | |
189 | v_min = min(s->get_v_offset(), v_min); | |
190 | ||
191 | const int delta = -min(v_min, 0); | |
192 | BOOST_FOREACH(shared_ptr<Signal> s, sigs) | |
193 | s->set_v_offset(s->get_v_offset() + delta); | |
194 | ||
195 | verticalScrollBar()->setSliderPosition(_v_offset + delta); | |
196 | v_scroll_value_changed(verticalScrollBar()->sliderPosition()); | |
197 | } | |
198 | ||
f25770e2 JH |
199 | void View::get_scroll_layout(double &length, double &offset) const |
200 | { | |
1b1ec774 | 201 | const shared_ptr<data::SignalData> sig_data = _session.get_data(); |
333d5bbc | 202 | if (!sig_data) |
f25770e2 JH |
203 | return; |
204 | ||
205 | length = _data_length / (sig_data->get_samplerate() * _scale); | |
206 | offset = _offset / _scale; | |
207 | } | |
208 | ||
cdf7bea7 | 209 | void View::update_scroll() |
adb4b10c JH |
210 | { |
211 | assert(_viewport); | |
212 | ||
213 | const QSize areaSize = _viewport->size(); | |
214 | ||
215 | // Set the horizontal scroll bar | |
216 | double length = 0, offset = 0; | |
f25770e2 JH |
217 | get_scroll_layout(length, offset); |
218 | length = max(length - areaSize.width(), 0.0); | |
adb4b10c | 219 | |
b4ef7f2a | 220 | horizontalScrollBar()->setPageStep(areaSize.width() / 2); |
f25770e2 | 221 | |
528bd8a1 JH |
222 | _updating_scroll = true; |
223 | ||
333d5bbc | 224 | if (length < MaxScrollValue) { |
f25770e2 JH |
225 | horizontalScrollBar()->setRange(0, length); |
226 | horizontalScrollBar()->setSliderPosition(offset); | |
227 | } else { | |
228 | horizontalScrollBar()->setRange(0, MaxScrollValue); | |
229 | horizontalScrollBar()->setSliderPosition( | |
230 | _offset * MaxScrollValue / (_scale * length)); | |
231 | } | |
adb4b10c | 232 | |
528bd8a1 JH |
233 | _updating_scroll = false; |
234 | ||
adb4b10c JH |
235 | // Set the vertical scrollbar |
236 | verticalScrollBar()->setPageStep(areaSize.height()); | |
237 | verticalScrollBar()->setRange(0, | |
4b192962 JH |
238 | _viewport->get_total_height() + SignalMargin - |
239 | areaSize.height()); | |
adb4b10c JH |
240 | } |
241 | ||
2e575351 JH |
242 | void View::reset_signal_layout() |
243 | { | |
2658961b | 244 | int offset = SignalMargin + SignalHeight; |
3868e5fa | 245 | const vector< shared_ptr<Signal> > sigs(_session.get_signals()); |
2e575351 JH |
246 | BOOST_FOREACH(shared_ptr<Signal> s, sigs) { |
247 | s->set_v_offset(offset); | |
8d44d030 | 248 | offset += SignalHeight + 2 * SignalMargin; |
2e575351 | 249 | } |
4b192962 JH |
250 | |
251 | normalize_layout(); | |
2e575351 JH |
252 | } |
253 | ||
cbd80f64 JH |
254 | bool View::eventFilter(QObject *object, QEvent *event) |
255 | { | |
256 | const QEvent::Type type = event->type(); | |
333d5bbc | 257 | if (type == QEvent::MouseMove) { |
cbd80f64 JH |
258 | |
259 | const QMouseEvent *const mouse_event = (QMouseEvent*)event; | |
333d5bbc | 260 | if (object == _viewport) |
cbd80f64 | 261 | _hover_point = mouse_event->pos(); |
333d5bbc | 262 | else if (object == _ruler) |
cbd80f64 | 263 | _hover_point = QPoint(mouse_event->x(), 0); |
333d5bbc | 264 | else if (object == _header) |
cbd80f64 JH |
265 | _hover_point = QPoint(0, mouse_event->y()); |
266 | else | |
267 | _hover_point = QPoint(-1, -1); | |
268 | ||
269 | hover_point_changed(); | |
270 | ||
333d5bbc | 271 | } else if (type == QEvent::Leave) { |
cbd80f64 JH |
272 | _hover_point = QPoint(-1, -1); |
273 | hover_point_changed(); | |
274 | } | |
275 | ||
276 | return QObject::eventFilter(object, event); | |
277 | } | |
278 | ||
cdf7bea7 | 279 | bool View::viewportEvent(QEvent *e) |
adb4b10c JH |
280 | { |
281 | switch(e->type()) { | |
282 | case QEvent::Paint: | |
283 | case QEvent::MouseButtonPress: | |
284 | case QEvent::MouseButtonRelease: | |
285 | case QEvent::MouseButtonDblClick: | |
286 | case QEvent::MouseMove: | |
287 | case QEvent::Wheel: | |
288 | return false; | |
289 | ||
290 | default: | |
291 | return QAbstractScrollArea::viewportEvent(e); | |
292 | } | |
293 | } | |
294 | ||
e314eca4 | 295 | void View::resizeEvent(QResizeEvent*) |
adb4b10c | 296 | { |
ccdd3ef5 JH |
297 | _ruler->setGeometry(_viewport->x(), 0, |
298 | _viewport->width(), _viewport->y()); | |
299 | _header->setGeometry(0, _viewport->y(), | |
1d8dca91 | 300 | _viewport->x(), _viewport->height()); |
adb4b10c JH |
301 | update_scroll(); |
302 | } | |
303 | ||
b16907d3 | 304 | void View::h_scroll_value_changed(int value) |
adb4b10c | 305 | { |
528bd8a1 JH |
306 | if (_updating_scroll) |
307 | return; | |
308 | ||
f25770e2 | 309 | const int range = horizontalScrollBar()->maximum(); |
333d5bbc | 310 | if (range < MaxScrollValue) |
f25770e2 JH |
311 | _offset = _scale * value; |
312 | else { | |
313 | double length = 0, offset; | |
314 | get_scroll_layout(length, offset); | |
315 | _offset = _scale * length * value / MaxScrollValue; | |
316 | } | |
317 | ||
ccdd3ef5 | 318 | _ruler->update(); |
adb4b10c JH |
319 | _viewport->update(); |
320 | } | |
321 | ||
cdf7bea7 | 322 | void View::v_scroll_value_changed(int value) |
adb4b10c JH |
323 | { |
324 | _v_offset = value; | |
1d8dca91 | 325 | _header->update(); |
adb4b10c JH |
326 | _viewport->update(); |
327 | } | |
328 | ||
69dd2b03 JH |
329 | void View::signals_changed() |
330 | { | |
331 | reset_signal_layout(); | |
332 | } | |
333 | ||
cdf7bea7 | 334 | void View::data_updated() |
adb4b10c JH |
335 | { |
336 | // Get the new data length | |
337 | _data_length = 0; | |
1b1ec774 | 338 | shared_ptr<data::Logic> sig_data = _session.get_data(); |
333d5bbc | 339 | if (sig_data) { |
1b1ec774 | 340 | deque< shared_ptr<data::LogicSnapshot> > &snapshots = |
adb4b10c | 341 | sig_data->get_snapshots(); |
1b1ec774 | 342 | BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots) |
333d5bbc | 343 | if (s) |
adb4b10c JH |
344 | _data_length = max(_data_length, |
345 | s->get_sample_count()); | |
346 | } | |
347 | ||
348 | // Update the scroll bars | |
349 | update_scroll(); | |
350 | ||
351 | // Repaint the view | |
352 | _viewport->update(); | |
353 | } | |
cdf7bea7 | 354 | |
ca4ec3ea JH |
355 | void View::marker_time_changed() |
356 | { | |
357 | _ruler->update(); | |
358 | _viewport->update(); | |
359 | } | |
360 | ||
07204819 | 361 | void View::on_signals_moved() |
54401bbb | 362 | { |
07204819 JH |
363 | update_scroll(); |
364 | signals_moved(); | |
54401bbb JH |
365 | } |
366 | ||
cdf7bea7 JH |
367 | } // namespace view |
368 | } // namespace pv |