]> sigrok.org Git - pulseview.git/blob - pv/view/view.cpp
View: Update view on capture state change
[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         setViewport(_viewport);
123
124         _viewport->installEventFilter(this);
125         _ruler->installEventFilter(this);
126         _cursorheader->installEventFilter(this);
127         _header->installEventFilter(this);
128
129         // Trigger the initial event manually. The default device has signals
130         // which were created before this object came into being
131         signals_changed();
132
133         // make sure the transparent widgets are on the top
134         _cursorheader->raise();
135         _header->raise();
136 }
137
138 SigSession& View::session()
139 {
140         return _session;
141 }
142
143 const SigSession& View::session() const
144 {
145         return _session;
146 }
147
148 double View::scale() const
149 {
150         return _scale;
151 }
152
153 double View::offset() const
154 {
155         return _offset;
156 }
157
158 int View::v_offset() const
159 {
160         return _v_offset;
161 }
162
163 void View::zoom(double steps)
164 {
165         zoom(steps, _viewport->width() / 2);
166 }
167
168 void View::zoom(double steps, int offset)
169 {
170         set_zoom(_scale * pow(3.0/2.0, -steps), offset);
171 }
172
173 void View::zoom_fit()
174 {
175         const pair<double, double> extents = get_time_extents();
176         const double delta = extents.second - extents.first;
177         if (delta < 1e-12)
178                 return;
179
180         assert(_viewport);
181         const int w = _viewport->width();
182         if (w <= 0)
183                 return;
184
185         const double scale = max(min(delta / w, MaxScale), MinScale);
186         set_scale_offset(scale, extents.first);
187 }
188
189 void View::zoom_one_to_one()
190 {
191         using pv::data::SignalData;
192
193         const vector< shared_ptr<Signal> > sigs(
194                 session().get_signals());
195
196         // Make a set of all the visible data objects
197         set< shared_ptr<SignalData> > visible_data = get_visible_data();
198         if (visible_data.empty())
199                 return;
200
201         double samplerate = 0.0;
202         for (const shared_ptr<SignalData> d : visible_data) {
203                 assert(d);
204                 samplerate = max(samplerate, d->samplerate());
205         }
206
207         if (samplerate == 0.0)
208                 return;
209
210         assert(_viewport);
211         const int w = _viewport->width();
212         if (w <= 0)
213                 return;
214
215         set_zoom(1.0 / samplerate, w / 2);
216 }
217
218 void View::set_scale_offset(double scale, double offset)
219 {
220         _scale = scale;
221         _offset = offset;
222
223         update_scroll();
224         _ruler->update();
225         _cursorheader->update();
226         _viewport->update();
227         scale_offset_changed();
228 }
229
230 vector< shared_ptr<Trace> > View::get_traces() const
231 {
232         const vector< shared_ptr<Signal> > sigs(
233                 session().get_signals());
234 #ifdef ENABLE_DECODE
235         const vector< shared_ptr<DecodeTrace> > decode_sigs(
236                 session().get_decode_signals());
237         vector< shared_ptr<Trace> > traces(
238                 sigs.size() + decode_sigs.size());
239 #else
240         vector< shared_ptr<Trace> > traces(sigs.size());
241 #endif
242
243         auto i = traces.begin();
244         i = copy(sigs.begin(), sigs.end(), i);
245 #ifdef ENABLE_DECODE
246         i = copy(decode_sigs.begin(), decode_sigs.end(), i);
247 #endif
248
249         stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets);
250         return traces;
251 }
252
253 list<weak_ptr<SelectableItem> > View::selected_items() const
254 {
255         list<weak_ptr<SelectableItem> > items;
256
257         // List the selected signals
258         const vector< shared_ptr<Trace> > traces(get_traces());
259         for (shared_ptr<Trace> t : traces) {
260                 assert(t);
261                 if (t->selected())
262                         items.push_back(t);
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         const vector< shared_ptr<Signal> > sigs(
277                 session().get_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<Trace> > traces(get_traces());
349
350         int v_min = INT_MAX;
351         for (const shared_ptr<Trace> t : traces)
352                 v_min = min(t->get_v_offset(), v_min);
353
354         const int delta = -min(v_min, 0);
355         for (shared_ptr<Trace> t : traces)
356                 t->set_v_offset(t->get_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 bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
433         const shared_ptr<Trace> &b)
434 {
435         assert(a);
436         assert(b);
437         return a->get_v_offset() < b->get_v_offset();
438 }
439
440 bool View::eventFilter(QObject *object, QEvent *event)
441 {
442         const QEvent::Type type = event->type();
443         if (type == QEvent::MouseMove) {
444
445                 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
446                 if (object == _viewport)
447                         _hover_point = mouse_event->pos();
448                 else if (object == _ruler || object == _cursorheader)
449                         _hover_point = QPoint(mouse_event->x(), 0);
450                 else if (object == _header)
451                         _hover_point = QPoint(0, mouse_event->y());
452                 else
453                         _hover_point = QPoint(-1, -1);
454
455                 hover_point_changed();
456
457         } else if (type == QEvent::Leave) {
458                 _hover_point = QPoint(-1, -1);
459                 hover_point_changed();
460         }
461
462         return QObject::eventFilter(object, event);
463 }
464
465 bool View::viewportEvent(QEvent *e)
466 {
467         switch(e->type()) {
468         case QEvent::Paint:
469         case QEvent::MouseButtonPress:
470         case QEvent::MouseButtonRelease:
471         case QEvent::MouseButtonDblClick:
472         case QEvent::MouseMove:
473         case QEvent::Wheel:
474         case QEvent::TouchBegin:
475         case QEvent::TouchUpdate:
476         case QEvent::TouchEnd:
477                 return false;
478
479         default:
480                 return QAbstractScrollArea::viewportEvent(e);
481         }
482 }
483
484 void View::resizeEvent(QResizeEvent*)
485 {
486         update_layout();
487 }
488
489 void View::h_scroll_value_changed(int value)
490 {
491         if (_updating_scroll)
492                 return;
493
494         const int range = horizontalScrollBar()->maximum();
495         if (range < MaxScrollValue)
496                 _offset = _scale * value;
497         else {
498                 double length = 0, offset;
499                 get_scroll_layout(length, offset);
500                 _offset = _scale * length * value / MaxScrollValue;
501         }
502
503         _ruler->update();
504         _cursorheader->update();
505         _viewport->update();
506 }
507
508 void View::v_scroll_value_changed(int value)
509 {
510         _v_offset = value;
511         _header->update();
512         _viewport->update();
513 }
514
515 void View::signals_changed()
516 {
517         int offset = SignalMargin + SignalHeight;
518         const vector< shared_ptr<Trace> > traces(get_traces());
519         for (shared_ptr<Trace> t : traces) {
520                 t->set_view(this);
521                 t->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 } // namespace view
556 } // namespace pv