]> sigrok.org Git - pulseview.git/blob - pv/view/view.cpp
1f1bd9acea32cd54c2873f5f1f8259d6f852ce0f
[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 Viewport* View::viewport()
149 {
150         return _viewport;
151 }
152
153 const Viewport* View::viewport() const
154 {
155         return _viewport;
156 }
157
158 double View::scale() const
159 {
160         return _scale;
161 }
162
163 double View::offset() const
164 {
165         return _offset;
166 }
167
168 int View::v_offset() const
169 {
170         return _v_offset;
171 }
172
173 void View::zoom(double steps)
174 {
175         zoom(steps, _viewport->width() / 2);
176 }
177
178 void View::zoom(double steps, int offset)
179 {
180         set_zoom(_scale * pow(3.0/2.0, -steps), offset);
181 }
182
183 void View::zoom_fit()
184 {
185         const pair<double, double> extents = get_time_extents();
186         const double delta = extents.second - extents.first;
187         if (delta < 1e-12)
188                 return;
189
190         assert(_viewport);
191         const int w = _viewport->width();
192         if (w <= 0)
193                 return;
194
195         const double scale = max(min(delta / w, MaxScale), MinScale);
196         set_scale_offset(scale, extents.first);
197 }
198
199 void View::zoom_one_to_one()
200 {
201         using pv::data::SignalData;
202
203         const vector< shared_ptr<Signal> > sigs(
204                 session().get_signals());
205
206         // Make a set of all the visible data objects
207         set< shared_ptr<SignalData> > visible_data = get_visible_data();
208         if (visible_data.empty())
209                 return;
210
211         double samplerate = 0.0;
212         for (const shared_ptr<SignalData> d : visible_data) {
213                 assert(d);
214                 samplerate = max(samplerate, d->samplerate());
215         }
216
217         if (samplerate == 0.0)
218                 return;
219
220         assert(_viewport);
221         const int w = _viewport->width();
222         if (w <= 0)
223                 return;
224
225         set_zoom(1.0 / samplerate, w / 2);
226 }
227
228 void View::set_scale_offset(double scale, double offset)
229 {
230         _scale = scale;
231         _offset = offset;
232
233         update_scroll();
234         _ruler->update();
235         _cursorheader->update();
236         _viewport->update();
237         scale_offset_changed();
238 }
239
240 vector< shared_ptr<Trace> > View::get_traces() const
241 {
242         const vector< shared_ptr<Signal> > sigs(
243                 session().get_signals());
244 #ifdef ENABLE_DECODE
245         const vector< shared_ptr<DecodeTrace> > decode_sigs(
246                 session().get_decode_signals());
247         vector< shared_ptr<Trace> > traces(
248                 sigs.size() + decode_sigs.size());
249 #else
250         vector< shared_ptr<Trace> > traces(sigs.size());
251 #endif
252
253         auto i = traces.begin();
254         i = copy(sigs.begin(), sigs.end(), i);
255 #ifdef ENABLE_DECODE
256         i = copy(decode_sigs.begin(), decode_sigs.end(), i);
257 #endif
258
259         stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets);
260         return traces;
261 }
262
263 list<weak_ptr<SelectableItem> > View::selected_items() const
264 {
265         list<weak_ptr<SelectableItem> > items;
266
267         // List the selected signals
268         const vector< shared_ptr<Trace> > traces(get_traces());
269         for (shared_ptr<Trace> t : traces) {
270                 assert(t);
271                 if (t->selected())
272                         items.push_back(t);
273         }
274
275         // List the selected cursors
276         if (_cursors.first()->selected())
277                 items.push_back(_cursors.first());
278         if (_cursors.second()->selected())
279                 items.push_back(_cursors.second());
280
281         return items;
282 }
283
284 set< shared_ptr<SignalData> > View::get_visible_data() const
285 {
286         const vector< shared_ptr<Signal> > sigs(
287                 session().get_signals());
288
289         // Make a set of all the visible data objects
290         set< shared_ptr<SignalData> > visible_data;
291         for (const shared_ptr<Signal> sig : sigs)
292                 if (sig->enabled())
293                         visible_data.insert(sig->data());
294
295         return visible_data;
296 }
297
298 pair<double, double> View::get_time_extents() const
299 {
300         const set< shared_ptr<SignalData> > visible_data = get_visible_data();
301         if (visible_data.empty())
302                 return make_pair(0.0, 0.0);
303
304         double left_time = DBL_MAX, right_time = DBL_MIN;
305         for (const shared_ptr<SignalData> d : visible_data)
306         {
307                 const double start_time = d->get_start_time();
308                 double samplerate = d->samplerate();
309                 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
310
311                 left_time = min(left_time, start_time);
312                 right_time = max(right_time, start_time +
313                         d->get_max_sample_count() / samplerate);
314         }
315
316         assert(left_time < right_time);
317         return make_pair(left_time, right_time);
318 }
319
320 bool View::cursors_shown() const
321 {
322         return _show_cursors;
323 }
324
325 void View::show_cursors(bool show)
326 {
327         _show_cursors = show;
328         _cursorheader->update();
329         _viewport->update();
330 }
331
332 void View::centre_cursors()
333 {
334         const double time_width = _scale * _viewport->width();
335         _cursors.first()->set_time(_offset + time_width * 0.4);
336         _cursors.second()->set_time(_offset + time_width * 0.6);
337         _cursorheader->update();
338         _viewport->update();
339 }
340
341 CursorPair& View::cursors()
342 {
343         return _cursors;
344 }
345
346 const CursorPair& View::cursors() const
347 {
348         return _cursors;
349 }
350
351 const QPoint& View::hover_point() const
352 {
353         return _hover_point;
354 }
355
356 void View::normalize_layout()
357 {
358         const vector< shared_ptr<Trace> > traces(get_traces());
359
360         int v_min = INT_MAX;
361         for (const shared_ptr<Trace> t : traces)
362                 v_min = min(t->get_v_offset(), v_min);
363
364         const int delta = -min(v_min, 0);
365         for (shared_ptr<Trace> t : traces)
366                 t->set_v_offset(t->get_v_offset() + delta);
367
368         verticalScrollBar()->setSliderPosition(_v_offset + delta);
369         v_scroll_value_changed(verticalScrollBar()->sliderPosition());
370 }
371
372 void View::update_viewport()
373 {
374         assert(_viewport);
375         _viewport->update();
376 }
377
378 void View::get_scroll_layout(double &length, double &offset) const
379 {
380         const pair<double, double> extents = get_time_extents();
381         length = (extents.second - extents.first) / _scale;
382         offset = _offset / _scale;
383 }
384
385 void View::set_zoom(double scale, int offset)
386 {
387         const double cursor_offset = _offset + _scale * offset;
388         const double new_scale = max(min(scale, MaxScale), MinScale);
389         const double new_offset = cursor_offset - new_scale * offset;
390         set_scale_offset(new_scale, new_offset);
391 }
392
393 void View::update_scroll()
394 {
395         assert(_viewport);
396
397         const QSize areaSize = _viewport->size();
398
399         // Set the horizontal scroll bar
400         double length = 0, offset = 0;
401         get_scroll_layout(length, offset);
402         length = max(length - areaSize.width(), 0.0);
403
404         horizontalScrollBar()->setPageStep(areaSize.width() / 2);
405
406         _updating_scroll = true;
407
408         if (length < MaxScrollValue) {
409                 horizontalScrollBar()->setRange(0, length);
410                 horizontalScrollBar()->setSliderPosition(offset);
411         } else {
412                 horizontalScrollBar()->setRange(0, MaxScrollValue);
413                 horizontalScrollBar()->setSliderPosition(
414                         _offset * MaxScrollValue / (_scale * length));
415         }
416
417         _updating_scroll = false;
418
419         // Set the vertical scrollbar
420         verticalScrollBar()->setPageStep(areaSize.height());
421         verticalScrollBar()->setRange(0,
422                 _viewport->get_total_height() + SignalMargin -
423                 areaSize.height());
424 }
425
426 void View::update_layout()
427 {
428         setViewportMargins(
429                 _header->sizeHint().width() - pv::view::Header::BaselineOffset,
430                 _ruler->sizeHint().height(), 0, 0);
431         _ruler->setGeometry(_viewport->x(), 0,
432                 _viewport->width(), _viewport->y());
433         _cursorheader->setGeometry(
434                 _viewport->x(),
435                 _ruler->sizeHint().height() - _cursorheader->sizeHint().height() / 2,
436                 _viewport->width(), _cursorheader->sizeHint().height());
437         _header->setGeometry(0, _viewport->y(),
438                 _header->sizeHint().width(), _viewport->height());
439         update_scroll();
440 }
441
442 bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
443         const shared_ptr<Trace> &b)
444 {
445         assert(a);
446         assert(b);
447         return a->get_v_offset() < b->get_v_offset();
448 }
449
450 bool View::eventFilter(QObject *object, QEvent *event)
451 {
452         const QEvent::Type type = event->type();
453         if (type == QEvent::MouseMove) {
454
455                 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
456                 if (object == _viewport)
457                         _hover_point = mouse_event->pos();
458                 else if (object == _ruler || object == _cursorheader)
459                         _hover_point = QPoint(mouse_event->x(), 0);
460                 else if (object == _header)
461                         _hover_point = QPoint(0, mouse_event->y());
462                 else
463                         _hover_point = QPoint(-1, -1);
464
465                 hover_point_changed();
466
467         } else if (type == QEvent::Leave) {
468                 _hover_point = QPoint(-1, -1);
469                 hover_point_changed();
470         }
471
472         return QObject::eventFilter(object, event);
473 }
474
475 bool View::viewportEvent(QEvent *e)
476 {
477         switch(e->type()) {
478         case QEvent::Paint:
479         case QEvent::MouseButtonPress:
480         case QEvent::MouseButtonRelease:
481         case QEvent::MouseButtonDblClick:
482         case QEvent::MouseMove:
483         case QEvent::Wheel:
484         case QEvent::TouchBegin:
485         case QEvent::TouchUpdate:
486         case QEvent::TouchEnd:
487                 return false;
488
489         default:
490                 return QAbstractScrollArea::viewportEvent(e);
491         }
492 }
493
494 void View::resizeEvent(QResizeEvent*)
495 {
496         update_layout();
497 }
498
499 void View::h_scroll_value_changed(int value)
500 {
501         if (_updating_scroll)
502                 return;
503
504         const int range = horizontalScrollBar()->maximum();
505         if (range < MaxScrollValue)
506                 _offset = _scale * value;
507         else {
508                 double length = 0, offset;
509                 get_scroll_layout(length, offset);
510                 _offset = _scale * length * value / MaxScrollValue;
511         }
512
513         _ruler->update();
514         _cursorheader->update();
515         _viewport->update();
516 }
517
518 void View::v_scroll_value_changed(int value)
519 {
520         _v_offset = value;
521         _header->update();
522         _viewport->update();
523 }
524
525 void View::signals_changed()
526 {
527         int offset = SignalMargin + SignalHeight;
528         const vector< shared_ptr<Trace> > traces(get_traces());
529         for (shared_ptr<Trace> t : traces) {
530                 t->set_view(this);
531                 t->set_v_offset(offset);
532                 offset += SignalHeight + 2 * SignalMargin;
533         }
534
535         update_layout();
536         normalize_layout();
537 }
538
539 void View::data_updated()
540 {
541         // Update the scroll bars
542         update_scroll();
543
544         // Repaint the view
545         _viewport->update();
546 }
547
548 void View::marker_time_changed()
549 {
550         _cursorheader->update();
551         _viewport->update();
552 }
553
554 void View::on_signals_moved()
555 {
556         update_scroll();
557         signals_moved();
558 }
559
560 void View::on_geometry_updated()
561 {
562         update_layout();
563 }
564
565 } // namespace view
566 } // namespace pv