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