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