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>
31 #include <boost/foreach.hpp>
34 #include <QMouseEvent>
37 #include "decodetrace.h"
44 #include "pv/sigsession.h"
45 #include "pv/data/logic.h"
46 #include "pv/data/logicsnapshot.h"
48 using namespace boost;
54 const double View::MaxScale = 1e9;
55 const double View::MinScale = 1e-15;
57 const int View::MaxScrollValue = INT_MAX / 2;
59 const int View::SignalHeight = 30;
60 const int View::SignalMargin = 10;
61 const int View::SignalSnapGridSize = 10;
63 const QColor View::CursorAreaColour(220, 231, 243);
65 const QSizeF View::LabelPadding(4, 0);
67 View::View(SigSession &session, QWidget *parent) :
68 QAbstractScrollArea(parent),
70 _viewport(new Viewport(*this)),
71 _ruler(new Ruler(*this)),
72 _header(new Header(*this)),
77 _updating_scroll(false),
82 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
83 this, SLOT(h_scroll_value_changed(int)));
84 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
85 this, SLOT(v_scroll_value_changed(int)));
87 connect(&_session, SIGNAL(signals_changed()),
88 this, SLOT(signals_changed()));
89 connect(&_session, SIGNAL(data_updated()),
90 this, SLOT(data_updated()));
92 connect(_cursors.first().get(), SIGNAL(time_changed()),
93 this, SLOT(marker_time_changed()));
94 connect(_cursors.second().get(), SIGNAL(time_changed()),
95 this, SLOT(marker_time_changed()));
97 connect(_header, SIGNAL(geometry_updated()),
98 this, SLOT(on_geometry_updated()));
99 connect(_header, SIGNAL(signals_moved()),
100 this, SLOT(on_signals_moved()));
102 connect(_header, SIGNAL(selection_changed()),
103 _ruler, SLOT(clear_selection()));
104 connect(_ruler, SIGNAL(selection_changed()),
105 _header, SLOT(clear_selection()));
107 connect(_header, SIGNAL(selection_changed()),
108 this, SIGNAL(selection_changed()));
109 connect(_ruler, SIGNAL(selection_changed()),
110 this, SIGNAL(selection_changed()));
112 setViewport(_viewport);
114 _viewport->installEventFilter(this);
115 _ruler->installEventFilter(this);
116 _header->installEventFilter(this);
119 SigSession& View::session()
124 const SigSession& View::session() const
129 double View::scale() const
134 double View::offset() const
139 int View::v_offset() const
144 void View::zoom(double steps)
146 zoom(steps, _viewport->width() / 2);
149 void View::zoom(double steps, int offset)
151 const double new_scale = max(min(_scale * pow(3.0/2.0, -steps),
152 MaxScale), MinScale);
153 set_zoom(new_scale, offset);
156 void View::zoom_fit()
158 using pv::data::SignalData;
160 const vector< shared_ptr<Signal> > sigs(
161 session().get_signals());
163 // Make a set of all the visible data objects
164 set< shared_ptr<SignalData> > visible_data;
165 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
167 visible_data.insert(sig->data());
169 if (visible_data.empty())
172 double left_time = DBL_MAX, right_time = DBL_MIN;
173 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data)
175 const double start_time = d->get_start_time();
176 left_time = min(left_time, start_time);
177 right_time = max(right_time, start_time +
178 d->get_max_sample_count() / d->samplerate());
181 assert(left_time < right_time);
182 if (right_time - left_time < 1e-12)
186 const int w = _viewport->width();
190 set_scale_offset((right_time - left_time) / w, left_time);
193 void View::zoom_one_to_one()
195 using pv::data::SignalData;
197 const vector< shared_ptr<Signal> > sigs(
198 session().get_signals());
200 // Make a set of all the visible data objects
201 set< shared_ptr<SignalData> > visible_data;
202 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
204 visible_data.insert(sig->data());
206 if (visible_data.empty())
209 double samplerate = 0.0;
210 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data) {
212 samplerate = max(samplerate, d->samplerate());
215 if (samplerate == 0.0)
219 const int w = _viewport->width();
223 set_zoom(1.0 / samplerate, w / 2);
226 void View::set_scale_offset(double scale, double offset)
234 scale_offset_changed();
237 vector< shared_ptr<Trace> > View::get_traces() const
239 const vector< shared_ptr<Signal> > sigs(
240 session().get_signals());
242 const vector< shared_ptr<DecodeTrace> > decode_sigs(
243 session().get_decode_signals());
244 vector< shared_ptr<Trace> > traces(
245 sigs.size() + decode_sigs.size());
247 vector< shared_ptr<Trace> > traces(sigs.size());
250 vector< shared_ptr<Trace> >::iterator i = traces.begin();
251 i = copy(sigs.begin(), sigs.end(), i);
253 i = copy(decode_sigs.begin(), decode_sigs.end(), i);
256 stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets);
260 list<weak_ptr<SelectableItem> > View::selected_items() const
262 list<weak_ptr<SelectableItem> > items;
264 // List the selected signals
265 const vector< shared_ptr<Trace> > traces(get_traces());
266 BOOST_FOREACH (shared_ptr<Trace> t, traces) {
272 // List the selected cursors
273 if (_cursors.first()->selected())
274 items.push_back(_cursors.first());
275 if (_cursors.second()->selected())
276 items.push_back(_cursors.second());
281 bool View::cursors_shown() const
283 return _show_cursors;
286 void View::show_cursors(bool show)
288 _show_cursors = show;
293 void View::centre_cursors()
295 const double time_width = _scale * _viewport->width();
296 _cursors.first()->set_time(_offset + time_width * 0.4);
297 _cursors.second()->set_time(_offset + time_width * 0.6);
302 CursorPair& View::cursors()
307 const CursorPair& View::cursors() const
312 const QPoint& View::hover_point() const
317 void View::normalize_layout()
319 const vector< shared_ptr<Trace> > traces(get_traces());
322 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
323 v_min = min(t->get_v_offset(), v_min);
325 const int delta = -min(v_min, 0);
326 BOOST_FOREACH(shared_ptr<Trace> t, traces)
327 t->set_v_offset(t->get_v_offset() + delta);
329 verticalScrollBar()->setSliderPosition(_v_offset + delta);
330 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
333 void View::update_viewport()
339 void View::get_scroll_layout(double &length, double &offset) const
341 const shared_ptr<data::SignalData> sig_data = _session.get_data();
345 length = _data_length / (sig_data->samplerate() * _scale);
346 offset = _offset / _scale;
349 void View::set_zoom(double scale, int offset)
351 const double cursor_offset = _offset + _scale * offset;
352 const double new_scale = max(min(scale, MaxScale), MinScale);
353 const double new_offset = cursor_offset - new_scale * offset;
354 set_scale_offset(new_scale, new_offset);
357 void View::update_scroll()
361 const QSize areaSize = _viewport->size();
363 // Set the horizontal scroll bar
364 double length = 0, offset = 0;
365 get_scroll_layout(length, offset);
366 length = max(length - areaSize.width(), 0.0);
368 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
370 _updating_scroll = true;
372 if (length < MaxScrollValue) {
373 horizontalScrollBar()->setRange(0, length);
374 horizontalScrollBar()->setSliderPosition(offset);
376 horizontalScrollBar()->setRange(0, MaxScrollValue);
377 horizontalScrollBar()->setSliderPosition(
378 _offset * MaxScrollValue / (_scale * length));
381 _updating_scroll = false;
383 // Set the vertical scrollbar
384 verticalScrollBar()->setPageStep(areaSize.height());
385 verticalScrollBar()->setRange(0,
386 _viewport->get_total_height() + SignalMargin -
390 void View::update_layout()
392 setViewportMargins(_header->sizeHint().width(),
393 _ruler->sizeHint().height(), 0, 0);
394 _ruler->setGeometry(_viewport->x(), 0,
395 _viewport->width(), _viewport->y());
396 _header->setGeometry(0, _viewport->y(),
397 _viewport->x(), _viewport->height());
401 bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
402 const shared_ptr<Trace> &b)
406 return a->get_v_offset() < b->get_v_offset();
409 bool View::eventFilter(QObject *object, QEvent *event)
411 const QEvent::Type type = event->type();
412 if (type == QEvent::MouseMove) {
414 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
415 if (object == _viewport)
416 _hover_point = mouse_event->pos();
417 else if (object == _ruler)
418 _hover_point = QPoint(mouse_event->x(), 0);
419 else if (object == _header)
420 _hover_point = QPoint(0, mouse_event->y());
422 _hover_point = QPoint(-1, -1);
424 hover_point_changed();
426 } else if (type == QEvent::Leave) {
427 _hover_point = QPoint(-1, -1);
428 hover_point_changed();
431 return QObject::eventFilter(object, event);
434 bool View::viewportEvent(QEvent *e)
438 case QEvent::MouseButtonPress:
439 case QEvent::MouseButtonRelease:
440 case QEvent::MouseButtonDblClick:
441 case QEvent::MouseMove:
446 return QAbstractScrollArea::viewportEvent(e);
450 void View::resizeEvent(QResizeEvent*)
455 void View::h_scroll_value_changed(int value)
457 if (_updating_scroll)
460 const int range = horizontalScrollBar()->maximum();
461 if (range < MaxScrollValue)
462 _offset = _scale * value;
464 double length = 0, offset;
465 get_scroll_layout(length, offset);
466 _offset = _scale * length * value / MaxScrollValue;
473 void View::v_scroll_value_changed(int value)
480 void View::signals_changed()
482 int offset = SignalMargin + SignalHeight;
483 const vector< shared_ptr<Trace> > traces(get_traces());
484 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
486 t->set_v_offset(offset);
487 offset += SignalHeight + 2 * SignalMargin;
494 void View::data_updated()
496 // Get the new data length
498 shared_ptr<data::Logic> sig_data = _session.get_data();
500 deque< shared_ptr<data::LogicSnapshot> > &snapshots =
501 sig_data->get_snapshots();
502 BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots)
504 _data_length = max(_data_length,
505 s->get_sample_count());
508 // Update the scroll bars
515 void View::marker_time_changed()
521 void View::on_signals_moved()
527 void View::on_geometry_updated()