2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
22 #include <libsigrokdecode/libsigrokdecode.h>
31 #include <QMouseEvent>
34 #include "cursorheader.h"
35 #include "decodetrace.h"
42 #include "pv/sigsession.h"
43 #include "pv/data/logic.h"
44 #include "pv/data/logicsnapshot.h"
46 using boost::shared_lock;
47 using boost::shared_mutex;
48 using pv::data::SignalData;
49 using std::back_inserter;
52 using std::lock_guard;
58 using std::shared_ptr;
65 const double View::MaxScale = 1e9;
66 const double View::MinScale = 1e-15;
68 const int View::MaxScrollValue = INT_MAX / 2;
70 const QColor View::CursorAreaColour(220, 231, 243);
72 const QSizeF View::LabelPadding(4, 0);
74 View::View(SigSession &session, QWidget *parent) :
75 QAbstractScrollArea(parent),
77 _viewport(new Viewport(*this)),
78 _ruler(new Ruler(*this)),
79 _cursorheader(new CursorHeader(*this)),
80 _header(new Header(*this)),
84 _updating_scroll(false),
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)));
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()));
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()));
108 connect(_header, SIGNAL(geometry_updated()),
109 this, SLOT(on_geometry_updated()));
110 connect(_header, SIGNAL(signals_moved()),
111 this, SLOT(on_signals_moved()));
113 connect(_header, SIGNAL(selection_changed()),
114 _cursorheader, SLOT(clear_selection()));
115 connect(_cursorheader, SIGNAL(selection_changed()),
116 _header, SLOT(clear_selection()));
118 connect(_header, SIGNAL(selection_changed()),
119 this, SIGNAL(selection_changed()));
120 connect(_cursorheader, SIGNAL(selection_changed()),
121 this, SIGNAL(selection_changed()));
123 connect(this, SIGNAL(hover_point_changed()),
124 this, SLOT(on_hover_point_changed()));
126 setViewport(_viewport);
128 _viewport->installEventFilter(this);
129 _ruler->installEventFilter(this);
130 _cursorheader->installEventFilter(this);
131 _header->installEventFilter(this);
133 // Trigger the initial event manually. The default device has signals
134 // which were created before this object came into being
137 // make sure the transparent widgets are on the top
138 _cursorheader->raise();
142 SigSession& View::session()
147 const SigSession& View::session() const
157 const View* View::view() const
162 Viewport* View::viewport()
167 const Viewport* View::viewport() const
172 double View::scale() const
177 double View::offset() const
182 int View::owner_v_offset() const
187 void View::zoom(double steps)
189 zoom(steps, _viewport->width() / 2);
192 void View::zoom(double steps, int offset)
194 set_zoom(_scale * pow(3.0/2.0, -steps), offset);
197 void View::zoom_fit()
199 const pair<double, double> extents = get_time_extents();
200 const double delta = extents.second - extents.first;
205 const int w = _viewport->width();
209 const double scale = max(min(delta / w, MaxScale), MinScale);
210 set_scale_offset(scale, extents.first);
213 void View::zoom_one_to_one()
215 using pv::data::SignalData;
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())
222 double samplerate = 0.0;
223 for (const shared_ptr<SignalData> d : visible_data) {
225 samplerate = max(samplerate, d->samplerate());
228 if (samplerate == 0.0)
232 const int w = _viewport->width();
236 set_zoom(1.0 / samplerate, w / 2);
239 void View::set_scale_offset(double scale, double offset)
246 _cursorheader->update();
248 scale_offset_changed();
251 set< shared_ptr<SignalData> > View::get_visible_data() const
253 shared_lock<shared_mutex> lock(session().signals_mutex());
254 const vector< shared_ptr<Signal> > &sigs(session().signals());
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)
260 visible_data.insert(sig->data());
265 pair<double, double> View::get_time_extents() const
267 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
268 if (visible_data.empty())
269 return make_pair(0.0, 0.0);
271 double left_time = DBL_MAX, right_time = DBL_MIN;
272 for (const shared_ptr<SignalData> d : visible_data)
274 const double start_time = d->get_start_time();
275 double samplerate = d->samplerate();
276 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
278 left_time = min(left_time, start_time);
279 right_time = max(right_time, start_time +
280 d->get_max_sample_count() / samplerate);
283 assert(left_time < right_time);
284 return make_pair(left_time, right_time);
287 bool View::cursors_shown() const
289 return _show_cursors;
292 void View::show_cursors(bool show)
294 _show_cursors = show;
295 _cursorheader->update();
299 void View::centre_cursors()
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();
308 CursorPair& View::cursors()
313 const CursorPair& View::cursors() const
318 const QPoint& View::hover_point() const
323 void View::normalize_layout()
326 for (const shared_ptr<RowItem> r : *this)
327 v_min = min(r->v_offset(), v_min);
329 const int delta = -min(v_min, 0);
330 for (shared_ptr<RowItem> r : *this)
331 r->set_v_offset(r->v_offset() + delta);
333 verticalScrollBar()->setSliderPosition(_v_offset + delta);
334 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
337 void View::update_viewport()
343 void View::get_scroll_layout(double &length, double &offset) const
345 const pair<double, double> extents = get_time_extents();
346 length = (extents.second - extents.first) / _scale;
347 offset = _offset / _scale;
350 void View::set_zoom(double scale, int offset)
352 const double cursor_offset = _offset + _scale * offset;
353 const double new_scale = max(min(scale, MaxScale), MinScale);
354 const double new_offset = cursor_offset - new_scale * offset;
355 set_scale_offset(new_scale, new_offset);
358 void View::update_scroll()
362 const QSize areaSize = _viewport->size();
364 // Set the horizontal scroll bar
365 double length = 0, offset = 0;
366 get_scroll_layout(length, offset);
367 length = max(length - areaSize.width(), 0.0);
369 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
371 _updating_scroll = true;
373 if (length < MaxScrollValue) {
374 horizontalScrollBar()->setRange(0, length);
375 horizontalScrollBar()->setSliderPosition(offset);
377 horizontalScrollBar()->setRange(0, MaxScrollValue);
378 horizontalScrollBar()->setSliderPosition(
379 _offset * MaxScrollValue / (_scale * length));
382 _updating_scroll = false;
384 // Set the vertical scrollbar
385 verticalScrollBar()->setPageStep(areaSize.height());
387 const pair<int, int> extents = v_extents();
388 const int extra_scroll_height = (extents.second - extents.first) / 4;
389 verticalScrollBar()->setRange(extents.first - extra_scroll_height,
390 extents.first + extra_scroll_height);
393 void View::update_layout()
396 _header->sizeHint().width() - pv::view::Header::BaselineOffset,
397 _ruler->sizeHint().height(), 0, 0);
398 _ruler->setGeometry(_viewport->x(), 0,
399 _viewport->width(), _viewport->y());
400 _cursorheader->setGeometry(
402 _ruler->sizeHint().height() - _cursorheader->sizeHint().height() / 2,
403 _viewport->width(), _cursorheader->sizeHint().height());
404 _header->setGeometry(0, _viewport->y(),
405 _header->sizeHint().width(), _viewport->height());
409 void View::paint_label(QPainter &p, int right, bool hover)
416 QRectF View::label_rect(int right)
422 bool View::eventFilter(QObject *object, QEvent *event)
424 const QEvent::Type type = event->type();
425 if (type == QEvent::MouseMove) {
427 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
428 if (object == _viewport)
429 _hover_point = mouse_event->pos();
430 else if (object == _ruler || object == _cursorheader)
431 _hover_point = QPoint(mouse_event->x(), 0);
432 else if (object == _header)
433 _hover_point = QPoint(0, mouse_event->y());
435 _hover_point = QPoint(-1, -1);
437 hover_point_changed();
439 } else if (type == QEvent::Leave) {
440 _hover_point = QPoint(-1, -1);
441 hover_point_changed();
444 return QObject::eventFilter(object, event);
447 bool View::viewportEvent(QEvent *e)
451 case QEvent::MouseButtonPress:
452 case QEvent::MouseButtonRelease:
453 case QEvent::MouseButtonDblClick:
454 case QEvent::MouseMove:
456 case QEvent::TouchBegin:
457 case QEvent::TouchUpdate:
458 case QEvent::TouchEnd:
462 return QAbstractScrollArea::viewportEvent(e);
466 void View::resizeEvent(QResizeEvent*)
471 void View::h_scroll_value_changed(int value)
473 if (_updating_scroll)
476 const int range = horizontalScrollBar()->maximum();
477 if (range < MaxScrollValue)
478 _offset = _scale * value;
480 double length = 0, offset;
481 get_scroll_layout(length, offset);
482 _offset = _scale * length * value / MaxScrollValue;
486 _cursorheader->update();
490 void View::v_scroll_value_changed(int value)
497 void View::signals_changed()
499 // Populate the traces
502 shared_lock<shared_mutex> lock(session().signals_mutex());
503 const vector< shared_ptr<Signal> > &sigs(session().signals());
508 const vector< shared_ptr<DecodeTrace> > decode_sigs(
509 session().get_decode_signals());
510 for (auto s : decode_sigs)
514 // Create the initial layout
516 for (shared_ptr<RowItem> r : *this) {
517 const pair<int, int> extents = r->v_extents();
519 offset += -extents.first;
520 r->set_v_offset(offset);
522 offset += extents.second;
528 // Update the child widgets
529 _header->signals_updated();
530 _viewport->signals_updated();
533 void View::data_updated()
535 // Update the scroll bars
542 void View::marker_time_changed()
544 _cursorheader->update();
548 void View::on_signals_moved()
554 void View::on_geometry_updated()
559 void View::on_hover_point_changed()
561 for (shared_ptr<RowItem> r : *this)
562 r->hover_point_changed();