]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/view.cpp
View: Added time_markers
[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
198vector< shared_ptr<TimeItem> > View::time_items() const
199{
200 vector< shared_ptr<TimeItem> > items;
201 items.push_back(cursors_.first());
202 items.push_back(cursors_.second());
203 return items;
204}
205
206double View::scale() const
207{
208 return scale_;
209}
210
211double View::offset() const
212{
213 return offset_;
214}
215
216int View::owner_visual_v_offset() const
217{
218 return -v_offset_;
219}
220
221unsigned int View::depth() const
222{
223 return 0;
224}
225
226unsigned int View::tick_prefix() const
227{
228 return tick_prefix_;
229}
230
231double View::tick_period() const
232{
233 return tick_period_;
234}
235
236void View::zoom(double steps)
237{
238 zoom(steps, viewport_->width() / 2);
239}
240
241void View::zoom(double steps, int offset)
242{
243 set_zoom(scale_ * pow(3.0/2.0, -steps), offset);
244}
245
246void View::zoom_fit()
247{
248 const pair<double, double> extents = get_time_extents();
249 const double delta = extents.second - extents.first;
250 if (delta < 1e-12)
251 return;
252
253 assert(viewport_);
254 const int w = viewport_->width();
255 if (w <= 0)
256 return;
257
258 const double scale = max(min(delta / w, MaxScale), MinScale);
259 set_scale_offset(scale, extents.first);
260}
261
262void View::zoom_one_to_one()
263{
264 using pv::data::SignalData;
265
266 // Make a set of all the visible data objects
267 set< shared_ptr<SignalData> > visible_data = get_visible_data();
268 if (visible_data.empty())
269 return;
270
271 double samplerate = 0.0;
272 for (const shared_ptr<SignalData> d : visible_data) {
273 assert(d);
274 const vector< shared_ptr<Segment> > segments =
275 d->segments();
276 for (const shared_ptr<Segment> &s : segments)
277 samplerate = max(samplerate, s->samplerate());
278 }
279
280 if (samplerate == 0.0)
281 return;
282
283 assert(viewport_);
284 const int w = viewport_->width();
285 if (w <= 0)
286 return;
287
288 set_zoom(1.0 / samplerate, w / 2);
289}
290
291void View::set_scale_offset(double scale, double offset)
292{
293 scale_ = scale;
294 offset_ = offset;
295
296 calculate_tick_spacing();
297
298 update_scroll();
299 ruler_->update();
300 cursorheader_->update();
301 viewport_->update();
302 scale_offset_changed();
303}
304
305set< shared_ptr<SignalData> > View::get_visible_data() const
306{
307 shared_lock<shared_mutex> lock(session().signals_mutex());
308 const vector< shared_ptr<Signal> > &sigs(session().signals());
309
310 // Make a set of all the visible data objects
311 set< shared_ptr<SignalData> > visible_data;
312 for (const shared_ptr<Signal> sig : sigs)
313 if (sig->enabled())
314 visible_data.insert(sig->data());
315
316 return visible_data;
317}
318
319pair<double, double> View::get_time_extents() const
320{
321 double left_time = DBL_MAX, right_time = DBL_MIN;
322 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
323 for (const shared_ptr<SignalData> d : visible_data)
324 {
325 const vector< shared_ptr<Segment> > segments =
326 d->segments();
327 for (const shared_ptr<Segment> &s : segments) {
328 double samplerate = s->samplerate();
329 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
330
331 const double start_time = s->start_time();
332 left_time = min(left_time, start_time);
333 right_time = max(right_time, start_time +
334 d->get_max_sample_count() / samplerate);
335 }
336 }
337
338 if (left_time == DBL_MAX && right_time == DBL_MIN)
339 return make_pair(0.0, 0.0);
340
341 assert(left_time < right_time);
342 return make_pair(left_time, right_time);
343}
344
345bool View::cursors_shown() const
346{
347 return show_cursors_;
348}
349
350void View::show_cursors(bool show)
351{
352 show_cursors_ = show;
353 cursorheader_->update();
354 viewport_->update();
355}
356
357void View::centre_cursors()
358{
359 const double time_width = scale_ * viewport_->width();
360 cursors_.first()->set_time(offset_ + time_width * 0.4);
361 cursors_.second()->set_time(offset_ + time_width * 0.6);
362 cursorheader_->update();
363 viewport_->update();
364}
365
366CursorPair& View::cursors()
367{
368 return cursors_;
369}
370
371const CursorPair& View::cursors() const
372{
373 return cursors_;
374}
375
376const QPoint& View::hover_point() const
377{
378 return hover_point_;
379}
380
381void View::update_viewport()
382{
383 assert(viewport_);
384 viewport_->update();
385 header_->update();
386}
387
388void View::restack_all_row_items()
389{
390 // Make a set of owners
391 unordered_set< RowItemOwner* > owners;
392 for (const auto &r : *this)
393 owners.insert(r->owner());
394
395 // Make a list that is sorted from deepest first
396 vector< RowItemOwner* > sorted_owners(owners.begin(), owners.end());
397 sort(sorted_owners.begin(), sorted_owners.end(),
398 [](const RowItemOwner* a, const RowItemOwner *b) {
399 return a->depth() > b->depth(); });
400
401 // Restack the items recursively
402 for (auto &o : sorted_owners)
403 o->restack_items();
404
405 // Animate the items to their destination
406 for (const auto &r : *this)
407 r->animate_to_layout_v_offset();
408}
409
410void View::get_scroll_layout(double &length, double &offset) const
411{
412 const pair<double, double> extents = get_time_extents();
413 length = (extents.second - extents.first) / scale_;
414 offset = offset_ / scale_;
415}
416
417void View::set_zoom(double scale, int offset)
418{
419 const double cursor_offset = offset_ + scale_ * offset;
420 const double new_scale = max(min(scale, MaxScale), MinScale);
421 const double new_offset = cursor_offset - new_scale * offset;
422 set_scale_offset(new_scale, new_offset);
423}
424
425void View::calculate_tick_spacing()
426{
427 const double SpacingIncrement = 32.0f;
428 const double MinValueSpacing = 32.0f;
429
430 double min_width = SpacingIncrement, typical_width;
431
432 QFontMetrics m(QApplication::font());
433
434 do {
435 const double min_period = scale_ * min_width;
436
437 const int order = (int)floorf(log10f(min_period));
438 const double order_decimal = pow(10.0, order);
439
440 unsigned int unit = 0;
441
442 do {
443 tick_period_ = order_decimal * ScaleUnits[unit++];
444 } while (tick_period_ < min_period &&
445 unit < countof(ScaleUnits));
446
447 tick_prefix_ = (order - pv::util::FirstSIPrefixPower) / 3;
448
449 typical_width = m.boundingRect(0, 0, INT_MAX, INT_MAX,
450 Qt::AlignLeft | Qt::AlignTop,
451 format_time(offset_, tick_prefix_)).width() +
452 MinValueSpacing;
453
454 min_width += SpacingIncrement;
455
456 } while(typical_width > tick_period_ / scale_);
457}
458
459void View::update_scroll()
460{
461 assert(viewport_);
462
463 const QSize areaSize = viewport_->size();
464
465 // Set the horizontal scroll bar
466 double length = 0, offset = 0;
467 get_scroll_layout(length, offset);
468 length = max(length - areaSize.width(), 0.0);
469
470 int major_tick_distance = tick_period_ / scale_;
471
472 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
473 horizontalScrollBar()->setSingleStep(major_tick_distance);
474
475 updating_scroll_ = true;
476
477 if (length < MaxScrollValue) {
478 horizontalScrollBar()->setRange(0, length);
479 horizontalScrollBar()->setSliderPosition(offset);
480 } else {
481 horizontalScrollBar()->setRange(0, MaxScrollValue);
482 horizontalScrollBar()->setSliderPosition(
483 offset_ * MaxScrollValue / (scale_ * length));
484 }
485
486 updating_scroll_ = false;
487
488 // Set the vertical scrollbar
489 verticalScrollBar()->setPageStep(areaSize.height());
490
491 const pair<int, int> extents = v_extents();
492 const int extra_scroll_height = (extents.second - extents.first) / 4;
493 verticalScrollBar()->setRange(extents.first - extra_scroll_height,
494 extents.first + extra_scroll_height);
495}
496
497void View::update_layout()
498{
499 setViewportMargins(
500 header_->sizeHint().width() - pv::view::Header::BaselineOffset,
501 ruler_->sizeHint().height(), 0, 0);
502 ruler_->setGeometry(viewport_->x(), 0,
503 viewport_->width(), viewport_->y());
504 cursorheader_->setGeometry(
505 viewport_->x(),
506 ruler_->sizeHint().height() - cursorheader_->sizeHint().height() / 2,
507 viewport_->width(), cursorheader_->sizeHint().height());
508 header_->setGeometry(0, viewport_->y(),
509 header_->sizeHint().width(), viewport_->height());
510 update_scroll();
511}
512
513void View::paint_label(QPainter &p, int right, bool hover)
514{
515 (void)p;
516 (void)right;
517 (void)hover;
518}
519
520QRectF View::label_rect(int right)
521{
522 (void)right;
523 return QRectF();
524}
525
526bool View::add_channels_to_owner(
527 const vector< shared_ptr<sigrok::Channel> > &channels,
528 RowItemOwner *owner, int &offset,
529 unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
530 &signal_map,
531 std::function<bool (shared_ptr<RowItem>)> filter_func)
532{
533 bool any_added = false;
534
535 assert(owner);
536
537 for (const auto &channel : channels)
538 {
539 const auto iter = signal_map.find(channel);
540 if (iter == signal_map.end() ||
541 (filter_func && !filter_func((*iter).second)))
542 continue;
543
544 shared_ptr<RowItem> row_item = (*iter).second;
545 owner->add_child_item(row_item);
546 apply_offset(row_item, offset);
547 signal_map.erase(iter);
548
549 any_added = true;
550 }
551
552 return any_added;
553}
554
555void View::apply_offset(shared_ptr<RowItem> row_item, int &offset) {
556 assert(row_item);
557 const pair<int, int> extents = row_item->v_extents();
558 if (row_item->enabled())
559 offset += -extents.first;
560 row_item->force_to_v_offset(offset);
561 if (row_item->enabled())
562 offset += extents.second;
563}
564
565bool View::eventFilter(QObject *object, QEvent *event)
566{
567 const QEvent::Type type = event->type();
568 if (type == QEvent::MouseMove) {
569
570 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
571 if (object == viewport_)
572 hover_point_ = mouse_event->pos();
573 else if (object == ruler_ || object == cursorheader_)
574 hover_point_ = QPoint(mouse_event->x(), 0);
575 else if (object == header_)
576 hover_point_ = QPoint(0, mouse_event->y());
577 else
578 hover_point_ = QPoint(-1, -1);
579
580 hover_point_changed();
581
582 } else if (type == QEvent::Leave) {
583 hover_point_ = QPoint(-1, -1);
584 hover_point_changed();
585 }
586
587 return QObject::eventFilter(object, event);
588}
589
590bool View::viewportEvent(QEvent *e)
591{
592 switch(e->type()) {
593 case QEvent::Paint:
594 case QEvent::MouseButtonPress:
595 case QEvent::MouseButtonRelease:
596 case QEvent::MouseButtonDblClick:
597 case QEvent::MouseMove:
598 case QEvent::Wheel:
599 case QEvent::TouchBegin:
600 case QEvent::TouchUpdate:
601 case QEvent::TouchEnd:
602 return false;
603
604 default:
605 return QAbstractScrollArea::viewportEvent(e);
606 }
607}
608
609void View::resizeEvent(QResizeEvent*)
610{
611 update_layout();
612}
613
614void View::appearance_changed(bool label, bool content)
615{
616 if (label)
617 header_->update();
618 if (content)
619 viewport_->update();
620}
621
622void View::extents_changed(bool horz, bool vert)
623{
624 sticky_events_ |=
625 (horz ? SelectableItemHExtentsChanged : 0) |
626 (vert ? SelectableItemVExtentsChanged : 0);
627 lazy_event_handler_.start();
628}
629
630void View::h_scroll_value_changed(int value)
631{
632 if (updating_scroll_)
633 return;
634
635 const int range = horizontalScrollBar()->maximum();
636 if (range < MaxScrollValue)
637 offset_ = scale_ * value;
638 else {
639 double length = 0, offset;
640 get_scroll_layout(length, offset);
641 offset_ = scale_ * length * value / MaxScrollValue;
642 }
643
644 ruler_->update();
645 cursorheader_->update();
646 viewport_->update();
647}
648
649void View::v_scroll_value_changed(int value)
650{
651 v_offset_ = value;
652 header_->update();
653 viewport_->update();
654}
655
656void View::signals_changed()
657{
658 int offset = 0;
659
660 // Populate the traces
661 clear_child_items();
662
663 shared_ptr<sigrok::Device> device = session_.device();
664 assert(device);
665
666 // Collect a set of signals
667 unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
668 signal_map;
669
670 shared_lock<shared_mutex> lock(session_.signals_mutex());
671 const vector< shared_ptr<Signal> > &sigs(session_.signals());
672
673 for (const shared_ptr<Signal> &sig : sigs)
674 signal_map[sig->channel()] = sig;
675
676 // Populate channel groups
677 for (auto entry : device->channel_groups())
678 {
679 const shared_ptr<sigrok::ChannelGroup> &group = entry.second;
680
681 if (group->channels().size() <= 1)
682 continue;
683
684 shared_ptr<TraceGroup> trace_group(new TraceGroup());
685 int child_offset = 0;
686 if (add_channels_to_owner(group->channels(),
687 trace_group.get(), child_offset, signal_map))
688 {
689 add_child_item(trace_group);
690 apply_offset(trace_group, offset);
691 }
692 }
693
694 // Add the remaining logic channels
695 shared_ptr<TraceGroup> logic_trace_group(new TraceGroup());
696 int child_offset = 0;
697
698 if (add_channels_to_owner(device->channels(),
699 logic_trace_group.get(), child_offset, signal_map,
700 [](shared_ptr<RowItem> r) -> bool {
701 return dynamic_pointer_cast<LogicSignal>(r) != nullptr;
702 }))
703
704 {
705 add_child_item(logic_trace_group);
706 apply_offset(logic_trace_group, offset);
707 }
708
709 // Add the remaining channels
710 add_channels_to_owner(device->channels(), this, offset, signal_map);
711 assert(signal_map.empty());
712
713 // Add decode signals
714#ifdef ENABLE_DECODE
715 const vector< shared_ptr<DecodeTrace> > decode_sigs(
716 session().get_decode_signals());
717 for (auto s : decode_sigs) {
718 add_child_item(s);
719 apply_offset(s, offset);
720 }
721#endif
722
723 update_layout();
724}
725
726void View::data_updated()
727{
728 // Update the scroll bars
729 update_scroll();
730
731 // Repaint the view
732 viewport_->update();
733}
734
735void View::marker_time_changed()
736{
737 cursorheader_->update();
738 viewport_->update();
739}
740
741void View::on_signals_moved()
742{
743 update_scroll();
744 signals_moved();
745}
746
747void View::process_sticky_events()
748{
749 if (sticky_events_ & SelectableItemHExtentsChanged)
750 update_layout();
751 if (sticky_events_ & SelectableItemVExtentsChanged)
752 restack_all_row_items();
753
754 // Clear the sticky events
755 sticky_events_ = 0;
756}
757
758void View::on_hover_point_changed()
759{
760 for (shared_ptr<RowItem> r : *this)
761 r->hover_point_changed();
762}
763
764} // namespace view
765} // namespace pv