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