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 boost::shared_ptr;
49 using boost::weak_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 _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 using pv::data::SignalData;
166 const vector< shared_ptr<Signal> > sigs(
167 session().get_signals());
169 // Make a set of all the visible data objects
170 set< shared_ptr<SignalData> > visible_data;
171 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
173 visible_data.insert(sig->data());
175 if (visible_data.empty())
178 double left_time = DBL_MAX, right_time = DBL_MIN;
179 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data)
181 const double start_time = d->get_start_time();
182 left_time = min(left_time, start_time);
183 right_time = max(right_time, start_time +
184 d->get_max_sample_count() / d->samplerate());
187 assert(left_time < right_time);
188 if (right_time - left_time < 1e-12)
192 const int w = _viewport->width();
196 set_scale_offset((right_time - left_time) / w, left_time);
199 void View::zoom_one_to_one()
201 using pv::data::SignalData;
203 const vector< shared_ptr<Signal> > sigs(
204 session().get_signals());
206 // Make a set of all the visible data objects
207 set< shared_ptr<SignalData> > visible_data;
208 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
210 visible_data.insert(sig->data());
212 if (visible_data.empty())
215 double samplerate = 0.0;
216 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data) {
218 samplerate = max(samplerate, d->samplerate());
221 if (samplerate == 0.0)
225 const int w = _viewport->width();
229 set_zoom(1.0 / samplerate, w / 2);
232 void View::set_scale_offset(double scale, double offset)
240 scale_offset_changed();
243 vector< shared_ptr<Trace> > View::get_traces() const
245 const vector< shared_ptr<Signal> > sigs(
246 session().get_signals());
248 const vector< shared_ptr<DecodeTrace> > decode_sigs(
249 session().get_decode_signals());
250 vector< shared_ptr<Trace> > traces(
251 sigs.size() + decode_sigs.size());
253 vector< shared_ptr<Trace> > traces(sigs.size());
256 vector< shared_ptr<Trace> >::iterator i = traces.begin();
257 i = copy(sigs.begin(), sigs.end(), i);
259 i = copy(decode_sigs.begin(), decode_sigs.end(), i);
262 stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets);
266 list<weak_ptr<SelectableItem> > View::selected_items() const
268 list<weak_ptr<SelectableItem> > items;
270 // List the selected signals
271 const vector< shared_ptr<Trace> > traces(get_traces());
272 BOOST_FOREACH (shared_ptr<Trace> t, traces) {
278 // List the selected cursors
279 if (_cursors.first()->selected())
280 items.push_back(_cursors.first());
281 if (_cursors.second()->selected())
282 items.push_back(_cursors.second());
287 bool View::cursors_shown() const
289 return _show_cursors;
292 void View::show_cursors(bool show)
294 _show_cursors = show;
299 void View::centre_cursors()
301 const double time_width = _scale * _viewport->width();
302 _cursors.first()->set_time(_offset + time_width * 0.4);
303 _cursors.second()->set_time(_offset + time_width * 0.6);
308 CursorPair& View::cursors()
313 const CursorPair& View::cursors() const
318 const QPoint& View::hover_point() const
323 void View::normalize_layout()
325 const vector< shared_ptr<Trace> > traces(get_traces());
328 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
329 v_min = min(t->get_v_offset(), v_min);
331 const int delta = -min(v_min, 0);
332 BOOST_FOREACH(shared_ptr<Trace> t, traces)
333 t->set_v_offset(t->get_v_offset() + delta);
335 verticalScrollBar()->setSliderPosition(_v_offset + delta);
336 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
339 void View::update_viewport()
345 void View::get_scroll_layout(double &length, double &offset) const
347 const shared_ptr<data::SignalData> sig_data = _session.get_data();
351 length = _data_length / (sig_data->samplerate() * _scale);
352 offset = _offset / _scale;
355 void View::set_zoom(double scale, int offset)
357 const double cursor_offset = _offset + _scale * offset;
358 const double new_scale = max(min(scale, MaxScale), MinScale);
359 const double new_offset = cursor_offset - new_scale * offset;
360 set_scale_offset(new_scale, new_offset);
363 void View::update_scroll()
367 const QSize areaSize = _viewport->size();
369 // Set the horizontal scroll bar
370 double length = 0, offset = 0;
371 get_scroll_layout(length, offset);
372 length = max(length - areaSize.width(), 0.0);
374 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
376 _updating_scroll = true;
378 if (length < MaxScrollValue) {
379 horizontalScrollBar()->setRange(0, length);
380 horizontalScrollBar()->setSliderPosition(offset);
382 horizontalScrollBar()->setRange(0, MaxScrollValue);
383 horizontalScrollBar()->setSliderPosition(
384 _offset * MaxScrollValue / (_scale * length));
387 _updating_scroll = false;
389 // Set the vertical scrollbar
390 verticalScrollBar()->setPageStep(areaSize.height());
391 verticalScrollBar()->setRange(0,
392 _viewport->get_total_height() + SignalMargin -
396 void View::update_layout()
398 setViewportMargins(_header->sizeHint().width(),
399 _ruler->sizeHint().height(), 0, 0);
400 _ruler->setGeometry(_viewport->x(), 0,
401 _viewport->width(), _viewport->y());
402 _header->setGeometry(0, _viewport->y(),
403 _viewport->x(), _viewport->height());
407 bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
408 const shared_ptr<Trace> &b)
412 return a->get_v_offset() < b->get_v_offset();
415 bool View::eventFilter(QObject *object, QEvent *event)
417 const QEvent::Type type = event->type();
418 if (type == QEvent::MouseMove) {
420 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
421 if (object == _viewport)
422 _hover_point = mouse_event->pos();
423 else if (object == _ruler)
424 _hover_point = QPoint(mouse_event->x(), 0);
425 else if (object == _header)
426 _hover_point = QPoint(0, mouse_event->y());
428 _hover_point = QPoint(-1, -1);
430 hover_point_changed();
432 } else if (type == QEvent::Leave) {
433 _hover_point = QPoint(-1, -1);
434 hover_point_changed();
437 return QObject::eventFilter(object, event);
440 bool View::viewportEvent(QEvent *e)
444 case QEvent::MouseButtonPress:
445 case QEvent::MouseButtonRelease:
446 case QEvent::MouseButtonDblClick:
447 case QEvent::MouseMove:
452 return QAbstractScrollArea::viewportEvent(e);
456 void View::resizeEvent(QResizeEvent*)
461 void View::h_scroll_value_changed(int value)
463 if (_updating_scroll)
466 const int range = horizontalScrollBar()->maximum();
467 if (range < MaxScrollValue)
468 _offset = _scale * value;
470 double length = 0, offset;
471 get_scroll_layout(length, offset);
472 _offset = _scale * length * value / MaxScrollValue;
479 void View::v_scroll_value_changed(int value)
486 void View::signals_changed()
488 int offset = SignalMargin + SignalHeight;
489 const vector< shared_ptr<Trace> > traces(get_traces());
490 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
492 t->set_v_offset(offset);
493 offset += SignalHeight + 2 * SignalMargin;
500 void View::data_updated()
502 // Get the new data length
504 shared_ptr<data::Logic> sig_data = _session.get_data();
506 deque< shared_ptr<data::LogicSnapshot> > &snapshots =
507 sig_data->get_snapshots();
508 BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots)
510 _data_length = max(_data_length,
511 s->get_sample_count());
514 // Update the scroll bars
521 void View::marker_time_changed()
527 void View::on_signals_moved()
533 void View::on_geometry_updated()