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 View::View(SigSession &session, QWidget *parent) :
59 QAbstractScrollArea(parent),
61 _viewport(new Viewport(*this)),
62 _ruler(new Ruler(*this)),
63 _header(new Header(*this)),
69 _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0),
73 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
74 this, SLOT(h_scroll_value_changed(int)));
75 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
76 this, SLOT(v_scroll_value_changed(int)));
77 connect(&_session, SIGNAL(data_updated()),
78 this, SLOT(data_updated()));
80 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
81 setViewport(_viewport);
83 _viewport->installEventFilter(this);
84 _ruler->installEventFilter(this);
85 _header->installEventFilter(this);
88 SigSession& View::session()
93 double View::scale() const
98 double View::offset() const
103 int View::v_offset() const
108 void View::zoom(double steps)
110 zoom(steps, (width() - LabelMarginWidth) / 2);
113 void View::zoom(double steps, int offset)
115 const double cursor_offset = _offset + _scale * offset;
116 _scale *= pow(3.0/2.0, -steps);
117 _scale = max(min(_scale, MaxScale), MinScale);
118 _offset = cursor_offset - _scale * offset;
126 void View::set_scale_offset(double scale, double offset)
136 bool View::cursors_shown() const
138 return _show_cursors;
141 void View::show_cursors(bool show)
143 _show_cursors = show;
148 std::pair<Cursor, Cursor>& View::cursors()
153 const QPoint& View::hover_point() const
158 void View::get_scroll_layout(double &length, double &offset) const
160 const shared_ptr<SignalData> sig_data = _session.get_data();
164 length = _data_length / (sig_data->get_samplerate() * _scale);
165 offset = _offset / _scale;
168 void View::update_scroll()
172 const QSize areaSize = _viewport->size();
174 // Set the horizontal scroll bar
175 double length = 0, offset = 0;
176 get_scroll_layout(length, offset);
177 length = max(length - areaSize.width(), 0.0);
179 horizontalScrollBar()->setPageStep(areaSize.width());
181 if(length < MaxScrollValue) {
182 horizontalScrollBar()->setRange(0, length);
183 horizontalScrollBar()->setSliderPosition(offset);
185 horizontalScrollBar()->setRange(0, MaxScrollValue);
186 horizontalScrollBar()->setSliderPosition(
187 _offset * MaxScrollValue / (_scale * length));
190 // Set the vertical scrollbar
191 verticalScrollBar()->setPageStep(areaSize.height());
192 verticalScrollBar()->setRange(0,
193 _viewport->get_total_height() - areaSize.height());
196 bool View::eventFilter(QObject *object, QEvent *event)
198 const QEvent::Type type = event->type();
199 if(type == QEvent::MouseMove) {
201 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
202 if(object == _viewport)
203 _hover_point = mouse_event->pos();
204 else if(object == _ruler)
205 _hover_point = QPoint(mouse_event->x(), 0);
206 else if(object == _header)
207 _hover_point = QPoint(0, mouse_event->y());
209 _hover_point = QPoint(-1, -1);
211 hover_point_changed();
213 } else if(type == QEvent::Leave) {
214 _hover_point = QPoint(-1, -1);
215 hover_point_changed();
218 return QObject::eventFilter(object, event);
221 bool View::viewportEvent(QEvent *e)
225 case QEvent::MouseButtonPress:
226 case QEvent::MouseButtonRelease:
227 case QEvent::MouseButtonDblClick:
228 case QEvent::MouseMove:
233 return QAbstractScrollArea::viewportEvent(e);
237 void View::resizeEvent(QResizeEvent *e)
239 _ruler->setGeometry(_viewport->x(), 0,
240 _viewport->width(), _viewport->y());
241 _header->setGeometry(0, _viewport->y(),
242 _viewport->x(), _viewport->height());
246 void View::h_scroll_value_changed(int value)
248 const int range = horizontalScrollBar()->maximum();
249 if(range < MaxScrollValue)
250 _offset = _scale * value;
252 double length = 0, offset;
253 get_scroll_layout(length, offset);
254 _offset = _scale * length * value / MaxScrollValue;
261 void View::v_scroll_value_changed(int value)
268 void View::data_updated()
270 // Get the new data length
272 shared_ptr<LogicData> sig_data = _session.get_data();
274 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
275 sig_data->get_snapshots();
276 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
278 _data_length = max(_data_length,
279 s->get_sample_count());
282 // Update the scroll bars