]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/view.cpp
TimeItem: Added new class - a base of TimeMarker, derived from SelectableItem
[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#ifdef ENABLE_DECODE
22#include <libsigrokdecode/libsigrokdecode.h>
23#endif
24
25#include <extdef.h>
26
27#include <cassert>
28#include <climits>
29#include <cmath>
30#include <mutex>
31#include <unordered_set>
32
33#include <QApplication>
34#include <QEvent>
35#include <QFontMetrics>
36#include <QMouseEvent>
37#include <QScrollBar>
38
39#include <libsigrok/libsigrok.hpp>
40
41#include "cursorheader.hpp"
42#include "decodetrace.hpp"
43#include "header.hpp"
44#include "logicsignal.hpp"
45#include "ruler.hpp"
46#include "signal.hpp"
47#include "tracegroup.hpp"
48#include "view.hpp"
49#include "viewport.hpp"
50
51#include "pv/session.hpp"
52#include "pv/data/logic.hpp"
53#include "pv/data/logicsegment.hpp"
54#include "pv/util.hpp"
55
56using boost::shared_lock;
57using boost::shared_mutex;
58
59using pv::data::SignalData;
60using pv::data::Segment;
61using pv::util::format_time;
62
63using std::back_inserter;
64using std::deque;
65using std::dynamic_pointer_cast;
66using std::list;
67using std::lock_guard;
68using std::max;
69using std::make_pair;
70using std::min;
71using std::pair;
72using std::set;
73using std::shared_ptr;
74using std::unordered_map;
75using std::unordered_set;
76using std::vector;
77using std::weak_ptr;
78
79namespace pv {
80namespace view {
81
82const double View::MaxScale = 1e9;
83const double View::MinScale = 1e-15;
84
85const int View::MaxScrollValue = INT_MAX / 2;
86
87const int View::ScaleUnits[3] = {1, 2, 5};
88
89const QColor View::CursorAreaColour(220, 231, 243);
90
91const QSizeF View::LabelPadding(4, 0);
92
93View::View(Session &session, QWidget *parent) :
94 QAbstractScrollArea(parent),
95 session_(session),
96 viewport_(new Viewport(*this)),
97 ruler_(new Ruler(*this)),
98 cursorheader_(new CursorHeader(*this)),
99 header_(new Header(*this)),
100 scale_(1e-6),
101 offset_(0),
102 v_offset_(0),
103 updating_scroll_(false),
104 tick_period_(0.0),
105 tick_prefix_(0),
106 show_cursors_(false),
107 cursors_(*this),
108 hover_point_(-1, -1)
109{
110 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
111 this, SLOT(h_scroll_value_changed(int)));
112 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
113 this, SLOT(v_scroll_value_changed(int)));
114
115 connect(&session_, SIGNAL(signals_changed()),
116 this, SLOT(signals_changed()));
117 connect(&session_, SIGNAL(capture_state_changed(int)),
118 this, SLOT(data_updated()));
119 connect(&session_, SIGNAL(data_received()),
120 this, SLOT(data_updated()));
121 connect(&session_, SIGNAL(frame_ended()),
122 this, SLOT(data_updated()));
123
124 connect(cursors_.first().get(), SIGNAL(time_changed()),
125 this, SLOT(marker_time_changed()));
126 connect(cursors_.second().get(), SIGNAL(time_changed()),
127 this, SLOT(marker_time_changed()));
128
129 connect(header_, SIGNAL(signals_moved()),
130 this, SLOT(on_signals_moved()));
131
132 connect(header_, SIGNAL(selection_changed()),
133 cursorheader_, SLOT(clear_selection()));
134 connect(cursorheader_, SIGNAL(selection_changed()),
135 header_, SLOT(clear_selection()));
136
137 connect(header_, SIGNAL(selection_changed()),
138 this, SIGNAL(selection_changed()));
139 connect(cursorheader_, SIGNAL(selection_changed()),
140 this, SIGNAL(selection_changed()));
141
142 connect(this, SIGNAL(hover_point_changed()),
143 this, SLOT(on_hover_point_changed()));
144
145 connect(&lazy_event_handler_, SIGNAL(timeout()),
146 this, SLOT(process_sticky_events()));
147 lazy_event_handler_.setSingleShot(true);
148
149 setViewport(viewport_);
150
151 viewport_->installEventFilter(this);
152 ruler_->installEventFilter(this);
153 cursorheader_->installEventFilter(this);
154 header_->installEventFilter(this);
155
156 // Trigger the initial event manually. The default device has signals
157 // which were created before this object came into being
158 signals_changed();
159
160 // make sure the transparent widgets are on the top
161 cursorheader_->raise();
162 header_->raise();
163
164 // Update the zoom state
165 calculate_tick_spacing();
166}
167
168Session& View::session()
169{
170 return session_;
171}
172
173const Session& View::session() const
174{
175 return session_;
176}
177
178View* View::view()
179{
180 return this;
181}
182
183const View* View::view() const
184{
185 return this;
186}
187
188Viewport* View::viewport()
189{
190 return viewport_;
191}
192
193const Viewport* View::viewport() const
194{
195 return viewport_;
196}
197
198double View::scale() const
199{
200 return scale_;
201}
202
203double View::offset() const
204{
205 return offset_;
206}
207
208int View::owner_visual_v_offset() const
209{
210 return -v_offset_;
211}
212
213unsigned int View::depth() const
214{
215 return 0;
216}
217
218unsigned int View::tick_prefix() const
219{
220 return tick_prefix_;
221}
222
223double View::tick_period() const
224{
225 return tick_period_;
226}
227
228void View::zoom(double steps)
229{
230 zoom(steps, viewport_->width() / 2);
231}
232
233void View::zoom(double steps, int offset)
234{
235 set_zoom(scale_ * pow(3.0/2.0, -steps), offset);
236}
237
238void View::zoom_fit()
239{
240 const pair<double, double> extents = get_time_extents();
241 const double delta = extents.second - extents.first;
242 if (delta < 1e-12)
243 return;
244
245 assert(viewport_);
246 const int w = viewport_->width();
247 if (w <= 0)
248 return;
249
250 const double scale = max(min(delta / w, MaxScale), MinScale);
251 set_scale_offset(scale, extents.first);
252}
253
254void View::zoom_one_to_one()
255{
256 using pv::data::SignalData;
257
258 // Make a set of all the visible data objects
259 set< shared_ptr<SignalData> > visible_data = get_visible_data();
260 if (visible_data.empty())
261 return;
262
263 double samplerate = 0.0;
264 for (const shared_ptr<SignalData> d : visible_data) {
265 assert(d);
266 const vector< shared_ptr<Segment> > segments =
267 d->segments();
268 for (const shared_ptr<Segment> &s : segments)
269 samplerate = max(samplerate, s->samplerate());
270 }
271
272 if (samplerate == 0.0)
273 return;
274
275 assert(viewport_);
276 const int w = viewport_->width();
277 if (w <= 0)
278 return;
279
280 set_zoom(1.0 / samplerate, w / 2);
281}
282
283void View::set_scale_offset(double scale, double offset)
284{
285 scale_ = scale;
286 offset_ = offset;
287
288 calculate_tick_spacing();
289
290 update_scroll();
291 ruler_->update();
292 cursorheader_->update();
293 viewport_->update();
294 scale_offset_changed();
295}
296
297set< shared_ptr<SignalData> > View::get_visible_data() const
298{
299 shared_lock<shared_mutex> lock(session().signals_mutex());
300 const vector< shared_ptr<Signal> > &sigs(session().signals());
301
302 // Make a set of all the visible data objects
303 set< shared_ptr<SignalData> > visible_data;
304 for (const shared_ptr<Signal> sig : sigs)
305 if (sig->enabled())
306 visible_data.insert(sig->data());
307
308 return visible_data;
309}
310
311pair<double, double> View::get_time_extents() const
312{
313 double left_time = DBL_MAX, right_time = DBL_MIN;
314 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
315 for (const shared_ptr<SignalData> d : visible_data)
316 {
317 const vector< shared_ptr<Segment> > segments =
318 d->segments();
319 for (const shared_ptr<Segment> &s : segments) {
320 double samplerate = s->samplerate();
321 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
322
323 const double start_time = s->start_time();
324 left_time = min(left_time, start_time);
325 right_time = max(right_time, start_time +
326 d->get_max_sample_count() / samplerate);
327 }
328 }
329
330 if (left_time == DBL_MAX && right_time == DBL_MIN)
331 return make_pair(0.0, 0.0);
332
333 assert(left_time < right_time);
334 return make_pair(left_time, right_time);
335}
336
337bool View::cursors_shown() const
338{
339 return show_cursors_;
340}
341
342void View::show_cursors(bool show)
343{
344 show_cursors_ = show;
345 cursorheader_->update();
346 viewport_->update();
347}
348
349void View::centre_cursors()
350{
351 const double time_width = scale_ * viewport_->width();
352 cursors_.first()->set_time(offset_ + time_width * 0.4);
353 cursors_.second()->set_time(offset_ + time_width * 0.6);
354 cursorheader_->update();
355 viewport_->update();
356}
357
358CursorPair& View::cursors()
359{
360 return cursors_;
361}
362
363const CursorPair& View::cursors() const
364{
365 return cursors_;
366}
367
368const QPoint& View::hover_point() const
369{
370 return hover_point_;
371}
372
373void View::update_viewport()
374{
375 assert(viewport_);
376 viewport_->update();
377 header_->update();
378}
379
380void View::restack_all_row_items()
381{
382 // Make a set of owners
383 unordered_set< RowItemOwner* > owners;
384 for (const auto &r : *this)
385 owners.insert(r->owner());
386
387 // Make a list that is sorted from deepest first
388 vector< RowItemOwner* > sorted_owners(owners.begin(), owners.end());
389 sort(sorted_owners.begin(), sorted_owners.end(),
390 [](const RowItemOwner* a, const RowItemOwner *b) {
391 return a->depth() > b->depth(); });
392
393 // Restack the items recursively
394 for (auto &o : sorted_owners)
395 o->restack_items();
396
397 // Animate the items to their destination
398 for (const auto &r : *this)
399 r->animate_to_layout_v_offset();
400}
401
402void View::get_scroll_layout(double &length, double &offset) const
403{
404 const pair<double, double> extents = get_time_extents();
405 length = (extents.second - extents.first) / scale_;
406 offset = offset_ / scale_;
407}
408
409void View::set_zoom(double scale, int offset)
410{
411 const double cursor_offset = offset_ + scale_ * offset;
412 const double new_scale = max(min(scale, MaxScale), MinScale);
413 const double new_offset = cursor_offset - new_scale * offset;
414 set_scale_offset(new_scale, new_offset);
415}
416
417void View::calculate_tick_spacing()
418{
419 const double SpacingIncrement = 32.0f;
420 const double MinValueSpacing = 32.0f;
421
422 double min_width = SpacingIncrement, typical_width;
423
424 QFontMetrics m(QApplication::font());
425
426 do {
427 const double min_period = scale_ * min_width;
428
429 const int order = (int)floorf(log10f(min_period));
430 const double order_decimal = pow(10.0, order);
431
432 unsigned int unit = 0;
433
434 do {
435 tick_period_ = order_decimal * ScaleUnits[unit++];
436 } while (tick_period_ < min_period &&
437 unit < countof(ScaleUnits));
438
439 tick_prefix_ = (order - pv::util::FirstSIPrefixPower) / 3;
440
441 typical_width = m.boundingRect(0, 0, INT_MAX, INT_MAX,
442 Qt::AlignLeft | Qt::AlignTop,
443 format_time(offset_, tick_prefix_)).width() +
444 MinValueSpacing;
445
446 min_width += SpacingIncrement;
447
448 } while(typical_width > tick_period_ / scale_);
449}
450
451void View::update_scroll()
452{
453 assert(viewport_);
454
455 const QSize areaSize = viewport_->size();
456
457 // Set the horizontal scroll bar
458 double length = 0, offset = 0;
459 get_scroll_layout(length, offset);
460 length = max(length - areaSize.width(), 0.0);
461
462 int major_tick_distance = tick_period_ / scale_;
463
464 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
465 horizontalScrollBar()->setSingleStep(major_tick_distance);
466
467 updating_scroll_ = true;
468
469 if (length < MaxScrollValue) {
470 horizontalScrollBar()->setRange(0, length);
471 horizontalScrollBar()->setSliderPosition(offset);
472 } else {
473 horizontalScrollBar()->setRange(0, MaxScrollValue);
474 horizontalScrollBar()->setSliderPosition(
475 offset_ * MaxScrollValue / (scale_ * length));
476 }
477
478 updating_scroll_ = false;
479
480 // Set the vertical scrollbar
481 verticalScrollBar()->setPageStep(areaSize.height());
482
483 const pair<int, int> extents = v_extents();
484 const int extra_scroll_height = (extents.second - extents.first) / 4;
485 verticalScrollBar()->setRange(extents.first - extra_scroll_height,
486 extents.first + extra_scroll_height);
487}
488
489void View::update_layout()
490{
491 setViewportMargins(
492 header_->sizeHint().width() - pv::view::Header::BaselineOffset,
493 ruler_->sizeHint().height(), 0, 0);
494 ruler_->setGeometry(viewport_->x(), 0,
495 viewport_->width(), viewport_->y());
496 cursorheader_->setGeometry(
497 viewport_->x(),
498 ruler_->sizeHint().height() - cursorheader_->sizeHint().height() / 2,
499 viewport_->width(), cursorheader_->sizeHint().height());
500 header_->setGeometry(0, viewport_->y(),
501 header_->sizeHint().width(), viewport_->height());
502 update_scroll();
503}
504
505void View::paint_label(QPainter &p, int right, bool hover)
506{
507 (void)p;
508 (void)right;
509 (void)hover;
510}
511
512QRectF View::label_rect(int right)
513{
514 (void)right;
515 return QRectF();
516}
517
518bool View::add_channels_to_owner(
519 const vector< shared_ptr<sigrok::Channel> > &channels,
520 RowItemOwner *owner, int &offset,
521 unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
522 &signal_map,
523 std::function<bool (shared_ptr<RowItem>)> filter_func)
524{
525 bool any_added = false;
526
527 assert(owner);
528
529 for (const auto &channel : channels)
530 {
531 const auto iter = signal_map.find(channel);
532 if (iter == signal_map.end() ||
533 (filter_func && !filter_func((*iter).second)))
534 continue;
535
536 shared_ptr<RowItem> row_item = (*iter).second;
537 owner->add_child_item(row_item);
538 apply_offset(row_item, offset);
539 signal_map.erase(iter);
540
541 any_added = true;
542 }
543
544 return any_added;
545}
546
547void View::apply_offset(shared_ptr<RowItem> row_item, int &offset) {
548 assert(row_item);
549 const pair<int, int> extents = row_item->v_extents();
550 if (row_item->enabled())
551 offset += -extents.first;
552 row_item->force_to_v_offset(offset);
553 if (row_item->enabled())
554 offset += extents.second;
555}
556
557bool View::eventFilter(QObject *object, QEvent *event)
558{
559 const QEvent::Type type = event->type();
560 if (type == QEvent::MouseMove) {
561
562 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
563 if (object == viewport_)
564 hover_point_ = mouse_event->pos();
565 else if (object == ruler_ || object == cursorheader_)
566 hover_point_ = QPoint(mouse_event->x(), 0);
567 else if (object == header_)
568 hover_point_ = QPoint(0, mouse_event->y());
569 else
570 hover_point_ = QPoint(-1, -1);
571
572 hover_point_changed();
573
574 } else if (type == QEvent::Leave) {
575 hover_point_ = QPoint(-1, -1);
576 hover_point_changed();
577 }
578
579 return QObject::eventFilter(object, event);
580}
581
582bool View::viewportEvent(QEvent *e)
583{
584 switch(e->type()) {
585 case QEvent::Paint:
586 case QEvent::MouseButtonPress:
587 case QEvent::MouseButtonRelease:
588 case QEvent::MouseButtonDblClick:
589 case QEvent::MouseMove:
590 case QEvent::Wheel:
591 case QEvent::TouchBegin:
592 case QEvent::TouchUpdate:
593 case QEvent::TouchEnd:
594 return false;
595
596 default:
597 return QAbstractScrollArea::viewportEvent(e);
598 }
599}
600
601void View::resizeEvent(QResizeEvent*)
602{
603 update_layout();
604}
605
606void View::appearance_changed(bool label, bool content)
607{
608 if (label)
609 header_->update();
610 if (content)
611 viewport_->update();
612}
613
614void View::extents_changed(bool horz, bool vert)
615{
616 sticky_events_ |=
617 (horz ? SelectableItemHExtentsChanged : 0) |
618 (vert ? SelectableItemVExtentsChanged : 0);
619 lazy_event_handler_.start();
620}
621
622void View::h_scroll_value_changed(int value)
623{
624 if (updating_scroll_)
625 return;
626
627 const int range = horizontalScrollBar()->maximum();
628 if (range < MaxScrollValue)
629 offset_ = scale_ * value;
630 else {
631 double length = 0, offset;
632 get_scroll_layout(length, offset);
633 offset_ = scale_ * length * value / MaxScrollValue;
634 }
635
636 ruler_->update();
637 cursorheader_->update();
638 viewport_->update();
639}
640
641void View::v_scroll_value_changed(int value)
642{
643 v_offset_ = value;
644 header_->update();
645 viewport_->update();
646}
647
648void View::signals_changed()
649{
650 int offset = 0;
651
652 // Populate the traces
653 clear_child_items();
654
655 shared_ptr<sigrok::Device> device = session_.device();
656 assert(device);
657
658 // Collect a set of signals
659 unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
660 signal_map;
661
662 shared_lock<shared_mutex> lock(session_.signals_mutex());
663 const vector< shared_ptr<Signal> > &sigs(session_.signals());
664
665 for (const shared_ptr<Signal> &sig : sigs)
666 signal_map[sig->channel()] = sig;
667
668 // Populate channel groups
669 for (auto entry : device->channel_groups())
670 {
671 const shared_ptr<sigrok::ChannelGroup> &group = entry.second;
672
673 if (group->channels().size() <= 1)
674 continue;
675
676 shared_ptr<TraceGroup> trace_group(new TraceGroup());
677 int child_offset = 0;
678 if (add_channels_to_owner(group->channels(),
679 trace_group.get(), child_offset, signal_map))
680 {
681 add_child_item(trace_group);
682 apply_offset(trace_group, offset);
683 }
684 }
685
686 // Add the remaining logic channels
687 shared_ptr<TraceGroup> logic_trace_group(new TraceGroup());
688 int child_offset = 0;
689
690 if (add_channels_to_owner(device->channels(),
691 logic_trace_group.get(), child_offset, signal_map,
692 [](shared_ptr<RowItem> r) -> bool {
693 return dynamic_pointer_cast<LogicSignal>(r) != nullptr;
694 }))
695
696 {
697 add_child_item(logic_trace_group);
698 apply_offset(logic_trace_group, offset);
699 }
700
701 // Add the remaining channels
702 add_channels_to_owner(device->channels(), this, offset, signal_map);
703 assert(signal_map.empty());
704
705 // Add decode signals
706#ifdef ENABLE_DECODE
707 const vector< shared_ptr<DecodeTrace> > decode_sigs(
708 session().get_decode_signals());
709 for (auto s : decode_sigs) {
710 add_child_item(s);
711 apply_offset(s, offset);
712 }
713#endif
714
715 update_layout();
716}
717
718void View::data_updated()
719{
720 // Update the scroll bars
721 update_scroll();
722
723 // Repaint the view
724 viewport_->update();
725}
726
727void View::marker_time_changed()
728{
729 cursorheader_->update();
730 viewport_->update();
731}
732
733void View::on_signals_moved()
734{
735 update_scroll();
736 signals_moved();
737}
738
739void View::process_sticky_events()
740{
741 if (sticky_events_ & SelectableItemHExtentsChanged)
742 update_layout();
743 if (sticky_events_ & SelectableItemVExtentsChanged)
744 restack_all_row_items();
745
746 // Clear the sticky events
747 sticky_events_ = 0;
748}
749
750void View::on_hover_point_changed()
751{
752 for (shared_ptr<RowItem> r : *this)
753 r->hover_point_changed();
754}
755
756} // namespace view
757} // namespace pv