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