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 = 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)),
74 _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0),
78 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
79 this, SLOT(h_scroll_value_changed(int)));
80 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
81 this, SLOT(v_scroll_value_changed(int)));
82 connect(&_session, SIGNAL(data_updated()),
83 this, SLOT(data_updated()));
85 connect(&_cursors.first, SIGNAL(time_changed()),
86 this, SLOT(marker_time_changed()));
87 connect(&_cursors.second, SIGNAL(time_changed()),
88 this, SLOT(marker_time_changed()));
90 connect(_header, SIGNAL(signals_moved()),
91 this, SLOT(on_signals_moved()));
93 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
94 setViewport(_viewport);
96 _viewport->installEventFilter(this);
97 _ruler->installEventFilter(this);
98 _header->installEventFilter(this);
101 SigSession& View::session()
106 double View::scale() const
111 double View::offset() const
116 int View::v_offset() const
121 void View::zoom(double steps)
123 zoom(steps, (width() - LabelMarginWidth) / 2);
126 void View::zoom(double steps, int offset)
128 const double cursor_offset = _offset + _scale * offset;
129 _scale *= pow(3.0/2.0, -steps);
130 _scale = max(min(_scale, MaxScale), MinScale);
131 _offset = cursor_offset - _scale * offset;
139 void View::set_scale_offset(double scale, double offset)
149 bool View::cursors_shown() const
151 return _show_cursors;
154 void View::show_cursors(bool show)
156 _show_cursors = show;
161 std::pair<Cursor, Cursor>& View::cursors()
166 const QPoint& View::hover_point() const
171 void View::normalize_layout()
173 vector< shared_ptr<Signal> > &sigs = _session.get_signals();
176 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
177 v_min = min(s->get_v_offset(), v_min);
179 const int delta = -min(v_min, 0);
180 BOOST_FOREACH(shared_ptr<Signal> s, sigs)
181 s->set_v_offset(s->get_v_offset() + delta);
183 verticalScrollBar()->setSliderPosition(_v_offset + delta);
184 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
187 void View::get_scroll_layout(double &length, double &offset) const
189 const shared_ptr<SignalData> sig_data = _session.get_data();
193 length = _data_length / (sig_data->get_samplerate() * _scale);
194 offset = _offset / _scale;
197 void View::update_scroll()
201 const QSize areaSize = _viewport->size();
203 // Set the horizontal scroll bar
204 double length = 0, offset = 0;
205 get_scroll_layout(length, offset);
206 length = max(length - areaSize.width(), 0.0);
208 horizontalScrollBar()->setPageStep(areaSize.width());
210 if(length < MaxScrollValue) {
211 horizontalScrollBar()->setRange(0, length);
212 horizontalScrollBar()->setSliderPosition(offset);
214 horizontalScrollBar()->setRange(0, MaxScrollValue);
215 horizontalScrollBar()->setSliderPosition(
216 _offset * MaxScrollValue / (_scale * length));
219 // Set the vertical scrollbar
220 verticalScrollBar()->setPageStep(areaSize.height());
221 verticalScrollBar()->setRange(0,
222 _viewport->get_total_height() + SignalMargin -
226 void View::reset_signal_layout()
228 int offset = SignalMargin;
229 vector< shared_ptr<Signal> > &sigs = _session.get_signals();
230 BOOST_FOREACH(shared_ptr<Signal> s, sigs) {
231 s->set_v_offset(offset);
232 offset += SignalHeight + 2 * SignalMargin;
238 bool View::eventFilter(QObject *object, QEvent *event)
240 const QEvent::Type type = event->type();
241 if(type == QEvent::MouseMove) {
243 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
244 if(object == _viewport)
245 _hover_point = mouse_event->pos();
246 else if(object == _ruler)
247 _hover_point = QPoint(mouse_event->x(), 0);
248 else if(object == _header)
249 _hover_point = QPoint(0, mouse_event->y());
251 _hover_point = QPoint(-1, -1);
253 hover_point_changed();
255 } else if(type == QEvent::Leave) {
256 _hover_point = QPoint(-1, -1);
257 hover_point_changed();
260 return QObject::eventFilter(object, event);
263 bool View::viewportEvent(QEvent *e)
267 case QEvent::MouseButtonPress:
268 case QEvent::MouseButtonRelease:
269 case QEvent::MouseButtonDblClick:
270 case QEvent::MouseMove:
275 return QAbstractScrollArea::viewportEvent(e);
279 void View::resizeEvent(QResizeEvent *e)
281 _ruler->setGeometry(_viewport->x(), 0,
282 _viewport->width(), _viewport->y());
283 _header->setGeometry(0, _viewport->y(),
284 _viewport->x(), _viewport->height());
288 void View::h_scroll_value_changed(int value)
290 const int range = horizontalScrollBar()->maximum();
291 if(range < MaxScrollValue)
292 _offset = _scale * value;
294 double length = 0, offset;
295 get_scroll_layout(length, offset);
296 _offset = _scale * length * value / MaxScrollValue;
303 void View::v_scroll_value_changed(int value)
310 void View::data_updated()
312 // Get the new data length
314 shared_ptr<LogicData> sig_data = _session.get_data();
316 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
317 sig_data->get_snapshots();
318 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
320 _data_length = max(_data_length,
321 s->get_sample_count());
324 // Update the scroll bars
330 /// @todo: Call this only once when the signals are first created.
331 reset_signal_layout();
334 void View::marker_time_changed()
340 void View::on_signals_moved()