2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <libsigrokdecode/libsigrokdecode.h>
30 #include <boost/functional/hash.hpp>
33 #include <QApplication>
38 #include <QFileDialog>
39 #include <QFormLayout>
42 #include <QMessageBox>
43 #include <QPushButton>
44 #include <QTextStream>
47 #include "decodetrace.hpp"
49 #include "viewport.hpp"
51 #include <pv/globalsettings.hpp>
52 #include <pv/session.hpp>
53 #include <pv/strnatcmp.hpp>
54 #include <pv/data/decodesignal.hpp>
55 #include <pv/data/decode/annotation.hpp>
56 #include <pv/data/decode/decoder.hpp>
57 #include <pv/data/logic.hpp>
58 #include <pv/data/logicsegment.hpp>
59 #include <pv/widgets/decodergroupbox.hpp>
60 #include <pv/widgets/decodermenu.hpp>
61 #include <pv/widgets/flowlayout.hpp>
65 using std::lock_guard;
69 using std::numeric_limits;
71 using std::shared_ptr;
75 using pv::data::decode::Annotation;
76 using pv::data::decode::AnnotationClass;
77 using pv::data::decode::Row;
78 using pv::data::decode::DecodeChannel;
79 using pv::data::DecodeSignal;
85 const QColor DecodeTrace::ErrorBgColor = QColor(0xEF, 0x29, 0x29);
86 const QColor DecodeTrace::NoDecodeColor = QColor(0x88, 0x8A, 0x85);
87 const QColor DecodeTrace::ExpandMarkerWarnColor = QColor(0xFF, 0xA5, 0x00); // QColorConstants::Svg::orange
88 const QColor DecodeTrace::ExpandMarkerHiddenColor = QColor(0x69, 0x69, 0x69); // QColorConstants::Svg::dimgray
89 const uint8_t DecodeTrace::ExpansionAreaHeaderAlpha = 10 * 255 / 100;
90 const uint8_t DecodeTrace::ExpansionAreaAlpha = 5 * 255 / 100;
92 const int DecodeTrace::ArrowSize = 6;
93 const double DecodeTrace::EndCapWidth = 5;
94 const int DecodeTrace::RowTitleMargin = 7;
95 const int DecodeTrace::DrawPadding = 100;
97 const int DecodeTrace::MaxTraceUpdateRate = 1; // No more than 1 Hz
98 const int DecodeTrace::AnimationDurationInTicks = 7;
99 const int DecodeTrace::HiddenRowHideDelay = 1000; // 1 second
102 * Helper function for forceUpdate()
104 void invalidateLayout(QLayout* layout)
106 // Recompute the given layout and all its child layouts recursively
107 for (int i = 0; i < layout->count(); i++) {
108 QLayoutItem *item = layout->itemAt(i);
111 invalidateLayout(item->layout());
116 layout->invalidate();
120 void forceUpdate(QWidget* widget)
122 // Update all child widgets recursively
123 for (QObject* child : widget->children())
124 if (child->isWidgetType())
125 forceUpdate((QWidget*)child);
127 // Invalidate the layout of the widget itself
128 if (widget->layout())
129 invalidateLayout(widget->layout());
133 ContainerWidget::ContainerWidget(QWidget *parent) :
138 void ContainerWidget::resizeEvent(QResizeEvent* event)
140 QWidget::resizeEvent(event);
146 DecodeTrace::DecodeTrace(pv::Session &session,
147 shared_ptr<data::SignalBase> signalbase, int index) :
150 show_hidden_rows_(false),
151 delete_mapper_(this),
152 show_hide_mapper_(this),
153 row_show_hide_mapper_(this)
155 decode_signal_ = dynamic_pointer_cast<data::DecodeSignal>(base_);
157 GlobalSettings settings;
158 always_show_all_rows_ = settings.value(GlobalSettings::Key_Dec_AlwaysShowAllRows).toBool();
160 GlobalSettings::add_change_handler(this);
162 // Determine shortest string we want to see displayed in full
163 QFontMetrics m(QApplication::font());
164 min_useful_label_width_ = m.width("XX"); // e.g. two hex characters
166 default_row_height_ = (ViewItemPaintParams::text_height() * 6) / 4;
167 annotation_height_ = (ViewItemPaintParams::text_height() * 5) / 4;
169 // For the base color, we want to start at a very different color for
170 // every decoder stack, so multiply the index with a number that is
171 // rather close to 180 degrees of the color circle but not a dividend of 360
172 // Note: The offset equals the color of the first annotation
174 const int h = (120 + 160 * index) % 360;
175 const int s = DECODE_COLOR_SATURATION;
176 const int v = DECODE_COLOR_VALUE;
177 color.setHsv(h, s, v);
178 base_->set_color(color);
180 connect(decode_signal_.get(), SIGNAL(color_changed(QColor)),
181 this, SLOT(on_color_changed(QColor)));
183 connect(decode_signal_.get(), SIGNAL(new_annotations()),
184 this, SLOT(on_new_annotations()));
185 connect(decode_signal_.get(), SIGNAL(decode_reset()),
186 this, SLOT(on_decode_reset()));
187 connect(decode_signal_.get(), SIGNAL(decode_finished()),
188 this, SLOT(on_decode_finished()));
189 connect(decode_signal_.get(), SIGNAL(channels_updated()),
190 this, SLOT(on_channels_updated()));
192 connect(&delete_mapper_, SIGNAL(mapped(int)),
193 this, SLOT(on_delete_decoder(int)));
194 connect(&show_hide_mapper_, SIGNAL(mapped(int)),
195 this, SLOT(on_show_hide_decoder(int)));
196 connect(&row_show_hide_mapper_, SIGNAL(mapped(int)),
197 this, SLOT(on_show_hide_row(int)));
198 connect(&class_show_hide_mapper_, SIGNAL(mapped(QWidget*)),
199 this, SLOT(on_show_hide_class(QWidget*)));
201 connect(&delayed_trace_updater_, SIGNAL(timeout()),
202 this, SLOT(on_delayed_trace_update()));
203 delayed_trace_updater_.setSingleShot(true);
204 delayed_trace_updater_.setInterval(1000 / MaxTraceUpdateRate);
206 connect(&animation_timer_, SIGNAL(timeout()),
207 this, SLOT(on_animation_timer()));
208 animation_timer_.setInterval(1000 / 50);
210 connect(&delayed_hidden_row_hider_, SIGNAL(timeout()),
211 this, SLOT(on_hide_hidden_rows()));
212 delayed_hidden_row_hider_.setSingleShot(true);
213 delayed_hidden_row_hider_.setInterval(HiddenRowHideDelay);
215 default_marker_shape_ << QPoint(0, -ArrowSize);
216 default_marker_shape_ << QPoint(ArrowSize, 0);
217 default_marker_shape_ << QPoint(0, ArrowSize);
220 DecodeTrace::~DecodeTrace()
222 GlobalSettings::remove_change_handler(this);
224 for (DecodeTraceRow& r : rows_) {
225 for (QCheckBox* cb : r.selectors)
228 delete r.selector_container;
229 delete r.header_container;
234 bool DecodeTrace::enabled() const
239 shared_ptr<data::SignalBase> DecodeTrace::base() const
244 void DecodeTrace::set_owner(TraceTreeItemOwner *owner)
246 Trace::set_owner(owner);
248 // The owner is set in trace::View::signals_changed(), which is a slot.
249 // So after this trace was added to the view, we won't have an owner
250 // that we need to initialize in update_rows(). Once we do, we call it
251 // from on_decode_reset().
255 pair<int, int> DecodeTrace::v_extents() const
257 // Make an empty decode trace appear symmetrical
258 if (visible_rows_ == 0)
259 return make_pair(-default_row_height_, default_row_height_);
261 unsigned int height = 0;
262 for (const DecodeTraceRow& r : rows_)
263 if (r.currently_visible)
266 return make_pair(-default_row_height_, height);
269 void DecodeTrace::paint_back(QPainter &p, ViewItemPaintParams &pp)
271 Trace::paint_back(p, pp);
272 paint_axis(p, pp, get_visual_y());
275 void DecodeTrace::paint_mid(QPainter &p, ViewItemPaintParams &pp)
277 lock_guard<mutex> lock(row_modification_mutex_);
278 unsigned int visible_rows;
280 #if DECODETRACE_SHOW_RENDER_TIME
281 render_time_.restart();
284 // Set default pen to allow for text width calculation
287 pair<uint64_t, uint64_t> sample_range = get_view_sample_range(pp.left(), pp.right());
289 // Just because the view says we see a certain sample range it
290 // doesn't mean we have this many decoded samples, too, so crop
291 // the range to what has been decoded already
292 sample_range.second = min((int64_t)sample_range.second,
293 decode_signal_->get_decoded_sample_count(current_segment_, false));
296 int y = get_visual_y();
298 for (DecodeTraceRow& r : rows_) {
299 // If the row is hidden, we don't want to fetch annotations
300 assert(r.decode_row);
301 assert(r.decode_row->decoder());
302 if ((!r.decode_row->decoder()->visible()) ||
303 ((!r.decode_row->visible() && (!show_hidden_rows_) && (!r.expanding) && (!r.expanded) && (!r.collapsing)))) {
304 r.currently_visible = false;
308 deque<const Annotation*> annotations;
309 decode_signal_->get_annotation_subset(annotations, r.decode_row,
310 current_segment_, sample_range.first, sample_range.second);
312 // Show row if there are visible annotations, when user wants to see
313 // all rows that have annotations somewhere and this one is one of them
314 // or when the row has at least one hidden annotation class
315 r.currently_visible = !annotations.empty();
316 if (!r.currently_visible) {
317 size_t ann_count = decode_signal_->get_annotation_count(r.decode_row, current_segment_);
318 r.currently_visible = ((always_show_all_rows_ || r.has_hidden_classes) &&
319 (ann_count > 0)) || r.expanded;
322 if (r.currently_visible) {
323 draw_annotations(annotations, p, pp, y, r);
329 draw_unresolved_period(p, pp.left(), pp.right());
331 if (visible_rows != visible_rows_) {
332 visible_rows_ = visible_rows;
334 // Call order is important, otherwise the lazy event handler won't work
335 owner_->extents_changed(false, true);
336 owner_->row_item_appearance_changed(false, true);
339 const QString err = decode_signal_->error_message();
341 draw_error(p, err, pp);
343 #if DECODETRACE_SHOW_RENDER_TIME
344 qDebug() << "Rendering" << base_->name() << "took" << render_time_.elapsed() << "ms";
348 void DecodeTrace::paint_fore(QPainter &p, ViewItemPaintParams &pp)
350 unsigned int y = get_visual_y();
352 update_expanded_rows();
354 for (const DecodeTraceRow& r : rows_) {
355 if (!r.currently_visible)
358 p.setPen(QPen(Qt::NoPen));
360 if (r.expand_marker_highlighted)
361 p.setBrush(QApplication::palette().brush(QPalette::Highlight));
362 else if (!r.decode_row->visible())
363 p.setBrush(ExpandMarkerHiddenColor);
364 else if (r.has_hidden_classes)
365 p.setBrush(ExpandMarkerWarnColor);
367 p.setBrush(QApplication::palette().brush(QPalette::WindowText));
369 // Draw expansion marker
370 QPolygon marker(r.expand_marker_shape);
371 marker.translate(pp.left(), y);
372 p.drawPolygon(marker);
374 p.setBrush(QApplication::palette().brush(QPalette::WindowText));
376 const QRect text_rect(pp.left() + ArrowSize * 2, y - r.height / 2,
377 pp.right() - pp.left(), r.height);
378 const QString h(r.decode_row->title());
379 const int f = Qt::AlignLeft | Qt::AlignVCenter |
383 p.setPen(QApplication::palette().color(QPalette::Base));
384 for (int dx = -1; dx <= 1; dx++)
385 for (int dy = -1; dy <= 1; dy++)
386 if (dx != 0 && dy != 0)
387 p.drawText(text_rect.translated(dx, dy), f, h);
390 if (!r.decode_row->visible())
391 p.setPen(ExpandMarkerHiddenColor);
393 p.setPen(QApplication::palette().color(QPalette::WindowText));
395 p.drawText(text_rect, f, h);
400 if (show_hover_marker_)
401 paint_hover_marker(p);
404 void DecodeTrace::update_stack_button()
406 const vector< shared_ptr<Decoder> > &stack = decode_signal_->decoder_stack();
408 // Only show decoders in the menu that can be stacked onto the last one in the stack
409 if (!stack.empty()) {
410 const srd_decoder* d = stack.back()->get_srd_decoder();
413 pv::widgets::DecoderMenu *const decoder_menu =
414 new pv::widgets::DecoderMenu(stack_button_, (const char*)(d->outputs->data));
415 connect(decoder_menu, SIGNAL(decoder_selected(srd_decoder*)),
416 this, SLOT(on_stack_decoder(srd_decoder*)));
418 decoder_menu->setStyleSheet("QMenu { menu-scrollable: 1; }");
420 stack_button_->setMenu(decoder_menu);
421 stack_button_->show();
426 // No decoders available for stacking
427 stack_button_->setMenu(nullptr);
428 stack_button_->hide();
431 void DecodeTrace::populate_popup_form(QWidget *parent, QFormLayout *form)
435 // Add the standard options
436 Trace::populate_popup_form(parent, form);
438 // Add the decoder options
440 channel_id_map_.clear();
441 init_state_map_.clear();
442 decoder_forms_.clear();
444 const vector< shared_ptr<Decoder> > &stack = decode_signal_->decoder_stack();
447 QLabel *const l = new QLabel(
448 tr("<p><i>No decoders in the stack</i></p>"));
449 l->setAlignment(Qt::AlignCenter);
452 auto iter = stack.cbegin();
453 for (int i = 0; i < (int)stack.size(); i++, iter++) {
454 shared_ptr<Decoder> dec(*iter);
455 create_decoder_form(i, dec, parent, form);
458 form->addRow(new QLabel(
459 tr("<i>* Required channels</i>"), parent));
462 // Add stacking button
463 stack_button_ = new QPushButton(tr("Stack Decoder"), parent);
464 stack_button_->setToolTip(tr("Stack a higher-level decoder on top of this one"));
465 update_stack_button();
467 QHBoxLayout *stack_button_box = new QHBoxLayout;
468 stack_button_box->addWidget(stack_button_, 0, Qt::AlignRight);
469 form->addRow(stack_button_box);
472 QMenu* DecodeTrace::create_header_context_menu(QWidget *parent)
474 QMenu *const menu = Trace::create_header_context_menu(parent);
476 menu->addSeparator();
478 QAction *const del = new QAction(tr("Delete"), this);
479 del->setShortcuts(QKeySequence::Delete);
480 connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
481 menu->addAction(del);
486 QMenu* DecodeTrace::create_view_context_menu(QWidget *parent, QPoint &click_pos)
488 // Get entries from default menu before adding our own
489 QMenu *const menu = new QMenu(parent);
491 QMenu* default_menu = Trace::create_view_context_menu(parent, click_pos);
493 for (QAction *action : default_menu->actions()) { // clazy:exclude=range-loop
494 menu->addAction(action);
495 if (action->parent() == default_menu)
496 action->setParent(menu);
500 // Add separator if needed
501 if (menu->actions().length() > 0)
502 menu->addSeparator();
505 selected_row_ = nullptr;
506 const DecodeTraceRow* r = get_row_at_point(click_pos);
508 selected_row_ = r->decode_row;
510 const View *const view = owner_->view();
512 QPoint pos = view->viewport()->mapFrom(parent, click_pos);
514 // Default sample range is "from here"
515 const pair<uint64_t, uint64_t> sample_range = get_view_sample_range(pos.x(), pos.x() + 1);
516 selected_sample_range_ = make_pair(sample_range.first, numeric_limits<uint64_t>::max());
518 if (decode_signal_->is_paused()) {
519 QAction *const resume =
520 new QAction(tr("Resume decoding"), this);
521 resume->setIcon(QIcon::fromTheme("media-playback-start",
522 QIcon(":/icons/media-playback-start.png")));
523 connect(resume, SIGNAL(triggered()), this, SLOT(on_pause_decode()));
524 menu->addAction(resume);
526 QAction *const pause =
527 new QAction(tr("Pause decoding"), this);
528 pause->setIcon(QIcon::fromTheme("media-playback-pause",
529 QIcon(":/icons/media-playback-pause.png")));
530 connect(pause, SIGNAL(triggered()), this, SLOT(on_pause_decode()));
531 menu->addAction(pause);
534 QAction *const copy_annotation_to_clipboard =
535 new QAction(tr("Copy annotation text to clipboard"), this);
536 copy_annotation_to_clipboard->setIcon(QIcon::fromTheme("edit-paste",
537 QIcon(":/icons/edit-paste.svg")));
538 connect(copy_annotation_to_clipboard, SIGNAL(triggered()), this, SLOT(on_copy_annotation_to_clipboard()));
539 menu->addAction(copy_annotation_to_clipboard);
541 menu->addSeparator();
543 QAction *const export_all_rows =
544 new QAction(tr("Export all annotations"), this);
545 export_all_rows->setIcon(QIcon::fromTheme("document-save-as",
546 QIcon(":/icons/document-save-as.png")));
547 connect(export_all_rows, SIGNAL(triggered()), this, SLOT(on_export_all_rows()));
548 menu->addAction(export_all_rows);
550 QAction *const export_row =
551 new QAction(tr("Export all annotations for this row"), this);
552 export_row->setIcon(QIcon::fromTheme("document-save-as",
553 QIcon(":/icons/document-save-as.png")));
554 connect(export_row, SIGNAL(triggered()), this, SLOT(on_export_row()));
555 menu->addAction(export_row);
557 menu->addSeparator();
559 QAction *const export_all_rows_from_here =
560 new QAction(tr("Export all annotations, starting here"), this);
561 export_all_rows_from_here->setIcon(QIcon::fromTheme("document-save-as",
562 QIcon(":/icons/document-save-as.png")));
563 connect(export_all_rows_from_here, SIGNAL(triggered()), this, SLOT(on_export_all_rows_from_here()));
564 menu->addAction(export_all_rows_from_here);
566 QAction *const export_row_from_here =
567 new QAction(tr("Export annotations for this row, starting here"), this);
568 export_row_from_here->setIcon(QIcon::fromTheme("document-save-as",
569 QIcon(":/icons/document-save-as.png")));
570 connect(export_row_from_here, SIGNAL(triggered()), this, SLOT(on_export_row_from_here()));
571 menu->addAction(export_row_from_here);
573 menu->addSeparator();
575 QAction *const export_all_rows_with_cursor =
576 new QAction(tr("Export all annotations within cursor range"), this);
577 export_all_rows_with_cursor->setIcon(QIcon::fromTheme("document-save-as",
578 QIcon(":/icons/document-save-as.png")));
579 connect(export_all_rows_with_cursor, SIGNAL(triggered()), this, SLOT(on_export_all_rows_with_cursor()));
580 menu->addAction(export_all_rows_with_cursor);
582 QAction *const export_row_with_cursor =
583 new QAction(tr("Export annotations for this row within cursor range"), this);
584 export_row_with_cursor->setIcon(QIcon::fromTheme("document-save-as",
585 QIcon(":/icons/document-save-as.png")));
586 connect(export_row_with_cursor, SIGNAL(triggered()), this, SLOT(on_export_row_with_cursor()));
587 menu->addAction(export_row_with_cursor);
589 if (!view->cursors()->enabled()) {
590 export_all_rows_with_cursor->setEnabled(false);
591 export_row_with_cursor->setEnabled(false);
597 void DecodeTrace::delete_pressed()
602 void DecodeTrace::hover_point_changed(const QPoint &hp)
604 Trace::hover_point_changed(hp);
608 DecodeTraceRow* hover_row = get_row_at_point(hp);
610 // Row expansion marker handling
611 for (DecodeTraceRow& r : rows_)
612 r.expand_marker_highlighted = false;
615 int row_y = get_row_y(hover_row);
616 if ((hp.x() > 0) && (hp.x() < (int)(ArrowSize + 3 + hover_row->title_width)) &&
617 (hp.y() > (int)(row_y - ArrowSize)) && (hp.y() < (int)(row_y + ArrowSize))) {
619 hover_row->expand_marker_highlighted = true;
620 show_hidden_rows_ = true;
621 delayed_hidden_row_hider_.start();
627 QString ann = get_annotation_at_point(hp);
629 if (!ann.isEmpty()) {
630 QFontMetrics m(QToolTip::font());
631 const QRect text_size = m.boundingRect(QRect(), 0, ann);
633 // This is OS-specific and unfortunately we can't query it, so
634 // use an approximation to at least try to minimize the error.
635 const int padding = default_row_height_ + 8;
637 // Make sure the tool tip doesn't overlap with the mouse cursor.
638 // If it did, the tool tip would constantly hide and re-appear.
639 // We also push it up by one row so that it appears above the
640 // decode trace, not below.
642 p.setX(hp.x() - (text_size.width() / 2) - padding);
644 p.setY(get_row_y(hover_row) - default_row_height_ -
645 text_size.height() - padding);
647 const View *const view = owner_->view();
649 QToolTip::showText(view->viewport()->mapToGlobal(p), ann);
652 QToolTip::hideText();
655 QToolTip::hideText();
658 void DecodeTrace::mouse_left_press_event(const QMouseEvent* event)
660 // Update container widths which depend on the scrollarea's current width
661 update_expanded_rows();
663 // Handle row expansion marker
664 for (DecodeTraceRow& r : rows_) {
665 if (!r.expand_marker_highlighted)
668 unsigned int y = get_row_y(&r);
669 if ((event->x() > 0) && (event->x() <= (int)(ArrowSize + 3 + r.title_width)) &&
670 (event->y() > (int)(y - (default_row_height_ / 2))) &&
671 (event->y() <= (int)(y + (default_row_height_ / 2)))) {
676 r.anim_shape = ArrowSize;
681 // Force geometry update of the widget container to get
682 // an up-to-date height (which also depends on the width)
683 forceUpdate(r.container);
685 r.container->setVisible(true);
686 r.expanded_height = 2 * default_row_height_ + r.container->sizeHint().height();
689 r.animation_step = 0;
690 r.anim_height = r.height;
692 animation_timer_.start();
697 void DecodeTrace::draw_annotations(deque<const Annotation*>& annotations,
698 QPainter &p, const ViewItemPaintParams &pp, int y, const DecodeTraceRow& row)
700 Annotation::Class block_class = 0;
701 bool block_class_uniform = true;
702 qreal block_start = 0;
703 int block_ann_count = 0;
705 const Annotation* prev_ann;
706 qreal prev_end = INT_MIN;
710 double samples_per_pixel, pixels_offset;
711 tie(pixels_offset, samples_per_pixel) =
712 get_pixels_offset_samples_per_pixel();
714 // Gather all annotations that form a visual "block" and draw them as such
715 for (const Annotation* a : annotations) {
717 const qreal abs_a_start = a->start_sample() / samples_per_pixel;
718 const qreal abs_a_end = a->end_sample() / samples_per_pixel;
720 const qreal a_start = abs_a_start - pixels_offset;
721 a_end = abs_a_end - pixels_offset;
723 const qreal a_width = a_end - a_start;
724 const qreal delta = a_end - prev_end;
726 bool a_is_separate = false;
728 // Annotation wider than the threshold for a useful label width?
729 if (a_width >= min_useful_label_width_) {
730 for (const QString &ann_text : *(a->annotations())) {
731 const qreal w = p.boundingRect(QRectF(), 0, ann_text).width();
732 // Annotation wide enough to fit a label? Don't put it in a block then
734 a_is_separate = true;
740 // Were the previous and this annotation more than a pixel apart?
741 if ((abs(delta) > 1) || a_is_separate) {
742 // Block was broken, draw annotations that form the current block
743 if (block_ann_count == 1)
744 draw_annotation(prev_ann, p, pp, y, row);
745 else if (block_ann_count > 0)
746 draw_annotation_block(block_start, prev_end, block_class,
747 block_class_uniform, p, y, row);
753 draw_annotation(a, p, pp, y, row);
754 // Next annotation must start a new block. delta will be > 1
755 // because we set prev_end to INT_MIN but that's okay since
756 // block_ann_count will be 0 and nothing will be drawn
763 if (block_ann_count == 0) {
764 block_start = a_start;
765 block_class = a->ann_class_id();
766 block_class_uniform = true;
768 if (a->ann_class_id() != block_class)
769 block_class_uniform = false;
775 if (block_ann_count == 1)
776 draw_annotation(prev_ann, p, pp, y, row);
777 else if (block_ann_count > 0)
778 draw_annotation_block(block_start, prev_end, block_class,
779 block_class_uniform, p, y, row);
782 void DecodeTrace::draw_annotation(const Annotation* a, QPainter &p,
783 const ViewItemPaintParams &pp, int y, const DecodeTraceRow& row) const
785 double samples_per_pixel, pixels_offset;
786 tie(pixels_offset, samples_per_pixel) =
787 get_pixels_offset_samples_per_pixel();
789 const double start = a->start_sample() / samples_per_pixel - pixels_offset;
790 const double end = a->end_sample() / samples_per_pixel - pixels_offset;
792 p.setPen(a->dark_color());
793 p.setBrush(a->color());
795 if ((start > (pp.right() + DrawPadding)) || (end < (pp.left() - DrawPadding)))
798 if (a->start_sample() == a->end_sample())
799 draw_instant(a, p, start, y);
801 draw_range(a, p, start, end, y, pp, row.title_width);
804 void DecodeTrace::draw_annotation_block(qreal start, qreal end,
805 Annotation::Class ann_class, bool use_ann_format, QPainter &p, int y,
806 const DecodeTraceRow& row) const
808 const double top = y + .5 - annotation_height_ / 2;
809 const double bottom = y + .5 + annotation_height_ / 2;
810 const double width = end - start;
812 // If all annotations in this block are of the same type, we can use the
813 // one format that all of these annotations have. Otherwise, we should use
814 // a neutral color (i.e. gray)
815 if (use_ann_format) {
816 p.setPen(row.decode_row->get_dark_class_color(ann_class));
817 p.setBrush(QBrush(row.decode_row->get_class_color(ann_class), Qt::Dense4Pattern));
819 p.setPen(QColor(Qt::darkGray));
820 p.setBrush(QBrush(Qt::gray, Qt::Dense4Pattern));
824 p.drawLine(QPointF(start, top), QPointF(start, bottom));
826 const QRectF rect(start, top, width, bottom - top);
827 const int r = annotation_height_ / 4;
828 p.drawRoundedRect(rect, r, r);
832 void DecodeTrace::draw_instant(const Annotation* a, QPainter &p, qreal x, int y) const
834 const QString text = a->annotations()->empty() ?
835 QString() : a->annotations()->back();
836 const qreal w = min((qreal)p.boundingRect(QRectF(), 0, text).width(),
837 0.0) + annotation_height_;
838 const QRectF rect(x - w / 2, y - annotation_height_ / 2, w, annotation_height_);
840 p.drawRoundedRect(rect, annotation_height_ / 2, annotation_height_ / 2);
843 p.drawText(rect, Qt::AlignCenter | Qt::AlignVCenter, text);
846 void DecodeTrace::draw_range(const Annotation* a, QPainter &p,
847 qreal start, qreal end, int y, const ViewItemPaintParams &pp,
848 int row_title_width) const
850 const qreal top = y + .5 - annotation_height_ / 2;
851 const qreal bottom = y + .5 + annotation_height_ / 2;
852 const vector<QString>* annotations = a->annotations();
854 // If the two ends are within 1 pixel, draw a vertical line
855 if (start + 1.0 > end) {
856 p.drawLine(QPointF(start, top), QPointF(start, bottom));
860 const qreal cap_width = min((end - start) / 4, EndCapWidth);
863 QPointF(start, y + .5f),
864 QPointF(start + cap_width, top),
865 QPointF(end - cap_width, top),
866 QPointF(end, y + .5f),
867 QPointF(end - cap_width, bottom),
868 QPointF(start + cap_width, bottom)
871 p.drawConvexPolygon(pts, countof(pts));
873 if (annotations->empty())
876 const int ann_start = start + cap_width;
877 const int ann_end = end - cap_width;
879 const int real_start = max(ann_start, pp.left() + ArrowSize + row_title_width);
880 const int real_end = min(ann_end, pp.right());
881 const int real_width = real_end - real_start;
883 QRectF rect(real_start, y - annotation_height_ / 2, real_width, annotation_height_);
884 if (rect.width() <= 4)
889 // Try to find an annotation that will fit
890 QString best_annotation;
893 for (const QString &s : *annotations) {
894 const int w = p.boundingRect(QRectF(), 0, s).width();
895 if (w <= rect.width() && w > best_width)
896 best_annotation = s, best_width = w;
899 if (best_annotation.isEmpty())
900 best_annotation = annotations->back();
902 // If not ellide the last in the list
903 p.drawText(rect, Qt::AlignCenter, p.fontMetrics().elidedText(
904 best_annotation, Qt::ElideRight, rect.width()));
907 void DecodeTrace::draw_error(QPainter &p, const QString &message,
908 const ViewItemPaintParams &pp)
910 const int y = get_visual_y();
912 double samples_per_pixel, pixels_offset;
913 tie(pixels_offset, samples_per_pixel) = get_pixels_offset_samples_per_pixel();
915 p.setPen(ErrorBgColor.darker());
916 p.setBrush(ErrorBgColor);
918 const QRectF bounding_rect = QRectF(pp.left(), INT_MIN / 2 + y, pp.right(), INT_MAX);
920 const QRectF text_rect = p.boundingRect(bounding_rect, Qt::AlignCenter, message);
921 const qreal r = text_rect.height() / 4;
923 p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r, Qt::AbsoluteSize);
926 p.drawText(text_rect, message);
929 void DecodeTrace::draw_unresolved_period(QPainter &p, int left, int right) const
931 double samples_per_pixel, pixels_offset;
933 const int64_t sample_count = decode_signal_->get_working_sample_count(current_segment_);
934 if (sample_count == 0)
937 const int64_t samples_decoded = decode_signal_->get_decoded_sample_count(current_segment_, true);
938 if (sample_count == samples_decoded)
941 const int y = get_visual_y();
943 tie(pixels_offset, samples_per_pixel) = get_pixels_offset_samples_per_pixel();
945 const double start = max(samples_decoded /
946 samples_per_pixel - pixels_offset, left - 1.0);
947 const double end = min(sample_count / samples_per_pixel -
948 pixels_offset, right + 1.0);
949 const QRectF no_decode_rect(start, y - (annotation_height_ / 2) - 0.5,
950 end - start, annotation_height_);
952 p.setPen(QPen(Qt::NoPen));
953 p.setBrush(Qt::white);
954 p.drawRect(no_decode_rect);
956 p.setPen(NoDecodeColor);
957 p.setBrush(QBrush(NoDecodeColor, Qt::Dense6Pattern));
958 p.drawRect(no_decode_rect);
961 pair<double, double> DecodeTrace::get_pixels_offset_samples_per_pixel() const
965 const View *view = owner_->view();
968 const double scale = view->scale();
971 const double pixels_offset =
972 ((view->offset() - decode_signal_->start_time()) / scale).convert_to<double>();
974 double samplerate = decode_signal_->get_samplerate();
976 // Show sample rate as 1Hz when it is unknown
977 if (samplerate == 0.0)
980 return make_pair(pixels_offset, samplerate * scale);
983 pair<uint64_t, uint64_t> DecodeTrace::get_view_sample_range(
984 int x_start, int x_end) const
986 double samples_per_pixel, pixels_offset;
987 tie(pixels_offset, samples_per_pixel) =
988 get_pixels_offset_samples_per_pixel();
990 const uint64_t start = (uint64_t)max(
991 (x_start + pixels_offset) * samples_per_pixel, 0.0);
992 const uint64_t end = (uint64_t)max(
993 (x_end + pixels_offset) * samples_per_pixel, 0.0);
995 return make_pair(start, end);
998 unsigned int DecodeTrace::get_row_y(const DecodeTraceRow* row) const
1002 unsigned int y = get_visual_y();
1004 for (const DecodeTraceRow& r : rows_) {
1005 if (!r.currently_visible)
1008 if (row->decode_row == r.decode_row)
1017 DecodeTraceRow* DecodeTrace::get_row_at_point(const QPoint &point)
1019 int y = get_visual_y() - (default_row_height_ / 2);
1021 for (DecodeTraceRow& r : rows_) {
1022 if (!r.currently_visible)
1025 if ((point.y() >= y) && (point.y() < (int)(y + r.height)))
1034 const QString DecodeTrace::get_annotation_at_point(const QPoint &point)
1039 const pair<uint64_t, uint64_t> sample_range =
1040 get_view_sample_range(point.x(), point.x() + 1);
1041 const DecodeTraceRow* r = get_row_at_point(point);
1046 if (point.y() > (int)(get_row_y(r) + (annotation_height_ / 2)))
1049 deque<const Annotation*> annotations;
1051 decode_signal_->get_annotation_subset(annotations, r->decode_row,
1052 current_segment_, sample_range.first, sample_range.second);
1054 return (annotations.empty()) ?
1055 QString() : annotations[0]->annotations()->front();
1058 void DecodeTrace::create_decoder_form(int index, shared_ptr<Decoder> &dec,
1059 QWidget *parent, QFormLayout *form)
1061 GlobalSettings settings;
1064 const srd_decoder *const decoder = dec->get_srd_decoder();
1067 const bool decoder_deletable = index > 0;
1069 pv::widgets::DecoderGroupBox *const group =
1070 new pv::widgets::DecoderGroupBox(
1071 QString::fromUtf8(decoder->name),
1072 tr("%1:\n%2").arg(QString::fromUtf8(decoder->longname),
1073 QString::fromUtf8(decoder->desc)),
1074 nullptr, decoder_deletable);
1075 group->set_decoder_visible(dec->visible());
1077 if (decoder_deletable) {
1078 delete_mapper_.setMapping(group, index);
1079 connect(group, SIGNAL(delete_decoder()), &delete_mapper_, SLOT(map()));
1082 show_hide_mapper_.setMapping(group, index);
1083 connect(group, SIGNAL(show_hide_decoder()),
1084 &show_hide_mapper_, SLOT(map()));
1086 QFormLayout *const decoder_form = new QFormLayout;
1087 group->add_layout(decoder_form);
1089 const vector<DecodeChannel> channels = decode_signal_->get_channels();
1092 for (const DecodeChannel& ch : channels) {
1093 // Ignore channels not part of the decoder we create the form for
1094 if (ch.decoder_ != dec)
1097 QComboBox *const combo = create_channel_selector(parent, &ch);
1098 QComboBox *const combo_init_state = create_channel_selector_init_state(parent, &ch);
1100 channel_id_map_[combo] = ch.id;
1101 init_state_map_[combo_init_state] = ch.id;
1103 connect(combo, SIGNAL(currentIndexChanged(int)),
1104 this, SLOT(on_channel_selected(int)));
1105 connect(combo_init_state, SIGNAL(currentIndexChanged(int)),
1106 this, SLOT(on_init_state_changed(int)));
1108 QHBoxLayout *const hlayout = new QHBoxLayout;
1109 hlayout->addWidget(combo);
1110 hlayout->addWidget(combo_init_state);
1112 if (!settings.value(GlobalSettings::Key_Dec_InitialStateConfigurable).toBool())
1113 combo_init_state->hide();
1115 const QString required_flag = ch.is_optional ? QString() : QString("*");
1116 decoder_form->addRow(tr("<b>%1</b> (%2) %3")
1117 .arg(ch.name, ch.desc, required_flag), hlayout);
1121 shared_ptr<binding::Decoder> binding(
1122 new binding::Decoder(decode_signal_, dec));
1123 binding->add_properties_to_form(decoder_form, true);
1125 bindings_.push_back(binding);
1127 form->addRow(group);
1128 decoder_forms_.push_back(group);
1131 QComboBox* DecodeTrace::create_channel_selector(QWidget *parent, const DecodeChannel *ch)
1133 const auto sigs(session_.signalbases());
1135 // Sort signals in natural order
1136 vector< shared_ptr<data::SignalBase> > sig_list(sigs.begin(), sigs.end());
1137 sort(sig_list.begin(), sig_list.end(),
1138 [](const shared_ptr<data::SignalBase> &a,
1139 const shared_ptr<data::SignalBase> &b) {
1140 return strnatcasecmp(a->name().toStdString(),
1141 b->name().toStdString()) < 0; });
1143 QComboBox *selector = new QComboBox(parent);
1145 selector->addItem("-", qVariantFromValue((void*)nullptr));
1147 if (!ch->assigned_signal)
1148 selector->setCurrentIndex(0);
1150 for (const shared_ptr<data::SignalBase> &b : sig_list) {
1152 if (b->logic_data() && b->enabled()) {
1153 selector->addItem(b->name(),
1154 qVariantFromValue((void*)b.get()));
1156 if (ch->assigned_signal == b.get())
1157 selector->setCurrentIndex(selector->count() - 1);
1164 QComboBox* DecodeTrace::create_channel_selector_init_state(QWidget *parent,
1165 const DecodeChannel *ch)
1167 QComboBox *selector = new QComboBox(parent);
1169 selector->addItem("0", qVariantFromValue((int)SRD_INITIAL_PIN_LOW));
1170 selector->addItem("1", qVariantFromValue((int)SRD_INITIAL_PIN_HIGH));
1171 selector->addItem("X", qVariantFromValue((int)SRD_INITIAL_PIN_SAME_AS_SAMPLE0));
1173 selector->setCurrentIndex(ch->initial_pin_state);
1175 selector->setToolTip("Initial (assumed) pin value before the first sample");
1180 void DecodeTrace::export_annotations(deque<const Annotation*>& annotations) const
1182 GlobalSettings settings;
1183 const QString dir = settings.value("MainWindow/SaveDirectory").toString();
1185 const QString file_name = QFileDialog::getSaveFileName(
1186 owner_->view(), tr("Export annotations"), dir, tr("Text Files (*.txt);;All Files (*)"));
1188 if (file_name.isEmpty())
1191 QString format = settings.value(GlobalSettings::Key_Dec_ExportFormat).toString();
1192 const QString quote = format.contains("%q") ? "\"" : "";
1193 format = format.remove("%q");
1195 const bool has_sample_range = format.contains("%s");
1196 const bool has_row_name = format.contains("%r");
1197 const bool has_dec_name = format.contains("%d");
1198 const bool has_class_name = format.contains("%c");
1199 const bool has_first_ann_text = format.contains("%1");
1200 const bool has_all_ann_text = format.contains("%a");
1202 QFile file(file_name);
1203 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
1204 QTextStream out_stream(&file);
1206 for (const Annotation* ann : annotations) {
1207 QString out_text = format;
1209 if (has_sample_range) {
1210 const QString sample_range = QString("%1-%2") \
1211 .arg(QString::number(ann->start_sample()), QString::number(ann->end_sample()));
1212 out_text = out_text.replace("%s", sample_range);
1216 out_text = out_text.replace("%d",
1217 quote + QString::fromUtf8(ann->row()->decoder()->name()) + quote);
1220 const QString row_name = quote + ann->row()->description() + quote;
1221 out_text = out_text.replace("%r", row_name);
1224 if (has_class_name) {
1225 const QString class_name = quote + ann->ann_class_name() + quote;
1226 out_text = out_text.replace("%c", class_name);
1229 if (has_first_ann_text) {
1230 const QString first_ann_text = quote + ann->annotations()->front() + quote;
1231 out_text = out_text.replace("%1", first_ann_text);
1234 if (has_all_ann_text) {
1235 QString all_ann_text;
1236 for (const QString &s : *(ann->annotations()))
1237 all_ann_text = all_ann_text + quote + s + quote + ",";
1238 all_ann_text.chop(1);
1240 out_text = out_text.replace("%a", all_ann_text);
1243 out_stream << out_text << '\n';
1246 if (out_stream.status() == QTextStream::Ok)
1250 QMessageBox msg(owner_->view());
1251 msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
1252 msg.setStandardButtons(QMessageBox::Ok);
1253 msg.setIcon(QMessageBox::Warning);
1257 void DecodeTrace::initialize_row_widgets(DecodeTraceRow* r, unsigned int row_id)
1259 // Set colors and fixed widths
1260 QFontMetrics m(QApplication::font());
1262 QPalette header_palette = owner_->view()->palette();
1263 QPalette selector_palette = owner_->view()->palette();
1265 if (GlobalSettings::current_theme_is_dark()) {
1266 header_palette.setColor(QPalette::Background,
1267 QColor(255, 255, 255, ExpansionAreaHeaderAlpha));
1268 selector_palette.setColor(QPalette::Background,
1269 QColor(255, 255, 255, ExpansionAreaAlpha));
1271 header_palette.setColor(QPalette::Background,
1272 QColor(0, 0, 0, ExpansionAreaHeaderAlpha));
1273 selector_palette.setColor(QPalette::Background,
1274 QColor(0, 0, 0, ExpansionAreaAlpha));
1277 const int w = m.boundingRect(r->decode_row->title()).width() + RowTitleMargin;
1280 // Set up top-level container
1281 connect(r->container, SIGNAL(widgetResized(QWidget*)),
1282 this, SLOT(on_row_container_resized(QWidget*)));
1284 QVBoxLayout* vlayout = new QVBoxLayout();
1285 r->container->setLayout(vlayout);
1287 // Add header container
1288 vlayout->addWidget(r->header_container);
1289 vlayout->setContentsMargins(0, 0, 0, 0);
1290 vlayout->setSpacing(0);
1291 QHBoxLayout* header_container_layout = new QHBoxLayout();
1292 r->header_container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
1293 r->header_container->setMinimumSize(0, default_row_height_);
1294 r->header_container->setLayout(header_container_layout);
1295 r->header_container->layout()->setContentsMargins(10, 2, 10, 2);
1297 r->header_container->setAutoFillBackground(true);
1298 r->header_container->setPalette(header_palette);
1300 // Add widgets inside the header container
1301 QCheckBox* cb = new QCheckBox();
1302 r->row_visibility_checkbox = cb;
1303 header_container_layout->addWidget(cb);
1304 cb->setText(tr("Show this row"));
1305 cb->setChecked(r->decode_row->visible());
1307 row_show_hide_mapper_.setMapping(cb, row_id);
1308 connect(cb, SIGNAL(stateChanged(int)),
1309 &row_show_hide_mapper_, SLOT(map()));
1311 QPushButton* btn = new QPushButton();
1312 header_container_layout->addWidget(btn);
1314 btn->setStyleSheet(":hover { background-color: palette(button); color: palette(button-text); border:0; }");
1315 btn->setText(tr("Show All"));
1316 btn->setProperty("decode_trace_row_ptr", QVariant::fromValue((void*)r));
1317 connect(btn, SIGNAL(clicked(bool)), this, SLOT(on_show_all_classes()));
1319 btn = new QPushButton();
1320 header_container_layout->addWidget(btn);
1322 btn->setStyleSheet(":hover { background-color: palette(button); color: palette(button-text); border:0; }");
1323 btn->setText(tr("Hide All"));
1324 btn->setProperty("decode_trace_row_ptr", QVariant::fromValue((void*)r));
1325 connect(btn, SIGNAL(clicked(bool)), this, SLOT(on_hide_all_classes()));
1327 header_container_layout->addStretch(); // To left-align the header widgets
1329 // Add selector container
1330 vlayout->addWidget(r->selector_container);
1331 r->selector_container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
1332 r->selector_container->setLayout(new FlowLayout(r->selector_container));
1334 r->selector_container->setAutoFillBackground(true);
1335 r->selector_container->setPalette(selector_palette);
1337 // Add all classes that can be toggled
1338 vector<AnnotationClass*> ann_classes = r->decode_row->ann_classes();
1340 for (const AnnotationClass* ann_class : ann_classes) {
1341 cb = new QCheckBox();
1342 cb->setText(tr(ann_class->description));
1343 cb->setChecked(ann_class->visible());
1345 int dim = ViewItemPaintParams::text_height() - 2;
1346 QPixmap pixmap(dim, dim);
1347 pixmap.fill(r->decode_row->get_class_color(ann_class->id));
1348 cb->setIcon(pixmap);
1350 r->selector_container->layout()->addWidget(cb);
1351 r->selectors.push_back(cb);
1353 cb->setProperty("ann_class_ptr", QVariant::fromValue((void*)ann_class));
1354 cb->setProperty("decode_trace_row_ptr", QVariant::fromValue((void*)r));
1356 class_show_hide_mapper_.setMapping(cb, cb);
1357 connect(cb, SIGNAL(stateChanged(int)),
1358 &class_show_hide_mapper_, SLOT(map()));
1362 void DecodeTrace::update_rows()
1367 lock_guard<mutex> lock(row_modification_mutex_);
1369 for (DecodeTraceRow& r : rows_)
1372 unsigned int row_id = 0;
1373 for (Row* decode_row : decode_signal_->get_rows()) {
1374 // Find row in our list
1375 auto r_it = find_if(rows_.begin(), rows_.end(),
1376 [&](DecodeTraceRow& r){ return r.decode_row == decode_row; });
1378 DecodeTraceRow* r = nullptr;
1379 if (r_it == rows_.end()) {
1380 // Row doesn't exist yet, create and append it
1382 nr.decode_row = decode_row;
1383 nr.decode_row->set_base_color(base_->color());
1384 nr.height = default_row_height_;
1385 nr.expanded_height = default_row_height_;
1386 nr.currently_visible = false;
1387 nr.has_hidden_classes = decode_row->has_hidden_classes();
1388 nr.expand_marker_highlighted = false;
1389 nr.expanding = false;
1390 nr.expanded = false;
1391 nr.collapsing = false;
1392 nr.expand_marker_shape = default_marker_shape_;
1393 nr.container = new ContainerWidget(owner_->view()->scrollarea());
1394 nr.header_container = new QWidget(nr.container);
1395 nr.selector_container = new QWidget(nr.container);
1397 rows_.push_back(nr);
1399 initialize_row_widgets(r, row_id);
1407 // If there's only one row, it must not be hidden or else it can't be un-hidden
1409 rows_.front().row_visibility_checkbox->setEnabled(false);
1411 // Remove any rows that no longer exist, obeying that iterators are invalidated
1416 for (unsigned int i = 0; i < rows_.size(); i++)
1417 if (!rows_[i].exists) {
1418 delete rows_[i].row_visibility_checkbox;
1420 for (QCheckBox* cb : rows_[i].selectors)
1423 delete rows_[i].selector_container;
1424 delete rows_[i].header_container;
1425 delete rows_[i].container;
1427 rows_.erase(rows_.begin() + i);
1431 } while (any_exists);
1434 void DecodeTrace::set_row_expanded(DecodeTraceRow* r)
1436 r->height = r->expanded_height;
1437 r->expanding = false;
1440 // For details on this, see on_animation_timer()
1441 r->expand_marker_shape.setPoint(0, 0, 0);
1442 r->expand_marker_shape.setPoint(1, ArrowSize, ArrowSize);
1443 r->expand_marker_shape.setPoint(2, 2*ArrowSize, 0);
1445 r->container->resize(owner_->view()->viewport()->width() - r->container->pos().x(),
1446 r->height - 2 * default_row_height_);
1449 void DecodeTrace::set_row_collapsed(DecodeTraceRow* r)
1451 r->height = default_row_height_;
1452 r->collapsing = false;
1453 r->expanded = false;
1454 r->expand_marker_shape = default_marker_shape_;
1455 r->container->setVisible(false);
1457 r->container->resize(owner_->view()->viewport()->width() - r->container->pos().x(),
1458 r->height - 2 * default_row_height_);
1461 void DecodeTrace::update_expanded_rows()
1463 for (DecodeTraceRow& r : rows_) {
1464 if (r.expanding || r.expanded)
1465 r.expanded_height = 2 * default_row_height_ + r.container->sizeHint().height();
1468 r.height = r.expanded_height;
1470 int x = 2 * ArrowSize;
1471 int y = get_row_y(&r) + default_row_height_;
1472 // Only update the position if it actually changes
1473 if ((x != r.container->pos().x()) || (y != r.container->pos().y()))
1474 r.container->move(x, y);
1476 int w = owner_->view()->viewport()->width() - x;
1477 int h = r.height - 2 * default_row_height_;
1478 // Only update the dimension if they actually change
1479 if ((w != r.container->sizeHint().width()) || (h != r.container->sizeHint().height()))
1480 r.container->resize(w, h);
1484 void DecodeTrace::on_setting_changed(const QString &key, const QVariant &value)
1486 Trace::on_setting_changed(key, value);
1488 if (key == GlobalSettings::Key_Dec_AlwaysShowAllRows)
1489 always_show_all_rows_ = value.toBool();
1492 void DecodeTrace::on_color_changed(const QColor &color)
1494 for (DecodeTraceRow& r : rows_)
1495 r.decode_row->set_base_color(color);
1498 owner_->row_item_appearance_changed(false, true);
1501 void DecodeTrace::on_new_annotations()
1503 if (!delayed_trace_updater_.isActive())
1504 delayed_trace_updater_.start();
1507 void DecodeTrace::on_delayed_trace_update()
1510 owner_->row_item_appearance_changed(false, true);
1513 void DecodeTrace::on_decode_reset()
1518 owner_->row_item_appearance_changed(false, true);
1521 void DecodeTrace::on_decode_finished()
1524 owner_->row_item_appearance_changed(false, true);
1527 void DecodeTrace::on_pause_decode()
1529 if (decode_signal_->is_paused())
1530 decode_signal_->resume_decode();
1532 decode_signal_->pause_decode();
1535 void DecodeTrace::on_delete()
1537 session_.remove_decode_signal(decode_signal_);
1540 void DecodeTrace::on_channel_selected(int)
1542 QComboBox *cb = qobject_cast<QComboBox*>(QObject::sender());
1544 // Determine signal that was selected
1545 const data::SignalBase *signal =
1546 (data::SignalBase*)cb->itemData(cb->currentIndex()).value<void*>();
1548 // Determine decode channel ID this combo box is the channel selector for
1549 const uint16_t id = channel_id_map_.at(cb);
1551 decode_signal_->assign_signal(id, signal);
1554 void DecodeTrace::on_channels_updated()
1557 owner_->row_item_appearance_changed(false, true);
1560 void DecodeTrace::on_init_state_changed(int)
1562 QComboBox *cb = qobject_cast<QComboBox*>(QObject::sender());
1564 // Determine inital pin state that was selected
1565 int init_state = cb->itemData(cb->currentIndex()).value<int>();
1567 // Determine decode channel ID this combo box is the channel selector for
1568 const uint16_t id = init_state_map_.at(cb);
1570 decode_signal_->set_initial_pin_state(id, init_state);
1573 void DecodeTrace::on_stack_decoder(srd_decoder *decoder)
1575 decode_signal_->stack_decoder(decoder);
1578 create_popup_form();
1581 void DecodeTrace::on_delete_decoder(int index)
1583 decode_signal_->remove_decoder(index);
1586 owner_->extents_changed(false, true);
1588 create_popup_form();
1591 void DecodeTrace::on_show_hide_decoder(int index)
1593 const bool state = decode_signal_->toggle_decoder_visibility(index);
1595 assert(index < (int)decoder_forms_.size());
1596 decoder_forms_[index]->set_decoder_visible(state);
1599 owner_->extents_changed(false, true);
1601 owner_->row_item_appearance_changed(false, true);
1604 void DecodeTrace::on_show_hide_row(int row_id)
1606 if (row_id >= (int)rows_.size())
1609 rows_[row_id].decode_row->set_visible(!rows_[row_id].decode_row->visible());
1611 if (!rows_[row_id].decode_row->visible())
1612 set_row_collapsed(&rows_[row_id]);
1614 owner_->extents_changed(false, true);
1615 owner_->row_item_appearance_changed(false, true);
1618 void DecodeTrace::on_show_hide_class(QWidget* sender)
1620 void* ann_class_ptr = sender->property("ann_class_ptr").value<void*>();
1621 assert(ann_class_ptr);
1622 AnnotationClass* ann_class = (AnnotationClass*)ann_class_ptr;
1624 ann_class->set_visible(!ann_class->visible());
1626 void* row_ptr = sender->property("decode_trace_row_ptr").value<void*>();
1628 DecodeTraceRow* row = (DecodeTraceRow*)row_ptr;
1630 row->has_hidden_classes = row->decode_row->has_hidden_classes();
1632 owner_->row_item_appearance_changed(false, true);
1635 void DecodeTrace::on_show_all_classes()
1637 void* row_ptr = QObject::sender()->property("decode_trace_row_ptr").value<void*>();
1639 DecodeTraceRow* row = (DecodeTraceRow*)row_ptr;
1641 for (QCheckBox* cb : row->selectors)
1642 cb->setChecked(true);
1644 row->has_hidden_classes = false;
1646 owner_->row_item_appearance_changed(false, true);
1649 void DecodeTrace::on_hide_all_classes()
1651 void* row_ptr = QObject::sender()->property("decode_trace_row_ptr").value<void*>();
1653 DecodeTraceRow* row = (DecodeTraceRow*)row_ptr;
1655 for (QCheckBox* cb : row->selectors)
1656 cb->setChecked(false);
1658 row->has_hidden_classes = true;
1660 owner_->row_item_appearance_changed(false, true);
1663 void DecodeTrace::on_row_container_resized(QWidget* sender)
1667 owner_->extents_changed(false, true);
1668 owner_->row_item_appearance_changed(false, true);
1671 void DecodeTrace::on_copy_annotation_to_clipboard()
1676 deque<const Annotation*> annotations;
1678 decode_signal_->get_annotation_subset(annotations, selected_row_,
1679 current_segment_, selected_sample_range_.first, selected_sample_range_.first);
1681 if (annotations.empty())
1684 QClipboard *clipboard = QApplication::clipboard();
1685 clipboard->setText(annotations.front()->annotations()->front(), QClipboard::Clipboard);
1687 if (clipboard->supportsSelection())
1688 clipboard->setText(annotations.front()->annotations()->front(), QClipboard::Selection);
1691 void DecodeTrace::on_export_row()
1693 selected_sample_range_ = make_pair(0, numeric_limits<uint64_t>::max());
1694 on_export_row_from_here();
1697 void DecodeTrace::on_export_all_rows()
1699 selected_sample_range_ = make_pair(0, numeric_limits<uint64_t>::max());
1700 on_export_all_rows_from_here();
1703 void DecodeTrace::on_export_row_with_cursor()
1705 const View *view = owner_->view();
1708 if (!view->cursors()->enabled())
1711 const double samplerate = session_.get_samplerate();
1713 const pv::util::Timestamp& start_time = view->cursors()->first()->time();
1714 const pv::util::Timestamp& end_time = view->cursors()->second()->time();
1716 const uint64_t start_sample = (uint64_t)max(
1717 0.0, start_time.convert_to<double>() * samplerate);
1718 const uint64_t end_sample = (uint64_t)max(
1719 0.0, end_time.convert_to<double>() * samplerate);
1721 // Are both cursors negative and thus were clamped to 0?
1722 if ((start_sample == 0) && (end_sample == 0))
1725 selected_sample_range_ = make_pair(start_sample, end_sample);
1726 on_export_row_from_here();
1729 void DecodeTrace::on_export_all_rows_with_cursor()
1731 const View *view = owner_->view();
1734 if (!view->cursors()->enabled())
1737 const double samplerate = session_.get_samplerate();
1739 const pv::util::Timestamp& start_time = view->cursors()->first()->time();
1740 const pv::util::Timestamp& end_time = view->cursors()->second()->time();
1742 const uint64_t start_sample = (uint64_t)max(
1743 0.0, start_time.convert_to<double>() * samplerate);
1744 const uint64_t end_sample = (uint64_t)max(
1745 0.0, end_time.convert_to<double>() * samplerate);
1747 // Are both cursors negative and thus were clamped to 0?
1748 if ((start_sample == 0) && (end_sample == 0))
1751 selected_sample_range_ = make_pair(start_sample, end_sample);
1752 on_export_all_rows_from_here();
1755 void DecodeTrace::on_export_row_from_here()
1760 deque<const Annotation*> annotations;
1762 decode_signal_->get_annotation_subset(annotations, selected_row_,
1763 current_segment_, selected_sample_range_.first, selected_sample_range_.second);
1765 if (annotations.empty())
1768 export_annotations(annotations);
1771 void DecodeTrace::on_export_all_rows_from_here()
1773 deque<const Annotation*> annotations;
1775 decode_signal_->get_annotation_subset(annotations, current_segment_,
1776 selected_sample_range_.first, selected_sample_range_.second);
1778 if (!annotations.empty())
1779 export_annotations(annotations);
1782 void DecodeTrace::on_animation_timer()
1784 bool animation_finished = true;
1786 for (DecodeTraceRow& r : rows_) {
1787 if (!(r.expanding || r.collapsing))
1790 unsigned int height_delta = r.expanded_height - default_row_height_;
1793 if (r.height < r.expanded_height) {
1794 r.anim_height += height_delta / (float)AnimationDurationInTicks;
1795 r.height = min((int)r.anim_height, (int)r.expanded_height);
1796 r.anim_shape += ArrowSize / (float)AnimationDurationInTicks;
1797 animation_finished = false;
1799 set_row_expanded(&r);
1803 if (r.height > default_row_height_) {
1804 r.anim_height -= height_delta / (float)AnimationDurationInTicks;
1805 r.height = max((int)r.anim_height, (int)0);
1806 r.anim_shape -= ArrowSize / (float)AnimationDurationInTicks;
1807 animation_finished = false;
1809 set_row_collapsed(&r);
1812 // The expansion marker shape switches between
1813 // 0/-A, A/0, 0/A (default state; anim_shape=0) and
1814 // 0/ 0, A/A, 2A/0 (expanded state; anim_shape=ArrowSize)
1816 r.expand_marker_shape.setPoint(0, 0, -ArrowSize + r.anim_shape);
1817 r.expand_marker_shape.setPoint(1, ArrowSize, r.anim_shape);
1818 r.expand_marker_shape.setPoint(2, 2*r.anim_shape, ArrowSize - r.anim_shape);
1821 if (animation_finished)
1822 animation_timer_.stop();
1824 owner_->extents_changed(false, true);
1825 owner_->row_item_appearance_changed(false, true);
1828 void DecodeTrace::on_hide_hidden_rows()
1830 // Make all hidden traces invisible again unless the user is hovering over a row name
1831 bool any_highlighted = false;
1833 for (DecodeTraceRow& r : rows_)
1834 if (r.expand_marker_highlighted)
1835 any_highlighted = true;
1837 if (!any_highlighted) {
1838 show_hidden_rows_ = false;
1840 owner_->extents_changed(false, true);
1841 owner_->row_item_appearance_changed(false, true);
1845 } // namespace trace
1846 } // namespace views