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