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 "../logicdata.h"
38 #include "../logicdatasnapshot.h"
39 #include "../sigsession.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 = 50;
56 const int View::SignalSnapGridSize = 10;
58 const QColor View::CursorAreaColour(220, 231, 243);
60 const QSizeF View::LabelPadding(4, 0);
62 View::View(SigSession &session, QWidget *parent) :
63 QAbstractScrollArea(parent),
65 _viewport(new Viewport(*this)),
66 _ruler(new Ruler(*this)),
67 _header(new Header(*this)),
73 _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0),
77 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
78 this, SLOT(h_scroll_value_changed(int)));
79 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
80 this, SLOT(v_scroll_value_changed(int)));
81 connect(&_session, SIGNAL(data_updated()),
82 this, SLOT(data_updated()));
84 connect(&_cursors.first, SIGNAL(time_changed()),
85 this, SLOT(marker_time_changed()));
86 connect(&_cursors.second, SIGNAL(time_changed()),
87 this, SLOT(marker_time_changed()));
89 connect(_header, SIGNAL(signals_moved()),
90 this, SLOT(signals_moved()));
92 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
93 setViewport(_viewport);
95 _viewport->installEventFilter(this);
96 _ruler->installEventFilter(this);
97 _header->installEventFilter(this);
100 SigSession& View::session()
105 double View::scale() const
110 double View::offset() const
115 int View::v_offset() const
120 void View::zoom(double steps)
122 zoom(steps, (width() - LabelMarginWidth) / 2);
125 void View::zoom(double steps, int offset)
127 const double cursor_offset = _offset + _scale * offset;
128 _scale *= pow(3.0/2.0, -steps);
129 _scale = max(min(_scale, MaxScale), MinScale);
130 _offset = cursor_offset - _scale * offset;
138 void View::set_scale_offset(double scale, double offset)
148 bool View::cursors_shown() const
150 return _show_cursors;
153 void View::show_cursors(bool show)
155 _show_cursors = show;
160 std::pair<Cursor, Cursor>& View::cursors()
165 const QPoint& View::hover_point() const
170 void View::get_scroll_layout(double &length, double &offset) const
172 const shared_ptr<SignalData> sig_data = _session.get_data();
176 length = _data_length / (sig_data->get_samplerate() * _scale);
177 offset = _offset / _scale;
180 void View::update_scroll()
184 const QSize areaSize = _viewport->size();
186 // Set the horizontal scroll bar
187 double length = 0, offset = 0;
188 get_scroll_layout(length, offset);
189 length = max(length - areaSize.width(), 0.0);
191 horizontalScrollBar()->setPageStep(areaSize.width());
193 if(length < MaxScrollValue) {
194 horizontalScrollBar()->setRange(0, length);
195 horizontalScrollBar()->setSliderPosition(offset);
197 horizontalScrollBar()->setRange(0, MaxScrollValue);
198 horizontalScrollBar()->setSliderPosition(
199 _offset * MaxScrollValue / (_scale * length));
202 // Set the vertical scrollbar
203 verticalScrollBar()->setPageStep(areaSize.height());
204 verticalScrollBar()->setRange(0,
205 _viewport->get_total_height() - areaSize.height());
208 void View::reset_signal_layout()
211 vector< shared_ptr<Signal> > &sigs = _session.get_signals();
212 BOOST_FOREACH(shared_ptr<Signal> s, sigs) {
213 s->set_v_offset(offset);
214 offset += SignalHeight;
218 bool View::eventFilter(QObject *object, QEvent *event)
220 const QEvent::Type type = event->type();
221 if(type == QEvent::MouseMove) {
223 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
224 if(object == _viewport)
225 _hover_point = mouse_event->pos();
226 else if(object == _ruler)
227 _hover_point = QPoint(mouse_event->x(), 0);
228 else if(object == _header)
229 _hover_point = QPoint(0, mouse_event->y());
231 _hover_point = QPoint(-1, -1);
233 hover_point_changed();
235 } else if(type == QEvent::Leave) {
236 _hover_point = QPoint(-1, -1);
237 hover_point_changed();
240 return QObject::eventFilter(object, event);
243 bool View::viewportEvent(QEvent *e)
247 case QEvent::MouseButtonPress:
248 case QEvent::MouseButtonRelease:
249 case QEvent::MouseButtonDblClick:
250 case QEvent::MouseMove:
255 return QAbstractScrollArea::viewportEvent(e);
259 void View::resizeEvent(QResizeEvent *e)
261 _ruler->setGeometry(_viewport->x(), 0,
262 _viewport->width(), _viewport->y());
263 _header->setGeometry(0, _viewport->y(),
264 _viewport->x(), _viewport->height());
268 void View::h_scroll_value_changed(int value)
270 const int range = horizontalScrollBar()->maximum();
271 if(range < MaxScrollValue)
272 _offset = _scale * value;
274 double length = 0, offset;
275 get_scroll_layout(length, offset);
276 _offset = _scale * length * value / MaxScrollValue;
283 void View::v_scroll_value_changed(int value)
290 void View::data_updated()
292 // Get the new data length
294 shared_ptr<LogicData> sig_data = _session.get_data();
296 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
297 sig_data->get_snapshots();
298 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
300 _data_length = max(_data_length,
301 s->get_sample_count());
304 // Update the scroll bars
310 /// @todo: Call this only once when the signals are first created.
311 reset_signal_layout();
314 void View::marker_time_changed()
320 void View::signals_moved()