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