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
25 #include <boost/foreach.hpp>
28 #include <QMouseEvent>
31 #include "decodesignal.h"
38 #include "pv/sigsession.h"
39 #include "pv/data/logic.h"
40 #include "pv/data/logicsnapshot.h"
42 using namespace boost;
48 const double View::MaxScale = 1e9;
49 const double View::MinScale = 1e-15;
51 const int View::LabelMarginWidth = 70;
52 const int View::RulerHeight = 30;
54 const int View::MaxScrollValue = INT_MAX / 2;
56 const int View::SignalHeight = 30;
57 const int View::SignalMargin = 10;
58 const int View::SignalSnapGridSize = 10;
60 const QColor View::CursorAreaColour(220, 231, 243);
62 const QSizeF View::LabelPadding(4, 0);
64 View::View(SigSession &session, QWidget *parent) :
65 QAbstractScrollArea(parent),
67 _viewport(new Viewport(*this)),
68 _ruler(new Ruler(*this)),
69 _header(new Header(*this)),
74 _updating_scroll(false),
79 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
80 this, SLOT(h_scroll_value_changed(int)));
81 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
82 this, SLOT(v_scroll_value_changed(int)));
84 connect(&_session, SIGNAL(signals_changed()),
85 this, SLOT(signals_changed()));
86 connect(&_session, SIGNAL(data_updated()),
87 this, SLOT(data_updated()));
89 connect(_cursors.first().get(), SIGNAL(time_changed()),
90 this, SLOT(marker_time_changed()));
91 connect(_cursors.second().get(), SIGNAL(time_changed()),
92 this, SLOT(marker_time_changed()));
94 connect(_header, SIGNAL(signals_moved()),
95 this, SLOT(on_signals_moved()));
97 connect(_header, SIGNAL(selection_changed()),
98 _ruler, SLOT(clear_selection()));
99 connect(_ruler, SIGNAL(selection_changed()),
100 _header, SLOT(clear_selection()));
102 connect(_header, SIGNAL(selection_changed()),
103 this, SIGNAL(selection_changed()));
104 connect(_ruler, SIGNAL(selection_changed()),
105 this, SIGNAL(selection_changed()));
107 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
108 setViewport(_viewport);
110 _viewport->installEventFilter(this);
111 _ruler->installEventFilter(this);
112 _header->installEventFilter(this);
115 SigSession& View::session()
120 const SigSession& View::session() const
125 double View::scale() const
130 double View::offset() const
135 int View::v_offset() const
140 void View::zoom(double steps)
142 zoom(steps, (width() - LabelMarginWidth) / 2);
145 void View::zoom(double steps, int offset)
147 const double cursor_offset = _offset + _scale * offset;
148 const double new_scale = max(min(_scale * pow(3.0/2.0, -steps),
149 MaxScale), MinScale);
150 const double new_offset = cursor_offset - new_scale * offset;
151 set_scale_offset(new_scale, new_offset);
154 void View::set_scale_offset(double scale, double offset)
162 scale_offset_changed();
165 vector< shared_ptr<Trace> > View::get_traces() const
167 const vector< shared_ptr<Signal> > sigs(
168 session().get_signals());
169 const vector< shared_ptr<DecodeSignal> > decode_sigs(
170 session().get_decode_signals());
171 vector< shared_ptr<Trace> > traces(
172 sigs.size() + decode_sigs.size());
174 vector< shared_ptr<Trace> >::iterator i = traces.begin();
175 i = copy(sigs.begin(), sigs.end(), i);
176 i = copy(decode_sigs.begin(), decode_sigs.end(), i);
178 sort(traces.begin(), traces.end(), compare_trace_v_offsets);
182 list<weak_ptr<SelectableItem> > View::selected_items() const
184 list<weak_ptr<SelectableItem> > items;
186 // List the selected signals
187 const vector< shared_ptr<Trace> > traces(get_traces());
188 BOOST_FOREACH (shared_ptr<Trace> t, traces) {
194 // List the selected cursors
195 if (_cursors.first()->selected())
196 items.push_back(_cursors.first());
197 if (_cursors.second()->selected())
198 items.push_back(_cursors.second());
203 bool View::cursors_shown() const
205 return _show_cursors;
208 void View::show_cursors(bool show)
210 _show_cursors = show;
215 void View::centre_cursors()
217 const double time_width = _scale * _viewport->width();
218 _cursors.first()->set_time(_offset + time_width * 0.4);
219 _cursors.second()->set_time(_offset + time_width * 0.6);
224 CursorPair& View::cursors()
229 const CursorPair& View::cursors() const
234 const QPoint& View::hover_point() const
239 void View::normalize_layout()
241 const vector< shared_ptr<Trace> > traces(get_traces());
244 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
245 v_min = min(t->get_v_offset(), v_min);
247 const int delta = -min(v_min, 0);
248 BOOST_FOREACH(shared_ptr<Trace> t, traces)
249 t->set_v_offset(t->get_v_offset() + delta);
251 verticalScrollBar()->setSliderPosition(_v_offset + delta);
252 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
255 void View::update_viewport()
261 void View::get_scroll_layout(double &length, double &offset) const
263 const shared_ptr<data::SignalData> sig_data = _session.get_data();
267 length = _data_length / (sig_data->get_samplerate() * _scale);
268 offset = _offset / _scale;
271 void View::update_scroll()
275 const QSize areaSize = _viewport->size();
277 // Set the horizontal scroll bar
278 double length = 0, offset = 0;
279 get_scroll_layout(length, offset);
280 length = max(length - areaSize.width(), 0.0);
282 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
284 _updating_scroll = true;
286 if (length < MaxScrollValue) {
287 horizontalScrollBar()->setRange(0, length);
288 horizontalScrollBar()->setSliderPosition(offset);
290 horizontalScrollBar()->setRange(0, MaxScrollValue);
291 horizontalScrollBar()->setSliderPosition(
292 _offset * MaxScrollValue / (_scale * length));
295 _updating_scroll = false;
297 // Set the vertical scrollbar
298 verticalScrollBar()->setPageStep(areaSize.height());
299 verticalScrollBar()->setRange(0,
300 _viewport->get_total_height() + SignalMargin -
304 bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
305 const shared_ptr<Trace> &b)
309 return a->get_v_offset() < b->get_v_offset();
312 bool View::eventFilter(QObject *object, QEvent *event)
314 const QEvent::Type type = event->type();
315 if (type == QEvent::MouseMove) {
317 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
318 if (object == _viewport)
319 _hover_point = mouse_event->pos();
320 else if (object == _ruler)
321 _hover_point = QPoint(mouse_event->x(), 0);
322 else if (object == _header)
323 _hover_point = QPoint(0, mouse_event->y());
325 _hover_point = QPoint(-1, -1);
327 hover_point_changed();
329 } else if (type == QEvent::Leave) {
330 _hover_point = QPoint(-1, -1);
331 hover_point_changed();
334 return QObject::eventFilter(object, event);
337 bool View::viewportEvent(QEvent *e)
341 case QEvent::MouseButtonPress:
342 case QEvent::MouseButtonRelease:
343 case QEvent::MouseButtonDblClick:
344 case QEvent::MouseMove:
349 return QAbstractScrollArea::viewportEvent(e);
353 void View::resizeEvent(QResizeEvent*)
355 _ruler->setGeometry(_viewport->x(), 0,
356 _viewport->width(), _viewport->y());
357 _header->setGeometry(0, _viewport->y(),
358 _viewport->x(), _viewport->height());
362 void View::h_scroll_value_changed(int value)
364 if (_updating_scroll)
367 const int range = horizontalScrollBar()->maximum();
368 if (range < MaxScrollValue)
369 _offset = _scale * value;
371 double length = 0, offset;
372 get_scroll_layout(length, offset);
373 _offset = _scale * length * value / MaxScrollValue;
380 void View::v_scroll_value_changed(int value)
387 void View::signals_changed()
389 int offset = SignalMargin + SignalHeight;
390 const vector< shared_ptr<Trace> > traces(get_traces());
391 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
393 t->set_v_offset(offset);
394 offset += SignalHeight + 2 * SignalMargin;
400 void View::data_updated()
402 // Get the new data length
404 shared_ptr<data::Logic> sig_data = _session.get_data();
406 deque< shared_ptr<data::LogicSnapshot> > &snapshots =
407 sig_data->get_snapshots();
408 BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots)
410 _data_length = max(_data_length,
411 s->get_sample_count());
414 // Update the scroll bars
421 void View::marker_time_changed()
427 void View::on_signals_moved()