]>
Commit | Line | Data |
---|---|---|
adb4b10c | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
adb4b10c JH |
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> | |
cbd80f64 | 28 | #include <QMouseEvent> |
adb4b10c JH |
29 | #include <QScrollBar> |
30 | ||
1d8dca91 | 31 | #include "header.h" |
ccdd3ef5 | 32 | #include "ruler.h" |
cdf7bea7 JH |
33 | #include "view.h" |
34 | #include "viewport.h" | |
adb4b10c | 35 | |
51e77110 JH |
36 | #include "../logicdata.h" |
37 | #include "../logicdatasnapshot.h" | |
38 | #include "../sigsession.h" | |
adb4b10c JH |
39 | |
40 | using namespace boost; | |
41 | using namespace std; | |
42 | ||
cdf7bea7 JH |
43 | namespace pv { |
44 | namespace view { | |
adb4b10c | 45 | |
cdf7bea7 JH |
46 | const double View::MaxScale = 1e9; |
47 | const double View::MinScale = 1e-15; | |
adb4b10c | 48 | |
cdf7bea7 JH |
49 | const int View::LabelMarginWidth = 70; |
50 | const int View::RulerHeight = 30; | |
51 | ||
f25770e2 JH |
52 | const int View::MaxScrollValue = INT_MAX / 2; |
53 | ||
1d8dca91 JH |
54 | const int View::SignalHeight = 50; |
55 | ||
cdf7bea7 | 56 | View::View(SigSession &session, QWidget *parent) : |
adb4b10c JH |
57 | QAbstractScrollArea(parent), |
58 | _session(session), | |
cdf7bea7 | 59 | _viewport(new Viewport(*this)), |
ccdd3ef5 | 60 | _ruler(new Ruler(*this)), |
1d8dca91 | 61 | _header(new Header(*this)), |
adb4b10c JH |
62 | _data_length(0), |
63 | _scale(1e-6), | |
64 | _offset(0), | |
cbd80f64 JH |
65 | _v_offset(0), |
66 | _hover_point(-1, -1) | |
adb4b10c JH |
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())); | |
1d8dca91 | 74 | |
ccdd3ef5 | 75 | setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0); |
adb4b10c | 76 | setViewport(_viewport); |
cbd80f64 JH |
77 | |
78 | _viewport->installEventFilter(this); | |
79 | _ruler->installEventFilter(this); | |
80 | _header->installEventFilter(this); | |
adb4b10c JH |
81 | } |
82 | ||
1d19ef83 JH |
83 | SigSession& View::session() |
84 | { | |
85 | return _session; | |
86 | } | |
87 | ||
cdf7bea7 | 88 | double View::scale() const |
adb4b10c JH |
89 | { |
90 | return _scale; | |
91 | } | |
92 | ||
cdf7bea7 | 93 | double View::offset() const |
adb4b10c JH |
94 | { |
95 | return _offset; | |
96 | } | |
97 | ||
cdf7bea7 | 98 | int View::v_offset() const |
adb4b10c JH |
99 | { |
100 | return _v_offset; | |
101 | } | |
102 | ||
cdf7bea7 | 103 | void View::zoom(double steps) |
adb4b10c JH |
104 | { |
105 | zoom(steps, (width() - LabelMarginWidth) / 2); | |
106 | } | |
107 | ||
17c0f398 JH |
108 | void 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 | ||
cdf7bea7 | 121 | void View::set_scale_offset(double scale, double offset) |
adb4b10c JH |
122 | { |
123 | _scale = scale; | |
124 | _offset = offset; | |
ccdd3ef5 | 125 | |
adb4b10c | 126 | update_scroll(); |
ccdd3ef5 | 127 | _ruler->update(); |
adb4b10c JH |
128 | _viewport->update(); |
129 | } | |
130 | ||
cbd80f64 JH |
131 | const QPoint& View::hover_point() const |
132 | { | |
133 | return _hover_point; | |
134 | } | |
135 | ||
f25770e2 JH |
136 | void 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 | ||
cdf7bea7 | 146 | void View::update_scroll() |
adb4b10c JH |
147 | { |
148 | assert(_viewport); | |
149 | ||
150 | const QSize areaSize = _viewport->size(); | |
151 | ||
152 | // Set the horizontal scroll bar | |
153 | double length = 0, offset = 0; | |
f25770e2 JH |
154 | get_scroll_layout(length, offset); |
155 | length = max(length - areaSize.width(), 0.0); | |
adb4b10c JH |
156 | |
157 | horizontalScrollBar()->setPageStep(areaSize.width()); | |
f25770e2 JH |
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 | } | |
adb4b10c JH |
167 | |
168 | // Set the vertical scrollbar | |
169 | verticalScrollBar()->setPageStep(areaSize.height()); | |
170 | verticalScrollBar()->setRange(0, | |
171 | _viewport->get_total_height() - areaSize.height()); | |
172 | } | |
173 | ||
cbd80f64 JH |
174 | bool 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 | ||
cdf7bea7 | 199 | bool View::viewportEvent(QEvent *e) |
adb4b10c JH |
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 | ||
cdf7bea7 | 215 | void View::resizeEvent(QResizeEvent *e) |
adb4b10c | 216 | { |
ccdd3ef5 JH |
217 | _ruler->setGeometry(_viewport->x(), 0, |
218 | _viewport->width(), _viewport->y()); | |
219 | _header->setGeometry(0, _viewport->y(), | |
1d8dca91 | 220 | _viewport->x(), _viewport->height()); |
adb4b10c JH |
221 | update_scroll(); |
222 | } | |
223 | ||
cdf7bea7 | 224 | void View::h_scroll_value_changed(int value) |
adb4b10c | 225 | { |
f25770e2 JH |
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 | ||
ccdd3ef5 | 235 | _ruler->update(); |
adb4b10c JH |
236 | _viewport->update(); |
237 | } | |
238 | ||
cdf7bea7 | 239 | void View::v_scroll_value_changed(int value) |
adb4b10c JH |
240 | { |
241 | _v_offset = value; | |
1d8dca91 | 242 | _header->update(); |
adb4b10c JH |
243 | _viewport->update(); |
244 | } | |
245 | ||
cdf7bea7 | 246 | void View::data_updated() |
adb4b10c JH |
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 | } | |
cdf7bea7 JH |
266 | |
267 | } // namespace view | |
268 | } // namespace pv |