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