]>
Commit | Line | Data |
---|---|---|
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 "signal.h" | |
34 | #include "view.h" | |
35 | #include "viewport.h" | |
36 | ||
37 | #include "pv/sigsession.h" | |
38 | #include "pv/data/logic.h" | |
39 | #include "pv/data/logicsnapshot.h" | |
40 | ||
41 | using namespace boost; | |
42 | using namespace std; | |
43 | ||
44 | namespace pv { | |
45 | namespace view { | |
46 | ||
47 | const double View::MaxScale = 1e9; | |
48 | const double View::MinScale = 1e-15; | |
49 | ||
50 | const int View::LabelMarginWidth = 70; | |
51 | const int View::RulerHeight = 30; | |
52 | ||
53 | const int View::MaxScrollValue = INT_MAX / 2; | |
54 | ||
55 | const int View::SignalHeight = 30; | |
56 | const int View::SignalMargin = 10; | |
57 | const int View::SignalSnapGridSize = 10; | |
58 | ||
59 | const QColor View::CursorAreaColour(220, 231, 243); | |
60 | ||
61 | const QSizeF View::LabelPadding(4, 0); | |
62 | ||
63 | View::View(SigSession &session, QWidget *parent) : | |
64 | QAbstractScrollArea(parent), | |
65 | _session(session), | |
66 | _viewport(new Viewport(*this)), | |
67 | _ruler(new Ruler(*this)), | |
68 | _header(new Header(*this)), | |
69 | _data_length(0), | |
70 | _scale(1e-6), | |
71 | _offset(0), | |
72 | _v_offset(0), | |
73 | _updating_scroll(false), | |
74 | _show_cursors(false), | |
75 | _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0), | |
76 | Cursor(*this, 1.0))), | |
77 | _hover_point(-1, -1) | |
78 | { | |
79 | connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), | |
80 | this, SLOT(h_scroll_value_changed(int))); | |
81 | connect(verticalScrollBar(), SIGNAL(valueChanged(int)), | |
82 | this, SLOT(v_scroll_value_changed(int))); | |
83 | ||
84 | connect(&_session, SIGNAL(signals_changed()), | |
85 | this, SLOT(signals_changed())); | |
86 | connect(&_session, SIGNAL(data_updated()), | |
87 | this, SLOT(data_updated())); | |
88 | ||
89 | connect(&_cursors.first, SIGNAL(time_changed()), | |
90 | this, SLOT(marker_time_changed())); | |
91 | connect(&_cursors.second, SIGNAL(time_changed()), | |
92 | this, SLOT(marker_time_changed())); | |
93 | ||
94 | connect(_header, SIGNAL(signals_moved()), | |
95 | this, SLOT(on_signals_moved())); | |
96 | ||
97 | setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0); | |
98 | setViewport(_viewport); | |
99 | ||
100 | _viewport->installEventFilter(this); | |
101 | _ruler->installEventFilter(this); | |
102 | _header->installEventFilter(this); | |
103 | } | |
104 | ||
105 | SigSession& View::session() | |
106 | { | |
107 | return _session; | |
108 | } | |
109 | ||
110 | double View::scale() const | |
111 | { | |
112 | return _scale; | |
113 | } | |
114 | ||
115 | double View::offset() const | |
116 | { | |
117 | return _offset; | |
118 | } | |
119 | ||
120 | int View::v_offset() const | |
121 | { | |
122 | return _v_offset; | |
123 | } | |
124 | ||
125 | void View::zoom(double steps) | |
126 | { | |
127 | zoom(steps, (width() - LabelMarginWidth) / 2); | |
128 | } | |
129 | ||
130 | void View::zoom(double steps, int offset) | |
131 | { | |
132 | const double cursor_offset = _offset + _scale * offset; | |
133 | _scale *= pow(3.0/2.0, -steps); | |
134 | _scale = max(min(_scale, MaxScale), MinScale); | |
135 | _offset = cursor_offset - _scale * offset; | |
136 | ||
137 | _ruler->update(); | |
138 | _viewport->update(); | |
139 | update_scroll(); | |
140 | } | |
141 | ||
142 | ||
143 | void View::set_scale_offset(double scale, double offset) | |
144 | { | |
145 | _scale = scale; | |
146 | _offset = offset; | |
147 | ||
148 | update_scroll(); | |
149 | _ruler->update(); | |
150 | _viewport->update(); | |
151 | } | |
152 | ||
153 | bool View::cursors_shown() const | |
154 | { | |
155 | return _show_cursors; | |
156 | } | |
157 | ||
158 | void View::show_cursors(bool show) | |
159 | { | |
160 | _show_cursors = show; | |
161 | _ruler->update(); | |
162 | _viewport->update(); | |
163 | } | |
164 | ||
165 | std::pair<Cursor, Cursor>& View::cursors() | |
166 | { | |
167 | return _cursors; | |
168 | } | |
169 | ||
170 | const QPoint& View::hover_point() const | |
171 | { | |
172 | return _hover_point; | |
173 | } | |
174 | ||
175 | void View::normalize_layout() | |
176 | { | |
177 | const vector< shared_ptr<Signal> > sigs(_session.get_signals()); | |
178 | ||
179 | int v_min = INT_MAX; | |
180 | BOOST_FOREACH(const shared_ptr<Signal> s, sigs) | |
181 | v_min = min(s->get_v_offset(), v_min); | |
182 | ||
183 | const int delta = -min(v_min, 0); | |
184 | BOOST_FOREACH(shared_ptr<Signal> s, sigs) | |
185 | s->set_v_offset(s->get_v_offset() + delta); | |
186 | ||
187 | verticalScrollBar()->setSliderPosition(_v_offset + delta); | |
188 | v_scroll_value_changed(verticalScrollBar()->sliderPosition()); | |
189 | } | |
190 | ||
191 | void View::get_scroll_layout(double &length, double &offset) const | |
192 | { | |
193 | const shared_ptr<data::SignalData> sig_data = _session.get_data(); | |
194 | if (!sig_data) | |
195 | return; | |
196 | ||
197 | length = _data_length / (sig_data->get_samplerate() * _scale); | |
198 | offset = _offset / _scale; | |
199 | } | |
200 | ||
201 | void View::update_scroll() | |
202 | { | |
203 | assert(_viewport); | |
204 | ||
205 | const QSize areaSize = _viewport->size(); | |
206 | ||
207 | // Set the horizontal scroll bar | |
208 | double length = 0, offset = 0; | |
209 | get_scroll_layout(length, offset); | |
210 | length = max(length - areaSize.width(), 0.0); | |
211 | ||
212 | horizontalScrollBar()->setPageStep(areaSize.width() / 2); | |
213 | ||
214 | _updating_scroll = true; | |
215 | ||
216 | if (length < MaxScrollValue) { | |
217 | horizontalScrollBar()->setRange(0, length); | |
218 | horizontalScrollBar()->setSliderPosition(offset); | |
219 | } else { | |
220 | horizontalScrollBar()->setRange(0, MaxScrollValue); | |
221 | horizontalScrollBar()->setSliderPosition( | |
222 | _offset * MaxScrollValue / (_scale * length)); | |
223 | } | |
224 | ||
225 | _updating_scroll = false; | |
226 | ||
227 | // Set the vertical scrollbar | |
228 | verticalScrollBar()->setPageStep(areaSize.height()); | |
229 | verticalScrollBar()->setRange(0, | |
230 | _viewport->get_total_height() + SignalMargin - | |
231 | areaSize.height()); | |
232 | } | |
233 | ||
234 | void View::reset_signal_layout() | |
235 | { | |
236 | int offset = SignalMargin + SignalHeight; | |
237 | const vector< shared_ptr<Signal> > sigs(_session.get_signals()); | |
238 | BOOST_FOREACH(shared_ptr<Signal> s, sigs) { | |
239 | s->set_v_offset(offset); | |
240 | offset += SignalHeight + 2 * SignalMargin; | |
241 | } | |
242 | ||
243 | normalize_layout(); | |
244 | } | |
245 | ||
246 | bool View::eventFilter(QObject *object, QEvent *event) | |
247 | { | |
248 | const QEvent::Type type = event->type(); | |
249 | if (type == QEvent::MouseMove) { | |
250 | ||
251 | const QMouseEvent *const mouse_event = (QMouseEvent*)event; | |
252 | if (object == _viewport) | |
253 | _hover_point = mouse_event->pos(); | |
254 | else if (object == _ruler) | |
255 | _hover_point = QPoint(mouse_event->x(), 0); | |
256 | else if (object == _header) | |
257 | _hover_point = QPoint(0, mouse_event->y()); | |
258 | else | |
259 | _hover_point = QPoint(-1, -1); | |
260 | ||
261 | hover_point_changed(); | |
262 | ||
263 | } else if (type == QEvent::Leave) { | |
264 | _hover_point = QPoint(-1, -1); | |
265 | hover_point_changed(); | |
266 | } | |
267 | ||
268 | return QObject::eventFilter(object, event); | |
269 | } | |
270 | ||
271 | bool View::viewportEvent(QEvent *e) | |
272 | { | |
273 | switch(e->type()) { | |
274 | case QEvent::Paint: | |
275 | case QEvent::MouseButtonPress: | |
276 | case QEvent::MouseButtonRelease: | |
277 | case QEvent::MouseButtonDblClick: | |
278 | case QEvent::MouseMove: | |
279 | case QEvent::Wheel: | |
280 | return false; | |
281 | ||
282 | default: | |
283 | return QAbstractScrollArea::viewportEvent(e); | |
284 | } | |
285 | } | |
286 | ||
287 | void View::resizeEvent(QResizeEvent*) | |
288 | { | |
289 | _ruler->setGeometry(_viewport->x(), 0, | |
290 | _viewport->width(), _viewport->y()); | |
291 | _header->setGeometry(0, _viewport->y(), | |
292 | _viewport->x(), _viewport->height()); | |
293 | update_scroll(); | |
294 | } | |
295 | ||
296 | void View::h_scroll_value_changed(int value) | |
297 | { | |
298 | if (_updating_scroll) | |
299 | return; | |
300 | ||
301 | const int range = horizontalScrollBar()->maximum(); | |
302 | if (range < MaxScrollValue) | |
303 | _offset = _scale * value; | |
304 | else { | |
305 | double length = 0, offset; | |
306 | get_scroll_layout(length, offset); | |
307 | _offset = _scale * length * value / MaxScrollValue; | |
308 | } | |
309 | ||
310 | _ruler->update(); | |
311 | _viewport->update(); | |
312 | } | |
313 | ||
314 | void View::v_scroll_value_changed(int value) | |
315 | { | |
316 | _v_offset = value; | |
317 | _header->update(); | |
318 | _viewport->update(); | |
319 | } | |
320 | ||
321 | void View::signals_changed() | |
322 | { | |
323 | reset_signal_layout(); | |
324 | } | |
325 | ||
326 | void View::data_updated() | |
327 | { | |
328 | // Get the new data length | |
329 | _data_length = 0; | |
330 | shared_ptr<data::Logic> sig_data = _session.get_data(); | |
331 | if (sig_data) { | |
332 | deque< shared_ptr<data::LogicSnapshot> > &snapshots = | |
333 | sig_data->get_snapshots(); | |
334 | BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots) | |
335 | if (s) | |
336 | _data_length = max(_data_length, | |
337 | s->get_sample_count()); | |
338 | } | |
339 | ||
340 | // Update the scroll bars | |
341 | update_scroll(); | |
342 | ||
343 | // Repaint the view | |
344 | _viewport->update(); | |
345 | } | |
346 | ||
347 | void View::marker_time_changed() | |
348 | { | |
349 | _ruler->update(); | |
350 | _viewport->update(); | |
351 | } | |
352 | ||
353 | void View::on_signals_moved() | |
354 | { | |
355 | update_scroll(); | |
356 | signals_moved(); | |
357 | } | |
358 | ||
359 | } // namespace view | |
360 | } // namespace pv |