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