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_updated()),
96 this, SLOT(data_updated()));
98 connect(_cursors.first().get(), SIGNAL(time_changed()),
99 this, SLOT(marker_time_changed()));
100 connect(_cursors.second().get(), SIGNAL(time_changed()),
101 this, SLOT(marker_time_changed()));
103 connect(_header, SIGNAL(geometry_updated()),
104 this, SLOT(on_geometry_updated()));
105 connect(_header, SIGNAL(signals_moved()),
106 this, SLOT(on_signals_moved()));
108 connect(_header, SIGNAL(selection_changed()),
109 _ruler, SLOT(clear_selection()));
110 connect(_ruler, SIGNAL(selection_changed()),
111 _header, SLOT(clear_selection()));
113 connect(_header, SIGNAL(selection_changed()),
114 this, SIGNAL(selection_changed()));
115 connect(_ruler, SIGNAL(selection_changed()),
116 this, SIGNAL(selection_changed()));
118 setViewport(_viewport);
120 _viewport->installEventFilter(this);
121 _ruler->installEventFilter(this);
122 _header->installEventFilter(this);
125 SigSession& View::session()
130 const SigSession& View::session() const
135 double View::scale() const
140 double View::offset() const
145 int View::v_offset() const
150 void View::zoom(double steps)
152 zoom(steps, _viewport->width() / 2);
155 void View::zoom(double steps, int offset)
157 const double new_scale = max(min(_scale * pow(3.0/2.0, -steps),
158 MaxScale), MinScale);
159 set_zoom(new_scale, offset);
162 void View::zoom_fit()
164 const pair<double, double> extents = get_time_extents();
165 const double delta = extents.second - extents.first;
170 const int w = _viewport->width();
174 set_scale_offset(delta / w, extents.first);
177 void View::zoom_one_to_one()
179 using pv::data::SignalData;
181 const vector< shared_ptr<Signal> > sigs(
182 session().get_signals());
184 // Make a set of all the visible data objects
185 set< shared_ptr<SignalData> > visible_data;
186 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
188 visible_data.insert(sig->data());
190 if (visible_data.empty())
193 double samplerate = 0.0;
194 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data) {
196 samplerate = max(samplerate, d->samplerate());
199 if (samplerate == 0.0)
203 const int w = _viewport->width();
207 set_zoom(1.0 / samplerate, w / 2);
210 void View::set_scale_offset(double scale, double offset)
218 scale_offset_changed();
221 vector< shared_ptr<Trace> > View::get_traces() const
223 const vector< shared_ptr<Signal> > sigs(
224 session().get_signals());
226 const vector< shared_ptr<DecodeTrace> > decode_sigs(
227 session().get_decode_signals());
228 vector< shared_ptr<Trace> > traces(
229 sigs.size() + decode_sigs.size());
231 vector< shared_ptr<Trace> > traces(sigs.size());
234 vector< shared_ptr<Trace> >::iterator i = traces.begin();
235 i = copy(sigs.begin(), sigs.end(), i);
237 i = copy(decode_sigs.begin(), decode_sigs.end(), i);
240 stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets);
244 list<weak_ptr<SelectableItem> > View::selected_items() const
246 list<weak_ptr<SelectableItem> > items;
248 // List the selected signals
249 const vector< shared_ptr<Trace> > traces(get_traces());
250 BOOST_FOREACH (shared_ptr<Trace> t, traces) {
256 // List the selected cursors
257 if (_cursors.first()->selected())
258 items.push_back(_cursors.first());
259 if (_cursors.second()->selected())
260 items.push_back(_cursors.second());
265 set< shared_ptr<SignalData> > View::get_visible_data() const
267 const vector< shared_ptr<Signal> > sigs(
268 session().get_signals());
270 // Make a set of all the visible data objects
271 set< shared_ptr<SignalData> > visible_data;
272 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
274 visible_data.insert(sig->data());
279 pair<double, double> View::get_time_extents() const
281 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
282 if (visible_data.empty())
283 return make_pair(0.0, 0.0);
285 double left_time = DBL_MAX, right_time = DBL_MIN;
286 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data)
288 const double start_time = d->get_start_time();
289 double samplerate = d->samplerate();
290 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
292 left_time = min(left_time, start_time);
293 right_time = max(right_time, start_time +
294 d->get_max_sample_count() / samplerate);
297 assert(left_time < right_time);
298 return make_pair(left_time, right_time);
301 bool View::cursors_shown() const
303 return _show_cursors;
306 void View::show_cursors(bool show)
308 _show_cursors = show;
313 void View::centre_cursors()
315 const double time_width = _scale * _viewport->width();
316 _cursors.first()->set_time(_offset + time_width * 0.4);
317 _cursors.second()->set_time(_offset + time_width * 0.6);
322 CursorPair& View::cursors()
327 const CursorPair& View::cursors() const
332 const QPoint& View::hover_point() const
337 void View::normalize_layout()
339 const vector< shared_ptr<Trace> > traces(get_traces());
342 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
343 v_min = min(t->get_v_offset(), v_min);
345 const int delta = -min(v_min, 0);
346 BOOST_FOREACH(shared_ptr<Trace> t, traces)
347 t->set_v_offset(t->get_v_offset() + delta);
349 verticalScrollBar()->setSliderPosition(_v_offset + delta);
350 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
353 void View::update_viewport()
359 void View::get_scroll_layout(double &length, double &offset) const
361 const pair<double, double> extents = get_time_extents();
362 length = (extents.second - extents.first) / _scale;
363 offset = _offset / _scale;
366 void View::set_zoom(double scale, int offset)
368 const double cursor_offset = _offset + _scale * offset;
369 const double new_scale = max(min(scale, MaxScale), MinScale);
370 const double new_offset = cursor_offset - new_scale * offset;
371 set_scale_offset(new_scale, new_offset);
374 void View::update_scroll()
378 const QSize areaSize = _viewport->size();
380 // Set the horizontal scroll bar
381 double length = 0, offset = 0;
382 get_scroll_layout(length, offset);
383 length = max(length - areaSize.width(), 0.0);
385 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
387 _updating_scroll = true;
389 if (length < MaxScrollValue) {
390 horizontalScrollBar()->setRange(0, length);
391 horizontalScrollBar()->setSliderPosition(offset);
393 horizontalScrollBar()->setRange(0, MaxScrollValue);
394 horizontalScrollBar()->setSliderPosition(
395 _offset * MaxScrollValue / (_scale * length));
398 _updating_scroll = false;
400 // Set the vertical scrollbar
401 verticalScrollBar()->setPageStep(areaSize.height());
402 verticalScrollBar()->setRange(0,
403 _viewport->get_total_height() + SignalMargin -
407 void View::update_layout()
409 setViewportMargins(_header->sizeHint().width(),
410 _ruler->sizeHint().height(), 0, 0);
411 _ruler->setGeometry(_viewport->x(), 0,
412 _viewport->width(), _viewport->y());
413 _header->setGeometry(0, _viewport->y(),
414 _viewport->x(), _viewport->height());
418 bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
419 const shared_ptr<Trace> &b)
423 return a->get_v_offset() < b->get_v_offset();
426 bool View::eventFilter(QObject *object, QEvent *event)
428 const QEvent::Type type = event->type();
429 if (type == QEvent::MouseMove) {
431 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
432 if (object == _viewport)
433 _hover_point = mouse_event->pos();
434 else if (object == _ruler)
435 _hover_point = QPoint(mouse_event->x(), 0);
436 else if (object == _header)
437 _hover_point = QPoint(0, mouse_event->y());
439 _hover_point = QPoint(-1, -1);
441 hover_point_changed();
443 } else if (type == QEvent::Leave) {
444 _hover_point = QPoint(-1, -1);
445 hover_point_changed();
448 return QObject::eventFilter(object, event);
451 bool View::viewportEvent(QEvent *e)
455 case QEvent::MouseButtonPress:
456 case QEvent::MouseButtonRelease:
457 case QEvent::MouseButtonDblClick:
458 case QEvent::MouseMove:
463 return QAbstractScrollArea::viewportEvent(e);
467 void View::resizeEvent(QResizeEvent*)
472 void View::h_scroll_value_changed(int value)
474 if (_updating_scroll)
477 const int range = horizontalScrollBar()->maximum();
478 if (range < MaxScrollValue)
479 _offset = _scale * value;
481 double length = 0, offset;
482 get_scroll_layout(length, offset);
483 _offset = _scale * length * value / MaxScrollValue;
490 void View::v_scroll_value_changed(int value)
497 void View::signals_changed()
499 int offset = SignalMargin + SignalHeight;
500 const vector< shared_ptr<Trace> > traces(get_traces());
501 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
503 t->set_v_offset(offset);
504 offset += SignalHeight + 2 * SignalMargin;
511 void View::data_updated()
513 // Update the scroll bars
520 void View::marker_time_changed()
526 void View::on_signals_moved()
532 void View::on_geometry_updated()