]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/view.cpp
Added hover point to pv::view::View
[pulseview.git] / pv / view / view.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <assert.h>
22#include <limits.h>
23#include <math.h>
24
25#include <boost/foreach.hpp>
26
27#include <QEvent>
28#include <QMouseEvent>
29#include <QScrollBar>
30
31#include "header.h"
32#include "ruler.h"
33#include "view.h"
34#include "viewport.h"
35
36#include "../logicdata.h"
37#include "../logicdatasnapshot.h"
38#include "../sigsession.h"
39
40using namespace boost;
41using namespace std;
42
43namespace pv {
44namespace view {
45
46const double View::MaxScale = 1e9;
47const double View::MinScale = 1e-15;
48
49const int View::LabelMarginWidth = 70;
50const int View::RulerHeight = 30;
51
52const int View::MaxScrollValue = INT_MAX / 2;
53
54const int View::SignalHeight = 50;
55
56View::View(SigSession &session, QWidget *parent) :
57 QAbstractScrollArea(parent),
58 _session(session),
59 _viewport(new Viewport(*this)),
60 _ruler(new Ruler(*this)),
61 _header(new Header(*this)),
62 _data_length(0),
63 _scale(1e-6),
64 _offset(0),
65 _v_offset(0),
66 _hover_point(-1, -1)
67{
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()));
74
75 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
76 setViewport(_viewport);
77
78 _viewport->installEventFilter(this);
79 _ruler->installEventFilter(this);
80 _header->installEventFilter(this);
81}
82
83SigSession& View::session()
84{
85 return _session;
86}
87
88double View::scale() const
89{
90 return _scale;
91}
92
93double View::offset() const
94{
95 return _offset;
96}
97
98int View::v_offset() const
99{
100 return _v_offset;
101}
102
103void View::zoom(double steps)
104{
105 zoom(steps, (width() - LabelMarginWidth) / 2);
106}
107
108void View::zoom(double steps, int offset)
109{
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;
114
115 _ruler->update();
116 _viewport->update();
117 update_scroll();
118}
119
120
121void View::set_scale_offset(double scale, double offset)
122{
123 _scale = scale;
124 _offset = offset;
125
126 update_scroll();
127 _ruler->update();
128 _viewport->update();
129}
130
131const QPoint& View::hover_point() const
132{
133 return _hover_point;
134}
135
136void View::get_scroll_layout(double &length, double &offset) const
137{
138 const shared_ptr<SignalData> sig_data = _session.get_data();
139 if(!sig_data)
140 return;
141
142 length = _data_length / (sig_data->get_samplerate() * _scale);
143 offset = _offset / _scale;
144}
145
146void View::update_scroll()
147{
148 assert(_viewport);
149
150 const QSize areaSize = _viewport->size();
151
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);
156
157 horizontalScrollBar()->setPageStep(areaSize.width());
158
159 if(length < MaxScrollValue) {
160 horizontalScrollBar()->setRange(0, length);
161 horizontalScrollBar()->setSliderPosition(offset);
162 } else {
163 horizontalScrollBar()->setRange(0, MaxScrollValue);
164 horizontalScrollBar()->setSliderPosition(
165 _offset * MaxScrollValue / (_scale * length));
166 }
167
168 // Set the vertical scrollbar
169 verticalScrollBar()->setPageStep(areaSize.height());
170 verticalScrollBar()->setRange(0,
171 _viewport->get_total_height() - areaSize.height());
172}
173
174bool View::eventFilter(QObject *object, QEvent *event)
175{
176 const QEvent::Type type = event->type();
177 if(type == QEvent::MouseMove) {
178
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());
186 else
187 _hover_point = QPoint(-1, -1);
188
189 hover_point_changed();
190
191 } else if(type == QEvent::Leave) {
192 _hover_point = QPoint(-1, -1);
193 hover_point_changed();
194 }
195
196 return QObject::eventFilter(object, event);
197}
198
199bool View::viewportEvent(QEvent *e)
200{
201 switch(e->type()) {
202 case QEvent::Paint:
203 case QEvent::MouseButtonPress:
204 case QEvent::MouseButtonRelease:
205 case QEvent::MouseButtonDblClick:
206 case QEvent::MouseMove:
207 case QEvent::Wheel:
208 return false;
209
210 default:
211 return QAbstractScrollArea::viewportEvent(e);
212 }
213}
214
215void View::resizeEvent(QResizeEvent *e)
216{
217 _ruler->setGeometry(_viewport->x(), 0,
218 _viewport->width(), _viewport->y());
219 _header->setGeometry(0, _viewport->y(),
220 _viewport->x(), _viewport->height());
221 update_scroll();
222}
223
224void View::h_scroll_value_changed(int value)
225{
226 const int range = horizontalScrollBar()->maximum();
227 if(range < MaxScrollValue)
228 _offset = _scale * value;
229 else {
230 double length = 0, offset;
231 get_scroll_layout(length, offset);
232 _offset = _scale * length * value / MaxScrollValue;
233 }
234
235 _ruler->update();
236 _viewport->update();
237}
238
239void View::v_scroll_value_changed(int value)
240{
241 _v_offset = value;
242 _header->update();
243 _viewport->update();
244}
245
246void View::data_updated()
247{
248 // Get the new data length
249 _data_length = 0;
250 shared_ptr<LogicData> sig_data = _session.get_data();
251 if(sig_data) {
252 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
253 sig_data->get_snapshots();
254 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
255 if(s)
256 _data_length = max(_data_length,
257 s->get_sample_count());
258 }
259
260 // Update the scroll bars
261 update_scroll();
262
263 // Repaint the view
264 _viewport->update();
265}
266
267} // namespace view
268} // namespace pv