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 View::View(SigSession &session, QWidget *parent) :
57 QAbstractScrollArea(parent),
59 _viewport(new Viewport(*this)),
60 _ruler(new Ruler(*this)),
61 _header(new Header(*this)),
68 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
69 this, SLOT(h_scroll_value_changed(int)));
70 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
71 this, SLOT(v_scroll_value_changed(int)));
72 connect(&_session, SIGNAL(data_updated()),
73 this, SLOT(data_updated()));
75 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
76 setViewport(_viewport);
78 _viewport->installEventFilter(this);
79 _ruler->installEventFilter(this);
80 _header->installEventFilter(this);
83 SigSession& View::session()
88 double View::scale() const
93 double View::offset() const
98 int View::v_offset() const
103 void View::zoom(double steps)
105 zoom(steps, (width() - LabelMarginWidth) / 2);
108 void View::zoom(double steps, int offset)
110 const double cursor_offset = _offset + _scale * offset;
111 _scale *= pow(3.0/2.0, -steps);
112 _scale = max(min(_scale, MaxScale), MinScale);
113 _offset = cursor_offset - _scale * offset;
121 void View::set_scale_offset(double scale, double offset)
131 const QPoint& View::hover_point() const
136 void View::get_scroll_layout(double &length, double &offset) const
138 const shared_ptr<SignalData> sig_data = _session.get_data();
142 length = _data_length / (sig_data->get_samplerate() * _scale);
143 offset = _offset / _scale;
146 void View::update_scroll()
150 const QSize areaSize = _viewport->size();
152 // Set the horizontal scroll bar
153 double length = 0, offset = 0;
154 get_scroll_layout(length, offset);
155 length = max(length - areaSize.width(), 0.0);
157 horizontalScrollBar()->setPageStep(areaSize.width());
159 if(length < MaxScrollValue) {
160 horizontalScrollBar()->setRange(0, length);
161 horizontalScrollBar()->setSliderPosition(offset);
163 horizontalScrollBar()->setRange(0, MaxScrollValue);
164 horizontalScrollBar()->setSliderPosition(
165 _offset * MaxScrollValue / (_scale * length));
168 // Set the vertical scrollbar
169 verticalScrollBar()->setPageStep(areaSize.height());
170 verticalScrollBar()->setRange(0,
171 _viewport->get_total_height() - areaSize.height());
174 bool View::eventFilter(QObject *object, QEvent *event)
176 const QEvent::Type type = event->type();
177 if(type == QEvent::MouseMove) {
179 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
180 if(object == _viewport)
181 _hover_point = mouse_event->pos();
182 else if(object == _ruler)
183 _hover_point = QPoint(mouse_event->x(), 0);
184 else if(object == _header)
185 _hover_point = QPoint(0, mouse_event->y());
187 _hover_point = QPoint(-1, -1);
189 hover_point_changed();
191 } else if(type == QEvent::Leave) {
192 _hover_point = QPoint(-1, -1);
193 hover_point_changed();
196 return QObject::eventFilter(object, event);
199 bool View::viewportEvent(QEvent *e)
203 case QEvent::MouseButtonPress:
204 case QEvent::MouseButtonRelease:
205 case QEvent::MouseButtonDblClick:
206 case QEvent::MouseMove:
211 return QAbstractScrollArea::viewportEvent(e);
215 void View::resizeEvent(QResizeEvent *e)
217 _ruler->setGeometry(_viewport->x(), 0,
218 _viewport->width(), _viewport->y());
219 _header->setGeometry(0, _viewport->y(),
220 _viewport->x(), _viewport->height());
224 void View::h_scroll_value_changed(int value)
226 const int range = horizontalScrollBar()->maximum();
227 if(range < MaxScrollValue)
228 _offset = _scale * value;
230 double length = 0, offset;
231 get_scroll_layout(length, offset);
232 _offset = _scale * length * value / MaxScrollValue;
239 void View::v_scroll_value_changed(int value)
246 void View::data_updated()
248 // Get the new data length
250 shared_ptr<LogicData> sig_data = _session.get_data();
252 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
253 sig_data->get_snapshots();
254 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
256 _data_length = max(_data_length,
257 s->get_sample_count());
260 // Update the scroll bars