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>
29 #include <boost/foreach.hpp>
32 #include <QMouseEvent>
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_ptr;
47 using boost::weak_ptr;
48 using pv::data::SignalData;
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 _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 _ruler, SLOT(clear_selection()));
112 connect(_ruler, SIGNAL(selection_changed()),
113 _header, SLOT(clear_selection()));
115 connect(_header, SIGNAL(selection_changed()),
116 this, SIGNAL(selection_changed()));
117 connect(_ruler, SIGNAL(selection_changed()),
118 this, SIGNAL(selection_changed()));
120 setViewport(_viewport);
122 _viewport->installEventFilter(this);
123 _ruler->installEventFilter(this);
124 _header->installEventFilter(this);
126 // Trigger the initial event manually. The default device has signals
127 // which were created before this object came into being
131 SigSession& View::session()
136 const SigSession& View::session() const
141 double View::scale() const
146 double View::offset() const
151 int View::v_offset() const
156 void View::zoom(double steps)
158 zoom(steps, _viewport->width() / 2);
161 void View::zoom(double steps, int offset)
163 set_zoom(_scale * pow(3.0/2.0, -steps), offset);
166 void View::zoom_fit()
168 const pair<double, double> extents = get_time_extents();
169 const double delta = extents.second - extents.first;
174 const int w = _viewport->width();
178 const double scale = max(min(delta / w, MaxScale), MinScale);
179 set_scale_offset(scale, extents.first);
182 void View::zoom_one_to_one()
184 using pv::data::SignalData;
186 const vector< shared_ptr<Signal> > sigs(
187 session().get_signals());
189 // Make a set of all the visible data objects
190 set< shared_ptr<SignalData> > visible_data = get_visible_data();
191 if (visible_data.empty())
194 double samplerate = 0.0;
195 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data) {
197 samplerate = max(samplerate, d->samplerate());
200 if (samplerate == 0.0)
204 const int w = _viewport->width();
208 set_zoom(1.0 / samplerate, w / 2);
211 void View::set_scale_offset(double scale, double offset)
219 scale_offset_changed();
222 vector< shared_ptr<Trace> > View::get_traces() const
224 const vector< shared_ptr<Signal> > sigs(
225 session().get_signals());
227 const vector< shared_ptr<DecodeTrace> > decode_sigs(
228 session().get_decode_signals());
229 vector< shared_ptr<Trace> > traces(
230 sigs.size() + decode_sigs.size());
232 vector< shared_ptr<Trace> > traces(sigs.size());
235 vector< shared_ptr<Trace> >::iterator i = traces.begin();
236 i = copy(sigs.begin(), sigs.end(), i);
238 i = copy(decode_sigs.begin(), decode_sigs.end(), i);
241 stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets);
245 list<weak_ptr<SelectableItem> > View::selected_items() const
247 list<weak_ptr<SelectableItem> > items;
249 // List the selected signals
250 const vector< shared_ptr<Trace> > traces(get_traces());
251 BOOST_FOREACH (shared_ptr<Trace> t, traces) {
257 // List the selected cursors
258 if (_cursors.first()->selected())
259 items.push_back(_cursors.first());
260 if (_cursors.second()->selected())
261 items.push_back(_cursors.second());
266 set< shared_ptr<SignalData> > View::get_visible_data() const
268 const vector< shared_ptr<Signal> > sigs(
269 session().get_signals());
271 // Make a set of all the visible data objects
272 set< shared_ptr<SignalData> > visible_data;
273 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
275 visible_data.insert(sig->data());
280 pair<double, double> View::get_time_extents() const
282 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
283 if (visible_data.empty())
284 return make_pair(0.0, 0.0);
286 double left_time = DBL_MAX, right_time = DBL_MIN;
287 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data)
289 const double start_time = d->get_start_time();
290 double samplerate = d->samplerate();
291 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
293 left_time = min(left_time, start_time);
294 right_time = max(right_time, start_time +
295 d->get_max_sample_count() / samplerate);
298 assert(left_time < right_time);
299 return make_pair(left_time, right_time);
302 bool View::cursors_shown() const
304 return _show_cursors;
307 void View::show_cursors(bool show)
309 _show_cursors = show;
314 void View::centre_cursors()
316 const double time_width = _scale * _viewport->width();
317 _cursors.first()->set_time(_offset + time_width * 0.4);
318 _cursors.second()->set_time(_offset + time_width * 0.6);
323 CursorPair& View::cursors()
328 const CursorPair& View::cursors() const
333 const QPoint& View::hover_point() const
338 void View::normalize_layout()
340 const vector< shared_ptr<Trace> > traces(get_traces());
343 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
344 v_min = min(t->get_v_offset(), v_min);
346 const int delta = -min(v_min, 0);
347 BOOST_FOREACH(shared_ptr<Trace> t, traces)
348 t->set_v_offset(t->get_v_offset() + delta);
350 verticalScrollBar()->setSliderPosition(_v_offset + delta);
351 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
354 void View::update_viewport()
360 void View::get_scroll_layout(double &length, double &offset) const
362 const pair<double, double> extents = get_time_extents();
363 length = (extents.second - extents.first) / _scale;
364 offset = _offset / _scale;
367 void View::set_zoom(double scale, int offset)
369 const double cursor_offset = _offset + _scale * offset;
370 const double new_scale = max(min(scale, MaxScale), MinScale);
371 const double new_offset = cursor_offset - new_scale * offset;
372 set_scale_offset(new_scale, new_offset);
375 void View::update_scroll()
379 const QSize areaSize = _viewport->size();
381 // Set the horizontal scroll bar
382 double length = 0, offset = 0;
383 get_scroll_layout(length, offset);
384 length = max(length - areaSize.width(), 0.0);
386 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
388 _updating_scroll = true;
390 if (length < MaxScrollValue) {
391 horizontalScrollBar()->setRange(0, length);
392 horizontalScrollBar()->setSliderPosition(offset);
394 horizontalScrollBar()->setRange(0, MaxScrollValue);
395 horizontalScrollBar()->setSliderPosition(
396 _offset * MaxScrollValue / (_scale * length));
399 _updating_scroll = false;
401 // Set the vertical scrollbar
402 verticalScrollBar()->setPageStep(areaSize.height());
403 verticalScrollBar()->setRange(0,
404 _viewport->get_total_height() + SignalMargin -
408 void View::update_layout()
410 setViewportMargins(_header->sizeHint().width(),
411 _ruler->sizeHint().height(), 0, 0);
412 _ruler->setGeometry(_viewport->x(), 0,
413 _viewport->width(), _viewport->y());
414 _header->setGeometry(0, _viewport->y(),
415 _viewport->x(), _viewport->height());
419 bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
420 const shared_ptr<Trace> &b)
424 return a->get_v_offset() < b->get_v_offset();
427 bool View::eventFilter(QObject *object, QEvent *event)
429 const QEvent::Type type = event->type();
430 if (type == QEvent::MouseMove) {
432 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
433 if (object == _viewport)
434 _hover_point = mouse_event->pos();
435 else if (object == _ruler)
436 _hover_point = QPoint(mouse_event->x(), 0);
437 else if (object == _header)
438 _hover_point = QPoint(0, mouse_event->y());
440 _hover_point = QPoint(-1, -1);
442 hover_point_changed();
444 } else if (type == QEvent::Leave) {
445 _hover_point = QPoint(-1, -1);
446 hover_point_changed();
449 return QObject::eventFilter(object, event);
452 bool View::viewportEvent(QEvent *e)
456 case QEvent::MouseButtonPress:
457 case QEvent::MouseButtonRelease:
458 case QEvent::MouseButtonDblClick:
459 case QEvent::MouseMove:
464 return QAbstractScrollArea::viewportEvent(e);
468 void View::resizeEvent(QResizeEvent*)
473 void View::h_scroll_value_changed(int value)
475 if (_updating_scroll)
478 const int range = horizontalScrollBar()->maximum();
479 if (range < MaxScrollValue)
480 _offset = _scale * value;
482 double length = 0, offset;
483 get_scroll_layout(length, offset);
484 _offset = _scale * length * value / MaxScrollValue;
491 void View::v_scroll_value_changed(int value)
498 void View::signals_changed()
500 int offset = SignalMargin + SignalHeight;
501 const vector< shared_ptr<Trace> > traces(get_traces());
502 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
504 t->set_v_offset(offset);
505 offset += SignalHeight + 2 * SignalMargin;
512 void View::data_updated()
514 // Update the scroll bars
521 void View::marker_time_changed()
527 void View::on_signals_moved()
533 void View::on_geometry_updated()