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