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