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>
27 #include <boost/foreach.hpp>
30 #include <QMouseEvent>
33 #include "decodetrace.h"
40 #include "pv/sigsession.h"
41 #include "pv/data/logic.h"
42 #include "pv/data/logicsnapshot.h"
44 using namespace boost;
50 const double View::MaxScale = 1e9;
51 const double View::MinScale = 1e-15;
53 const int View::LabelMarginWidth = 70;
54 const int View::RulerHeight = 30;
56 const int View::MaxScrollValue = INT_MAX / 2;
58 const int View::SignalHeight = 30;
59 const int View::SignalMargin = 10;
60 const int View::SignalSnapGridSize = 10;
62 const QColor View::CursorAreaColour(220, 231, 243);
64 const QSizeF View::LabelPadding(4, 0);
66 View::View(SigSession &session, QWidget *parent) :
67 QAbstractScrollArea(parent),
69 _viewport(new Viewport(*this)),
70 _ruler(new Ruler(*this)),
71 _header(new Header(*this)),
76 _updating_scroll(false),
81 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
82 this, SLOT(h_scroll_value_changed(int)));
83 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
84 this, SLOT(v_scroll_value_changed(int)));
86 connect(&_session, SIGNAL(signals_changed()),
87 this, SLOT(signals_changed()));
88 connect(&_session, SIGNAL(data_updated()),
89 this, SLOT(data_updated()));
91 connect(_cursors.first().get(), SIGNAL(time_changed()),
92 this, SLOT(marker_time_changed()));
93 connect(_cursors.second().get(), SIGNAL(time_changed()),
94 this, SLOT(marker_time_changed()));
96 connect(_header, SIGNAL(signals_moved()),
97 this, SLOT(on_signals_moved()));
99 connect(_header, SIGNAL(selection_changed()),
100 _ruler, SLOT(clear_selection()));
101 connect(_ruler, SIGNAL(selection_changed()),
102 _header, SLOT(clear_selection()));
104 connect(_header, SIGNAL(selection_changed()),
105 this, SIGNAL(selection_changed()));
106 connect(_ruler, SIGNAL(selection_changed()),
107 this, SIGNAL(selection_changed()));
109 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
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, (width() - LabelMarginWidth) / 2);
147 void View::zoom(double steps, int offset)
149 const double cursor_offset = _offset + _scale * offset;
150 const double new_scale = max(min(_scale * pow(3.0/2.0, -steps),
151 MaxScale), MinScale);
152 const double new_offset = cursor_offset - new_scale * offset;
153 set_scale_offset(new_scale, new_offset);
156 void View::set_scale_offset(double scale, double offset)
164 scale_offset_changed();
167 vector< shared_ptr<Trace> > View::get_traces() const
169 const vector< shared_ptr<Signal> > sigs(
170 session().get_signals());
171 const vector< shared_ptr<DecodeTrace> > decode_sigs(
172 session().get_decode_signals());
173 vector< shared_ptr<Trace> > traces(
174 sigs.size() + decode_sigs.size());
176 vector< shared_ptr<Trace> >::iterator i = traces.begin();
177 i = copy(sigs.begin(), sigs.end(), i);
178 i = copy(decode_sigs.begin(), decode_sigs.end(), i);
180 sort(traces.begin(), traces.end(), compare_trace_v_offsets);
184 list<weak_ptr<SelectableItem> > View::selected_items() const
186 list<weak_ptr<SelectableItem> > items;
188 // List the selected signals
189 const vector< shared_ptr<Trace> > traces(get_traces());
190 BOOST_FOREACH (shared_ptr<Trace> t, traces) {
196 // List the selected cursors
197 if (_cursors.first()->selected())
198 items.push_back(_cursors.first());
199 if (_cursors.second()->selected())
200 items.push_back(_cursors.second());
205 bool View::cursors_shown() const
207 return _show_cursors;
210 void View::show_cursors(bool show)
212 _show_cursors = show;
217 void View::centre_cursors()
219 const double time_width = _scale * _viewport->width();
220 _cursors.first()->set_time(_offset + time_width * 0.4);
221 _cursors.second()->set_time(_offset + time_width * 0.6);
226 CursorPair& View::cursors()
231 const CursorPair& View::cursors() const
236 const QPoint& View::hover_point() const
241 void View::normalize_layout()
243 const vector< shared_ptr<Trace> > traces(get_traces());
246 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
247 v_min = min(t->get_v_offset(), v_min);
249 const int delta = -min(v_min, 0);
250 BOOST_FOREACH(shared_ptr<Trace> t, traces)
251 t->set_v_offset(t->get_v_offset() + delta);
253 verticalScrollBar()->setSliderPosition(_v_offset + delta);
254 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
257 void View::update_viewport()
263 void View::get_scroll_layout(double &length, double &offset) const
265 const shared_ptr<data::SignalData> sig_data = _session.get_data();
269 length = _data_length / (sig_data->get_samplerate() * _scale);
270 offset = _offset / _scale;
273 void View::update_scroll()
277 const QSize areaSize = _viewport->size();
279 // Set the horizontal scroll bar
280 double length = 0, offset = 0;
281 get_scroll_layout(length, offset);
282 length = max(length - areaSize.width(), 0.0);
284 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
286 _updating_scroll = true;
288 if (length < MaxScrollValue) {
289 horizontalScrollBar()->setRange(0, length);
290 horizontalScrollBar()->setSliderPosition(offset);
292 horizontalScrollBar()->setRange(0, MaxScrollValue);
293 horizontalScrollBar()->setSliderPosition(
294 _offset * MaxScrollValue / (_scale * length));
297 _updating_scroll = false;
299 // Set the vertical scrollbar
300 verticalScrollBar()->setPageStep(areaSize.height());
301 verticalScrollBar()->setRange(0,
302 _viewport->get_total_height() + SignalMargin -
306 bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
307 const shared_ptr<Trace> &b)
311 return a->get_v_offset() < b->get_v_offset();
314 bool View::eventFilter(QObject *object, QEvent *event)
316 const QEvent::Type type = event->type();
317 if (type == QEvent::MouseMove) {
319 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
320 if (object == _viewport)
321 _hover_point = mouse_event->pos();
322 else if (object == _ruler)
323 _hover_point = QPoint(mouse_event->x(), 0);
324 else if (object == _header)
325 _hover_point = QPoint(0, mouse_event->y());
327 _hover_point = QPoint(-1, -1);
329 hover_point_changed();
331 } else if (type == QEvent::Leave) {
332 _hover_point = QPoint(-1, -1);
333 hover_point_changed();
336 return QObject::eventFilter(object, event);
339 bool View::viewportEvent(QEvent *e)
343 case QEvent::MouseButtonPress:
344 case QEvent::MouseButtonRelease:
345 case QEvent::MouseButtonDblClick:
346 case QEvent::MouseMove:
351 return QAbstractScrollArea::viewportEvent(e);
355 void View::resizeEvent(QResizeEvent*)
357 _ruler->setGeometry(_viewport->x(), 0,
358 _viewport->width(), _viewport->y());
359 _header->setGeometry(0, _viewport->y(),
360 _viewport->x(), _viewport->height());
364 void View::h_scroll_value_changed(int value)
366 if (_updating_scroll)
369 const int range = horizontalScrollBar()->maximum();
370 if (range < MaxScrollValue)
371 _offset = _scale * value;
373 double length = 0, offset;
374 get_scroll_layout(length, offset);
375 _offset = _scale * length * value / MaxScrollValue;
382 void View::v_scroll_value_changed(int value)
389 void View::signals_changed()
391 int offset = SignalMargin + SignalHeight;
392 const vector< shared_ptr<Trace> > traces(get_traces());
393 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
395 t->set_v_offset(offset);
396 offset += SignalHeight + 2 * SignalMargin;
402 void View::data_updated()
404 // Get the new data length
406 shared_ptr<data::Logic> sig_data = _session.get_data();
408 deque< shared_ptr<data::LogicSnapshot> > &snapshots =
409 sig_data->get_snapshots();
410 BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots)
412 _data_length = max(_data_length,
413 s->get_sample_count());
416 // Update the scroll bars
423 void View::marker_time_changed()
429 void View::on_signals_moved()