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>
37 #include "pv/sigsession.h"
38 #include "pv/data/logic.h"
39 #include "pv/data/logicsnapshot.h"
41 using namespace boost;
47 const double View::MaxScale = 1e9;
48 const double View::MinScale = 1e-15;
50 const int View::LabelMarginWidth = 70;
51 const int View::RulerHeight = 30;
53 const int View::MaxScrollValue = INT_MAX / 2;
55 const int View::SignalHeight = 30;
56 const int View::SignalMargin = 10;
57 const int View::SignalSnapGridSize = 10;
59 const QColor View::CursorAreaColour(220, 231, 243);
61 const QSizeF View::LabelPadding(4, 0);
63 View::View(SigSession &session, QWidget *parent) :
64 QAbstractScrollArea(parent),
66 _viewport(new Viewport(*this)),
67 _ruler(new Ruler(*this)),
68 _header(new Header(*this)),
73 _updating_scroll(false),
75 _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0),
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, SIGNAL(time_changed()),
90 this, SLOT(marker_time_changed()));
91 connect(&_cursors.second, SIGNAL(time_changed()),
92 this, SLOT(marker_time_changed()));
94 connect(_header, SIGNAL(signals_moved()),
95 this, SLOT(on_signals_moved()));
97 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
98 setViewport(_viewport);
100 _viewport->installEventFilter(this);
101 _ruler->installEventFilter(this);
102 _header->installEventFilter(this);
105 SigSession& View::session()
110 double View::scale() const
115 double View::offset() const
120 int View::v_offset() const
125 void View::zoom(double steps)
127 zoom(steps, (width() - LabelMarginWidth) / 2);
130 void View::zoom(double steps, int offset)
132 const double cursor_offset = _offset + _scale * offset;
133 _scale *= pow(3.0/2.0, -steps);
134 _scale = max(min(_scale, MaxScale), MinScale);
135 _offset = cursor_offset - _scale * offset;
143 void View::set_scale_offset(double scale, double offset)
153 bool View::cursors_shown() const
155 return _show_cursors;
158 void View::show_cursors(bool show)
160 _show_cursors = show;
165 std::pair<Cursor, Cursor>& View::cursors()
170 const QPoint& View::hover_point() const
175 void View::normalize_layout()
177 const vector< shared_ptr<Signal> > sigs(_session.get_signals());
180 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
181 v_min = min(s->get_v_offset(), v_min);
183 const int delta = -min(v_min, 0);
184 BOOST_FOREACH(shared_ptr<Signal> s, sigs)
185 s->set_v_offset(s->get_v_offset() + delta);
187 verticalScrollBar()->setSliderPosition(_v_offset + delta);
188 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
191 void View::get_scroll_layout(double &length, double &offset) const
193 const shared_ptr<data::SignalData> sig_data = _session.get_data();
197 length = _data_length / (sig_data->get_samplerate() * _scale);
198 offset = _offset / _scale;
201 void View::update_scroll()
205 const QSize areaSize = _viewport->size();
207 // Set the horizontal scroll bar
208 double length = 0, offset = 0;
209 get_scroll_layout(length, offset);
210 length = max(length - areaSize.width(), 0.0);
212 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
214 _updating_scroll = true;
216 if (length < MaxScrollValue) {
217 horizontalScrollBar()->setRange(0, length);
218 horizontalScrollBar()->setSliderPosition(offset);
220 horizontalScrollBar()->setRange(0, MaxScrollValue);
221 horizontalScrollBar()->setSliderPosition(
222 _offset * MaxScrollValue / (_scale * length));
225 _updating_scroll = false;
227 // Set the vertical scrollbar
228 verticalScrollBar()->setPageStep(areaSize.height());
229 verticalScrollBar()->setRange(0,
230 _viewport->get_total_height() + SignalMargin -
234 void View::reset_signal_layout()
236 int offset = SignalMargin + SignalHeight;
237 const vector< shared_ptr<Signal> > sigs(_session.get_signals());
238 BOOST_FOREACH(shared_ptr<Signal> s, sigs) {
239 s->set_v_offset(offset);
240 offset += SignalHeight + 2 * SignalMargin;
246 bool View::eventFilter(QObject *object, QEvent *event)
248 const QEvent::Type type = event->type();
249 if (type == QEvent::MouseMove) {
251 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
252 if (object == _viewport)
253 _hover_point = mouse_event->pos();
254 else if (object == _ruler)
255 _hover_point = QPoint(mouse_event->x(), 0);
256 else if (object == _header)
257 _hover_point = QPoint(0, mouse_event->y());
259 _hover_point = QPoint(-1, -1);
261 hover_point_changed();
263 } else if (type == QEvent::Leave) {
264 _hover_point = QPoint(-1, -1);
265 hover_point_changed();
268 return QObject::eventFilter(object, event);
271 bool View::viewportEvent(QEvent *e)
275 case QEvent::MouseButtonPress:
276 case QEvent::MouseButtonRelease:
277 case QEvent::MouseButtonDblClick:
278 case QEvent::MouseMove:
283 return QAbstractScrollArea::viewportEvent(e);
287 void View::resizeEvent(QResizeEvent*)
289 _ruler->setGeometry(_viewport->x(), 0,
290 _viewport->width(), _viewport->y());
291 _header->setGeometry(0, _viewport->y(),
292 _viewport->x(), _viewport->height());
296 void View::h_scroll_value_changed(int value)
298 if (_updating_scroll)
301 const int range = horizontalScrollBar()->maximum();
302 if (range < MaxScrollValue)
303 _offset = _scale * value;
305 double length = 0, offset;
306 get_scroll_layout(length, offset);
307 _offset = _scale * length * value / MaxScrollValue;
314 void View::v_scroll_value_changed(int value)
321 void View::signals_changed()
323 reset_signal_layout();
326 void View::data_updated()
328 // Get the new data length
330 shared_ptr<data::Logic> sig_data = _session.get_data();
332 deque< shared_ptr<data::LogicSnapshot> > &snapshots =
333 sig_data->get_snapshots();
334 BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots)
336 _data_length = max(_data_length,
337 s->get_sample_count());
340 // Update the scroll bars
347 void View::marker_time_changed()
353 void View::on_signals_moved()