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