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 transparent widgets are on the top
132 _cursorheader->raise();
136 SigSession& View::session()
141 const SigSession& View::session() const
146 double View::scale() const
151 double View::offset() const
156 int View::v_offset() const
161 void View::zoom(double steps)
163 zoom(steps, _viewport->width() / 2);
166 void View::zoom(double steps, int offset)
168 set_zoom(_scale * pow(3.0/2.0, -steps), offset);
171 void View::zoom_fit()
173 const pair<double, double> extents = get_time_extents();
174 const double delta = extents.second - extents.first;
179 const int w = _viewport->width();
183 const double scale = max(min(delta / w, MaxScale), MinScale);
184 set_scale_offset(scale, extents.first);
187 void View::zoom_one_to_one()
189 using pv::data::SignalData;
191 const vector< shared_ptr<Signal> > sigs(
192 session().get_signals());
194 // Make a set of all the visible data objects
195 set< shared_ptr<SignalData> > visible_data = get_visible_data();
196 if (visible_data.empty())
199 double samplerate = 0.0;
200 for (const shared_ptr<SignalData> d : visible_data) {
202 samplerate = max(samplerate, d->samplerate());
205 if (samplerate == 0.0)
209 const int w = _viewport->width();
213 set_zoom(1.0 / samplerate, w / 2);
216 void View::set_scale_offset(double scale, double offset)
223 _cursorheader->update();
225 scale_offset_changed();
228 vector< shared_ptr<Trace> > View::get_traces() const
230 const vector< shared_ptr<Signal> > sigs(
231 session().get_signals());
233 const vector< shared_ptr<DecodeTrace> > decode_sigs(
234 session().get_decode_signals());
235 vector< shared_ptr<Trace> > traces(
236 sigs.size() + decode_sigs.size());
238 vector< shared_ptr<Trace> > traces(sigs.size());
241 auto i = traces.begin();
242 i = copy(sigs.begin(), sigs.end(), i);
244 i = copy(decode_sigs.begin(), decode_sigs.end(), i);
247 stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets);
251 list<weak_ptr<SelectableItem> > View::selected_items() const
253 list<weak_ptr<SelectableItem> > items;
255 // List the selected signals
256 const vector< shared_ptr<Trace> > traces(get_traces());
257 for (shared_ptr<Trace> t : traces) {
263 // List the selected cursors
264 if (_cursors.first()->selected())
265 items.push_back(_cursors.first());
266 if (_cursors.second()->selected())
267 items.push_back(_cursors.second());
272 set< shared_ptr<SignalData> > View::get_visible_data() const
274 const vector< shared_ptr<Signal> > sigs(
275 session().get_signals());
277 // Make a set of all the visible data objects
278 set< shared_ptr<SignalData> > visible_data;
279 for (const shared_ptr<Signal> sig : sigs)
281 visible_data.insert(sig->data());
286 pair<double, double> View::get_time_extents() const
288 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
289 if (visible_data.empty())
290 return make_pair(0.0, 0.0);
292 double left_time = DBL_MAX, right_time = DBL_MIN;
293 for (const shared_ptr<SignalData> d : visible_data)
295 const double start_time = d->get_start_time();
296 double samplerate = d->samplerate();
297 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
299 left_time = min(left_time, start_time);
300 right_time = max(right_time, start_time +
301 d->get_max_sample_count() / samplerate);
304 assert(left_time < right_time);
305 return make_pair(left_time, right_time);
308 bool View::cursors_shown() const
310 return _show_cursors;
313 void View::show_cursors(bool show)
315 _show_cursors = show;
316 _cursorheader->update();
320 void View::centre_cursors()
322 const double time_width = _scale * _viewport->width();
323 _cursors.first()->set_time(_offset + time_width * 0.4);
324 _cursors.second()->set_time(_offset + time_width * 0.6);
325 _cursorheader->update();
329 CursorPair& View::cursors()
334 const CursorPair& View::cursors() const
339 const QPoint& View::hover_point() const
344 void View::normalize_layout()
346 const vector< shared_ptr<Trace> > traces(get_traces());
349 for (const shared_ptr<Trace> t : traces)
350 v_min = min(t->get_v_offset(), v_min);
352 const int delta = -min(v_min, 0);
353 for (shared_ptr<Trace> t : traces)
354 t->set_v_offset(t->get_v_offset() + delta);
356 verticalScrollBar()->setSliderPosition(_v_offset + delta);
357 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
360 void View::update_viewport()
366 void View::get_scroll_layout(double &length, double &offset) const
368 const pair<double, double> extents = get_time_extents();
369 length = (extents.second - extents.first) / _scale;
370 offset = _offset / _scale;
373 void View::set_zoom(double scale, int offset)
375 const double cursor_offset = _offset + _scale * offset;
376 const double new_scale = max(min(scale, MaxScale), MinScale);
377 const double new_offset = cursor_offset - new_scale * offset;
378 set_scale_offset(new_scale, new_offset);
381 void View::update_scroll()
385 const QSize areaSize = _viewport->size();
387 // Set the horizontal scroll bar
388 double length = 0, offset = 0;
389 get_scroll_layout(length, offset);
390 length = max(length - areaSize.width(), 0.0);
392 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
394 _updating_scroll = true;
396 if (length < MaxScrollValue) {
397 horizontalScrollBar()->setRange(0, length);
398 horizontalScrollBar()->setSliderPosition(offset);
400 horizontalScrollBar()->setRange(0, MaxScrollValue);
401 horizontalScrollBar()->setSliderPosition(
402 _offset * MaxScrollValue / (_scale * length));
405 _updating_scroll = false;
407 // Set the vertical scrollbar
408 verticalScrollBar()->setPageStep(areaSize.height());
409 verticalScrollBar()->setRange(0,
410 _viewport->get_total_height() + SignalMargin -
414 void View::update_layout()
417 _header->sizeHint().width() - pv::view::Header::BaselineOffset,
418 _ruler->sizeHint().height(), 0, 0);
419 _ruler->setGeometry(_viewport->x(), 0,
420 _viewport->width(), _viewport->y());
421 _cursorheader->setGeometry(
423 _ruler->sizeHint().height() - _cursorheader->sizeHint().height() / 2,
424 _viewport->width(), _cursorheader->sizeHint().height());
425 _header->setGeometry(0, _viewport->y(),
426 _header->sizeHint().width(), _viewport->height());
430 bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
431 const shared_ptr<Trace> &b)
435 return a->get_v_offset() < b->get_v_offset();
438 bool View::eventFilter(QObject *object, QEvent *event)
440 const QEvent::Type type = event->type();
441 if (type == QEvent::MouseMove) {
443 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
444 if (object == _viewport)
445 _hover_point = mouse_event->pos();
446 else if (object == _ruler || object == _cursorheader)
447 _hover_point = QPoint(mouse_event->x(), 0);
448 else if (object == _header)
449 _hover_point = QPoint(0, mouse_event->y());
451 _hover_point = QPoint(-1, -1);
453 hover_point_changed();
455 } else if (type == QEvent::Leave) {
456 _hover_point = QPoint(-1, -1);
457 hover_point_changed();
460 return QObject::eventFilter(object, event);
463 bool View::viewportEvent(QEvent *e)
467 case QEvent::MouseButtonPress:
468 case QEvent::MouseButtonRelease:
469 case QEvent::MouseButtonDblClick:
470 case QEvent::MouseMove:
472 case QEvent::TouchBegin:
473 case QEvent::TouchUpdate:
474 case QEvent::TouchEnd:
478 return QAbstractScrollArea::viewportEvent(e);
482 void View::resizeEvent(QResizeEvent*)
487 void View::h_scroll_value_changed(int value)
489 if (_updating_scroll)
492 const int range = horizontalScrollBar()->maximum();
493 if (range < MaxScrollValue)
494 _offset = _scale * value;
496 double length = 0, offset;
497 get_scroll_layout(length, offset);
498 _offset = _scale * length * value / MaxScrollValue;
502 _cursorheader->update();
506 void View::v_scroll_value_changed(int value)
513 void View::signals_changed()
515 int offset = SignalMargin + SignalHeight;
516 const vector< shared_ptr<Trace> > traces(get_traces());
517 for (shared_ptr<Trace> t : traces) {
519 t->set_v_offset(offset);
520 offset += SignalHeight + 2 * SignalMargin;
527 void View::data_updated()
529 // Update the scroll bars
536 void View::marker_time_changed()
538 _cursorheader->update();
542 void View::on_signals_moved()
548 void View::on_geometry_updated()