]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/view.cpp
RowItem: Split appart visual and layout v offsets
[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(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 // Make a set of all the visible data objects
218 set< shared_ptr<SignalData> > visible_data = get_visible_data();
219 if (visible_data.empty())
220 return;
221
222 double samplerate = 0.0;
223 for (const shared_ptr<SignalData> d : visible_data) {
224 assert(d);
225 samplerate = max(samplerate, d->samplerate());
226 }
227
228 if (samplerate == 0.0)
229 return;
230
231 assert(_viewport);
232 const int w = _viewport->width();
233 if (w <= 0)
234 return;
235
236 set_zoom(1.0 / samplerate, w / 2);
237}
238
239void View::set_scale_offset(double scale, double offset)
240{
241 _scale = scale;
242 _offset = offset;
243
244 update_scroll();
245 _ruler->update();
246 _cursorheader->update();
247 _viewport->update();
248 scale_offset_changed();
249}
250
251set< shared_ptr<SignalData> > View::get_visible_data() const
252{
253 shared_lock<shared_mutex> lock(session().signals_mutex());
254 const vector< shared_ptr<Signal> > &sigs(session().signals());
255
256 // Make a set of all the visible data objects
257 set< shared_ptr<SignalData> > visible_data;
258 for (const shared_ptr<Signal> sig : sigs)
259 if (sig->enabled())
260 visible_data.insert(sig->data());
261
262 return visible_data;
263}
264
265pair<double, double> View::get_time_extents() const
266{
267 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
268 if (visible_data.empty())
269 return make_pair(0.0, 0.0);
270
271 double left_time = DBL_MAX, right_time = DBL_MIN;
272 for (const shared_ptr<SignalData> d : visible_data)
273 {
274 const double start_time = d->get_start_time();
275 double samplerate = d->samplerate();
276 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
277
278 left_time = min(left_time, start_time);
279 right_time = max(right_time, start_time +
280 d->get_max_sample_count() / samplerate);
281 }
282
283 assert(left_time < right_time);
284 return make_pair(left_time, right_time);
285}
286
287bool View::cursors_shown() const
288{
289 return _show_cursors;
290}
291
292void View::show_cursors(bool show)
293{
294 _show_cursors = show;
295 _cursorheader->update();
296 _viewport->update();
297}
298
299void View::centre_cursors()
300{
301 const double time_width = _scale * _viewport->width();
302 _cursors.first()->set_time(_offset + time_width * 0.4);
303 _cursors.second()->set_time(_offset + time_width * 0.6);
304 _cursorheader->update();
305 _viewport->update();
306}
307
308CursorPair& View::cursors()
309{
310 return _cursors;
311}
312
313const CursorPair& View::cursors() const
314{
315 return _cursors;
316}
317
318const QPoint& View::hover_point() const
319{
320 return _hover_point;
321}
322
323void View::update_viewport()
324{
325 assert(_viewport);
326 _viewport->update();
327}
328
329void View::get_scroll_layout(double &length, double &offset) const
330{
331 const pair<double, double> extents = get_time_extents();
332 length = (extents.second - extents.first) / _scale;
333 offset = _offset / _scale;
334}
335
336void View::set_zoom(double scale, int offset)
337{
338 const double cursor_offset = _offset + _scale * offset;
339 const double new_scale = max(min(scale, MaxScale), MinScale);
340 const double new_offset = cursor_offset - new_scale * offset;
341 set_scale_offset(new_scale, new_offset);
342}
343
344void View::update_scroll()
345{
346 assert(_viewport);
347
348 const QSize areaSize = _viewport->size();
349
350 // Set the horizontal scroll bar
351 double length = 0, offset = 0;
352 get_scroll_layout(length, offset);
353 length = max(length - areaSize.width(), 0.0);
354
355 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
356
357 _updating_scroll = true;
358
359 if (length < MaxScrollValue) {
360 horizontalScrollBar()->setRange(0, length);
361 horizontalScrollBar()->setSliderPosition(offset);
362 } else {
363 horizontalScrollBar()->setRange(0, MaxScrollValue);
364 horizontalScrollBar()->setSliderPosition(
365 _offset * MaxScrollValue / (_scale * length));
366 }
367
368 _updating_scroll = false;
369
370 // Set the vertical scrollbar
371 verticalScrollBar()->setPageStep(areaSize.height());
372
373 const pair<int, int> extents = v_extents();
374 const int extra_scroll_height = (extents.second - extents.first) / 4;
375 verticalScrollBar()->setRange(extents.first - extra_scroll_height,
376 extents.first + extra_scroll_height);
377}
378
379void View::update_layout()
380{
381 setViewportMargins(
382 _header->sizeHint().width() - pv::view::Header::BaselineOffset,
383 _ruler->sizeHint().height(), 0, 0);
384 _ruler->setGeometry(_viewport->x(), 0,
385 _viewport->width(), _viewport->y());
386 _cursorheader->setGeometry(
387 _viewport->x(),
388 _ruler->sizeHint().height() - _cursorheader->sizeHint().height() / 2,
389 _viewport->width(), _cursorheader->sizeHint().height());
390 _header->setGeometry(0, _viewport->y(),
391 _header->sizeHint().width(), _viewport->height());
392 update_scroll();
393}
394
395void View::paint_label(QPainter &p, int right, bool hover)
396{
397 (void)p;
398 (void)right;
399 (void)hover;
400}
401
402QRectF View::label_rect(int right)
403{
404 (void)right;
405 return QRectF();
406}
407
408bool View::eventFilter(QObject *object, QEvent *event)
409{
410 const QEvent::Type type = event->type();
411 if (type == QEvent::MouseMove) {
412
413 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
414 if (object == _viewport)
415 _hover_point = mouse_event->pos();
416 else if (object == _ruler || object == _cursorheader)
417 _hover_point = QPoint(mouse_event->x(), 0);
418 else if (object == _header)
419 _hover_point = QPoint(0, mouse_event->y());
420 else
421 _hover_point = QPoint(-1, -1);
422
423 hover_point_changed();
424
425 } else if (type == QEvent::Leave) {
426 _hover_point = QPoint(-1, -1);
427 hover_point_changed();
428 }
429
430 return QObject::eventFilter(object, event);
431}
432
433bool View::viewportEvent(QEvent *e)
434{
435 switch(e->type()) {
436 case QEvent::Paint:
437 case QEvent::MouseButtonPress:
438 case QEvent::MouseButtonRelease:
439 case QEvent::MouseButtonDblClick:
440 case QEvent::MouseMove:
441 case QEvent::Wheel:
442 case QEvent::TouchBegin:
443 case QEvent::TouchUpdate:
444 case QEvent::TouchEnd:
445 return false;
446
447 default:
448 return QAbstractScrollArea::viewportEvent(e);
449 }
450}
451
452void View::resizeEvent(QResizeEvent*)
453{
454 update_layout();
455}
456
457void View::h_scroll_value_changed(int value)
458{
459 if (_updating_scroll)
460 return;
461
462 const int range = horizontalScrollBar()->maximum();
463 if (range < MaxScrollValue)
464 _offset = _scale * value;
465 else {
466 double length = 0, offset;
467 get_scroll_layout(length, offset);
468 _offset = _scale * length * value / MaxScrollValue;
469 }
470
471 _ruler->update();
472 _cursorheader->update();
473 _viewport->update();
474}
475
476void View::v_scroll_value_changed(int value)
477{
478 _v_offset = value;
479 _header->update();
480 _viewport->update();
481}
482
483void View::signals_changed()
484{
485 // Populate the traces
486 clear_child_items();
487
488 shared_lock<shared_mutex> lock(session().signals_mutex());
489 const vector< shared_ptr<Signal> > &sigs(session().signals());
490 for (auto s : sigs)
491 add_child_item(s);
492
493#ifdef ENABLE_DECODE
494 const vector< shared_ptr<DecodeTrace> > decode_sigs(
495 session().get_decode_signals());
496 for (auto s : decode_sigs)
497 add_child_item(s);
498#endif
499
500 // Create the initial layout
501 int offset = 0;
502 for (shared_ptr<RowItem> r : *this) {
503 const pair<int, int> extents = r->v_extents();
504 if (r->enabled())
505 offset += -extents.first;
506 r->force_to_v_offset(offset);
507 if (r->enabled())
508 offset += extents.second;
509 }
510
511 update_layout();
512
513 // Update the child widgets
514 _header->signals_updated();
515 _viewport->signals_updated();
516}
517
518void View::data_updated()
519{
520 // Update the scroll bars
521 update_scroll();
522
523 // Repaint the view
524 _viewport->update();
525}
526
527void View::marker_time_changed()
528{
529 _cursorheader->update();
530 _viewport->update();
531}
532
533void View::on_signals_moved()
534{
535 update_scroll();
536 signals_moved();
537}
538
539void View::on_geometry_updated()
540{
541 update_layout();
542}
543
544void View::on_hover_point_changed()
545{
546 for (shared_ptr<RowItem> r : *this)
547 r->hover_point_changed();
548}
549
550} // namespace view
551} // namespace pv