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
21 #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 namespace boost;
52 const double View::MaxScale = 1e9;
53 const double View::MinScale = 1e-15;
55 const int View::MaxScrollValue = INT_MAX / 2;
57 const int View::SignalHeight = 30;
58 const int View::SignalMargin = 10;
59 const int View::SignalSnapGridSize = 10;
61 const QColor View::CursorAreaColour(220, 231, 243);
63 const QSizeF View::LabelPadding(4, 0);
65 View::View(SigSession &session, QWidget *parent) :
66 QAbstractScrollArea(parent),
68 _viewport(new Viewport(*this)),
69 _ruler(new Ruler(*this)),
70 _header(new Header(*this)),
75 _updating_scroll(false),
80 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
81 this, SLOT(h_scroll_value_changed(int)));
82 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
83 this, SLOT(v_scroll_value_changed(int)));
85 connect(&_session, SIGNAL(signals_changed()),
86 this, SLOT(signals_changed()));
87 connect(&_session, SIGNAL(data_updated()),
88 this, SLOT(data_updated()));
90 connect(_cursors.first().get(), SIGNAL(time_changed()),
91 this, SLOT(marker_time_changed()));
92 connect(_cursors.second().get(), SIGNAL(time_changed()),
93 this, SLOT(marker_time_changed()));
95 connect(_header, SIGNAL(geometry_updated()),
96 this, SLOT(on_geometry_updated()));
97 connect(_header, SIGNAL(signals_moved()),
98 this, SLOT(on_signals_moved()));
100 connect(_header, SIGNAL(selection_changed()),
101 _ruler, SLOT(clear_selection()));
102 connect(_ruler, SIGNAL(selection_changed()),
103 _header, SLOT(clear_selection()));
105 connect(_header, SIGNAL(selection_changed()),
106 this, SIGNAL(selection_changed()));
107 connect(_ruler, SIGNAL(selection_changed()),
108 this, SIGNAL(selection_changed()));
110 setViewport(_viewport);
112 _viewport->installEventFilter(this);
113 _ruler->installEventFilter(this);
114 _header->installEventFilter(this);
117 SigSession& View::session()
122 const SigSession& View::session() const
127 double View::scale() const
132 double View::offset() const
137 int View::v_offset() const
142 void View::zoom(double steps)
144 zoom(steps, _viewport->width() / 2);
147 void View::zoom(double steps, int offset)
149 const double new_scale = max(min(_scale * pow(3.0/2.0, -steps),
150 MaxScale), MinScale);
151 set_zoom(new_scale, offset);
154 void View::zoom_fit()
156 using pv::data::SignalData;
158 const vector< shared_ptr<Signal> > sigs(
159 session().get_signals());
161 // Make a set of all the visible data objects
162 set< shared_ptr<SignalData> > visible_data;
163 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
165 visible_data.insert(sig->data());
167 if (visible_data.empty())
170 double left_time = DBL_MAX, right_time = DBL_MIN;
171 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data)
173 const double start_time = d->get_start_time();
174 left_time = min(left_time, start_time);
175 right_time = max(right_time, start_time +
176 d->get_max_sample_count() / d->samplerate());
179 assert(left_time < right_time);
180 if (right_time - left_time < 1e-12)
184 const int w = _viewport->width();
188 set_scale_offset((right_time - left_time) / w, left_time);
191 void View::zoom_one_to_one()
193 using pv::data::SignalData;
195 const vector< shared_ptr<Signal> > sigs(
196 session().get_signals());
198 // Make a set of all the visible data objects
199 set< shared_ptr<SignalData> > visible_data;
200 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
202 visible_data.insert(sig->data());
204 if (visible_data.empty())
207 double samplerate = 0.0;
208 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data) {
210 samplerate = max(samplerate, d->samplerate());
213 if (samplerate == 0.0)
217 const int w = _viewport->width();
221 set_zoom(1.0 / samplerate, w / 2);
224 void View::set_scale_offset(double scale, double offset)
232 scale_offset_changed();
235 vector< shared_ptr<Trace> > View::get_traces() const
237 const vector< shared_ptr<Signal> > sigs(
238 session().get_signals());
239 const vector< shared_ptr<DecodeTrace> > decode_sigs(
240 session().get_decode_signals());
241 vector< shared_ptr<Trace> > traces(
242 sigs.size() + decode_sigs.size());
244 vector< shared_ptr<Trace> >::iterator i = traces.begin();
245 i = copy(sigs.begin(), sigs.end(), i);
246 i = copy(decode_sigs.begin(), decode_sigs.end(), i);
248 stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets);
252 list<weak_ptr<SelectableItem> > View::selected_items() const
254 list<weak_ptr<SelectableItem> > items;
256 // List the selected signals
257 const vector< shared_ptr<Trace> > traces(get_traces());
258 BOOST_FOREACH (shared_ptr<Trace> t, traces) {
264 // List the selected cursors
265 if (_cursors.first()->selected())
266 items.push_back(_cursors.first());
267 if (_cursors.second()->selected())
268 items.push_back(_cursors.second());
273 bool View::cursors_shown() const
275 return _show_cursors;
278 void View::show_cursors(bool show)
280 _show_cursors = show;
285 void View::centre_cursors()
287 const double time_width = _scale * _viewport->width();
288 _cursors.first()->set_time(_offset + time_width * 0.4);
289 _cursors.second()->set_time(_offset + time_width * 0.6);
294 CursorPair& View::cursors()
299 const CursorPair& View::cursors() const
304 const QPoint& View::hover_point() const
309 void View::normalize_layout()
311 const vector< shared_ptr<Trace> > traces(get_traces());
314 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
315 v_min = min(t->get_v_offset(), v_min);
317 const int delta = -min(v_min, 0);
318 BOOST_FOREACH(shared_ptr<Trace> t, traces)
319 t->set_v_offset(t->get_v_offset() + delta);
321 verticalScrollBar()->setSliderPosition(_v_offset + delta);
322 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
325 void View::update_viewport()
331 void View::get_scroll_layout(double &length, double &offset) const
333 const shared_ptr<data::SignalData> sig_data = _session.get_data();
337 length = _data_length / (sig_data->samplerate() * _scale);
338 offset = _offset / _scale;
341 void View::set_zoom(double scale, int offset)
343 const double cursor_offset = _offset + _scale * offset;
344 const double new_scale = max(min(scale, MaxScale), MinScale);
345 const double new_offset = cursor_offset - new_scale * offset;
346 set_scale_offset(new_scale, new_offset);
349 void View::update_scroll()
353 const QSize areaSize = _viewport->size();
355 // Set the horizontal scroll bar
356 double length = 0, offset = 0;
357 get_scroll_layout(length, offset);
358 length = max(length - areaSize.width(), 0.0);
360 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
362 _updating_scroll = true;
364 if (length < MaxScrollValue) {
365 horizontalScrollBar()->setRange(0, length);
366 horizontalScrollBar()->setSliderPosition(offset);
368 horizontalScrollBar()->setRange(0, MaxScrollValue);
369 horizontalScrollBar()->setSliderPosition(
370 _offset * MaxScrollValue / (_scale * length));
373 _updating_scroll = false;
375 // Set the vertical scrollbar
376 verticalScrollBar()->setPageStep(areaSize.height());
377 verticalScrollBar()->setRange(0,
378 _viewport->get_total_height() + SignalMargin -
382 void View::update_layout()
384 setViewportMargins(_header->sizeHint().width(),
385 _ruler->sizeHint().height(), 0, 0);
386 _ruler->setGeometry(_viewport->x(), 0,
387 _viewport->width(), _viewport->y());
388 _header->setGeometry(0, _viewport->y(),
389 _viewport->x(), _viewport->height());
393 bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
394 const shared_ptr<Trace> &b)
398 return a->get_v_offset() < b->get_v_offset();
401 bool View::eventFilter(QObject *object, QEvent *event)
403 const QEvent::Type type = event->type();
404 if (type == QEvent::MouseMove) {
406 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
407 if (object == _viewport)
408 _hover_point = mouse_event->pos();
409 else if (object == _ruler)
410 _hover_point = QPoint(mouse_event->x(), 0);
411 else if (object == _header)
412 _hover_point = QPoint(0, mouse_event->y());
414 _hover_point = QPoint(-1, -1);
416 hover_point_changed();
418 } else if (type == QEvent::Leave) {
419 _hover_point = QPoint(-1, -1);
420 hover_point_changed();
423 return QObject::eventFilter(object, event);
426 bool View::viewportEvent(QEvent *e)
430 case QEvent::MouseButtonPress:
431 case QEvent::MouseButtonRelease:
432 case QEvent::MouseButtonDblClick:
433 case QEvent::MouseMove:
438 return QAbstractScrollArea::viewportEvent(e);
442 void View::resizeEvent(QResizeEvent*)
447 void View::h_scroll_value_changed(int value)
449 if (_updating_scroll)
452 const int range = horizontalScrollBar()->maximum();
453 if (range < MaxScrollValue)
454 _offset = _scale * value;
456 double length = 0, offset;
457 get_scroll_layout(length, offset);
458 _offset = _scale * length * value / MaxScrollValue;
465 void View::v_scroll_value_changed(int value)
472 void View::signals_changed()
474 int offset = SignalMargin + SignalHeight;
475 const vector< shared_ptr<Trace> > traces(get_traces());
476 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
478 t->set_v_offset(offset);
479 offset += SignalHeight + 2 * SignalMargin;
486 void View::data_updated()
488 // Get the new data length
490 shared_ptr<data::Logic> sig_data = _session.get_data();
492 deque< shared_ptr<data::LogicSnapshot> > &snapshots =
493 sig_data->get_snapshots();
494 BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots)
496 _data_length = max(_data_length,
497 s->get_sample_count());
500 // Update the scroll bars
507 void View::marker_time_changed()
513 void View::on_signals_moved()
519 void View::on_geometry_updated()