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;
57 const QColor View::CursorAreaColour(220, 231, 243);
59 const QSizeF View::LabelPadding(4, 0);
61 View::View(SigSession &session, QWidget *parent) :
62 QAbstractScrollArea(parent),
64 _viewport(new Viewport(*this)),
65 _ruler(new Ruler(*this)),
66 _header(new Header(*this)),
72 _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0),
76 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
77 this, SLOT(h_scroll_value_changed(int)));
78 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
79 this, SLOT(v_scroll_value_changed(int)));
80 connect(&_session, SIGNAL(data_updated()),
81 this, SLOT(data_updated()));
83 connect(&_cursors.first, SIGNAL(time_changed()),
84 this, SLOT(marker_time_changed()));
85 connect(&_cursors.second, SIGNAL(time_changed()),
86 this, SLOT(marker_time_changed()));
88 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
89 setViewport(_viewport);
91 _viewport->installEventFilter(this);
92 _ruler->installEventFilter(this);
93 _header->installEventFilter(this);
96 SigSession& View::session()
101 double View::scale() const
106 double View::offset() const
111 int View::v_offset() const
116 void View::zoom(double steps)
118 zoom(steps, (width() - LabelMarginWidth) / 2);
121 void View::zoom(double steps, int offset)
123 const double cursor_offset = _offset + _scale * offset;
124 _scale *= pow(3.0/2.0, -steps);
125 _scale = max(min(_scale, MaxScale), MinScale);
126 _offset = cursor_offset - _scale * offset;
134 void View::set_scale_offset(double scale, double offset)
144 bool View::cursors_shown() const
146 return _show_cursors;
149 void View::show_cursors(bool show)
151 _show_cursors = show;
156 std::pair<Cursor, Cursor>& View::cursors()
161 const QPoint& View::hover_point() const
166 void View::get_scroll_layout(double &length, double &offset) const
168 const shared_ptr<SignalData> sig_data = _session.get_data();
172 length = _data_length / (sig_data->get_samplerate() * _scale);
173 offset = _offset / _scale;
176 void View::update_scroll()
180 const QSize areaSize = _viewport->size();
182 // Set the horizontal scroll bar
183 double length = 0, offset = 0;
184 get_scroll_layout(length, offset);
185 length = max(length - areaSize.width(), 0.0);
187 horizontalScrollBar()->setPageStep(areaSize.width());
189 if(length < MaxScrollValue) {
190 horizontalScrollBar()->setRange(0, length);
191 horizontalScrollBar()->setSliderPosition(offset);
193 horizontalScrollBar()->setRange(0, MaxScrollValue);
194 horizontalScrollBar()->setSliderPosition(
195 _offset * MaxScrollValue / (_scale * length));
198 // Set the vertical scrollbar
199 verticalScrollBar()->setPageStep(areaSize.height());
200 verticalScrollBar()->setRange(0,
201 _viewport->get_total_height() - areaSize.height());
204 void View::reset_signal_layout()
207 vector< shared_ptr<Signal> > &sigs = _session.get_signals();
208 BOOST_FOREACH(shared_ptr<Signal> s, sigs) {
209 s->set_v_offset(offset);
210 offset += SignalHeight;
214 bool View::eventFilter(QObject *object, QEvent *event)
216 const QEvent::Type type = event->type();
217 if(type == QEvent::MouseMove) {
219 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
220 if(object == _viewport)
221 _hover_point = mouse_event->pos();
222 else if(object == _ruler)
223 _hover_point = QPoint(mouse_event->x(), 0);
224 else if(object == _header)
225 _hover_point = QPoint(0, mouse_event->y());
227 _hover_point = QPoint(-1, -1);
229 hover_point_changed();
231 } else if(type == QEvent::Leave) {
232 _hover_point = QPoint(-1, -1);
233 hover_point_changed();
236 return QObject::eventFilter(object, event);
239 bool View::viewportEvent(QEvent *e)
243 case QEvent::MouseButtonPress:
244 case QEvent::MouseButtonRelease:
245 case QEvent::MouseButtonDblClick:
246 case QEvent::MouseMove:
251 return QAbstractScrollArea::viewportEvent(e);
255 void View::resizeEvent(QResizeEvent *e)
257 _ruler->setGeometry(_viewport->x(), 0,
258 _viewport->width(), _viewport->y());
259 _header->setGeometry(0, _viewport->y(),
260 _viewport->x(), _viewport->height());
264 void View::h_scroll_value_changed(int value)
266 const int range = horizontalScrollBar()->maximum();
267 if(range < MaxScrollValue)
268 _offset = _scale * value;
270 double length = 0, offset;
271 get_scroll_layout(length, offset);
272 _offset = _scale * length * value / MaxScrollValue;
279 void View::v_scroll_value_changed(int value)
286 void View::data_updated()
288 // Get the new data length
290 shared_ptr<LogicData> sig_data = _session.get_data();
292 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
293 sig_data->get_snapshots();
294 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
296 _data_length = max(_data_length,
297 s->get_sample_count());
300 // Update the scroll bars
306 /// @todo: Call this only once when the signals are first created.
307 reset_signal_layout();
310 void View::marker_time_changed()