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