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;
53 using std::shared_ptr;
60 const double View::MaxScale = 1e9;
61 const double View::MinScale = 1e-15;
63 const int View::MaxScrollValue = INT_MAX / 2;
65 const int View::SignalHeight = 30;
66 const int View::SignalMargin = 10;
67 const int View::SignalSnapGridSize = 10;
69 const QColor View::CursorAreaColour(220, 231, 243);
71 const QSizeF View::LabelPadding(4, 0);
73 View::View(SigSession &session, QWidget *parent) :
74 QAbstractScrollArea(parent),
76 _viewport(new Viewport(*this)),
77 _ruler(new Ruler(*this)),
78 _cursorheader(new CursorHeader(*this)),
79 _header(new Header(*this)),
83 _updating_scroll(false),
88 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
89 this, SLOT(h_scroll_value_changed(int)));
90 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
91 this, SLOT(v_scroll_value_changed(int)));
93 connect(&_session, SIGNAL(signals_changed()),
94 this, SLOT(signals_changed()));
95 connect(&_session, SIGNAL(data_received()),
96 this, SLOT(data_updated()));
97 connect(&_session, SIGNAL(frame_ended()),
98 this, SLOT(data_updated()));
100 connect(_cursors.first().get(), SIGNAL(time_changed()),
101 this, SLOT(marker_time_changed()));
102 connect(_cursors.second().get(), SIGNAL(time_changed()),
103 this, SLOT(marker_time_changed()));
105 connect(_header, SIGNAL(geometry_updated()),
106 this, SLOT(on_geometry_updated()));
107 connect(_header, SIGNAL(signals_moved()),
108 this, SLOT(on_signals_moved()));
110 connect(_header, SIGNAL(selection_changed()),
111 _cursorheader, SLOT(clear_selection()));
112 connect(_cursorheader, SIGNAL(selection_changed()),
113 _header, SLOT(clear_selection()));
115 connect(_header, SIGNAL(selection_changed()),
116 this, SIGNAL(selection_changed()));
117 connect(_cursorheader, SIGNAL(selection_changed()),
118 this, SIGNAL(selection_changed()));
120 setViewport(_viewport);
122 _viewport->installEventFilter(this);
123 _ruler->installEventFilter(this);
124 _cursorheader->installEventFilter(this);
125 _header->installEventFilter(this);
127 // Trigger the initial event manually. The default device has signals
128 // which were created before this object came into being
131 // make sure the cursorheader is over the ruler
132 _cursorheader->raise();
135 SigSession& View::session()
140 const SigSession& View::session() const
145 double View::scale() const
150 double View::offset() const
155 int View::v_offset() const
160 void View::zoom(double steps)
162 zoom(steps, _viewport->width() / 2);
165 void View::zoom(double steps, int offset)
167 set_zoom(_scale * pow(3.0/2.0, -steps), offset);
170 void View::zoom_fit()
172 const pair<double, double> extents = get_time_extents();
173 const double delta = extents.second - extents.first;
178 const int w = _viewport->width();
182 const double scale = max(min(delta / w, MaxScale), MinScale);
183 set_scale_offset(scale, extents.first);
186 void View::zoom_one_to_one()
188 using pv::data::SignalData;
190 const vector< shared_ptr<Signal> > sigs(
191 session().get_signals());
193 // Make a set of all the visible data objects
194 set< shared_ptr<SignalData> > visible_data = get_visible_data();
195 if (visible_data.empty())
198 double samplerate = 0.0;
199 for (const shared_ptr<SignalData> d : visible_data) {
201 samplerate = max(samplerate, d->samplerate());
204 if (samplerate == 0.0)
208 const int w = _viewport->width();
212 set_zoom(1.0 / samplerate, w / 2);
215 void View::set_scale_offset(double scale, double offset)
222 _cursorheader->update();
224 scale_offset_changed();
227 vector< shared_ptr<Trace> > View::get_traces() const
229 const vector< shared_ptr<Signal> > sigs(
230 session().get_signals());
232 const vector< shared_ptr<DecodeTrace> > decode_sigs(
233 session().get_decode_signals());
234 vector< shared_ptr<Trace> > traces(
235 sigs.size() + decode_sigs.size());
237 vector< shared_ptr<Trace> > traces(sigs.size());
240 auto i = traces.begin();
241 i = copy(sigs.begin(), sigs.end(), i);
243 i = copy(decode_sigs.begin(), decode_sigs.end(), i);
246 stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets);
250 list<weak_ptr<SelectableItem> > View::selected_items() const
252 list<weak_ptr<SelectableItem> > items;
254 // List the selected signals
255 const vector< shared_ptr<Trace> > traces(get_traces());
256 for (shared_ptr<Trace> t : traces) {
262 // List the selected cursors
263 if (_cursors.first()->selected())
264 items.push_back(_cursors.first());
265 if (_cursors.second()->selected())
266 items.push_back(_cursors.second());
271 set< shared_ptr<SignalData> > View::get_visible_data() const
273 const vector< shared_ptr<Signal> > sigs(
274 session().get_signals());
276 // Make a set of all the visible data objects
277 set< shared_ptr<SignalData> > visible_data;
278 for (const shared_ptr<Signal> sig : sigs)
280 visible_data.insert(sig->data());
285 pair<double, double> View::get_time_extents() const
287 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
288 if (visible_data.empty())
289 return make_pair(0.0, 0.0);
291 double left_time = DBL_MAX, right_time = DBL_MIN;
292 for (const shared_ptr<SignalData> d : visible_data)
294 const double start_time = d->get_start_time();
295 double samplerate = d->samplerate();
296 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
298 left_time = min(left_time, start_time);
299 right_time = max(right_time, start_time +
300 d->get_max_sample_count() / samplerate);
303 assert(left_time < right_time);
304 return make_pair(left_time, right_time);
307 bool View::cursors_shown() const
309 return _show_cursors;
312 void View::show_cursors(bool show)
314 _show_cursors = show;
315 _cursorheader->update();
319 void View::centre_cursors()
321 const double time_width = _scale * _viewport->width();
322 _cursors.first()->set_time(_offset + time_width * 0.4);
323 _cursors.second()->set_time(_offset + time_width * 0.6);
324 _cursorheader->update();
328 CursorPair& View::cursors()
333 const CursorPair& View::cursors() const
338 const QPoint& View::hover_point() const
343 void View::normalize_layout()
345 const vector< shared_ptr<Trace> > traces(get_traces());
348 for (const shared_ptr<Trace> t : traces)
349 v_min = min(t->get_v_offset(), v_min);
351 const int delta = -min(v_min, 0);
352 for (shared_ptr<Trace> t : traces)
353 t->set_v_offset(t->get_v_offset() + delta);
355 verticalScrollBar()->setSliderPosition(_v_offset + delta);
356 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
359 void View::update_viewport()
365 void View::get_scroll_layout(double &length, double &offset) const
367 const pair<double, double> extents = get_time_extents();
368 length = (extents.second - extents.first) / _scale;
369 offset = _offset / _scale;
372 void View::set_zoom(double scale, int offset)
374 const double cursor_offset = _offset + _scale * offset;
375 const double new_scale = max(min(scale, MaxScale), MinScale);
376 const double new_offset = cursor_offset - new_scale * offset;
377 set_scale_offset(new_scale, new_offset);
380 void View::update_scroll()
384 const QSize areaSize = _viewport->size();
386 // Set the horizontal scroll bar
387 double length = 0, offset = 0;
388 get_scroll_layout(length, offset);
389 length = max(length - areaSize.width(), 0.0);
391 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
393 _updating_scroll = true;
395 if (length < MaxScrollValue) {
396 horizontalScrollBar()->setRange(0, length);
397 horizontalScrollBar()->setSliderPosition(offset);
399 horizontalScrollBar()->setRange(0, MaxScrollValue);
400 horizontalScrollBar()->setSliderPosition(
401 _offset * MaxScrollValue / (_scale * length));
404 _updating_scroll = false;
406 // Set the vertical scrollbar
407 verticalScrollBar()->setPageStep(areaSize.height());
408 verticalScrollBar()->setRange(0,
409 _viewport->get_total_height() + SignalMargin -
413 void View::update_layout()
415 setViewportMargins(_header->sizeHint().width(),
416 _ruler->sizeHint().height(), 0, 0);
417 _ruler->setGeometry(_viewport->x(), 0,
418 _viewport->width(), _viewport->y());
419 _cursorheader->setGeometry(
421 _ruler->sizeHint().height() - _cursorheader->sizeHint().height() / 2,
422 _viewport->width(), _cursorheader->sizeHint().height());
423 _header->setGeometry(0, _viewport->y(),
424 _viewport->x(), _viewport->height());
428 bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
429 const shared_ptr<Trace> &b)
433 return a->get_v_offset() < b->get_v_offset();
436 bool View::eventFilter(QObject *object, QEvent *event)
438 const QEvent::Type type = event->type();
439 if (type == QEvent::MouseMove) {
441 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
442 if (object == _viewport)
443 _hover_point = mouse_event->pos();
444 else if (object == _ruler || object == _cursorheader)
445 _hover_point = QPoint(mouse_event->x(), 0);
446 else if (object == _header)
447 _hover_point = QPoint(0, mouse_event->y());
449 _hover_point = QPoint(-1, -1);
451 hover_point_changed();
453 } else if (type == QEvent::Leave) {
454 _hover_point = QPoint(-1, -1);
455 hover_point_changed();
458 return QObject::eventFilter(object, event);
461 bool View::viewportEvent(QEvent *e)
465 case QEvent::MouseButtonPress:
466 case QEvent::MouseButtonRelease:
467 case QEvent::MouseButtonDblClick:
468 case QEvent::MouseMove:
473 return QAbstractScrollArea::viewportEvent(e);
477 void View::resizeEvent(QResizeEvent*)
482 void View::h_scroll_value_changed(int value)
484 if (_updating_scroll)
487 const int range = horizontalScrollBar()->maximum();
488 if (range < MaxScrollValue)
489 _offset = _scale * value;
491 double length = 0, offset;
492 get_scroll_layout(length, offset);
493 _offset = _scale * length * value / MaxScrollValue;
497 _cursorheader->update();
501 void View::v_scroll_value_changed(int value)
508 void View::signals_changed()
510 int offset = SignalMargin + SignalHeight;
511 const vector< shared_ptr<Trace> > traces(get_traces());
512 for (shared_ptr<Trace> t : traces) {
514 t->set_v_offset(offset);
515 offset += SignalHeight + 2 * SignalMargin;
522 void View::data_updated()
524 // Update the scroll bars
531 void View::marker_time_changed()
533 _cursorheader->update();
537 void View::on_signals_moved()
543 void View::on_geometry_updated()