]> sigrok.org Git - pulseview.git/blame - pv/view/view.cpp
Partial fix to scroll overflow issues
[pulseview.git] / pv / view / view.cpp
CommitLineData
adb4b10c
JH
1/*
2 * This file is part of the sigrok 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>
f25770e2 22#include <limits.h>
adb4b10c
JH
23#include <math.h>
24
25#include <boost/foreach.hpp>
26
27#include <QEvent>
28#include <QScrollBar>
29
1d8dca91 30#include "header.h"
ccdd3ef5 31#include "ruler.h"
cdf7bea7
JH
32#include "view.h"
33#include "viewport.h"
adb4b10c 34
cdf7bea7
JH
35#include "../../logicdata.h"
36#include "../../logicdatasnapshot.h"
37#include "../../sigsession.h"
adb4b10c
JH
38
39using namespace boost;
40using namespace std;
41
cdf7bea7
JH
42namespace pv {
43namespace view {
adb4b10c 44
cdf7bea7
JH
45const double View::MaxScale = 1e9;
46const double View::MinScale = 1e-15;
adb4b10c 47
cdf7bea7
JH
48const int View::LabelMarginWidth = 70;
49const int View::RulerHeight = 30;
50
f25770e2
JH
51const int View::MaxScrollValue = INT_MAX / 2;
52
1d8dca91
JH
53const int View::SignalHeight = 50;
54
cdf7bea7 55View::View(SigSession &session, QWidget *parent) :
adb4b10c
JH
56 QAbstractScrollArea(parent),
57 _session(session),
cdf7bea7 58 _viewport(new Viewport(*this)),
ccdd3ef5 59 _ruler(new Ruler(*this)),
1d8dca91 60 _header(new Header(*this)),
adb4b10c
JH
61 _data_length(0),
62 _scale(1e-6),
63 _offset(0),
64 _v_offset(0)
65{
66 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
67 this, SLOT(h_scroll_value_changed(int)));
68 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
69 this, SLOT(v_scroll_value_changed(int)));
70 connect(&_session, SIGNAL(data_updated()),
71 this, SLOT(data_updated()));
1d8dca91 72
ccdd3ef5 73 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
adb4b10c
JH
74 setViewport(_viewport);
75}
76
1d19ef83
JH
77SigSession& View::session()
78{
79 return _session;
80}
81
cdf7bea7 82double View::scale() const
adb4b10c
JH
83{
84 return _scale;
85}
86
cdf7bea7 87double View::offset() const
adb4b10c
JH
88{
89 return _offset;
90}
91
cdf7bea7 92int View::v_offset() const
adb4b10c
JH
93{
94 return _v_offset;
95}
96
cdf7bea7 97void View::zoom(double steps)
adb4b10c
JH
98{
99 zoom(steps, (width() - LabelMarginWidth) / 2);
100}
101
17c0f398
JH
102void View::zoom(double steps, int offset)
103{
104 const double cursor_offset = _offset + _scale * offset;
105 _scale *= pow(3.0/2.0, -steps);
106 _scale = max(min(_scale, MaxScale), MinScale);
107 _offset = cursor_offset - _scale * offset;
108
109 _ruler->update();
110 _viewport->update();
111 update_scroll();
112}
113
114
cdf7bea7 115void View::set_scale_offset(double scale, double offset)
adb4b10c
JH
116{
117 _scale = scale;
118 _offset = offset;
ccdd3ef5 119
adb4b10c 120 update_scroll();
ccdd3ef5 121 _ruler->update();
adb4b10c
JH
122 _viewport->update();
123}
124
f25770e2
JH
125void View::get_scroll_layout(double &length, double &offset) const
126{
127 const shared_ptr<SignalData> sig_data = _session.get_data();
128 if(!sig_data)
129 return;
130
131 length = _data_length / (sig_data->get_samplerate() * _scale);
132 offset = _offset / _scale;
133}
134
cdf7bea7 135void View::update_scroll()
adb4b10c
JH
136{
137 assert(_viewport);
138
139 const QSize areaSize = _viewport->size();
140
141 // Set the horizontal scroll bar
142 double length = 0, offset = 0;
f25770e2
JH
143 get_scroll_layout(length, offset);
144 length = max(length - areaSize.width(), 0.0);
adb4b10c
JH
145
146 horizontalScrollBar()->setPageStep(areaSize.width());
f25770e2
JH
147
148 if(length < MaxScrollValue) {
149 horizontalScrollBar()->setRange(0, length);
150 horizontalScrollBar()->setSliderPosition(offset);
151 } else {
152 horizontalScrollBar()->setRange(0, MaxScrollValue);
153 horizontalScrollBar()->setSliderPosition(
154 _offset * MaxScrollValue / (_scale * length));
155 }
adb4b10c
JH
156
157 // Set the vertical scrollbar
158 verticalScrollBar()->setPageStep(areaSize.height());
159 verticalScrollBar()->setRange(0,
160 _viewport->get_total_height() - areaSize.height());
161}
162
cdf7bea7 163bool View::viewportEvent(QEvent *e)
adb4b10c
JH
164{
165 switch(e->type()) {
166 case QEvent::Paint:
167 case QEvent::MouseButtonPress:
168 case QEvent::MouseButtonRelease:
169 case QEvent::MouseButtonDblClick:
170 case QEvent::MouseMove:
171 case QEvent::Wheel:
172 return false;
173
174 default:
175 return QAbstractScrollArea::viewportEvent(e);
176 }
177}
178
cdf7bea7 179void View::resizeEvent(QResizeEvent *e)
adb4b10c 180{
ccdd3ef5
JH
181 _ruler->setGeometry(_viewport->x(), 0,
182 _viewport->width(), _viewport->y());
183 _header->setGeometry(0, _viewport->y(),
1d8dca91 184 _viewport->x(), _viewport->height());
adb4b10c
JH
185 update_scroll();
186}
187
cdf7bea7 188void View::h_scroll_value_changed(int value)
adb4b10c 189{
f25770e2
JH
190 const int range = horizontalScrollBar()->maximum();
191 if(range < MaxScrollValue)
192 _offset = _scale * value;
193 else {
194 double length = 0, offset;
195 get_scroll_layout(length, offset);
196 _offset = _scale * length * value / MaxScrollValue;
197 }
198
ccdd3ef5 199 _ruler->update();
adb4b10c
JH
200 _viewport->update();
201}
202
cdf7bea7 203void View::v_scroll_value_changed(int value)
adb4b10c
JH
204{
205 _v_offset = value;
1d8dca91 206 _header->update();
adb4b10c
JH
207 _viewport->update();
208}
209
cdf7bea7 210void View::data_updated()
adb4b10c
JH
211{
212 // Get the new data length
213 _data_length = 0;
214 shared_ptr<LogicData> sig_data = _session.get_data();
215 if(sig_data) {
216 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
217 sig_data->get_snapshots();
218 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
219 if(s)
220 _data_length = max(_data_length,
221 s->get_sample_count());
222 }
223
224 // Update the scroll bars
225 update_scroll();
226
227 // Repaint the view
228 _viewport->update();
229}
cdf7bea7
JH
230
231} // namespace view
232} // namespace pv