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::LabelMarginWidth = 70;
56 const int View::RulerHeight = 30;
58 const int View::MaxScrollValue = INT_MAX / 2;
60 const int View::SignalHeight = 30;
61 const int View::SignalMargin = 10;
62 const int View::SignalSnapGridSize = 10;
64 const QColor View::CursorAreaColour(220, 231, 243);
66 const QSizeF View::LabelPadding(4, 0);
68 View::View(SigSession &session, QWidget *parent) :
69 QAbstractScrollArea(parent),
71 _viewport(new Viewport(*this)),
72 _ruler(new Ruler(*this)),
73 _header(new Header(*this)),
78 _updating_scroll(false),
83 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
84 this, SLOT(h_scroll_value_changed(int)));
85 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
86 this, SLOT(v_scroll_value_changed(int)));
88 connect(&_session, SIGNAL(signals_changed()),
89 this, SLOT(signals_changed()));
90 connect(&_session, SIGNAL(data_updated()),
91 this, SLOT(data_updated()));
93 connect(_cursors.first().get(), SIGNAL(time_changed()),
94 this, SLOT(marker_time_changed()));
95 connect(_cursors.second().get(), SIGNAL(time_changed()),
96 this, SLOT(marker_time_changed()));
98 connect(_header, SIGNAL(signals_moved()),
99 this, SLOT(on_signals_moved()));
101 connect(_header, SIGNAL(selection_changed()),
102 _ruler, SLOT(clear_selection()));
103 connect(_ruler, SIGNAL(selection_changed()),
104 _header, SLOT(clear_selection()));
106 connect(_header, SIGNAL(selection_changed()),
107 this, SIGNAL(selection_changed()));
108 connect(_ruler, SIGNAL(selection_changed()),
109 this, SIGNAL(selection_changed()));
111 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
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, (width() - LabelMarginWidth) / 2);
149 void View::zoom(double steps, int offset)
151 const double cursor_offset = _offset + _scale * offset;
152 const double new_scale = max(min(_scale * pow(3.0/2.0, -steps),
153 MaxScale), MinScale);
154 const double new_offset = cursor_offset - new_scale * offset;
155 set_scale_offset(new_scale, new_offset);
158 void View::zoom_fit()
160 using pv::data::SignalData;
162 const vector< shared_ptr<Signal> > sigs(
163 session().get_signals());
165 // Make a set of all the visible data objects
166 set< shared_ptr<SignalData> > visible_data;
167 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
169 visible_data.insert(sig->data());
171 if (visible_data.empty())
174 double left_time = DBL_MAX, right_time = DBL_MIN;
175 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data)
177 const double start_time = d->get_start_time();
178 left_time = min(left_time, start_time);
179 right_time = max(right_time, start_time +
180 d->get_max_sample_count() / d->samplerate());
183 assert(left_time < right_time);
184 if (right_time - left_time < 1e-12)
188 const int w = _viewport->width();
192 set_scale_offset((right_time - left_time) / w, left_time);
195 void View::set_scale_offset(double scale, double offset)
203 scale_offset_changed();
206 vector< shared_ptr<Trace> > View::get_traces() const
208 const vector< shared_ptr<Signal> > sigs(
209 session().get_signals());
210 const vector< shared_ptr<DecodeTrace> > decode_sigs(
211 session().get_decode_signals());
212 vector< shared_ptr<Trace> > traces(
213 sigs.size() + decode_sigs.size());
215 vector< shared_ptr<Trace> >::iterator i = traces.begin();
216 i = copy(sigs.begin(), sigs.end(), i);
217 i = copy(decode_sigs.begin(), decode_sigs.end(), i);
219 stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets);
223 list<weak_ptr<SelectableItem> > View::selected_items() const
225 list<weak_ptr<SelectableItem> > items;
227 // List the selected signals
228 const vector< shared_ptr<Trace> > traces(get_traces());
229 BOOST_FOREACH (shared_ptr<Trace> t, traces) {
235 // List the selected cursors
236 if (_cursors.first()->selected())
237 items.push_back(_cursors.first());
238 if (_cursors.second()->selected())
239 items.push_back(_cursors.second());
244 bool View::cursors_shown() const
246 return _show_cursors;
249 void View::show_cursors(bool show)
251 _show_cursors = show;
256 void View::centre_cursors()
258 const double time_width = _scale * _viewport->width();
259 _cursors.first()->set_time(_offset + time_width * 0.4);
260 _cursors.second()->set_time(_offset + time_width * 0.6);
265 CursorPair& View::cursors()
270 const CursorPair& View::cursors() const
275 const QPoint& View::hover_point() const
280 void View::normalize_layout()
282 const vector< shared_ptr<Trace> > traces(get_traces());
285 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
286 v_min = min(t->get_v_offset(), v_min);
288 const int delta = -min(v_min, 0);
289 BOOST_FOREACH(shared_ptr<Trace> t, traces)
290 t->set_v_offset(t->get_v_offset() + delta);
292 verticalScrollBar()->setSliderPosition(_v_offset + delta);
293 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
296 void View::update_viewport()
302 void View::get_scroll_layout(double &length, double &offset) const
304 const shared_ptr<data::SignalData> sig_data = _session.get_data();
308 length = _data_length / (sig_data->samplerate() * _scale);
309 offset = _offset / _scale;
312 void View::update_scroll()
316 const QSize areaSize = _viewport->size();
318 // Set the horizontal scroll bar
319 double length = 0, offset = 0;
320 get_scroll_layout(length, offset);
321 length = max(length - areaSize.width(), 0.0);
323 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
325 _updating_scroll = true;
327 if (length < MaxScrollValue) {
328 horizontalScrollBar()->setRange(0, length);
329 horizontalScrollBar()->setSliderPosition(offset);
331 horizontalScrollBar()->setRange(0, MaxScrollValue);
332 horizontalScrollBar()->setSliderPosition(
333 _offset * MaxScrollValue / (_scale * length));
336 _updating_scroll = false;
338 // Set the vertical scrollbar
339 verticalScrollBar()->setPageStep(areaSize.height());
340 verticalScrollBar()->setRange(0,
341 _viewport->get_total_height() + SignalMargin -
345 bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
346 const shared_ptr<Trace> &b)
350 return a->get_v_offset() < b->get_v_offset();
353 bool View::eventFilter(QObject *object, QEvent *event)
355 const QEvent::Type type = event->type();
356 if (type == QEvent::MouseMove) {
358 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
359 if (object == _viewport)
360 _hover_point = mouse_event->pos();
361 else if (object == _ruler)
362 _hover_point = QPoint(mouse_event->x(), 0);
363 else if (object == _header)
364 _hover_point = QPoint(0, mouse_event->y());
366 _hover_point = QPoint(-1, -1);
368 hover_point_changed();
370 } else if (type == QEvent::Leave) {
371 _hover_point = QPoint(-1, -1);
372 hover_point_changed();
375 return QObject::eventFilter(object, event);
378 bool View::viewportEvent(QEvent *e)
382 case QEvent::MouseButtonPress:
383 case QEvent::MouseButtonRelease:
384 case QEvent::MouseButtonDblClick:
385 case QEvent::MouseMove:
390 return QAbstractScrollArea::viewportEvent(e);
394 void View::resizeEvent(QResizeEvent*)
396 _ruler->setGeometry(_viewport->x(), 0,
397 _viewport->width(), _viewport->y());
398 _header->setGeometry(0, _viewport->y(),
399 _viewport->x(), _viewport->height());
403 void View::h_scroll_value_changed(int value)
405 if (_updating_scroll)
408 const int range = horizontalScrollBar()->maximum();
409 if (range < MaxScrollValue)
410 _offset = _scale * value;
412 double length = 0, offset;
413 get_scroll_layout(length, offset);
414 _offset = _scale * length * value / MaxScrollValue;
421 void View::v_scroll_value_changed(int value)
428 void View::signals_changed()
430 int offset = SignalMargin + SignalHeight;
431 const vector< shared_ptr<Trace> > traces(get_traces());
432 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
434 t->set_v_offset(offset);
435 offset += SignalHeight + 2 * SignalMargin;
441 void View::data_updated()
443 // Get the new data length
445 shared_ptr<data::Logic> sig_data = _session.get_data();
447 deque< shared_ptr<data::LogicSnapshot> > &snapshots =
448 sig_data->get_snapshots();
449 BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots)
451 _data_length = max(_data_length,
452 s->get_sample_count());
455 // Update the scroll bars
462 void View::marker_time_changed()
468 void View::on_signals_moved()