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