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