]>
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 | ||
269528f5 | 21 | #ifdef ENABLE_DECODE |
4e5a4405 | 22 | #include <libsigrokdecode/libsigrokdecode.h> |
269528f5 | 23 | #endif |
4e5a4405 | 24 | |
adb4b10c | 25 | #include <assert.h> |
f25770e2 | 26 | #include <limits.h> |
adb4b10c JH |
27 | #include <math.h> |
28 | ||
29 | #include <boost/foreach.hpp> | |
30 | ||
31 | #include <QEvent> | |
cbd80f64 | 32 | #include <QMouseEvent> |
adb4b10c JH |
33 | #include <QScrollBar> |
34 | ||
b9329558 | 35 | #include "decodetrace.h" |
1d8dca91 | 36 | #include "header.h" |
ccdd3ef5 | 37 | #include "ruler.h" |
2e575351 | 38 | #include "signal.h" |
cdf7bea7 JH |
39 | #include "view.h" |
40 | #include "viewport.h" | |
adb4b10c | 41 | |
1b1ec774 JH |
42 | #include "pv/sigsession.h" |
43 | #include "pv/data/logic.h" | |
44 | #include "pv/data/logicsnapshot.h" | |
adb4b10c | 45 | |
819f4c25 JH |
46 | using boost::shared_ptr; |
47 | using boost::weak_ptr; | |
1bc6525b | 48 | using pv::data::SignalData; |
819f4c25 JH |
49 | using std::deque; |
50 | using std::list; | |
51 | using std::max; | |
1bc6525b | 52 | using std::make_pair; |
819f4c25 | 53 | using std::min; |
1bc6525b | 54 | using std::pair; |
819f4c25 JH |
55 | using std::set; |
56 | using std::vector; | |
adb4b10c | 57 | |
cdf7bea7 JH |
58 | namespace pv { |
59 | namespace view { | |
adb4b10c | 60 | |
cdf7bea7 JH |
61 | const double View::MaxScale = 1e9; |
62 | const double View::MinScale = 1e-15; | |
adb4b10c | 63 | |
f25770e2 JH |
64 | const int View::MaxScrollValue = INT_MAX / 2; |
65 | ||
8d44d030 JH |
66 | const int View::SignalHeight = 30; |
67 | const int View::SignalMargin = 10; | |
49153683 | 68 | const int View::SignalSnapGridSize = 10; |
1d8dca91 | 69 | |
f76af637 JH |
70 | const QColor View::CursorAreaColour(220, 231, 243); |
71 | ||
2e04f9bd JH |
72 | const QSizeF View::LabelPadding(4, 0); |
73 | ||
cdf7bea7 | 74 | View::View(SigSession &session, QWidget *parent) : |
adb4b10c JH |
75 | QAbstractScrollArea(parent), |
76 | _session(session), | |
cdf7bea7 | 77 | _viewport(new Viewport(*this)), |
ccdd3ef5 | 78 | _ruler(new Ruler(*this)), |
1d8dca91 | 79 | _header(new Header(*this)), |
adb4b10c JH |
80 | _scale(1e-6), |
81 | _offset(0), | |
cbd80f64 | 82 | _v_offset(0), |
528bd8a1 | 83 | _updating_scroll(false), |
f76af637 | 84 | _show_cursors(false), |
b42d25c4 | 85 | _cursors(*this), |
cbd80f64 | 86 | _hover_point(-1, -1) |
adb4b10c | 87 | { |
b16907d3 JH |
88 | connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), |
89 | this, SLOT(h_scroll_value_changed(int))); | |
adb4b10c JH |
90 | connect(verticalScrollBar(), SIGNAL(valueChanged(int)), |
91 | this, SLOT(v_scroll_value_changed(int))); | |
69dd2b03 JH |
92 | |
93 | connect(&_session, SIGNAL(signals_changed()), | |
94 | this, SLOT(signals_changed())); | |
adb4b10c JH |
95 | connect(&_session, SIGNAL(data_updated()), |
96 | this, SLOT(data_updated())); | |
1d8dca91 | 97 | |
58864c5c | 98 | connect(_cursors.first().get(), SIGNAL(time_changed()), |
ca4ec3ea | 99 | this, SLOT(marker_time_changed())); |
58864c5c | 100 | connect(_cursors.second().get(), SIGNAL(time_changed()), |
ca4ec3ea JH |
101 | this, SLOT(marker_time_changed())); |
102 | ||
d7c0ca4a JH |
103 | connect(_header, SIGNAL(geometry_updated()), |
104 | this, SLOT(on_geometry_updated())); | |
54401bbb | 105 | connect(_header, SIGNAL(signals_moved()), |
07204819 | 106 | this, SLOT(on_signals_moved())); |
54401bbb | 107 | |
17348f85 JH |
108 | connect(_header, SIGNAL(selection_changed()), |
109 | _ruler, SLOT(clear_selection())); | |
110 | connect(_ruler, SIGNAL(selection_changed()), | |
111 | _header, SLOT(clear_selection())); | |
112 | ||
8b454527 JH |
113 | connect(_header, SIGNAL(selection_changed()), |
114 | this, SIGNAL(selection_changed())); | |
115 | connect(_ruler, SIGNAL(selection_changed()), | |
116 | this, SIGNAL(selection_changed())); | |
117 | ||
adb4b10c | 118 | setViewport(_viewport); |
cbd80f64 JH |
119 | |
120 | _viewport->installEventFilter(this); | |
121 | _ruler->installEventFilter(this); | |
122 | _header->installEventFilter(this); | |
adb4b10c JH |
123 | } |
124 | ||
1d19ef83 JH |
125 | SigSession& View::session() |
126 | { | |
127 | return _session; | |
128 | } | |
129 | ||
38eeddea JH |
130 | const SigSession& View::session() const |
131 | { | |
132 | return _session; | |
133 | } | |
134 | ||
cdf7bea7 | 135 | double View::scale() const |
adb4b10c JH |
136 | { |
137 | return _scale; | |
138 | } | |
139 | ||
cdf7bea7 | 140 | double View::offset() const |
adb4b10c JH |
141 | { |
142 | return _offset; | |
143 | } | |
144 | ||
cdf7bea7 | 145 | int View::v_offset() const |
adb4b10c JH |
146 | { |
147 | return _v_offset; | |
148 | } | |
149 | ||
cdf7bea7 | 150 | void View::zoom(double steps) |
adb4b10c | 151 | { |
f3f98f8f | 152 | zoom(steps, _viewport->width() / 2); |
adb4b10c JH |
153 | } |
154 | ||
17c0f398 JH |
155 | void View::zoom(double steps, int offset) |
156 | { | |
e31551da JH |
157 | const double new_scale = max(min(_scale * pow(3.0/2.0, -steps), |
158 | MaxScale), MinScale); | |
d1e7d82c | 159 | set_zoom(new_scale, offset); |
17c0f398 JH |
160 | } |
161 | ||
ca46b534 JH |
162 | void View::zoom_fit() |
163 | { | |
1bc6525b JH |
164 | const pair<double, double> extents = get_time_extents(); |
165 | const double delta = extents.second - extents.first; | |
166 | if (delta < 1e-12) | |
ca46b534 JH |
167 | return; |
168 | ||
169 | assert(_viewport); | |
170 | const int w = _viewport->width(); | |
171 | if (w <= 0) | |
172 | return; | |
173 | ||
1bc6525b | 174 | set_scale_offset(delta / w, extents.first); |
ca46b534 JH |
175 | } |
176 | ||
d1e7d82c JH |
177 | void View::zoom_one_to_one() |
178 | { | |
179 | using pv::data::SignalData; | |
180 | ||
181 | const vector< shared_ptr<Signal> > sigs( | |
182 | session().get_signals()); | |
183 | ||
184 | // Make a set of all the visible data objects | |
185 | set< shared_ptr<SignalData> > visible_data; | |
186 | BOOST_FOREACH(const shared_ptr<Signal> sig, sigs) | |
187 | if (sig->enabled()) | |
188 | visible_data.insert(sig->data()); | |
189 | ||
190 | if (visible_data.empty()) | |
191 | return; | |
192 | ||
193 | double samplerate = 0.0; | |
194 | BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data) { | |
195 | assert(d); | |
196 | samplerate = max(samplerate, d->samplerate()); | |
197 | } | |
198 | ||
199 | if (samplerate == 0.0) | |
200 | return; | |
201 | ||
202 | assert(_viewport); | |
203 | const int w = _viewport->width(); | |
204 | if (w <= 0) | |
205 | return; | |
206 | ||
207 | set_zoom(1.0 / samplerate, w / 2); | |
208 | } | |
209 | ||
cdf7bea7 | 210 | void View::set_scale_offset(double scale, double offset) |
adb4b10c JH |
211 | { |
212 | _scale = scale; | |
213 | _offset = offset; | |
ccdd3ef5 | 214 | |
adb4b10c | 215 | update_scroll(); |
ccdd3ef5 | 216 | _ruler->update(); |
adb4b10c | 217 | _viewport->update(); |
e0fc5810 | 218 | scale_offset_changed(); |
adb4b10c JH |
219 | } |
220 | ||
38eeddea JH |
221 | vector< shared_ptr<Trace> > View::get_traces() const |
222 | { | |
223 | const vector< shared_ptr<Signal> > sigs( | |
224 | session().get_signals()); | |
269528f5 | 225 | #ifdef ENABLE_DECODE |
b9329558 | 226 | const vector< shared_ptr<DecodeTrace> > decode_sigs( |
38eeddea JH |
227 | session().get_decode_signals()); |
228 | vector< shared_ptr<Trace> > traces( | |
229 | sigs.size() + decode_sigs.size()); | |
269528f5 JH |
230 | #else |
231 | vector< shared_ptr<Trace> > traces(sigs.size()); | |
232 | #endif | |
38eeddea JH |
233 | |
234 | vector< shared_ptr<Trace> >::iterator i = traces.begin(); | |
235 | i = copy(sigs.begin(), sigs.end(), i); | |
269528f5 | 236 | #ifdef ENABLE_DECODE |
38eeddea | 237 | i = copy(decode_sigs.begin(), decode_sigs.end(), i); |
269528f5 | 238 | #endif |
38eeddea | 239 | |
9a2bc5fc | 240 | stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets); |
38eeddea JH |
241 | return traces; |
242 | } | |
243 | ||
94936133 JH |
244 | list<weak_ptr<SelectableItem> > View::selected_items() const |
245 | { | |
246 | list<weak_ptr<SelectableItem> > items; | |
247 | ||
248 | // List the selected signals | |
38eeddea JH |
249 | const vector< shared_ptr<Trace> > traces(get_traces()); |
250 | BOOST_FOREACH (shared_ptr<Trace> t, traces) { | |
251 | assert(t); | |
252 | if (t->selected()) | |
253 | items.push_back(t); | |
94936133 JH |
254 | } |
255 | ||
256 | // List the selected cursors | |
257 | if (_cursors.first()->selected()) | |
258 | items.push_back(_cursors.first()); | |
259 | if (_cursors.second()->selected()) | |
260 | items.push_back(_cursors.second()); | |
261 | ||
262 | return items; | |
263 | } | |
264 | ||
1bc6525b JH |
265 | set< shared_ptr<SignalData> > View::get_visible_data() const |
266 | { | |
267 | const vector< shared_ptr<Signal> > sigs( | |
268 | session().get_signals()); | |
269 | ||
270 | // Make a set of all the visible data objects | |
271 | set< shared_ptr<SignalData> > visible_data; | |
272 | BOOST_FOREACH(const shared_ptr<Signal> sig, sigs) | |
273 | if (sig->enabled()) | |
274 | visible_data.insert(sig->data()); | |
275 | ||
276 | return visible_data; | |
277 | } | |
278 | ||
279 | pair<double, double> View::get_time_extents() const | |
280 | { | |
281 | const set< shared_ptr<SignalData> > visible_data = get_visible_data(); | |
282 | if (visible_data.empty()) | |
283 | return make_pair(0.0, 0.0); | |
284 | ||
285 | double left_time = DBL_MAX, right_time = DBL_MIN; | |
286 | BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data) | |
287 | { | |
288 | const double start_time = d->get_start_time(); | |
a472a884 JH |
289 | double samplerate = d->samplerate(); |
290 | samplerate = (samplerate <= 0.0) ? 1.0 : samplerate; | |
291 | ||
1bc6525b JH |
292 | left_time = min(left_time, start_time); |
293 | right_time = max(right_time, start_time + | |
a472a884 | 294 | d->get_max_sample_count() / samplerate); |
1bc6525b JH |
295 | } |
296 | ||
297 | assert(left_time < right_time); | |
298 | return make_pair(left_time, right_time); | |
299 | } | |
300 | ||
f76af637 JH |
301 | bool View::cursors_shown() const |
302 | { | |
303 | return _show_cursors; | |
304 | } | |
305 | ||
306 | void View::show_cursors(bool show) | |
307 | { | |
308 | _show_cursors = show; | |
309 | _ruler->update(); | |
310 | _viewport->update(); | |
311 | } | |
312 | ||
b4d91e56 JH |
313 | void View::centre_cursors() |
314 | { | |
315 | const double time_width = _scale * _viewport->width(); | |
58864c5c JH |
316 | _cursors.first()->set_time(_offset + time_width * 0.4); |
317 | _cursors.second()->set_time(_offset + time_width * 0.6); | |
b4d91e56 JH |
318 | _ruler->update(); |
319 | _viewport->update(); | |
320 | } | |
321 | ||
b42d25c4 | 322 | CursorPair& View::cursors() |
f76af637 JH |
323 | { |
324 | return _cursors; | |
325 | } | |
326 | ||
58864c5c JH |
327 | const CursorPair& View::cursors() const |
328 | { | |
329 | return _cursors; | |
330 | } | |
331 | ||
cbd80f64 JH |
332 | const QPoint& View::hover_point() const |
333 | { | |
334 | return _hover_point; | |
335 | } | |
336 | ||
4b192962 JH |
337 | void View::normalize_layout() |
338 | { | |
38eeddea | 339 | const vector< shared_ptr<Trace> > traces(get_traces()); |
4b192962 JH |
340 | |
341 | int v_min = INT_MAX; | |
38eeddea JH |
342 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) |
343 | v_min = min(t->get_v_offset(), v_min); | |
4b192962 JH |
344 | |
345 | const int delta = -min(v_min, 0); | |
38eeddea JH |
346 | BOOST_FOREACH(shared_ptr<Trace> t, traces) |
347 | t->set_v_offset(t->get_v_offset() + delta); | |
4b192962 JH |
348 | |
349 | verticalScrollBar()->setSliderPosition(_v_offset + delta); | |
350 | v_scroll_value_changed(verticalScrollBar()->sliderPosition()); | |
351 | } | |
352 | ||
9cef9567 JH |
353 | void View::update_viewport() |
354 | { | |
355 | assert(_viewport); | |
356 | _viewport->update(); | |
357 | } | |
358 | ||
f25770e2 JH |
359 | void View::get_scroll_layout(double &length, double &offset) const |
360 | { | |
1bc6525b JH |
361 | const pair<double, double> extents = get_time_extents(); |
362 | length = (extents.second - extents.first) / _scale; | |
f25770e2 JH |
363 | offset = _offset / _scale; |
364 | } | |
365 | ||
d1e7d82c JH |
366 | void View::set_zoom(double scale, int offset) |
367 | { | |
368 | const double cursor_offset = _offset + _scale * offset; | |
369 | const double new_scale = max(min(scale, MaxScale), MinScale); | |
370 | const double new_offset = cursor_offset - new_scale * offset; | |
371 | set_scale_offset(new_scale, new_offset); | |
372 | } | |
373 | ||
cdf7bea7 | 374 | void View::update_scroll() |
adb4b10c JH |
375 | { |
376 | assert(_viewport); | |
377 | ||
378 | const QSize areaSize = _viewport->size(); | |
379 | ||
380 | // Set the horizontal scroll bar | |
381 | double length = 0, offset = 0; | |
f25770e2 JH |
382 | get_scroll_layout(length, offset); |
383 | length = max(length - areaSize.width(), 0.0); | |
adb4b10c | 384 | |
b4ef7f2a | 385 | horizontalScrollBar()->setPageStep(areaSize.width() / 2); |
f25770e2 | 386 | |
528bd8a1 JH |
387 | _updating_scroll = true; |
388 | ||
333d5bbc | 389 | if (length < MaxScrollValue) { |
f25770e2 JH |
390 | horizontalScrollBar()->setRange(0, length); |
391 | horizontalScrollBar()->setSliderPosition(offset); | |
392 | } else { | |
393 | horizontalScrollBar()->setRange(0, MaxScrollValue); | |
394 | horizontalScrollBar()->setSliderPosition( | |
395 | _offset * MaxScrollValue / (_scale * length)); | |
396 | } | |
adb4b10c | 397 | |
528bd8a1 JH |
398 | _updating_scroll = false; |
399 | ||
adb4b10c JH |
400 | // Set the vertical scrollbar |
401 | verticalScrollBar()->setPageStep(areaSize.height()); | |
402 | verticalScrollBar()->setRange(0, | |
4b192962 JH |
403 | _viewport->get_total_height() + SignalMargin - |
404 | areaSize.height()); | |
adb4b10c JH |
405 | } |
406 | ||
d7c0ca4a JH |
407 | void View::update_layout() |
408 | { | |
a6c1726e JH |
409 | setViewportMargins(_header->sizeHint().width(), |
410 | _ruler->sizeHint().height(), 0, 0); | |
d7c0ca4a JH |
411 | _ruler->setGeometry(_viewport->x(), 0, |
412 | _viewport->width(), _viewport->y()); | |
413 | _header->setGeometry(0, _viewport->y(), | |
414 | _viewport->x(), _viewport->height()); | |
415 | update_scroll(); | |
416 | } | |
417 | ||
38eeddea JH |
418 | bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a, |
419 | const shared_ptr<Trace> &b) | |
420 | { | |
421 | assert(a); | |
422 | assert(b); | |
423 | return a->get_v_offset() < b->get_v_offset(); | |
424 | } | |
425 | ||
cbd80f64 JH |
426 | bool View::eventFilter(QObject *object, QEvent *event) |
427 | { | |
428 | const QEvent::Type type = event->type(); | |
333d5bbc | 429 | if (type == QEvent::MouseMove) { |
cbd80f64 JH |
430 | |
431 | const QMouseEvent *const mouse_event = (QMouseEvent*)event; | |
333d5bbc | 432 | if (object == _viewport) |
cbd80f64 | 433 | _hover_point = mouse_event->pos(); |
333d5bbc | 434 | else if (object == _ruler) |
cbd80f64 | 435 | _hover_point = QPoint(mouse_event->x(), 0); |
333d5bbc | 436 | else if (object == _header) |
cbd80f64 JH |
437 | _hover_point = QPoint(0, mouse_event->y()); |
438 | else | |
439 | _hover_point = QPoint(-1, -1); | |
440 | ||
441 | hover_point_changed(); | |
442 | ||
333d5bbc | 443 | } else if (type == QEvent::Leave) { |
cbd80f64 JH |
444 | _hover_point = QPoint(-1, -1); |
445 | hover_point_changed(); | |
446 | } | |
447 | ||
448 | return QObject::eventFilter(object, event); | |
449 | } | |
450 | ||
cdf7bea7 | 451 | bool View::viewportEvent(QEvent *e) |
adb4b10c JH |
452 | { |
453 | switch(e->type()) { | |
454 | case QEvent::Paint: | |
455 | case QEvent::MouseButtonPress: | |
456 | case QEvent::MouseButtonRelease: | |
457 | case QEvent::MouseButtonDblClick: | |
458 | case QEvent::MouseMove: | |
459 | case QEvent::Wheel: | |
460 | return false; | |
461 | ||
462 | default: | |
463 | return QAbstractScrollArea::viewportEvent(e); | |
464 | } | |
465 | } | |
466 | ||
e314eca4 | 467 | void View::resizeEvent(QResizeEvent*) |
adb4b10c | 468 | { |
d7c0ca4a | 469 | update_layout(); |
adb4b10c JH |
470 | } |
471 | ||
b16907d3 | 472 | void View::h_scroll_value_changed(int value) |
adb4b10c | 473 | { |
528bd8a1 JH |
474 | if (_updating_scroll) |
475 | return; | |
476 | ||
f25770e2 | 477 | const int range = horizontalScrollBar()->maximum(); |
333d5bbc | 478 | if (range < MaxScrollValue) |
f25770e2 JH |
479 | _offset = _scale * value; |
480 | else { | |
481 | double length = 0, offset; | |
482 | get_scroll_layout(length, offset); | |
483 | _offset = _scale * length * value / MaxScrollValue; | |
484 | } | |
485 | ||
ccdd3ef5 | 486 | _ruler->update(); |
adb4b10c JH |
487 | _viewport->update(); |
488 | } | |
489 | ||
cdf7bea7 | 490 | void View::v_scroll_value_changed(int value) |
adb4b10c JH |
491 | { |
492 | _v_offset = value; | |
1d8dca91 | 493 | _header->update(); |
adb4b10c JH |
494 | _viewport->update(); |
495 | } | |
496 | ||
69dd2b03 JH |
497 | void View::signals_changed() |
498 | { | |
ef8311a4 | 499 | int offset = SignalMargin + SignalHeight; |
01fd3263 JH |
500 | const vector< shared_ptr<Trace> > traces(get_traces()); |
501 | BOOST_FOREACH(shared_ptr<Trace> t, traces) { | |
502 | t->set_view(this); | |
01fd3263 | 503 | t->set_v_offset(offset); |
ef8311a4 JH |
504 | offset += SignalHeight + 2 * SignalMargin; |
505 | } | |
506 | ||
a6c1726e | 507 | update_layout(); |
ef8311a4 | 508 | normalize_layout(); |
69dd2b03 JH |
509 | } |
510 | ||
cdf7bea7 | 511 | void View::data_updated() |
adb4b10c | 512 | { |
adb4b10c JH |
513 | // Update the scroll bars |
514 | update_scroll(); | |
515 | ||
516 | // Repaint the view | |
517 | _viewport->update(); | |
518 | } | |
cdf7bea7 | 519 | |
ca4ec3ea JH |
520 | void View::marker_time_changed() |
521 | { | |
522 | _ruler->update(); | |
523 | _viewport->update(); | |
524 | } | |
525 | ||
07204819 | 526 | void View::on_signals_moved() |
54401bbb | 527 | { |
07204819 JH |
528 | update_scroll(); |
529 | signals_moved(); | |
54401bbb JH |
530 | } |
531 | ||
d7c0ca4a JH |
532 | void View::on_geometry_updated() |
533 | { | |
534 | update_layout(); | |
535 | } | |
536 | ||
cdf7bea7 JH |
537 | } // namespace view |
538 | } // namespace pv |