]> sigrok.org Git - pulseview.git/blob - pv/view/view.cpp
Renamed get_samplerate to samplerate
[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 #include <libsigrokdecode/libsigrokdecode.h>
22
23 #include <assert.h>
24 #include <limits.h>
25 #include <math.h>
26
27 #include <set>
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 namespace boost;
47 using namespace std;
48
49 namespace pv {
50 namespace view {
51
52 const double View::MaxScale = 1e9;
53 const double View::MinScale = 1e-15;
54
55 const int View::LabelMarginWidth = 70;
56 const int View::RulerHeight = 30;
57
58 const int View::MaxScrollValue = INT_MAX / 2;
59
60 const int View::SignalHeight = 30;
61 const int View::SignalMargin = 10;
62 const int View::SignalSnapGridSize = 10;
63
64 const QColor View::CursorAreaColour(220, 231, 243);
65
66 const QSizeF View::LabelPadding(4, 0);
67
68 View::View(SigSession &session, QWidget *parent) :
69         QAbstractScrollArea(parent),
70         _session(session),
71         _viewport(new Viewport(*this)),
72         _ruler(new Ruler(*this)),
73         _header(new Header(*this)),
74         _data_length(0),
75         _scale(1e-6),
76         _offset(0),
77         _v_offset(0),
78         _updating_scroll(false),
79         _show_cursors(false),
80         _cursors(*this),
81         _hover_point(-1, -1)
82 {
83         connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
84                 this, SLOT(h_scroll_value_changed(int)));
85         connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
86                 this, SLOT(v_scroll_value_changed(int)));
87
88         connect(&_session, SIGNAL(signals_changed()),
89                 this, SLOT(signals_changed()));
90         connect(&_session, SIGNAL(data_updated()),
91                 this, SLOT(data_updated()));
92
93         connect(_cursors.first().get(), SIGNAL(time_changed()),
94                 this, SLOT(marker_time_changed()));
95         connect(_cursors.second().get(), SIGNAL(time_changed()),
96                 this, SLOT(marker_time_changed()));
97
98         connect(_header, SIGNAL(signals_moved()),
99                 this, SLOT(on_signals_moved()));
100
101         connect(_header, SIGNAL(selection_changed()),
102                 _ruler, SLOT(clear_selection()));
103         connect(_ruler, SIGNAL(selection_changed()),
104                 _header, SLOT(clear_selection()));
105
106         connect(_header, SIGNAL(selection_changed()),
107                 this, SIGNAL(selection_changed()));
108         connect(_ruler, SIGNAL(selection_changed()),
109                 this, SIGNAL(selection_changed()));
110
111         setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
112         setViewport(_viewport);
113
114         _viewport->installEventFilter(this);
115         _ruler->installEventFilter(this);
116         _header->installEventFilter(this);
117 }
118
119 SigSession& View::session()
120 {
121         return _session;
122 }
123
124 const SigSession& View::session() const
125 {
126         return _session;
127 }
128
129 double View::scale() const
130 {
131         return _scale;
132 }
133
134 double View::offset() const
135 {
136         return _offset;
137 }
138
139 int View::v_offset() const
140 {
141         return _v_offset;
142 }
143
144 void View::zoom(double steps)
145 {
146         zoom(steps, (width() - LabelMarginWidth) / 2);
147 }
148
149 void View::zoom(double steps, int offset)
150 {
151         const double cursor_offset = _offset + _scale * offset;
152         const double new_scale = max(min(_scale * pow(3.0/2.0, -steps),
153                 MaxScale), MinScale);
154         const double new_offset = cursor_offset - new_scale * offset;
155         set_scale_offset(new_scale, new_offset);
156 }
157
158 void View::zoom_fit()
159 {
160         using pv::data::SignalData;
161
162         const vector< shared_ptr<Signal> > sigs(
163                 session().get_signals());
164
165         // Make a set of all the visible data objects
166         set< shared_ptr<SignalData> > visible_data;
167         BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
168                 if (sig->enabled())
169                         visible_data.insert(sig->data());
170
171         if (visible_data.empty())
172                 return;
173
174         double left_time = DBL_MAX, right_time = DBL_MIN;
175         BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data)
176         {
177                 const double start_time = d->get_start_time();
178                 left_time = min(left_time, start_time);
179                 right_time = max(right_time, start_time +
180                         d->get_max_sample_count() / d->samplerate());
181         }
182
183         assert(left_time < right_time);
184         if (right_time - left_time < 1e-12)
185                 return;
186
187         assert(_viewport);
188         const int w = _viewport->width();
189         if (w <= 0)
190                 return;
191
192         set_scale_offset((right_time - left_time) / w, left_time);      
193 }
194
195 void View::set_scale_offset(double scale, double offset)
196 {
197         _scale = scale;
198         _offset = offset;
199
200         update_scroll();
201         _ruler->update();
202         _viewport->update();
203         scale_offset_changed();
204 }
205
206 vector< shared_ptr<Trace> > View::get_traces() const
207 {
208         const vector< shared_ptr<Signal> > sigs(
209                 session().get_signals());
210         const vector< shared_ptr<DecodeTrace> > decode_sigs(
211                 session().get_decode_signals());
212         vector< shared_ptr<Trace> > traces(
213                 sigs.size() + decode_sigs.size());
214
215         vector< shared_ptr<Trace> >::iterator i = traces.begin();
216         i = copy(sigs.begin(), sigs.end(), i);
217         i = copy(decode_sigs.begin(), decode_sigs.end(), i);
218
219         sort(traces.begin(), traces.end(), compare_trace_v_offsets);
220         return traces;
221 }
222
223 list<weak_ptr<SelectableItem> > View::selected_items() const
224 {
225         list<weak_ptr<SelectableItem> > items;
226
227         // List the selected signals
228         const vector< shared_ptr<Trace> > traces(get_traces());
229         BOOST_FOREACH (shared_ptr<Trace> t, traces) {
230                 assert(t);
231                 if (t->selected())
232                         items.push_back(t);
233         }
234
235         // List the selected cursors
236         if (_cursors.first()->selected())
237                 items.push_back(_cursors.first());
238         if (_cursors.second()->selected())
239                 items.push_back(_cursors.second());
240
241         return items;
242 }
243
244 bool View::cursors_shown() const
245 {
246         return _show_cursors;
247 }
248
249 void View::show_cursors(bool show)
250 {
251         _show_cursors = show;
252         _ruler->update();
253         _viewport->update();
254 }
255
256 void View::centre_cursors()
257 {
258         const double time_width = _scale * _viewport->width();
259         _cursors.first()->set_time(_offset + time_width * 0.4);
260         _cursors.second()->set_time(_offset + time_width * 0.6);
261         _ruler->update();
262         _viewport->update();
263 }
264
265 CursorPair& View::cursors()
266 {
267         return _cursors;
268 }
269
270 const CursorPair& View::cursors() const
271 {
272         return _cursors;
273 }
274
275 const QPoint& View::hover_point() const
276 {
277         return _hover_point;
278 }
279
280 void View::normalize_layout()
281 {
282         const vector< shared_ptr<Trace> > traces(get_traces());
283
284         int v_min = INT_MAX;
285         BOOST_FOREACH(const shared_ptr<Trace> t, traces)
286                 v_min = min(t->get_v_offset(), v_min);
287
288         const int delta = -min(v_min, 0);
289         BOOST_FOREACH(shared_ptr<Trace> t, traces)
290                 t->set_v_offset(t->get_v_offset() + delta);
291
292         verticalScrollBar()->setSliderPosition(_v_offset + delta);
293         v_scroll_value_changed(verticalScrollBar()->sliderPosition());
294 }
295
296 void View::update_viewport()
297 {
298         assert(_viewport);
299         _viewport->update();
300 }
301
302 void View::get_scroll_layout(double &length, double &offset) const
303 {
304         const shared_ptr<data::SignalData> sig_data = _session.get_data();
305         if (!sig_data)
306                 return;
307
308         length = _data_length / (sig_data->samplerate() * _scale);
309         offset = _offset / _scale;
310 }
311
312 void View::update_scroll()
313 {
314         assert(_viewport);
315
316         const QSize areaSize = _viewport->size();
317
318         // Set the horizontal scroll bar
319         double length = 0, offset = 0;
320         get_scroll_layout(length, offset);
321         length = max(length - areaSize.width(), 0.0);
322
323         horizontalScrollBar()->setPageStep(areaSize.width() / 2);
324
325         _updating_scroll = true;
326
327         if (length < MaxScrollValue) {
328                 horizontalScrollBar()->setRange(0, length);
329                 horizontalScrollBar()->setSliderPosition(offset);
330         } else {
331                 horizontalScrollBar()->setRange(0, MaxScrollValue);
332                 horizontalScrollBar()->setSliderPosition(
333                         _offset * MaxScrollValue / (_scale * length));
334         }
335
336         _updating_scroll = false;
337
338         // Set the vertical scrollbar
339         verticalScrollBar()->setPageStep(areaSize.height());
340         verticalScrollBar()->setRange(0,
341                 _viewport->get_total_height() + SignalMargin -
342                 areaSize.height());
343 }
344
345 bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
346         const shared_ptr<Trace> &b)
347 {
348         assert(a);
349         assert(b);
350         return a->get_v_offset() < b->get_v_offset();
351 }
352
353 bool View::eventFilter(QObject *object, QEvent *event)
354 {
355         const QEvent::Type type = event->type();
356         if (type == QEvent::MouseMove) {
357
358                 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
359                 if (object == _viewport)
360                         _hover_point = mouse_event->pos();
361                 else if (object == _ruler)
362                         _hover_point = QPoint(mouse_event->x(), 0);
363                 else if (object == _header)
364                         _hover_point = QPoint(0, mouse_event->y());
365                 else
366                         _hover_point = QPoint(-1, -1);
367
368                 hover_point_changed();
369
370         } else if (type == QEvent::Leave) {
371                 _hover_point = QPoint(-1, -1);
372                 hover_point_changed();
373         }
374
375         return QObject::eventFilter(object, event);
376 }
377
378 bool View::viewportEvent(QEvent *e)
379 {
380         switch(e->type()) {
381         case QEvent::Paint:
382         case QEvent::MouseButtonPress:
383         case QEvent::MouseButtonRelease:
384         case QEvent::MouseButtonDblClick:
385         case QEvent::MouseMove:
386         case QEvent::Wheel:
387                 return false;
388
389         default:
390                 return QAbstractScrollArea::viewportEvent(e);
391         }
392 }
393
394 void View::resizeEvent(QResizeEvent*)
395 {
396         _ruler->setGeometry(_viewport->x(), 0,
397                 _viewport->width(), _viewport->y());
398         _header->setGeometry(0, _viewport->y(),
399                 _viewport->x(), _viewport->height());
400         update_scroll();
401 }
402
403 void View::h_scroll_value_changed(int value)
404 {
405         if (_updating_scroll)
406                 return;
407
408         const int range = horizontalScrollBar()->maximum();
409         if (range < MaxScrollValue)
410                 _offset = _scale * value;
411         else {
412                 double length = 0, offset;
413                 get_scroll_layout(length, offset);
414                 _offset = _scale * length * value / MaxScrollValue;
415         }
416
417         _ruler->update();
418         _viewport->update();
419 }
420
421 void View::v_scroll_value_changed(int value)
422 {
423         _v_offset = value;
424         _header->update();
425         _viewport->update();
426 }
427
428 void View::signals_changed()
429 {
430         int offset = SignalMargin + SignalHeight;
431         const vector< shared_ptr<Trace> > traces(get_traces());
432         BOOST_FOREACH(shared_ptr<Trace> t, traces) {
433                 t->set_view(this);
434                 t->set_v_offset(offset);
435                 offset += SignalHeight + 2 * SignalMargin;
436         }
437
438         normalize_layout();
439 }
440
441 void View::data_updated()
442 {
443         // Get the new data length
444         _data_length = 0;
445         shared_ptr<data::Logic> sig_data = _session.get_data();
446         if (sig_data) {
447                 deque< shared_ptr<data::LogicSnapshot> > &snapshots =
448                         sig_data->get_snapshots();
449                 BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots)
450                         if (s)
451                                 _data_length = max(_data_length,
452                                         s->get_sample_count());
453         }
454
455         // Update the scroll bars
456         update_scroll();
457
458         // Repaint the view
459         _viewport->update();
460 }
461
462 void View::marker_time_changed()
463 {
464         _ruler->update();
465         _viewport->update();
466 }
467
468 void View::on_signals_moved()
469 {
470         update_scroll();
471         signals_moved();
472 }
473
474 } // namespace view
475 } // namespace pv