]>
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 | #ifdef ENABLE_DECODE | |
22 | #include <libsigrokdecode/libsigrokdecode.h> | |
23 | #endif | |
24 | ||
25 | #include <assert.h> | |
26 | #include <limits.h> | |
27 | #include <math.h> | |
28 | ||
29 | #include <boost/foreach.hpp> | |
30 | ||
31 | #include <QEvent> | |
32 | #include <QMouseEvent> | |
33 | #include <QScrollBar> | |
34 | ||
35 | #include "decodetrace.h" | |
36 | #include "header.h" | |
37 | #include "ruler.h" | |
38 | #include "signal.h" | |
39 | #include "view.h" | |
40 | #include "viewport.h" | |
41 | ||
42 | #include "pv/sigsession.h" | |
43 | #include "pv/data/logic.h" | |
44 | #include "pv/data/logicsnapshot.h" | |
45 | ||
46 | using boost::shared_ptr; | |
47 | using boost::weak_ptr; | |
48 | using pv::data::SignalData; | |
49 | using std::deque; | |
50 | using std::list; | |
51 | using std::max; | |
52 | using std::make_pair; | |
53 | using std::min; | |
54 | using std::pair; | |
55 | using std::set; | |
56 | using std::vector; | |
57 | ||
58 | namespace pv { | |
59 | namespace view { | |
60 | ||
61 | const double View::MaxScale = 1e9; | |
62 | const double View::MinScale = 1e-15; | |
63 | ||
64 | const int View::MaxScrollValue = INT_MAX / 2; | |
65 | ||
66 | const int View::SignalHeight = 30; | |
67 | const int View::SignalMargin = 10; | |
68 | const int View::SignalSnapGridSize = 10; | |
69 | ||
70 | const QColor View::CursorAreaColour(220, 231, 243); | |
71 | ||
72 | const QSizeF View::LabelPadding(4, 0); | |
73 | ||
74 | View::View(SigSession &session, QWidget *parent) : | |
75 | QAbstractScrollArea(parent), | |
76 | _session(session), | |
77 | _viewport(new Viewport(*this)), | |
78 | _ruler(new Ruler(*this)), | |
79 | _header(new Header(*this)), | |
80 | _scale(1e-6), | |
81 | _offset(0), | |
82 | _v_offset(0), | |
83 | _updating_scroll(false), | |
84 | _show_cursors(false), | |
85 | _cursors(*this), | |
86 | _hover_point(-1, -1) | |
87 | { | |
88 | connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), | |
89 | this, SLOT(h_scroll_value_changed(int))); | |
90 | connect(verticalScrollBar(), SIGNAL(valueChanged(int)), | |
91 | this, SLOT(v_scroll_value_changed(int))); | |
92 | ||
93 | connect(&_session, SIGNAL(signals_changed()), | |
94 | this, SLOT(signals_changed())); | |
95 | connect(&_session, SIGNAL(data_updated()), | |
96 | this, SLOT(data_updated())); | |
97 | ||
98 | connect(_cursors.first().get(), SIGNAL(time_changed()), | |
99 | this, SLOT(marker_time_changed())); | |
100 | connect(_cursors.second().get(), SIGNAL(time_changed()), | |
101 | this, SLOT(marker_time_changed())); | |
102 | ||
103 | connect(_header, SIGNAL(geometry_updated()), | |
104 | this, SLOT(on_geometry_updated())); | |
105 | connect(_header, SIGNAL(signals_moved()), | |
106 | this, SLOT(on_signals_moved())); | |
107 | ||
108 | connect(_header, SIGNAL(selection_changed()), | |
109 | _ruler, SLOT(clear_selection())); | |
110 | connect(_ruler, SIGNAL(selection_changed()), | |
111 | _header, SLOT(clear_selection())); | |
112 | ||
113 | connect(_header, SIGNAL(selection_changed()), | |
114 | this, SIGNAL(selection_changed())); | |
115 | connect(_ruler, SIGNAL(selection_changed()), | |
116 | this, SIGNAL(selection_changed())); | |
117 | ||
118 | setViewport(_viewport); | |
119 | ||
120 | _viewport->installEventFilter(this); | |
121 | _ruler->installEventFilter(this); | |
122 | _header->installEventFilter(this); | |
123 | ||
124 | signals_changed(); | |
125 | } | |
126 | ||
127 | SigSession& View::session() | |
128 | { | |
129 | return _session; | |
130 | } | |
131 | ||
132 | const SigSession& View::session() const | |
133 | { | |
134 | return _session; | |
135 | } | |
136 | ||
137 | double View::scale() const | |
138 | { | |
139 | return _scale; | |
140 | } | |
141 | ||
142 | double View::offset() const | |
143 | { | |
144 | return _offset; | |
145 | } | |
146 | ||
147 | int View::v_offset() const | |
148 | { | |
149 | return _v_offset; | |
150 | } | |
151 | ||
152 | void View::zoom(double steps) | |
153 | { | |
154 | zoom(steps, _viewport->width() / 2); | |
155 | } | |
156 | ||
157 | void View::zoom(double steps, int offset) | |
158 | { | |
159 | set_zoom(_scale * pow(3.0/2.0, -steps), offset); | |
160 | } | |
161 | ||
162 | void View::zoom_fit() | |
163 | { | |
164 | const pair<double, double> extents = get_time_extents(); | |
165 | const double delta = extents.second - extents.first; | |
166 | if (delta < 1e-12) | |
167 | return; | |
168 | ||
169 | assert(_viewport); | |
170 | const int w = _viewport->width(); | |
171 | if (w <= 0) | |
172 | return; | |
173 | ||
174 | const double scale = max(min(delta / w, MaxScale), MinScale); | |
175 | set_scale_offset(scale, extents.first); | |
176 | } | |
177 | ||
178 | void View::zoom_one_to_one() | |
179 | { | |
180 | using pv::data::SignalData; | |
181 | ||
182 | const vector< shared_ptr<Signal> > sigs( | |
183 | session().get_signals()); | |
184 | ||
185 | // Make a set of all the visible data objects | |
186 | set< shared_ptr<SignalData> > visible_data = get_visible_data(); | |
187 | if (visible_data.empty()) | |
188 | return; | |
189 | ||
190 | double samplerate = 0.0; | |
191 | BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data) { | |
192 | assert(d); | |
193 | samplerate = max(samplerate, d->samplerate()); | |
194 | } | |
195 | ||
196 | if (samplerate == 0.0) | |
197 | return; | |
198 | ||
199 | assert(_viewport); | |
200 | const int w = _viewport->width(); | |
201 | if (w <= 0) | |
202 | return; | |
203 | ||
204 | set_zoom(1.0 / samplerate, w / 2); | |
205 | } | |
206 | ||
207 | void View::set_scale_offset(double scale, double offset) | |
208 | { | |
209 | _scale = scale; | |
210 | _offset = offset; | |
211 | ||
212 | update_scroll(); | |
213 | _ruler->update(); | |
214 | _viewport->update(); | |
215 | scale_offset_changed(); | |
216 | } | |
217 | ||
218 | vector< shared_ptr<Trace> > View::get_traces() const | |
219 | { | |
220 | const vector< shared_ptr<Signal> > sigs( | |
221 | session().get_signals()); | |
222 | #ifdef ENABLE_DECODE | |
223 | const vector< shared_ptr<DecodeTrace> > decode_sigs( | |
224 | session().get_decode_signals()); | |
225 | vector< shared_ptr<Trace> > traces( | |
226 | sigs.size() + decode_sigs.size()); | |
227 | #else | |
228 | vector< shared_ptr<Trace> > traces(sigs.size()); | |
229 | #endif | |
230 | ||
231 | vector< shared_ptr<Trace> >::iterator i = traces.begin(); | |
232 | i = copy(sigs.begin(), sigs.end(), i); | |
233 | #ifdef ENABLE_DECODE | |
234 | i = copy(decode_sigs.begin(), decode_sigs.end(), i); | |
235 | #endif | |
236 | ||
237 | stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets); | |
238 | return traces; | |
239 | } | |
240 | ||
241 | list<weak_ptr<SelectableItem> > View::selected_items() const | |
242 | { | |
243 | list<weak_ptr<SelectableItem> > items; | |
244 | ||
245 | // List the selected signals | |
246 | const vector< shared_ptr<Trace> > traces(get_traces()); | |
247 | BOOST_FOREACH (shared_ptr<Trace> t, traces) { | |
248 | assert(t); | |
249 | if (t->selected()) | |
250 | items.push_back(t); | |
251 | } | |
252 | ||
253 | // List the selected cursors | |
254 | if (_cursors.first()->selected()) | |
255 | items.push_back(_cursors.first()); | |
256 | if (_cursors.second()->selected()) | |
257 | items.push_back(_cursors.second()); | |
258 | ||
259 | return items; | |
260 | } | |
261 | ||
262 | set< shared_ptr<SignalData> > View::get_visible_data() const | |
263 | { | |
264 | const vector< shared_ptr<Signal> > sigs( | |
265 | session().get_signals()); | |
266 | ||
267 | // Make a set of all the visible data objects | |
268 | set< shared_ptr<SignalData> > visible_data; | |
269 | BOOST_FOREACH(const shared_ptr<Signal> sig, sigs) | |
270 | if (sig->enabled()) | |
271 | visible_data.insert(sig->data()); | |
272 | ||
273 | return visible_data; | |
274 | } | |
275 | ||
276 | pair<double, double> View::get_time_extents() const | |
277 | { | |
278 | const set< shared_ptr<SignalData> > visible_data = get_visible_data(); | |
279 | if (visible_data.empty()) | |
280 | return make_pair(0.0, 0.0); | |
281 | ||
282 | double left_time = DBL_MAX, right_time = DBL_MIN; | |
283 | BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data) | |
284 | { | |
285 | const double start_time = d->get_start_time(); | |
286 | double samplerate = d->samplerate(); | |
287 | samplerate = (samplerate <= 0.0) ? 1.0 : samplerate; | |
288 | ||
289 | left_time = min(left_time, start_time); | |
290 | right_time = max(right_time, start_time + | |
291 | d->get_max_sample_count() / samplerate); | |
292 | } | |
293 | ||
294 | assert(left_time < right_time); | |
295 | return make_pair(left_time, right_time); | |
296 | } | |
297 | ||
298 | bool View::cursors_shown() const | |
299 | { | |
300 | return _show_cursors; | |
301 | } | |
302 | ||
303 | void View::show_cursors(bool show) | |
304 | { | |
305 | _show_cursors = show; | |
306 | _ruler->update(); | |
307 | _viewport->update(); | |
308 | } | |
309 | ||
310 | void View::centre_cursors() | |
311 | { | |
312 | const double time_width = _scale * _viewport->width(); | |
313 | _cursors.first()->set_time(_offset + time_width * 0.4); | |
314 | _cursors.second()->set_time(_offset + time_width * 0.6); | |
315 | _ruler->update(); | |
316 | _viewport->update(); | |
317 | } | |
318 | ||
319 | CursorPair& View::cursors() | |
320 | { | |
321 | return _cursors; | |
322 | } | |
323 | ||
324 | const CursorPair& View::cursors() const | |
325 | { | |
326 | return _cursors; | |
327 | } | |
328 | ||
329 | const QPoint& View::hover_point() const | |
330 | { | |
331 | return _hover_point; | |
332 | } | |
333 | ||
334 | void View::normalize_layout() | |
335 | { | |
336 | const vector< shared_ptr<Trace> > traces(get_traces()); | |
337 | ||
338 | int v_min = INT_MAX; | |
339 | BOOST_FOREACH(const shared_ptr<Trace> t, traces) | |
340 | v_min = min(t->get_v_offset(), v_min); | |
341 | ||
342 | const int delta = -min(v_min, 0); | |
343 | BOOST_FOREACH(shared_ptr<Trace> t, traces) | |
344 | t->set_v_offset(t->get_v_offset() + delta); | |
345 | ||
346 | verticalScrollBar()->setSliderPosition(_v_offset + delta); | |
347 | v_scroll_value_changed(verticalScrollBar()->sliderPosition()); | |
348 | } | |
349 | ||
350 | void View::update_viewport() | |
351 | { | |
352 | assert(_viewport); | |
353 | _viewport->update(); | |
354 | } | |
355 | ||
356 | void View::get_scroll_layout(double &length, double &offset) const | |
357 | { | |
358 | const pair<double, double> extents = get_time_extents(); | |
359 | length = (extents.second - extents.first) / _scale; | |
360 | offset = _offset / _scale; | |
361 | } | |
362 | ||
363 | void View::set_zoom(double scale, int offset) | |
364 | { | |
365 | const double cursor_offset = _offset + _scale * offset; | |
366 | const double new_scale = max(min(scale, MaxScale), MinScale); | |
367 | const double new_offset = cursor_offset - new_scale * offset; | |
368 | set_scale_offset(new_scale, new_offset); | |
369 | } | |
370 | ||
371 | void View::update_scroll() | |
372 | { | |
373 | assert(_viewport); | |
374 | ||
375 | const QSize areaSize = _viewport->size(); | |
376 | ||
377 | // Set the horizontal scroll bar | |
378 | double length = 0, offset = 0; | |
379 | get_scroll_layout(length, offset); | |
380 | length = max(length - areaSize.width(), 0.0); | |
381 | ||
382 | horizontalScrollBar()->setPageStep(areaSize.width() / 2); | |
383 | ||
384 | _updating_scroll = true; | |
385 | ||
386 | if (length < MaxScrollValue) { | |
387 | horizontalScrollBar()->setRange(0, length); | |
388 | horizontalScrollBar()->setSliderPosition(offset); | |
389 | } else { | |
390 | horizontalScrollBar()->setRange(0, MaxScrollValue); | |
391 | horizontalScrollBar()->setSliderPosition( | |
392 | _offset * MaxScrollValue / (_scale * length)); | |
393 | } | |
394 | ||
395 | _updating_scroll = false; | |
396 | ||
397 | // Set the vertical scrollbar | |
398 | verticalScrollBar()->setPageStep(areaSize.height()); | |
399 | verticalScrollBar()->setRange(0, | |
400 | _viewport->get_total_height() + SignalMargin - | |
401 | areaSize.height()); | |
402 | } | |
403 | ||
404 | void View::update_layout() | |
405 | { | |
406 | setViewportMargins(_header->sizeHint().width(), | |
407 | _ruler->sizeHint().height(), 0, 0); | |
408 | _ruler->setGeometry(_viewport->x(), 0, | |
409 | _viewport->width(), _viewport->y()); | |
410 | _header->setGeometry(0, _viewport->y(), | |
411 | _viewport->x(), _viewport->height()); | |
412 | update_scroll(); | |
413 | } | |
414 | ||
415 | bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a, | |
416 | const shared_ptr<Trace> &b) | |
417 | { | |
418 | assert(a); | |
419 | assert(b); | |
420 | return a->get_v_offset() < b->get_v_offset(); | |
421 | } | |
422 | ||
423 | bool View::eventFilter(QObject *object, QEvent *event) | |
424 | { | |
425 | const QEvent::Type type = event->type(); | |
426 | if (type == QEvent::MouseMove) { | |
427 | ||
428 | const QMouseEvent *const mouse_event = (QMouseEvent*)event; | |
429 | if (object == _viewport) | |
430 | _hover_point = mouse_event->pos(); | |
431 | else if (object == _ruler) | |
432 | _hover_point = QPoint(mouse_event->x(), 0); | |
433 | else if (object == _header) | |
434 | _hover_point = QPoint(0, mouse_event->y()); | |
435 | else | |
436 | _hover_point = QPoint(-1, -1); | |
437 | ||
438 | hover_point_changed(); | |
439 | ||
440 | } else if (type == QEvent::Leave) { | |
441 | _hover_point = QPoint(-1, -1); | |
442 | hover_point_changed(); | |
443 | } | |
444 | ||
445 | return QObject::eventFilter(object, event); | |
446 | } | |
447 | ||
448 | bool View::viewportEvent(QEvent *e) | |
449 | { | |
450 | switch(e->type()) { | |
451 | case QEvent::Paint: | |
452 | case QEvent::MouseButtonPress: | |
453 | case QEvent::MouseButtonRelease: | |
454 | case QEvent::MouseButtonDblClick: | |
455 | case QEvent::MouseMove: | |
456 | case QEvent::Wheel: | |
457 | return false; | |
458 | ||
459 | default: | |
460 | return QAbstractScrollArea::viewportEvent(e); | |
461 | } | |
462 | } | |
463 | ||
464 | void View::resizeEvent(QResizeEvent*) | |
465 | { | |
466 | update_layout(); | |
467 | } | |
468 | ||
469 | void View::h_scroll_value_changed(int value) | |
470 | { | |
471 | if (_updating_scroll) | |
472 | return; | |
473 | ||
474 | const int range = horizontalScrollBar()->maximum(); | |
475 | if (range < MaxScrollValue) | |
476 | _offset = _scale * value; | |
477 | else { | |
478 | double length = 0, offset; | |
479 | get_scroll_layout(length, offset); | |
480 | _offset = _scale * length * value / MaxScrollValue; | |
481 | } | |
482 | ||
483 | _ruler->update(); | |
484 | _viewport->update(); | |
485 | } | |
486 | ||
487 | void View::v_scroll_value_changed(int value) | |
488 | { | |
489 | _v_offset = value; | |
490 | _header->update(); | |
491 | _viewport->update(); | |
492 | } | |
493 | ||
494 | void View::signals_changed() | |
495 | { | |
496 | int offset = SignalMargin + SignalHeight; | |
497 | const vector< shared_ptr<Trace> > traces(get_traces()); | |
498 | BOOST_FOREACH(shared_ptr<Trace> t, traces) { | |
499 | t->set_view(this); | |
500 | t->set_v_offset(offset); | |
501 | offset += SignalHeight + 2 * SignalMargin; | |
502 | } | |
503 | ||
504 | update_layout(); | |
505 | normalize_layout(); | |
506 | } | |
507 | ||
508 | void View::data_updated() | |
509 | { | |
510 | // Update the scroll bars | |
511 | update_scroll(); | |
512 | ||
513 | // Repaint the view | |
514 | _viewport->update(); | |
515 | } | |
516 | ||
517 | void View::marker_time_changed() | |
518 | { | |
519 | _ruler->update(); | |
520 | _viewport->update(); | |
521 | } | |
522 | ||
523 | void View::on_signals_moved() | |
524 | { | |
525 | update_scroll(); | |
526 | signals_moved(); | |
527 | } | |
528 | ||
529 | void View::on_geometry_updated() | |
530 | { | |
531 | update_layout(); | |
532 | } | |
533 | ||
534 | } // namespace view | |
535 | } // namespace pv |