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>
36 #include "../logicdata.h"
37 #include "../logicdatasnapshot.h"
38 #include "../sigsession.h"
40 using namespace boost;
46 const double View::MaxScale = 1e9;
47 const double View::MinScale = 1e-15;
49 const int View::LabelMarginWidth = 70;
50 const int View::RulerHeight = 30;
52 const int View::MaxScrollValue = INT_MAX / 2;
54 const int View::SignalHeight = 50;
56 const QColor View::CursorAreaColour(220, 231, 243);
58 const QSizeF View::LabelPadding(4, 0);
60 View::View(SigSession &session, QWidget *parent) :
61 QAbstractScrollArea(parent),
63 _viewport(new Viewport(*this)),
64 _ruler(new Ruler(*this)),
65 _header(new Header(*this)),
71 _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0),
75 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
76 this, SLOT(h_scroll_value_changed(int)));
77 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
78 this, SLOT(v_scroll_value_changed(int)));
79 connect(&_session, SIGNAL(data_updated()),
80 this, SLOT(data_updated()));
82 connect(&_cursors.first, SIGNAL(time_changed()),
83 this, SLOT(marker_time_changed()));
84 connect(&_cursors.second, SIGNAL(time_changed()),
85 this, SLOT(marker_time_changed()));
87 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
88 setViewport(_viewport);
90 _viewport->installEventFilter(this);
91 _ruler->installEventFilter(this);
92 _header->installEventFilter(this);
95 SigSession& View::session()
100 double View::scale() const
105 double View::offset() const
110 int View::v_offset() const
115 void View::zoom(double steps)
117 zoom(steps, (width() - LabelMarginWidth) / 2);
120 void View::zoom(double steps, int offset)
122 const double cursor_offset = _offset + _scale * offset;
123 _scale *= pow(3.0/2.0, -steps);
124 _scale = max(min(_scale, MaxScale), MinScale);
125 _offset = cursor_offset - _scale * offset;
133 void View::set_scale_offset(double scale, double offset)
143 bool View::cursors_shown() const
145 return _show_cursors;
148 void View::show_cursors(bool show)
150 _show_cursors = show;
155 std::pair<Cursor, Cursor>& View::cursors()
160 const QPoint& View::hover_point() const
165 void View::get_scroll_layout(double &length, double &offset) const
167 const shared_ptr<SignalData> sig_data = _session.get_data();
171 length = _data_length / (sig_data->get_samplerate() * _scale);
172 offset = _offset / _scale;
175 void View::update_scroll()
179 const QSize areaSize = _viewport->size();
181 // Set the horizontal scroll bar
182 double length = 0, offset = 0;
183 get_scroll_layout(length, offset);
184 length = max(length - areaSize.width(), 0.0);
186 horizontalScrollBar()->setPageStep(areaSize.width());
188 if(length < MaxScrollValue) {
189 horizontalScrollBar()->setRange(0, length);
190 horizontalScrollBar()->setSliderPosition(offset);
192 horizontalScrollBar()->setRange(0, MaxScrollValue);
193 horizontalScrollBar()->setSliderPosition(
194 _offset * MaxScrollValue / (_scale * length));
197 // Set the vertical scrollbar
198 verticalScrollBar()->setPageStep(areaSize.height());
199 verticalScrollBar()->setRange(0,
200 _viewport->get_total_height() - areaSize.height());
203 bool View::eventFilter(QObject *object, QEvent *event)
205 const QEvent::Type type = event->type();
206 if(type == QEvent::MouseMove) {
208 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
209 if(object == _viewport)
210 _hover_point = mouse_event->pos();
211 else if(object == _ruler)
212 _hover_point = QPoint(mouse_event->x(), 0);
213 else if(object == _header)
214 _hover_point = QPoint(0, mouse_event->y());
216 _hover_point = QPoint(-1, -1);
218 hover_point_changed();
220 } else if(type == QEvent::Leave) {
221 _hover_point = QPoint(-1, -1);
222 hover_point_changed();
225 return QObject::eventFilter(object, event);
228 bool View::viewportEvent(QEvent *e)
232 case QEvent::MouseButtonPress:
233 case QEvent::MouseButtonRelease:
234 case QEvent::MouseButtonDblClick:
235 case QEvent::MouseMove:
240 return QAbstractScrollArea::viewportEvent(e);
244 void View::resizeEvent(QResizeEvent *e)
246 _ruler->setGeometry(_viewport->x(), 0,
247 _viewport->width(), _viewport->y());
248 _header->setGeometry(0, _viewport->y(),
249 _viewport->x(), _viewport->height());
253 void View::h_scroll_value_changed(int value)
255 const int range = horizontalScrollBar()->maximum();
256 if(range < MaxScrollValue)
257 _offset = _scale * value;
259 double length = 0, offset;
260 get_scroll_layout(length, offset);
261 _offset = _scale * length * value / MaxScrollValue;
268 void View::v_scroll_value_changed(int value)
275 void View::data_updated()
277 // Get the new data length
279 shared_ptr<LogicData> sig_data = _session.get_data();
281 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
282 sig_data->get_snapshots();
283 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
285 _data_length = max(_data_length,
286 s->get_sample_count());
289 // Update the scroll bars
296 void View::marker_time_changed()