]> sigrok.org Git - pulseview.git/blame - pv/view/view.cpp
Added zoom one-to-one
[pulseview.git] / pv / view / view.cpp
CommitLineData
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
46using namespace boost;
47using namespace std;
48
cdf7bea7
JH
49namespace pv {
50namespace view {
adb4b10c 51
cdf7bea7
JH
52const double View::MaxScale = 1e9;
53const double View::MinScale = 1e-15;
adb4b10c 54
cdf7bea7
JH
55const int View::LabelMarginWidth = 70;
56const int View::RulerHeight = 30;
57
f25770e2
JH
58const int View::MaxScrollValue = INT_MAX / 2;
59
8d44d030
JH
60const int View::SignalHeight = 30;
61const int View::SignalMargin = 10;
49153683 62const int View::SignalSnapGridSize = 10;
1d8dca91 63
f76af637
JH
64const QColor View::CursorAreaColour(220, 231, 243);
65
2e04f9bd
JH
66const QSizeF View::LabelPadding(4, 0);
67
cdf7bea7 68View::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
119SigSession& View::session()
120{
121 return _session;
122}
123
38eeddea
JH
124const SigSession& View::session() const
125{
126 return _session;
127}
128
cdf7bea7 129double View::scale() const
adb4b10c
JH
130{
131 return _scale;
132}
133
cdf7bea7 134double View::offset() const
adb4b10c
JH
135{
136 return _offset;
137}
138
cdf7bea7 139int View::v_offset() const
adb4b10c
JH
140{
141 return _v_offset;
142}
143
cdf7bea7 144void View::zoom(double steps)
adb4b10c
JH
145{
146 zoom(steps, (width() - LabelMarginWidth) / 2);
147}
148
17c0f398
JH
149void View::zoom(double steps, int offset)
150{
e31551da
JH
151 const double new_scale = max(min(_scale * pow(3.0/2.0, -steps),
152 MaxScale), MinScale);
d1e7d82c 153 set_zoom(new_scale, offset);
17c0f398
JH
154}
155
ca46b534
JH
156void View::zoom_fit()
157{
158 using pv::data::SignalData;
159
160 const vector< shared_ptr<Signal> > sigs(
161 session().get_signals());
162
163 // Make a set of all the visible data objects
164 set< shared_ptr<SignalData> > visible_data;
165 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
166 if (sig->enabled())
167 visible_data.insert(sig->data());
168
169 if (visible_data.empty())
170 return;
171
172 double left_time = DBL_MAX, right_time = DBL_MIN;
173 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data)
174 {
175 const double start_time = d->get_start_time();
176 left_time = min(left_time, start_time);
177 right_time = max(right_time, start_time +
9d28da5a 178 d->get_max_sample_count() / d->samplerate());
ca46b534
JH
179 }
180
181 assert(left_time < right_time);
182 if (right_time - left_time < 1e-12)
183 return;
184
185 assert(_viewport);
186 const int w = _viewport->width();
187 if (w <= 0)
188 return;
189
190 set_scale_offset((right_time - left_time) / w, left_time);
191}
192
d1e7d82c
JH
193void View::zoom_one_to_one()
194{
195 using pv::data::SignalData;
196
197 const vector< shared_ptr<Signal> > sigs(
198 session().get_signals());
199
200 // Make a set of all the visible data objects
201 set< shared_ptr<SignalData> > visible_data;
202 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
203 if (sig->enabled())
204 visible_data.insert(sig->data());
205
206 if (visible_data.empty())
207 return;
208
209 double samplerate = 0.0;
210 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data) {
211 assert(d);
212 samplerate = max(samplerate, d->samplerate());
213 }
214
215 if (samplerate == 0.0)
216 return;
217
218 assert(_viewport);
219 const int w = _viewport->width();
220 if (w <= 0)
221 return;
222
223 set_zoom(1.0 / samplerate, w / 2);
224}
225
cdf7bea7 226void View::set_scale_offset(double scale, double offset)
adb4b10c
JH
227{
228 _scale = scale;
229 _offset = offset;
ccdd3ef5 230
adb4b10c 231 update_scroll();
ccdd3ef5 232 _ruler->update();
adb4b10c 233 _viewport->update();
e0fc5810 234 scale_offset_changed();
adb4b10c
JH
235}
236
38eeddea
JH
237vector< shared_ptr<Trace> > View::get_traces() const
238{
239 const vector< shared_ptr<Signal> > sigs(
240 session().get_signals());
b9329558 241 const vector< shared_ptr<DecodeTrace> > decode_sigs(
38eeddea
JH
242 session().get_decode_signals());
243 vector< shared_ptr<Trace> > traces(
244 sigs.size() + decode_sigs.size());
245
246 vector< shared_ptr<Trace> >::iterator i = traces.begin();
247 i = copy(sigs.begin(), sigs.end(), i);
248 i = copy(decode_sigs.begin(), decode_sigs.end(), i);
249
9a2bc5fc 250 stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets);
38eeddea
JH
251 return traces;
252}
253
94936133
JH
254list<weak_ptr<SelectableItem> > View::selected_items() const
255{
256 list<weak_ptr<SelectableItem> > items;
257
258 // List the selected signals
38eeddea
JH
259 const vector< shared_ptr<Trace> > traces(get_traces());
260 BOOST_FOREACH (shared_ptr<Trace> t, traces) {
261 assert(t);
262 if (t->selected())
263 items.push_back(t);
94936133
JH
264 }
265
266 // List the selected cursors
267 if (_cursors.first()->selected())
268 items.push_back(_cursors.first());
269 if (_cursors.second()->selected())
270 items.push_back(_cursors.second());
271
272 return items;
273}
274
f76af637
JH
275bool View::cursors_shown() const
276{
277 return _show_cursors;
278}
279
280void View::show_cursors(bool show)
281{
282 _show_cursors = show;
283 _ruler->update();
284 _viewport->update();
285}
286
b4d91e56
JH
287void View::centre_cursors()
288{
289 const double time_width = _scale * _viewport->width();
58864c5c
JH
290 _cursors.first()->set_time(_offset + time_width * 0.4);
291 _cursors.second()->set_time(_offset + time_width * 0.6);
b4d91e56
JH
292 _ruler->update();
293 _viewport->update();
294}
295
b42d25c4 296CursorPair& View::cursors()
f76af637
JH
297{
298 return _cursors;
299}
300
58864c5c
JH
301const CursorPair& View::cursors() const
302{
303 return _cursors;
304}
305
cbd80f64
JH
306const QPoint& View::hover_point() const
307{
308 return _hover_point;
309}
310
4b192962
JH
311void View::normalize_layout()
312{
38eeddea 313 const vector< shared_ptr<Trace> > traces(get_traces());
4b192962
JH
314
315 int v_min = INT_MAX;
38eeddea
JH
316 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
317 v_min = min(t->get_v_offset(), v_min);
4b192962
JH
318
319 const int delta = -min(v_min, 0);
38eeddea
JH
320 BOOST_FOREACH(shared_ptr<Trace> t, traces)
321 t->set_v_offset(t->get_v_offset() + delta);
4b192962
JH
322
323 verticalScrollBar()->setSliderPosition(_v_offset + delta);
324 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
325}
326
9cef9567
JH
327void View::update_viewport()
328{
329 assert(_viewport);
330 _viewport->update();
331}
332
f25770e2
JH
333void View::get_scroll_layout(double &length, double &offset) const
334{
1b1ec774 335 const shared_ptr<data::SignalData> sig_data = _session.get_data();
333d5bbc 336 if (!sig_data)
f25770e2
JH
337 return;
338
9d28da5a 339 length = _data_length / (sig_data->samplerate() * _scale);
f25770e2
JH
340 offset = _offset / _scale;
341}
342
d1e7d82c
JH
343void View::set_zoom(double scale, int offset)
344{
345 const double cursor_offset = _offset + _scale * offset;
346 const double new_scale = max(min(scale, MaxScale), MinScale);
347 const double new_offset = cursor_offset - new_scale * offset;
348 set_scale_offset(new_scale, new_offset);
349}
350
cdf7bea7 351void View::update_scroll()
adb4b10c
JH
352{
353 assert(_viewport);
354
355 const QSize areaSize = _viewport->size();
356
357 // Set the horizontal scroll bar
358 double length = 0, offset = 0;
f25770e2
JH
359 get_scroll_layout(length, offset);
360 length = max(length - areaSize.width(), 0.0);
adb4b10c 361
b4ef7f2a 362 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
f25770e2 363
528bd8a1
JH
364 _updating_scroll = true;
365
333d5bbc 366 if (length < MaxScrollValue) {
f25770e2
JH
367 horizontalScrollBar()->setRange(0, length);
368 horizontalScrollBar()->setSliderPosition(offset);
369 } else {
370 horizontalScrollBar()->setRange(0, MaxScrollValue);
371 horizontalScrollBar()->setSliderPosition(
372 _offset * MaxScrollValue / (_scale * length));
373 }
adb4b10c 374
528bd8a1
JH
375 _updating_scroll = false;
376
adb4b10c
JH
377 // Set the vertical scrollbar
378 verticalScrollBar()->setPageStep(areaSize.height());
379 verticalScrollBar()->setRange(0,
4b192962
JH
380 _viewport->get_total_height() + SignalMargin -
381 areaSize.height());
adb4b10c
JH
382}
383
38eeddea
JH
384bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
385 const shared_ptr<Trace> &b)
386{
387 assert(a);
388 assert(b);
389 return a->get_v_offset() < b->get_v_offset();
390}
391
cbd80f64
JH
392bool View::eventFilter(QObject *object, QEvent *event)
393{
394 const QEvent::Type type = event->type();
333d5bbc 395 if (type == QEvent::MouseMove) {
cbd80f64
JH
396
397 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
333d5bbc 398 if (object == _viewport)
cbd80f64 399 _hover_point = mouse_event->pos();
333d5bbc 400 else if (object == _ruler)
cbd80f64 401 _hover_point = QPoint(mouse_event->x(), 0);
333d5bbc 402 else if (object == _header)
cbd80f64
JH
403 _hover_point = QPoint(0, mouse_event->y());
404 else
405 _hover_point = QPoint(-1, -1);
406
407 hover_point_changed();
408
333d5bbc 409 } else if (type == QEvent::Leave) {
cbd80f64
JH
410 _hover_point = QPoint(-1, -1);
411 hover_point_changed();
412 }
413
414 return QObject::eventFilter(object, event);
415}
416
cdf7bea7 417bool View::viewportEvent(QEvent *e)
adb4b10c
JH
418{
419 switch(e->type()) {
420 case QEvent::Paint:
421 case QEvent::MouseButtonPress:
422 case QEvent::MouseButtonRelease:
423 case QEvent::MouseButtonDblClick:
424 case QEvent::MouseMove:
425 case QEvent::Wheel:
426 return false;
427
428 default:
429 return QAbstractScrollArea::viewportEvent(e);
430 }
431}
432
e314eca4 433void View::resizeEvent(QResizeEvent*)
adb4b10c 434{
ccdd3ef5
JH
435 _ruler->setGeometry(_viewport->x(), 0,
436 _viewport->width(), _viewport->y());
437 _header->setGeometry(0, _viewport->y(),
1d8dca91 438 _viewport->x(), _viewport->height());
adb4b10c
JH
439 update_scroll();
440}
441
b16907d3 442void View::h_scroll_value_changed(int value)
adb4b10c 443{
528bd8a1
JH
444 if (_updating_scroll)
445 return;
446
f25770e2 447 const int range = horizontalScrollBar()->maximum();
333d5bbc 448 if (range < MaxScrollValue)
f25770e2
JH
449 _offset = _scale * value;
450 else {
451 double length = 0, offset;
452 get_scroll_layout(length, offset);
453 _offset = _scale * length * value / MaxScrollValue;
454 }
455
ccdd3ef5 456 _ruler->update();
adb4b10c
JH
457 _viewport->update();
458}
459
cdf7bea7 460void View::v_scroll_value_changed(int value)
adb4b10c
JH
461{
462 _v_offset = value;
1d8dca91 463 _header->update();
adb4b10c
JH
464 _viewport->update();
465}
466
69dd2b03
JH
467void View::signals_changed()
468{
ef8311a4 469 int offset = SignalMargin + SignalHeight;
01fd3263
JH
470 const vector< shared_ptr<Trace> > traces(get_traces());
471 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
472 t->set_view(this);
01fd3263 473 t->set_v_offset(offset);
ef8311a4
JH
474 offset += SignalHeight + 2 * SignalMargin;
475 }
476
477 normalize_layout();
69dd2b03
JH
478}
479
cdf7bea7 480void View::data_updated()
adb4b10c
JH
481{
482 // Get the new data length
483 _data_length = 0;
1b1ec774 484 shared_ptr<data::Logic> sig_data = _session.get_data();
333d5bbc 485 if (sig_data) {
1b1ec774 486 deque< shared_ptr<data::LogicSnapshot> > &snapshots =
adb4b10c 487 sig_data->get_snapshots();
1b1ec774 488 BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots)
333d5bbc 489 if (s)
adb4b10c
JH
490 _data_length = max(_data_length,
491 s->get_sample_count());
492 }
493
494 // Update the scroll bars
495 update_scroll();
496
497 // Repaint the view
498 _viewport->update();
499}
cdf7bea7 500
ca4ec3ea
JH
501void View::marker_time_changed()
502{
503 _ruler->update();
504 _viewport->update();
505}
506
07204819 507void View::on_signals_moved()
54401bbb 508{
07204819
JH
509 update_scroll();
510 signals_moved();
54401bbb
JH
511}
512
cdf7bea7
JH
513} // namespace view
514} // namespace pv