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