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