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