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