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>
30 #include <QMouseEvent>
33 #include "cursorheader.h"
34 #include "decodetrace.h"
41 #include "pv/sigsession.h"
42 #include "pv/data/logic.h"
43 #include "pv/data/logicsnapshot.h"
45 using pv::data::SignalData;
46 using std::back_inserter;
54 using std::shared_ptr;
61 const double View::MaxScale = 1e9;
62 const double View::MinScale = 1e-15;
64 const int View::MaxScrollValue = INT_MAX / 2;
66 const int View::SignalHeight = 30;
67 const int View::SignalMargin = 10;
68 const int View::SignalSnapGridSize = 10;
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
152 Viewport* View::viewport()
157 const Viewport* View::viewport() const
162 double View::scale() const
167 double View::offset() const
172 int View::v_offset() const
177 void View::zoom(double steps)
179 zoom(steps, _viewport->width() / 2);
182 void View::zoom(double steps, int offset)
184 set_zoom(_scale * pow(3.0/2.0, -steps), offset);
187 void View::zoom_fit()
189 const pair<double, double> extents = get_time_extents();
190 const double delta = extents.second - extents.first;
195 const int w = _viewport->width();
199 const double scale = max(min(delta / w, MaxScale), MinScale);
200 set_scale_offset(scale, extents.first);
203 void View::zoom_one_to_one()
205 using pv::data::SignalData;
207 const vector< shared_ptr<Signal> > sigs(
208 session().get_signals());
210 // Make a set of all the visible data objects
211 set< shared_ptr<SignalData> > visible_data = get_visible_data();
212 if (visible_data.empty())
215 double samplerate = 0.0;
216 for (const shared_ptr<SignalData> d : visible_data) {
218 samplerate = max(samplerate, d->samplerate());
221 if (samplerate == 0.0)
225 const int w = _viewport->width();
229 set_zoom(1.0 / samplerate, w / 2);
232 void View::set_scale_offset(double scale, double offset)
239 _cursorheader->update();
241 scale_offset_changed();
244 vector< shared_ptr<Trace> > View::get_traces() const
246 vector< shared_ptr<Trace> > traces;
248 const vector< shared_ptr<Signal> > sigs(
249 session().get_signals());
250 copy(sigs.begin(), sigs.end(), back_inserter(traces));
253 const vector< shared_ptr<DecodeTrace> > decode_sigs(
254 session().get_decode_signals());
255 copy(decode_sigs.begin(), decode_sigs.end(), back_inserter(traces));
258 stable_sort(traces.begin(), traces.end(),
259 [](const shared_ptr<Trace> &a, const shared_ptr<Trace> &b) {
260 return a->v_offset() < b->v_offset(); });
265 list<weak_ptr<SelectableItem> > View::selected_items() const
267 list<weak_ptr<SelectableItem> > items;
269 // List the selected signals
270 const vector< shared_ptr<Trace> > traces(get_traces());
271 for (shared_ptr<Trace> t : traces) {
277 // List the selected cursors
278 if (_cursors.first()->selected())
279 items.push_back(_cursors.first());
280 if (_cursors.second()->selected())
281 items.push_back(_cursors.second());
286 set< shared_ptr<SignalData> > View::get_visible_data() const
288 const vector< shared_ptr<Signal> > sigs(
289 session().get_signals());
291 // Make a set of all the visible data objects
292 set< shared_ptr<SignalData> > visible_data;
293 for (const shared_ptr<Signal> sig : sigs)
295 visible_data.insert(sig->data());
300 pair<double, double> View::get_time_extents() const
302 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
303 if (visible_data.empty())
304 return make_pair(0.0, 0.0);
306 double left_time = DBL_MAX, right_time = DBL_MIN;
307 for (const shared_ptr<SignalData> d : visible_data)
309 const double start_time = d->get_start_time();
310 double samplerate = d->samplerate();
311 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
313 left_time = min(left_time, start_time);
314 right_time = max(right_time, start_time +
315 d->get_max_sample_count() / samplerate);
318 assert(left_time < right_time);
319 return make_pair(left_time, right_time);
322 bool View::cursors_shown() const
324 return _show_cursors;
327 void View::show_cursors(bool show)
329 _show_cursors = show;
330 _cursorheader->update();
334 void View::centre_cursors()
336 const double time_width = _scale * _viewport->width();
337 _cursors.first()->set_time(_offset + time_width * 0.4);
338 _cursors.second()->set_time(_offset + time_width * 0.6);
339 _cursorheader->update();
343 CursorPair& View::cursors()
348 const CursorPair& View::cursors() const
353 const QPoint& View::hover_point() const
358 void View::normalize_layout()
360 const vector< shared_ptr<Trace> > traces(get_traces());
363 for (const shared_ptr<Trace> t : traces)
364 v_min = min(t->v_offset(), v_min);
366 const int delta = -min(v_min, 0);
367 for (shared_ptr<Trace> t : traces)
368 t->set_v_offset(t->v_offset() + delta);
370 verticalScrollBar()->setSliderPosition(_v_offset + delta);
371 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
374 void View::update_viewport()
380 void View::get_scroll_layout(double &length, double &offset) const
382 const pair<double, double> extents = get_time_extents();
383 length = (extents.second - extents.first) / _scale;
384 offset = _offset / _scale;
387 void View::set_zoom(double scale, int offset)
389 const double cursor_offset = _offset + _scale * offset;
390 const double new_scale = max(min(scale, MaxScale), MinScale);
391 const double new_offset = cursor_offset - new_scale * offset;
392 set_scale_offset(new_scale, new_offset);
395 void View::update_scroll()
399 const QSize areaSize = _viewport->size();
401 // Set the horizontal scroll bar
402 double length = 0, offset = 0;
403 get_scroll_layout(length, offset);
404 length = max(length - areaSize.width(), 0.0);
406 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
408 _updating_scroll = true;
410 if (length < MaxScrollValue) {
411 horizontalScrollBar()->setRange(0, length);
412 horizontalScrollBar()->setSliderPosition(offset);
414 horizontalScrollBar()->setRange(0, MaxScrollValue);
415 horizontalScrollBar()->setSliderPosition(
416 _offset * MaxScrollValue / (_scale * length));
419 _updating_scroll = false;
421 // Set the vertical scrollbar
422 verticalScrollBar()->setPageStep(areaSize.height());
423 verticalScrollBar()->setRange(0,
424 _viewport->get_total_height() + SignalMargin -
428 void View::update_layout()
431 _header->sizeHint().width() - pv::view::Header::BaselineOffset,
432 _ruler->sizeHint().height(), 0, 0);
433 _ruler->setGeometry(_viewport->x(), 0,
434 _viewport->width(), _viewport->y());
435 _cursorheader->setGeometry(
437 _ruler->sizeHint().height() - _cursorheader->sizeHint().height() / 2,
438 _viewport->width(), _cursorheader->sizeHint().height());
439 _header->setGeometry(0, _viewport->y(),
440 _header->sizeHint().width(), _viewport->height());
444 bool View::eventFilter(QObject *object, QEvent *event)
446 const QEvent::Type type = event->type();
447 if (type == QEvent::MouseMove) {
449 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
450 if (object == _viewport)
451 _hover_point = mouse_event->pos();
452 else if (object == _ruler || object == _cursorheader)
453 _hover_point = QPoint(mouse_event->x(), 0);
454 else if (object == _header)
455 _hover_point = QPoint(0, mouse_event->y());
457 _hover_point = QPoint(-1, -1);
459 hover_point_changed();
461 } else if (type == QEvent::Leave) {
462 _hover_point = QPoint(-1, -1);
463 hover_point_changed();
466 return QObject::eventFilter(object, event);
469 bool View::viewportEvent(QEvent *e)
473 case QEvent::MouseButtonPress:
474 case QEvent::MouseButtonRelease:
475 case QEvent::MouseButtonDblClick:
476 case QEvent::MouseMove:
478 case QEvent::TouchBegin:
479 case QEvent::TouchUpdate:
480 case QEvent::TouchEnd:
484 return QAbstractScrollArea::viewportEvent(e);
488 void View::resizeEvent(QResizeEvent*)
493 void View::h_scroll_value_changed(int value)
495 if (_updating_scroll)
498 const int range = horizontalScrollBar()->maximum();
499 if (range < MaxScrollValue)
500 _offset = _scale * value;
502 double length = 0, offset;
503 get_scroll_layout(length, offset);
504 _offset = _scale * length * value / MaxScrollValue;
508 _cursorheader->update();
512 void View::v_scroll_value_changed(int value)
519 void View::signals_changed()
521 int offset = SignalMargin + SignalHeight;
522 const vector< shared_ptr<Trace> > traces(get_traces());
523 for (shared_ptr<Trace> t : traces) {
525 t->set_v_offset(offset);
526 offset += SignalHeight + 2 * SignalMargin;
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 const vector< shared_ptr<Trace> > traces(get_traces());
562 for (shared_ptr<Trace> t : traces)
563 t->hover_point_changed();