]> sigrok.org Git - pulseview.git/blame - pv/views/trace/decodetrace.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / views / trace / decodetrace.cpp
CommitLineData
55d3603d
JH
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
55d3603d
JH
18 */
19
55d3603d 20#include <libsigrokdecode/libsigrokdecode.h>
55d3603d 21
99ba5f28 22#include <limits>
c3a740dd 23#include <mutex>
5c48ce32 24#include <tuple>
c3a740dd 25
06bb4e6a
JH
26#include <extdef.h>
27
a855d71e 28#include <boost/functional/hash.hpp>
b213ef09 29
c51482b3 30#include <QAction>
d7c0ca4a 31#include <QApplication>
c764c995 32#include <QClipboard>
c6b4e925 33#include <QCheckBox>
4e5a4405 34#include <QComboBox>
5a9146ac 35#include <QDebug>
be843692 36#include <QFileDialog>
4e5a4405
JH
37#include <QFormLayout>
38#include <QLabel>
b213ef09 39#include <QMenu>
be843692 40#include <QMessageBox>
ce94e4fd 41#include <QPushButton>
be843692 42#include <QTextStream>
e2f90c50 43#include <QToolTip>
c51482b3 44
2acdb232 45#include "decodetrace.hpp"
1573bf16
SA
46#include "view.hpp"
47#include "viewport.hpp"
2acdb232 48
1cc1c8de 49#include <pv/globalsettings.hpp>
ad908057
SA
50#include <pv/session.hpp>
51#include <pv/strnatcmp.hpp>
52#include <pv/data/decodesignal.hpp>
aca9aa83 53#include <pv/data/decode/annotation.hpp>
2acdb232
JH
54#include <pv/data/decode/decoder.hpp>
55#include <pv/data/logic.hpp>
f3d66e52 56#include <pv/data/logicsegment.hpp>
2acdb232
JH
57#include <pv/widgets/decodergroupbox.hpp>
58#include <pv/widgets/decodermenu.hpp>
adf9e022 59#include <pv/widgets/flowlayout.hpp>
119aff65 60
5c48ce32 61using std::abs;
84002113
SA
62using std::find_if;
63using std::lock_guard;
7f8517f6 64using std::make_pair;
819f4c25 65using std::max;
819f4c25 66using std::min;
99ba5f28 67using std::numeric_limits;
7f8517f6 68using std::pair;
f9abf97e 69using std::shared_ptr;
53e35b2d 70using std::tie;
819f4c25 71using std::vector;
55d3603d 72
ecd07c20 73using pv::data::decode::Annotation;
6a26fc44 74using pv::data::decode::AnnotationClass;
ecd07c20 75using pv::data::decode::Row;
a82325d1 76using pv::data::decode::DecodeChannel;
9f97b357 77using pv::data::DecodeSignal;
ecd07c20 78
55d3603d 79namespace pv {
f4e57597 80namespace views {
1573bf16 81namespace trace {
55d3603d 82
641574bc 83const QColor DecodeTrace::NoDecodeColor = QColor(0x88, 0x8A, 0x85);
81dc0221 84const QColor DecodeTrace::ExpandMarkerWarnColor = QColor(0xFF, 0xA5, 0x00); // QColorConstants::Svg::orange
440bdf50 85const QColor DecodeTrace::ExpandMarkerHiddenColor = QColor(0x69, 0x69, 0x69); // QColorConstants::Svg::dimgray
c6b4e925
SA
86const uint8_t DecodeTrace::ExpansionAreaHeaderAlpha = 10 * 255 / 100;
87const uint8_t DecodeTrace::ExpansionAreaAlpha = 5 * 255 / 100;
ad50ac1a 88
74bf6666 89const int DecodeTrace::ArrowSize = 6;
06e810f2 90const double DecodeTrace::EndCapWidth = 5;
74bf6666 91const int DecodeTrace::RowTitleMargin = 7;
06e810f2
JH
92const int DecodeTrace::DrawPadding = 100;
93
5b6ae103 94const int DecodeTrace::MaxTraceUpdateRate = 1; // No more than 1 Hz
440bdf50
SA
95const int DecodeTrace::AnimationDurationInTicks = 7;
96const int DecodeTrace::HiddenRowHideDelay = 1000; // 1 second
adf9e022
SA
97
98/**
99 * Helper function for forceUpdate()
100 */
101void invalidateLayout(QLayout* layout)
102{
103 // Recompute the given layout and all its child layouts recursively
104 for (int i = 0; i < layout->count(); i++) {
105 QLayoutItem *item = layout->itemAt(i);
106
107 if (item->layout())
108 invalidateLayout(item->layout());
109 else
110 item->invalidate();
111 }
112
113 layout->invalidate();
114 layout->activate();
115}
116
117void forceUpdate(QWidget* widget)
118{
119 // Update all child widgets recursively
120 for (QObject* child : widget->children())
121 if (child->isWidgetType())
122 forceUpdate((QWidget*)child);
123
124 // Invalidate the layout of the widget itself
125 if (widget->layout())
126 invalidateLayout(widget->layout());
127}
128
129
130ContainerWidget::ContainerWidget(QWidget *parent) :
131 QWidget(parent)
132{
133}
134
135void ContainerWidget::resizeEvent(QResizeEvent* event)
136{
137 QWidget::resizeEvent(event);
138
139 widgetResized(this);
140}
141
142
2b81ae46 143DecodeTrace::DecodeTrace(pv::Session &session,
bb7dd726 144 shared_ptr<data::SignalBase> signalbase, int index) :
bf0edd2b 145 Trace(signalbase),
8dbbc7f0 146 session_(session),
440bdf50 147 show_hidden_rows_(false),
8dbbc7f0 148 delete_mapper_(this),
c6b4e925
SA
149 show_hide_mapper_(this),
150 row_show_hide_mapper_(this)
55d3603d 151{
ad908057 152 decode_signal_ = dynamic_pointer_cast<data::DecodeSignal>(base_);
e0fc5810 153
ab185f78
SA
154 GlobalSettings settings;
155 always_show_all_rows_ = settings.value(GlobalSettings::Key_Dec_AlwaysShowAllRows).toBool();
156
157 GlobalSettings::add_change_handler(this);
158
752281db
SA
159 // Determine shortest string we want to see displayed in full
160 QFontMetrics m(QApplication::font());
ae726b70
VO
161 // e.g. two hex characters
162 min_useful_label_width_ = util::text_width(m, "XX");
752281db 163
84002113 164 default_row_height_ = (ViewItemPaintParams::text_height() * 6) / 4;
9741d6e0 165 annotation_height_ = (ViewItemPaintParams::text_height() * 5) / 4;
84002113 166
9ba13f5e
SA
167 // For the base color, we want to start at a very different color for
168 // every decoder stack, so multiply the index with a number that is
169 // rather close to 180 degrees of the color circle but not a dividend of 360
170 // Note: The offset equals the color of the first annotation
171 QColor color;
172 const int h = (120 + 160 * index) % 360;
ae30ff42
SA
173 const int s = DECODE_COLOR_SATURATION;
174 const int v = DECODE_COLOR_VALUE;
9ba13f5e
SA
175 color.setHsv(h, s, v);
176 base_->set_color(color);
9cef9567 177
ae30ff42
SA
178 connect(decode_signal_.get(), SIGNAL(color_changed(QColor)),
179 this, SLOT(on_color_changed(QColor)));
180
ad908057
SA
181 connect(decode_signal_.get(), SIGNAL(new_annotations()),
182 this, SLOT(on_new_annotations()));
eee3eab9
SA
183 connect(decode_signal_.get(), SIGNAL(decode_reset()),
184 this, SLOT(on_decode_reset()));
1b56c646
SA
185 connect(decode_signal_.get(), SIGNAL(decode_finished()),
186 this, SLOT(on_decode_finished()));
9f97b357
SA
187 connect(decode_signal_.get(), SIGNAL(channels_updated()),
188 this, SLOT(on_channels_updated()));
1ed73ebd
VPP
189#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
190 connect(&delete_mapper_, SIGNAL(mappedInt(int)),
191 this, SLOT(on_delete_decoder(int)));
192 connect(&show_hide_mapper_, SIGNAL(mappedInt(int)),
193 this, SLOT(on_show_hide_decoder(int)));
194 connect(&row_show_hide_mapper_, SIGNAL(mappedInt(int)),
195 this, SLOT(on_show_hide_row(int)));
196 connect(&class_show_hide_mapper_, SIGNAL(mappedObject(QObject*)),
197 this, SLOT(on_show_hide_class(QObject*)));
198#else
8dbbc7f0 199 connect(&delete_mapper_, SIGNAL(mapped(int)),
613d097c 200 this, SLOT(on_delete_decoder(int)));
8dbbc7f0 201 connect(&show_hide_mapper_, SIGNAL(mapped(int)),
dd048a7e 202 this, SLOT(on_show_hide_decoder(int)));
c6b4e925
SA
203 connect(&row_show_hide_mapper_, SIGNAL(mapped(int)),
204 this, SLOT(on_show_hide_row(int)));
6a26fc44
SA
205 connect(&class_show_hide_mapper_, SIGNAL(mapped(QWidget*)),
206 this, SLOT(on_show_hide_class(QWidget*)));
1ed73ebd 207#endif
5b6ae103
SA
208
209 connect(&delayed_trace_updater_, SIGNAL(timeout()),
210 this, SLOT(on_delayed_trace_update()));
211 delayed_trace_updater_.setSingleShot(true);
212 delayed_trace_updater_.setInterval(1000 / MaxTraceUpdateRate);
74bf6666
SA
213
214 connect(&animation_timer_, SIGNAL(timeout()),
215 this, SLOT(on_animation_timer()));
216 animation_timer_.setInterval(1000 / 50);
217
440bdf50
SA
218 connect(&delayed_hidden_row_hider_, SIGNAL(timeout()),
219 this, SLOT(on_hide_hidden_rows()));
220 delayed_hidden_row_hider_.setSingleShot(true);
221 delayed_hidden_row_hider_.setInterval(HiddenRowHideDelay);
222
74bf6666
SA
223 default_marker_shape_ << QPoint(0, -ArrowSize);
224 default_marker_shape_ << QPoint(ArrowSize, 0);
225 default_marker_shape_ << QPoint(0, ArrowSize);
55d3603d
JH
226}
227
ab185f78
SA
228DecodeTrace::~DecodeTrace()
229{
230 GlobalSettings::remove_change_handler(this);
dca3cbee 231
6a26fc44 232 for (DecodeTraceRow& r : rows_) {
c6b4e925
SA
233 for (QCheckBox* cb : r.selectors)
234 delete cb;
235
236 delete r.selector_container;
237 delete r.header_container;
dca3cbee 238 delete r.container;
c6b4e925 239 }
ab185f78
SA
240}
241
b9329558 242bool DecodeTrace::enabled() const
55d3603d
JH
243{
244 return true;
245}
246
6f925ba9 247shared_ptr<data::SignalBase> DecodeTrace::base() const
b6b267bb 248{
bb7dd726 249 return base_;
b6b267bb
JH
250}
251
a653b83d
SA
252void DecodeTrace::set_owner(TraceTreeItemOwner *owner)
253{
254 Trace::set_owner(owner);
255
256 // The owner is set in trace::View::signals_changed(), which is a slot.
257 // So after this trace was added to the view, we won't have an owner
258 // that we need to initialize in update_rows(). Once we do, we call it
259 // from on_decode_reset().
260 on_decode_reset();
261}
262
a5d93c27
JH
263pair<int, int> DecodeTrace::v_extents() const
264{
e40a79cb 265 // Make an empty decode trace appear symmetrical
38b77a3b 266 if (visible_rows_ == 0)
84002113
SA
267 return make_pair(-default_row_height_, default_row_height_);
268
269 unsigned int height = 0;
6a26fc44 270 for (const DecodeTraceRow& r : rows_)
84002113
SA
271 if (r.currently_visible)
272 height += r.height;
e40a79cb 273
84002113 274 return make_pair(-default_row_height_, height);
a5d93c27
JH
275}
276
60938e04 277void DecodeTrace::paint_back(QPainter &p, ViewItemPaintParams &pp)
fe08b6e8 278{
3eb29afd 279 Trace::paint_back(p, pp);
97904bf7 280 paint_axis(p, pp, get_visual_y());
fe08b6e8
JH
281}
282
60938e04 283void DecodeTrace::paint_mid(QPainter &p, ViewItemPaintParams &pp)
55d3603d 284{
84002113 285 lock_guard<mutex> lock(row_modification_mutex_);
a767229e 286 unsigned int visible_rows;
84002113 287
5a9146ac
SA
288#if DECODETRACE_SHOW_RENDER_TIME
289 render_time_.restart();
290#endif
291
cd0c558b
SA
292 // Set default pen to allow for text width calculation
293 p.setPen(Qt::black);
294
67fb15bf 295 pair<uint64_t, uint64_t> sample_range = get_view_sample_range(pp.left(), pp.right());
b82908ab
SA
296
297 // Just because the view says we see a certain sample range it
298 // doesn't mean we have this many decoded samples, too, so crop
299 // the range to what has been decoded already
300 sample_range.second = min((int64_t)sample_range.second,
301 decode_signal_->get_decoded_sample_count(current_segment_, false));
5dfeb70f 302
a767229e 303 visible_rows = 0;
84002113 304 int y = get_visual_y();
aee9dcf3 305
6a26fc44 306 for (DecodeTraceRow& r : rows_) {
c6b4e925 307 // If the row is hidden, we don't want to fetch annotations
6a26fc44
SA
308 assert(r.decode_row);
309 assert(r.decode_row->decoder());
440bdf50
SA
310 if ((!r.decode_row->decoder()->visible()) ||
311 ((!r.decode_row->visible() && (!show_hidden_rows_) && (!r.expanding) && (!r.expanded) && (!r.collapsing)))) {
9741d6e0
SA
312 r.currently_visible = false;
313 continue;
314 }
315
462941e2 316 deque<const Annotation*> annotations;
84002113 317 decode_signal_->get_annotation_subset(annotations, r.decode_row,
72435789 318 current_segment_, sample_range.first, sample_range.second);
f994f496 319
41293691 320 // Show row if there are visible annotations, when user wants to see
f994f496 321 // all rows that have annotations somewhere and this one is one of them
41293691 322 // or when the row has at least one hidden annotation class
6a26fc44
SA
323 r.currently_visible = !annotations.empty();
324 if (!r.currently_visible) {
325 size_t ann_count = decode_signal_->get_annotation_count(r.decode_row, current_segment_);
e211a945
SA
326 r.currently_visible = ((always_show_all_rows_ || r.has_hidden_classes) &&
327 (ann_count > 0)) || r.expanded;
6a26fc44 328 }
f994f496 329
84002113 330 if (r.currently_visible) {
bdb97140 331 draw_annotations(annotations, p, pp, y, r);
84002113 332 y += r.height;
a767229e 333 visible_rows++;
f9101a91 334 }
7e674e43 335 }
5dfeb70f 336
bdb97140 337 draw_unresolved_period(p, pp.left(), pp.right());
eee89ff8 338
a767229e
SA
339 if (visible_rows != visible_rows_) {
340 visible_rows_ = visible_rows;
a303c2d8 341
6d2802aa
SA
342 // Call order is important, otherwise the lazy event handler won't work
343 owner_->extents_changed(false, true);
344 owner_->row_item_appearance_changed(false, true);
345 }
1ae18301 346
f6a93932 347 const QString err = base_->get_error_message();
1ae18301 348 if (!err.isEmpty())
f6a93932 349 paint_error(p, pp);
5a9146ac
SA
350
351#if DECODETRACE_SHOW_RENDER_TIME
352 qDebug() << "Rendering" << base_->name() << "took" << render_time_.elapsed() << "ms";
353#endif
55d3603d
JH
354}
355
60938e04 356void DecodeTrace::paint_fore(QPainter &p, ViewItemPaintParams &pp)
88908838 357{
84002113 358 unsigned int y = get_visual_y();
88908838 359
d4c3b059
SA
360 update_expanded_rows();
361
6a26fc44 362 for (const DecodeTraceRow& r : rows_) {
84002113
SA
363 if (!r.currently_visible)
364 continue;
88908838
JH
365
366 p.setPen(QPen(Qt::NoPen));
88908838 367
84002113 368 if (r.expand_marker_highlighted)
74bf6666 369 p.setBrush(QApplication::palette().brush(QPalette::Highlight));
440bdf50
SA
370 else if (!r.decode_row->visible())
371 p.setBrush(ExpandMarkerHiddenColor);
81dc0221
SA
372 else if (r.has_hidden_classes)
373 p.setBrush(ExpandMarkerWarnColor);
84002113
SA
374 else
375 p.setBrush(QApplication::palette().brush(QPalette::WindowText));
376
377 // Draw expansion marker
74bf6666
SA
378 QPolygon marker(r.expand_marker_shape);
379 marker.translate(pp.left(), y);
380 p.drawPolygon(marker);
88908838 381
84002113
SA
382 p.setBrush(QApplication::palette().brush(QPalette::WindowText));
383
384 const QRect text_rect(pp.left() + ArrowSize * 2, y - r.height / 2,
385 pp.right() - pp.left(), r.height);
6a26fc44 386 const QString h(r.decode_row->title());
88908838
JH
387 const int f = Qt::AlignLeft | Qt::AlignVCenter |
388 Qt::TextDontClip;
389
390 // Draw the outline
391 p.setPen(QApplication::palette().color(QPalette::Base));
392 for (int dx = -1; dx <= 1; dx++)
393 for (int dy = -1; dy <= 1; dy++)
394 if (dx != 0 && dy != 0)
84002113 395 p.drawText(text_rect.translated(dx, dy), f, h);
88908838
JH
396
397 // Draw the text
440bdf50
SA
398 if (!r.decode_row->visible())
399 p.setPen(ExpandMarkerHiddenColor);
400 else
401 p.setPen(QApplication::palette().color(QPalette::WindowText));
402
84002113
SA
403 p.drawText(text_rect, f, h);
404
405 y += r.height;
88908838 406 }
1931b5f9
SA
407
408 if (show_hover_marker_)
409 paint_hover_marker(p);
88908838
JH
410}
411
cf0a3c9c
SA
412void DecodeTrace::update_stack_button()
413{
5a9146ac 414 const vector< shared_ptr<Decoder> > &stack = decode_signal_->decoder_stack();
cf0a3c9c
SA
415
416 // Only show decoders in the menu that can be stacked onto the last one in the stack
417 if (!stack.empty()) {
6a26fc44 418 const srd_decoder* d = stack.back()->get_srd_decoder();
cf0a3c9c
SA
419
420 if (d->outputs) {
421 pv::widgets::DecoderMenu *const decoder_menu =
422 new pv::widgets::DecoderMenu(stack_button_, (const char*)(d->outputs->data));
423 connect(decoder_menu, SIGNAL(decoder_selected(srd_decoder*)),
424 this, SLOT(on_stack_decoder(srd_decoder*)));
425
15784350
SA
426 decoder_menu->setStyleSheet("QMenu { menu-scrollable: 1; }");
427
cf0a3c9c
SA
428 stack_button_->setMenu(decoder_menu);
429 stack_button_->show();
430 return;
431 }
432 }
433
434 // No decoders available for stacking
435 stack_button_->setMenu(nullptr);
436 stack_button_->hide();
437}
438
b9329558 439void DecodeTrace::populate_popup_form(QWidget *parent, QFormLayout *form)
4e5a4405 440{
4e5a4405 441 assert(form);
4e5a4405 442
7491a29f 443 // Add the standard options
4e5a4405
JH
444 Trace::populate_popup_form(parent, form);
445
7491a29f 446 // Add the decoder options
8dbbc7f0 447 bindings_.clear();
9f97b357
SA
448 channel_id_map_.clear();
449 init_state_map_.clear();
8dbbc7f0 450 decoder_forms_.clear();
4e5a4405 451
47747218 452 const vector< shared_ptr<Decoder> > &stack = decode_signal_->decoder_stack();
4e5a4405 453
2ad82c2e 454 if (stack.empty()) {
5069084a
JH
455 QLabel *const l = new QLabel(
456 tr("<p><i>No decoders in the stack</i></p>"));
457 l->setAlignment(Qt::AlignCenter);
458 form->addRow(l);
2ad82c2e 459 } else {
f46e495e 460 auto iter = stack.cbegin();
5069084a
JH
461 for (int i = 0; i < (int)stack.size(); i++, iter++) {
462 shared_ptr<Decoder> dec(*iter);
463 create_decoder_form(i, dec, parent, form);
464 }
465
466 form->addRow(new QLabel(
8bd26d8b 467 tr("<i>* Required channels</i>"), parent));
5069084a 468 }
4e5a4405 469
ce94e4fd 470 // Add stacking button
cf0a3c9c
SA
471 stack_button_ = new QPushButton(tr("Stack Decoder"), parent);
472 stack_button_->setToolTip(tr("Stack a higher-level decoder on top of this one"));
473 update_stack_button();
ce94e4fd
JH
474
475 QHBoxLayout *stack_button_box = new QHBoxLayout;
cf0a3c9c 476 stack_button_box->addWidget(stack_button_, 0, Qt::AlignRight);
ce94e4fd 477 form->addRow(stack_button_box);
4e5a4405
JH
478}
479
9e773fec 480QMenu* DecodeTrace::create_header_context_menu(QWidget *parent)
c51482b3 481{
9e773fec 482 QMenu *const menu = Trace::create_header_context_menu(parent);
c51482b3
JH
483
484 menu->addSeparator();
485
486 QAction *const del = new QAction(tr("Delete"), this);
a2d21018 487 del->setShortcuts(QKeySequence::Delete);
c51482b3
JH
488 connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
489 menu->addAction(del);
490
491 return menu;
492}
493
be843692
SA
494QMenu* DecodeTrace::create_view_context_menu(QWidget *parent, QPoint &click_pos)
495{
79b53a1a
SA
496 // Get entries from default menu before adding our own
497 QMenu *const menu = new QMenu(parent);
498
499 QMenu* default_menu = Trace::create_view_context_menu(parent, click_pos);
500 if (default_menu) {
7a0d99e6 501 for (QAction *action : default_menu->actions()) { // clazy:exclude=range-loop
79b53a1a
SA
502 menu->addAction(action);
503 if (action->parent() == default_menu)
504 action->setParent(menu);
505 }
506 delete default_menu;
507
508 // Add separator if needed
509 if (menu->actions().length() > 0)
510 menu->addSeparator();
511 }
512
84002113 513 selected_row_ = nullptr;
6a26fc44 514 const DecodeTraceRow* r = get_row_at_point(click_pos);
84002113 515 if (r)
6a26fc44 516 selected_row_ = r->decode_row;
be843692 517
c764c995
SA
518 const View *const view = owner_->view();
519 assert(view);
520 QPoint pos = view->viewport()->mapFrom(parent, click_pos);
521
99ba5f28 522 // Default sample range is "from here"
c764c995 523 const pair<uint64_t, uint64_t> sample_range = get_view_sample_range(pos.x(), pos.x() + 1);
99ba5f28 524 selected_sample_range_ = make_pair(sample_range.first, numeric_limits<uint64_t>::max());
be843692 525
556259d2
SA
526 if (decode_signal_->is_paused()) {
527 QAction *const resume =
528 new QAction(tr("Resume decoding"), this);
529 resume->setIcon(QIcon::fromTheme("media-playback-start",
530 QIcon(":/icons/media-playback-start.png")));
531 connect(resume, SIGNAL(triggered()), this, SLOT(on_pause_decode()));
532 menu->addAction(resume);
533 } else {
534 QAction *const pause =
535 new QAction(tr("Pause decoding"), this);
536 pause->setIcon(QIcon::fromTheme("media-playback-pause",
537 QIcon(":/icons/media-playback-pause.png")));
538 connect(pause, SIGNAL(triggered()), this, SLOT(on_pause_decode()));
539 menu->addAction(pause);
540 }
541
c764c995
SA
542 QAction *const copy_annotation_to_clipboard =
543 new QAction(tr("Copy annotation text to clipboard"), this);
544 copy_annotation_to_clipboard->setIcon(QIcon::fromTheme("edit-paste",
d01fcb19 545 QIcon(":/icons/edit-paste.svg")));
c764c995
SA
546 connect(copy_annotation_to_clipboard, SIGNAL(triggered()), this, SLOT(on_copy_annotation_to_clipboard()));
547 menu->addAction(copy_annotation_to_clipboard);
548
556259d2
SA
549 menu->addSeparator();
550
5a914348 551 QAction *const export_all_rows =
556259d2 552 new QAction(tr("Export all annotations"), this);
5a914348
SA
553 export_all_rows->setIcon(QIcon::fromTheme("document-save-as",
554 QIcon(":/icons/document-save-as.png")));
555 connect(export_all_rows, SIGNAL(triggered()), this, SLOT(on_export_all_rows()));
556 menu->addAction(export_all_rows);
557
be843692 558 QAction *const export_row =
556259d2 559 new QAction(tr("Export all annotations for this row"), this);
be843692
SA
560 export_row->setIcon(QIcon::fromTheme("document-save-as",
561 QIcon(":/icons/document-save-as.png")));
5a914348 562 connect(export_row, SIGNAL(triggered()), this, SLOT(on_export_row()));
be843692
SA
563 menu->addAction(export_row);
564
5a914348
SA
565 menu->addSeparator();
566
567 QAction *const export_all_rows_from_here =
568 new QAction(tr("Export all annotations, starting here"), this);
569 export_all_rows_from_here->setIcon(QIcon::fromTheme("document-save-as",
570 QIcon(":/icons/document-save-as.png")));
571 connect(export_all_rows_from_here, SIGNAL(triggered()), this, SLOT(on_export_all_rows_from_here()));
572 menu->addAction(export_all_rows_from_here);
573
be843692
SA
574 QAction *const export_row_from_here =
575 new QAction(tr("Export annotations for this row, starting here"), this);
576 export_row_from_here->setIcon(QIcon::fromTheme("document-save-as",
577 QIcon(":/icons/document-save-as.png")));
5a914348 578 connect(export_row_from_here, SIGNAL(triggered()), this, SLOT(on_export_row_from_here()));
be843692
SA
579 menu->addAction(export_row_from_here);
580
99ba5f28
SA
581 menu->addSeparator();
582
583 QAction *const export_all_rows_with_cursor =
584 new QAction(tr("Export all annotations within cursor range"), this);
585 export_all_rows_with_cursor->setIcon(QIcon::fromTheme("document-save-as",
586 QIcon(":/icons/document-save-as.png")));
587 connect(export_all_rows_with_cursor, SIGNAL(triggered()), this, SLOT(on_export_all_rows_with_cursor()));
588 menu->addAction(export_all_rows_with_cursor);
589
590 QAction *const export_row_with_cursor =
591 new QAction(tr("Export annotations for this row within cursor range"), this);
592 export_row_with_cursor->setIcon(QIcon::fromTheme("document-save-as",
593 QIcon(":/icons/document-save-as.png")));
594 connect(export_row_with_cursor, SIGNAL(triggered()), this, SLOT(on_export_row_with_cursor()));
595 menu->addAction(export_row_with_cursor);
596
99ba5f28
SA
597 if (!view->cursors()->enabled()) {
598 export_all_rows_with_cursor->setEnabled(false);
599 export_row_with_cursor->setEnabled(false);
600 }
601
be843692
SA
602 return menu;
603}
604
74bf6666
SA
605void DecodeTrace::delete_pressed()
606{
607 on_delete();
608}
609
610void DecodeTrace::hover_point_changed(const QPoint &hp)
611{
612 Trace::hover_point_changed(hp);
613
614 assert(owner_);
615
6a26fc44 616 DecodeTraceRow* hover_row = get_row_at_point(hp);
74bf6666
SA
617
618 // Row expansion marker handling
6a26fc44 619 for (DecodeTraceRow& r : rows_)
74bf6666
SA
620 r.expand_marker_highlighted = false;
621
622 if (hover_row) {
f14dee77
SA
623 const pair<int, int> extents = v_extents();
624 const int trace_top = get_visual_y() + extents.first;
625 const int trace_btm = get_visual_y() + extents.second;
626
41293691 627 if ((hp.x() > 0) && (hp.x() < (int)(ArrowSize + 3 + hover_row->title_width)) &&
f14dee77 628 (hp.y() > trace_top) && (hp.y() < trace_btm)) {
440bdf50 629
74bf6666 630 hover_row->expand_marker_highlighted = true;
440bdf50
SA
631 show_hidden_rows_ = true;
632 delayed_hidden_row_hider_.start();
633 }
74bf6666
SA
634 }
635
636 // Tooltip handling
637 if (hp.x() > 0) {
638 QString ann = get_annotation_at_point(hp);
639
640 if (!ann.isEmpty()) {
641 QFontMetrics m(QToolTip::font());
642 const QRect text_size = m.boundingRect(QRect(), 0, ann);
643
644 // This is OS-specific and unfortunately we can't query it, so
645 // use an approximation to at least try to minimize the error.
646 const int padding = default_row_height_ + 8;
647
648 // Make sure the tool tip doesn't overlap with the mouse cursor.
649 // If it did, the tool tip would constantly hide and re-appear.
650 // We also push it up by one row so that it appears above the
651 // decode trace, not below.
652 QPoint p = hp;
653 p.setX(hp.x() - (text_size.width() / 2) - padding);
654
655 p.setY(get_row_y(hover_row) - default_row_height_ -
656 text_size.height() - padding);
657
658 const View *const view = owner_->view();
659 assert(view);
660 QToolTip::showText(view->viewport()->mapToGlobal(p), ann);
661
662 } else
663 QToolTip::hideText();
664
665 } else
666 QToolTip::hideText();
667}
668
669void DecodeTrace::mouse_left_press_event(const QMouseEvent* event)
670{
adf9e022
SA
671 // Update container widths which depend on the scrollarea's current width
672 update_expanded_rows();
673
74bf6666 674 // Handle row expansion marker
6a26fc44 675 for (DecodeTraceRow& r : rows_) {
74bf6666
SA
676 if (!r.expand_marker_highlighted)
677 continue;
678
679 unsigned int y = get_row_y(&r);
1ed73ebd
VPP
680 bool need_anim = true;
681#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
682 need_anim &= event->position().x() > 0;
683 need_anim &= event->position().x() <= (int)(ArrowSize + 3 + r.title_width);
684 need_anim &= event->position().y() > (int)(y - (default_row_height_ / 2));
685 need_anim &= event->position().y() <= (int)(y + (default_row_height_ / 2));
686#else
687 need_anim &= event->x() > 0;
688 need_anim &= event->x() <= (int)(ArrowSize + 3 + r.title_width);
689 need_anim &= event->y() > (int)(y - (default_row_height_ / 2));
690 need_anim &= event->y() <= (int)(y + (default_row_height_ / 2));
691#endif
692 if (need_anim) {
74bf6666
SA
693 if (r.expanded) {
694 r.collapsing = true;
695 r.expanded = false;
696 r.anim_shape = ArrowSize;
697 } else {
698 r.expanding = true;
699 r.anim_shape = 0;
adf9e022
SA
700
701 // Force geometry update of the widget container to get
702 // an up-to-date height (which also depends on the width)
703 forceUpdate(r.container);
704
dca3cbee 705 r.container->setVisible(true);
adf9e022 706 r.expanded_height = 2 * default_row_height_ + r.container->sizeHint().height();
74bf6666
SA
707 }
708
709 r.animation_step = 0;
710 r.anim_height = r.height;
dca3cbee 711
74bf6666
SA
712 animation_timer_.start();
713 }
714 }
715}
716
462941e2 717void DecodeTrace::draw_annotations(deque<const Annotation*>& annotations,
bdb97140 718 QPainter &p, const ViewItemPaintParams &pp, int y, const DecodeTraceRow& row)
50631798 719{
1e948182 720 uint32_t block_class = 0;
20d90d22 721 bool block_class_uniform = true;
5c48ce32 722 qreal block_start = 0;
20d90d22
SA
723 int block_ann_count = 0;
724
5a9146ac 725 const Annotation* prev_ann;
5c48ce32 726 qreal prev_end = INT_MIN;
20d90d22 727
5c48ce32 728 qreal a_end;
50631798
SA
729
730 double samples_per_pixel, pixels_offset;
731 tie(pixels_offset, samples_per_pixel) =
732 get_pixels_offset_samples_per_pixel();
733
734 // Gather all annotations that form a visual "block" and draw them as such
5a9146ac 735 for (const Annotation* a : annotations) {
50631798 736
5a9146ac
SA
737 const qreal abs_a_start = a->start_sample() / samples_per_pixel;
738 const qreal abs_a_end = a->end_sample() / samples_per_pixel;
bdc2a99b 739
5c48ce32 740 const qreal a_start = abs_a_start - pixels_offset;
20d90d22
SA
741 a_end = abs_a_end - pixels_offset;
742
5c48ce32
SA
743 const qreal a_width = a_end - a_start;
744 const qreal delta = a_end - prev_end;
bdc2a99b
SA
745
746 bool a_is_separate = false;
747
748 // Annotation wider than the threshold for a useful label width?
752281db 749 if (a_width >= min_useful_label_width_) {
462941e2 750 for (const QString &ann_text : *(a->annotations())) {
5c48ce32 751 const qreal w = p.boundingRect(QRectF(), 0, ann_text).width();
bdc2a99b
SA
752 // Annotation wide enough to fit a label? Don't put it in a block then
753 if (w <= a_width) {
754 a_is_separate = true;
755 break;
756 }
757 }
758 }
50631798 759
bdc2a99b
SA
760 // Were the previous and this annotation more than a pixel apart?
761 if ((abs(delta) > 1) || a_is_separate) {
762 // Block was broken, draw annotations that form the current block
20d90d22 763 if (block_ann_count == 1)
bdb97140 764 draw_annotation(prev_ann, p, pp, y, row);
20d90d22
SA
765 else if (block_ann_count > 0)
766 draw_annotation_block(block_start, prev_end, block_class,
bdb97140 767 block_class_uniform, p, y, row);
50631798 768
20d90d22 769 block_ann_count = 0;
50631798
SA
770 }
771
bdc2a99b 772 if (a_is_separate) {
bdb97140 773 draw_annotation(a, p, pp, y, row);
bdc2a99b 774 // Next annotation must start a new block. delta will be > 1
20d90d22
SA
775 // because we set prev_end to INT_MIN but that's okay since
776 // block_ann_count will be 0 and nothing will be drawn
777 prev_end = INT_MIN;
5c48ce32 778 block_ann_count = 0;
bdc2a99b 779 } else {
20d90d22 780 prev_end = a_end;
5a9146ac 781 prev_ann = a;
20d90d22
SA
782
783 if (block_ann_count == 0) {
784 block_start = a_start;
462941e2 785 block_class = a->ann_class_id();
20d90d22
SA
786 block_class_uniform = true;
787 } else
462941e2 788 if (a->ann_class_id() != block_class)
20d90d22
SA
789 block_class_uniform = false;
790
791 block_ann_count++;
bdc2a99b 792 }
50631798
SA
793 }
794
20d90d22 795 if (block_ann_count == 1)
bdb97140 796 draw_annotation(prev_ann, p, pp, y, row);
20d90d22
SA
797 else if (block_ann_count > 0)
798 draw_annotation_block(block_start, prev_end, block_class,
bdb97140 799 block_class_uniform, p, y, row);
50631798
SA
800}
801
bdb97140
SA
802void DecodeTrace::draw_annotation(const Annotation* a, QPainter &p,
803 const ViewItemPaintParams &pp, int y, const DecodeTraceRow& row) const
06e810f2 804{
53e35b2d
JH
805 double samples_per_pixel, pixels_offset;
806 tie(pixels_offset, samples_per_pixel) =
807 get_pixels_offset_samples_per_pixel();
7f8517f6 808
bdb97140 809 const double start = a->start_sample() / samples_per_pixel - pixels_offset;
5a9146ac 810 const double end = a->end_sample() / samples_per_pixel - pixels_offset;
287d607f 811
ae30ff42
SA
812 p.setPen(a->dark_color());
813 p.setBrush(a->color());
06e810f2 814
462941e2 815 if ((start > (pp.right() + DrawPadding)) || (end < (pp.left() - DrawPadding)))
06e810f2
JH
816 return;
817
5a9146ac 818 if (a->start_sample() == a->end_sample())
bdb97140 819 draw_instant(a, p, start, y);
06e810f2 820 else
bdb97140 821 draw_range(a, p, start, end, y, pp, row.title_width);
06e810f2
JH
822}
823
5c48ce32 824void DecodeTrace::draw_annotation_block(qreal start, qreal end,
1e948182 825 uint32_t ann_class, bool use_ann_format, QPainter &p, int y,
bdb97140 826 const DecodeTraceRow& row) const
50631798 827{
bdb97140
SA
828 const double top = y + .5 - annotation_height_ / 2;
829 const double bottom = y + .5 + annotation_height_ / 2;
462941e2 830 const double width = end - start;
3082ee93 831
20d90d22
SA
832 // If all annotations in this block are of the same type, we can use the
833 // one format that all of these annotations have. Otherwise, we should use
834 // a neutral color (i.e. gray)
835 if (use_ann_format) {
ae30ff42
SA
836 p.setPen(row.decode_row->get_dark_class_color(ann_class));
837 p.setBrush(QBrush(row.decode_row->get_class_color(ann_class), Qt::Dense4Pattern));
20d90d22 838 } else {
462941e2 839 p.setPen(QColor(Qt::darkGray));
20d90d22
SA
840 p.setBrush(QBrush(Qt::gray, Qt::Dense4Pattern));
841 }
842
462941e2
SA
843 if (width <= 1)
844 p.drawLine(QPointF(start, top), QPointF(start, bottom));
845 else {
846 const QRectF rect(start, top, width, bottom - top);
847 const int r = annotation_height_ / 4;
848 p.drawRoundedRect(rect, r, r);
849 }
50631798
SA
850}
851
bdb97140 852void DecodeTrace::draw_instant(const Annotation* a, QPainter &p, qreal x, int y) const
06e810f2 853{
462941e2
SA
854 const QString text = a->annotations()->empty() ?
855 QString() : a->annotations()->back();
5c48ce32 856 const qreal w = min((qreal)p.boundingRect(QRectF(), 0, text).width(),
bdb97140
SA
857 0.0) + annotation_height_;
858 const QRectF rect(x - w / 2, y - annotation_height_ / 2, w, annotation_height_);
06e810f2 859
bdb97140 860 p.drawRoundedRect(rect, annotation_height_ / 2, annotation_height_ / 2);
06e810f2 861
2a56e448 862 p.setPen(Qt::black);
06e810f2
JH
863 p.drawText(rect, Qt::AlignCenter | Qt::AlignVCenter, text);
864}
865
bdb97140 866void DecodeTrace::draw_range(const Annotation* a, QPainter &p,
5a9146ac 867 qreal start, qreal end, int y, const ViewItemPaintParams &pp,
f765c3db 868 int row_title_width) const
06e810f2 869{
bdb97140
SA
870 const qreal top = y + .5 - annotation_height_ / 2;
871 const qreal bottom = y + .5 + annotation_height_ / 2;
462941e2 872 const vector<QString>* annotations = a->annotations();
06e810f2 873
06e810f2 874 // If the two ends are within 1 pixel, draw a vertical line
2ad82c2e 875 if (start + 1.0 > end) {
06e810f2
JH
876 p.drawLine(QPointF(start, top), QPointF(start, bottom));
877 return;
878 }
879
5c48ce32 880 const qreal cap_width = min((end - start) / 4, EndCapWidth);
06e810f2
JH
881
882 QPointF pts[] = {
883 QPointF(start, y + .5f),
884 QPointF(start + cap_width, top),
885 QPointF(end - cap_width, top),
886 QPointF(end, y + .5f),
887 QPointF(end - cap_width, bottom),
888 QPointF(start + cap_width, bottom)
889 };
890
891 p.drawConvexPolygon(pts, countof(pts));
892
462941e2 893 if (annotations->empty())
06e810f2
JH
894 return;
895
7352be72
SA
896 const int ann_start = start + cap_width;
897 const int ann_end = end - cap_width;
898
74bf6666 899 const int real_start = max(ann_start, pp.left() + ArrowSize + row_title_width);
6f925ba9 900 const int real_end = min(ann_end, pp.right());
7352be72
SA
901 const int real_width = real_end - real_start;
902
bdb97140 903 QRectF rect(real_start, y - annotation_height_ / 2, real_width, annotation_height_);
0f290e9b
JH
904 if (rect.width() <= 4)
905 return;
906
2a56e448 907 p.setPen(Qt::black);
06e810f2
JH
908
909 // Try to find an annotation that will fit
910 QString best_annotation;
911 int best_width = 0;
912
462941e2 913 for (const QString &s : *annotations) {
5a9146ac 914 const int w = p.boundingRect(QRectF(), 0, s).width();
06e810f2 915 if (w <= rect.width() && w > best_width)
5a9146ac 916 best_annotation = s, best_width = w;
06e810f2
JH
917 }
918
919 if (best_annotation.isEmpty())
462941e2 920 best_annotation = annotations->back();
06e810f2
JH
921
922 // If not ellide the last in the list
923 p.drawText(rect, Qt::AlignCenter, p.fontMetrics().elidedText(
924 best_annotation, Qt::ElideRight, rect.width()));
925}
926
bdb97140 927void DecodeTrace::draw_unresolved_period(QPainter &p, int left, int right) const
5dfeb70f 928{
53e35b2d
JH
929 double samples_per_pixel, pixels_offset;
930
5ecf957f 931 const int64_t sample_count = decode_signal_->get_working_sample_count(current_segment_);
5dfeb70f
JH
932 if (sample_count == 0)
933 return;
934
b82908ab 935 const int64_t samples_decoded = decode_signal_->get_decoded_sample_count(current_segment_, true);
5dfeb70f
JH
936 if (sample_count == samples_decoded)
937 return;
938
be9e7b4b 939 const int y = get_visual_y();
7f8517f6 940
ff83d980 941 tie(pixels_offset, samples_per_pixel) = get_pixels_offset_samples_per_pixel();
7f8517f6 942
5dfeb70f
JH
943 const double start = max(samples_decoded /
944 samples_per_pixel - pixels_offset, left - 1.0);
945 const double end = min(sample_count / samples_per_pixel -
946 pixels_offset, right + 1.0);
bdb97140
SA
947 const QRectF no_decode_rect(start, y - (annotation_height_ / 2) - 0.5,
948 end - start, annotation_height_);
5dfeb70f
JH
949
950 p.setPen(QPen(Qt::NoPen));
951 p.setBrush(Qt::white);
952 p.drawRect(no_decode_rect);
953
641574bc
SA
954 p.setPen(NoDecodeColor);
955 p.setBrush(QBrush(NoDecodeColor, Qt::Dense6Pattern));
5dfeb70f
JH
956 p.drawRect(no_decode_rect);
957}
958
53e35b2d 959pair<double, double> DecodeTrace::get_pixels_offset_samples_per_pixel() const
7f8517f6 960{
8dbbc7f0 961 assert(owner_);
7f8517f6 962
8dbbc7f0 963 const View *view = owner_->view();
eae6e30a
JH
964 assert(view);
965
966 const double scale = view->scale();
7f8517f6
SA
967 assert(scale > 0);
968
53e35b2d 969 const double pixels_offset =
ff83d980 970 ((view->offset() - decode_signal_->start_time()) / scale).convert_to<double>();
7f8517f6 971
85125b0f 972 double samplerate = decode_signal_->get_samplerate();
7f8517f6
SA
973
974 // Show sample rate as 1Hz when it is unknown
975 if (samplerate == 0.0)
976 samplerate = 1.0;
977
53e35b2d 978 return make_pair(pixels_offset, samplerate * scale);
7f8517f6
SA
979}
980
67fb15bf 981pair<uint64_t, uint64_t> DecodeTrace::get_view_sample_range(
db1bf6bf 982 int x_start, int x_end) const
7f8517f6 983{
53e35b2d
JH
984 double samples_per_pixel, pixels_offset;
985 tie(pixels_offset, samples_per_pixel) =
986 get_pixels_offset_samples_per_pixel();
7f8517f6 987
db1bf6bf
JH
988 const uint64_t start = (uint64_t)max(
989 (x_start + pixels_offset) * samples_per_pixel, 0.0);
990 const uint64_t end = (uint64_t)max(
991 (x_end + pixels_offset) * samples_per_pixel, 0.0);
7f8517f6
SA
992
993 return make_pair(start, end);
994}
995
6a26fc44 996unsigned int DecodeTrace::get_row_y(const DecodeTraceRow* row) const
e2f90c50 997{
84002113 998 assert(row);
e2f90c50 999
84002113 1000 unsigned int y = get_visual_y();
99029fda 1001
6a26fc44 1002 for (const DecodeTraceRow& r : rows_) {
9741d6e0
SA
1003 if (!r.currently_visible)
1004 continue;
1005
1006 if (row->decode_row == r.decode_row)
84002113 1007 break;
9741d6e0
SA
1008 else
1009 y += r.height;
1010 }
99029fda 1011
84002113
SA
1012 return y;
1013}
99029fda 1014
6a26fc44 1015DecodeTraceRow* DecodeTrace::get_row_at_point(const QPoint &point)
84002113 1016{
9741d6e0 1017 int y = get_visual_y() - (default_row_height_ / 2);
84002113 1018
6a26fc44 1019 for (DecodeTraceRow& r : rows_) {
84002113
SA
1020 if (!r.currently_visible)
1021 continue;
1022
1023 if ((point.y() >= y) && (point.y() < (int)(y + r.height)))
1024 return &r;
1025
1026 y += r.height;
1027 }
e2f90c50 1028
84002113 1029 return nullptr;
e2f90c50
SA
1030}
1031
117cdea3 1032const QString DecodeTrace::get_annotation_at_point(const QPoint &point)
e2f90c50 1033{
117cdea3
JH
1034 if (!enabled())
1035 return QString();
e2f90c50 1036
117cdea3 1037 const pair<uint64_t, uint64_t> sample_range =
67fb15bf 1038 get_view_sample_range(point.x(), point.x() + 1);
6a26fc44 1039 const DecodeTraceRow* r = get_row_at_point(point);
84002113
SA
1040
1041 if (!r)
117cdea3 1042 return QString();
e2f90c50 1043
9741d6e0
SA
1044 if (point.y() > (int)(get_row_y(r) + (annotation_height_ / 2)))
1045 return QString();
1046
462941e2 1047 deque<const Annotation*> annotations;
e2f90c50 1048
84002113 1049 decode_signal_->get_annotation_subset(annotations, r->decode_row,
72435789 1050 current_segment_, sample_range.first, sample_range.second);
e2f90c50 1051
c583ea05
SA
1052 const Annotation* a = annotations.empty() ? nullptr : annotations[0];
1053
1054 // Create a string of the format "CLASS: VALUE"
1055 QString s;
1056 if (a) {
1057 if (!a->ann_class_description().isEmpty())
1058 s = a->ann_class_description() + ": ";
1059 s += a->annotations()->front();
1060 }
1061
1062 return s;
e2f90c50
SA
1063}
1064
5a9146ac
SA
1065void DecodeTrace::create_decoder_form(int index, shared_ptr<Decoder> &dec,
1066 QWidget *parent, QFormLayout *form)
7491a29f 1067{
1cc1c8de 1068 GlobalSettings settings;
7491a29f
JH
1069
1070 assert(dec);
6a26fc44 1071 const srd_decoder *const decoder = dec->get_srd_decoder();
7491a29f
JH
1072 assert(decoder);
1073
ff59fa2c
SA
1074 const bool decoder_deletable = index > 0;
1075
204bae45 1076 pv::widgets::DecoderGroupBox *const group =
27e8df22 1077 new pv::widgets::DecoderGroupBox(
580b4f25
UH
1078 QString::fromUtf8(decoder->name),
1079 tr("%1:\n%2").arg(QString::fromUtf8(decoder->longname),
1080 QString::fromUtf8(decoder->desc)),
1081 nullptr, decoder_deletable);
5d3ca591 1082 group->set_decoder_visible(dec->visible());
613d097c 1083
ff59fa2c
SA
1084 if (decoder_deletable) {
1085 delete_mapper_.setMapping(group, index);
1086 connect(group, SIGNAL(delete_decoder()), &delete_mapper_, SLOT(map()));
1087 }
613d097c 1088
8dbbc7f0 1089 show_hide_mapper_.setMapping(group, index);
dd048a7e 1090 connect(group, SIGNAL(show_hide_decoder()),
8dbbc7f0 1091 &show_hide_mapper_, SLOT(map()));
dd048a7e 1092
204bae45
JH
1093 QFormLayout *const decoder_form = new QFormLayout;
1094 group->add_layout(decoder_form);
7491a29f 1095
47747218 1096 const vector<DecodeChannel> channels = decode_signal_->get_channels();
407c9ebe 1097
9f97b357 1098 // Add the channels
7a0d99e6 1099 for (const DecodeChannel& ch : channels) {
9f97b357
SA
1100 // Ignore channels not part of the decoder we create the form for
1101 if (ch.decoder_ != dec)
1102 continue;
407c9ebe 1103
9f97b357
SA
1104 QComboBox *const combo = create_channel_selector(parent, &ch);
1105 QComboBox *const combo_init_state = create_channel_selector_init_state(parent, &ch);
407c9ebe 1106
9f97b357
SA
1107 channel_id_map_[combo] = ch.id;
1108 init_state_map_[combo_init_state] = ch.id;
407c9ebe 1109
7491a29f 1110 connect(combo, SIGNAL(currentIndexChanged(int)),
6ac6242b 1111 this, SLOT(on_channel_selected(int)));
9f97b357
SA
1112 connect(combo_init_state, SIGNAL(currentIndexChanged(int)),
1113 this, SLOT(on_init_state_changed(int)));
407c9ebe
UH
1114
1115 QHBoxLayout *const hlayout = new QHBoxLayout;
1116 hlayout->addWidget(combo);
9f97b357 1117 hlayout->addWidget(combo_init_state);
407c9ebe 1118
1cc1c8de 1119 if (!settings.value(GlobalSettings::Key_Dec_InitialStateConfigurable).toBool())
9f97b357 1120 combo_init_state->hide();
7491a29f 1121
9f97b357
SA
1122 const QString required_flag = ch.is_optional ? QString() : QString("*");
1123 decoder_form->addRow(tr("<b>%1</b> (%2) %3")
1124 .arg(ch.name, ch.desc, required_flag), hlayout);
7491a29f
JH
1125 }
1126
1127 // Add the options
3cc9ad7b 1128 shared_ptr<binding::Decoder> binding(
946b52e1 1129 new binding::Decoder(decode_signal_, dec));
204bae45 1130 binding->add_properties_to_form(decoder_form, true);
7491a29f 1131
8dbbc7f0 1132 bindings_.push_back(binding);
204bae45
JH
1133
1134 form->addRow(group);
8dbbc7f0 1135 decoder_forms_.push_back(group);
7491a29f
JH
1136}
1137
9f97b357 1138QComboBox* DecodeTrace::create_channel_selector(QWidget *parent, const DecodeChannel *ch)
4e5a4405 1139{
47e9e7bb 1140 const auto sigs(session_.signalbases());
78b0af3e 1141
9f97b357 1142 // Sort signals in natural order
47e9e7bb 1143 vector< shared_ptr<data::SignalBase> > sig_list(sigs.begin(), sigs.end());
6f925ba9 1144 sort(sig_list.begin(), sig_list.end(),
47e9e7bb
SA
1145 [](const shared_ptr<data::SignalBase> &a,
1146 const shared_ptr<data::SignalBase> &b) {
1147 return strnatcasecmp(a->name().toStdString(),
1148 b->name().toStdString()) < 0; });
4e5a4405 1149
4e5a4405
JH
1150 QComboBox *selector = new QComboBox(parent);
1151
009fc9ae 1152 selector->addItem("-", QVariant::fromValue((void*)nullptr));
4e5a4405 1153
9f97b357 1154 if (!ch->assigned_signal)
4e5a4405
JH
1155 selector->setCurrentIndex(0);
1156
47e9e7bb
SA
1157 for (const shared_ptr<data::SignalBase> &b : sig_list) {
1158 assert(b);
79c4a9c8 1159 if (b->logic_data() && b->enabled()) {
cf1541a1 1160 selector->addItem(b->name(), QVariant::fromValue(b));
5da5d081 1161
cf1541a1 1162 if (ch->assigned_signal == b)
9f97b357 1163 selector->setCurrentIndex(selector->count() - 1);
4e5a4405
JH
1164 }
1165 }
1166
1167 return selector;
1168}
1169
9f97b357
SA
1170QComboBox* DecodeTrace::create_channel_selector_init_state(QWidget *parent,
1171 const DecodeChannel *ch)
407c9ebe
UH
1172{
1173 QComboBox *selector = new QComboBox(parent);
1174
009fc9ae
SA
1175 selector->addItem("0", QVariant::fromValue((int)SRD_INITIAL_PIN_LOW));
1176 selector->addItem("1", QVariant::fromValue((int)SRD_INITIAL_PIN_HIGH));
1177 selector->addItem("X", QVariant::fromValue((int)SRD_INITIAL_PIN_SAME_AS_SAMPLE0));
407c9ebe 1178
9f97b357 1179 selector->setCurrentIndex(ch->initial_pin_state);
407c9ebe
UH
1180
1181 selector->setToolTip("Initial (assumed) pin value before the first sample");
1182
1183 return selector;
1184}
1185
462941e2 1186void DecodeTrace::export_annotations(deque<const Annotation*>& annotations) const
5a914348 1187{
5a914348
SA
1188 GlobalSettings settings;
1189 const QString dir = settings.value("MainWindow/SaveDirectory").toString();
1190
1191 const QString file_name = QFileDialog::getSaveFileName(
1192 owner_->view(), tr("Export annotations"), dir, tr("Text Files (*.txt);;All Files (*)"));
1193
1194 if (file_name.isEmpty())
1195 return;
1196
1197 QString format = settings.value(GlobalSettings::Key_Dec_ExportFormat).toString();
1198 const QString quote = format.contains("%q") ? "\"" : "";
1199 format = format.remove("%q");
1200
761f8302
SA
1201 const bool has_sample_range = format.contains("%s");
1202 const bool has_row_name = format.contains("%r");
1203 const bool has_dec_name = format.contains("%d");
1204 const bool has_class_name = format.contains("%c");
1205 const bool has_first_ann_text = format.contains("%1");
1206 const bool has_all_ann_text = format.contains("%a");
1207
5a914348
SA
1208 QFile file(file_name);
1209 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
1210 QTextStream out_stream(&file);
1211
462941e2 1212 for (const Annotation* ann : annotations) {
761f8302 1213 QString out_text = format;
5a914348 1214
761f8302
SA
1215 if (has_sample_range) {
1216 const QString sample_range = QString("%1-%2") \
1217 .arg(QString::number(ann->start_sample()), QString::number(ann->end_sample()));
1218 out_text = out_text.replace("%s", sample_range);
1219 }
5a914348 1220
761f8302
SA
1221 if (has_dec_name)
1222 out_text = out_text.replace("%d",
1223 quote + QString::fromUtf8(ann->row()->decoder()->name()) + quote);
5a914348 1224
761f8302
SA
1225 if (has_row_name) {
1226 const QString row_name = quote + ann->row()->description() + quote;
1227 out_text = out_text.replace("%r", row_name);
1228 }
1229
1230 if (has_class_name) {
1231 const QString class_name = quote + ann->ann_class_name() + quote;
1232 out_text = out_text.replace("%c", class_name);
1233 }
1234
1235 if (has_first_ann_text) {
1236 const QString first_ann_text = quote + ann->annotations()->front() + quote;
1237 out_text = out_text.replace("%1", first_ann_text);
1238 }
1239
1240 if (has_all_ann_text) {
1241 QString all_ann_text;
1242 for (const QString &s : *(ann->annotations()))
1243 all_ann_text = all_ann_text + quote + s + quote + ",";
1244 all_ann_text.chop(1);
1245
1246 out_text = out_text.replace("%a", all_ann_text);
1247 }
5a914348 1248
5a914348
SA
1249 out_stream << out_text << '\n';
1250 }
1251
1252 if (out_stream.status() == QTextStream::Ok)
1253 return;
1254 }
1255
1256 QMessageBox msg(owner_->view());
970fca0d 1257 msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
5a914348
SA
1258 msg.setStandardButtons(QMessageBox::Ok);
1259 msg.setIcon(QMessageBox::Warning);
1260 msg.exec();
1261}
1262
6a26fc44 1263void DecodeTrace::initialize_row_widgets(DecodeTraceRow* r, unsigned int row_id)
84002113 1264{
adf9e022 1265 // Set colors and fixed widths
84002113
SA
1266 QFontMetrics m(QApplication::font());
1267
c6b4e925
SA
1268 QPalette header_palette = owner_->view()->palette();
1269 QPalette selector_palette = owner_->view()->palette();
1270
62c09685 1271#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
1ed73ebd
VPP
1272 if (GlobalSettings::current_theme_is_dark()) {
1273 header_palette.setColor(QPalette::Window,
1274 QColor(255, 255, 255, ExpansionAreaHeaderAlpha));
1275 selector_palette.setColor(QPalette::Window,
1276 QColor(255, 255, 255, ExpansionAreaAlpha));
1277 } else {
1278 header_palette.setColor(QPalette::Window,
1279 QColor(0, 0, 0, ExpansionAreaHeaderAlpha));
1280 selector_palette.setColor(QPalette::Window,
1281 QColor(0, 0, 0, ExpansionAreaAlpha));
1282 }
1283#else
c6b4e925
SA
1284 if (GlobalSettings::current_theme_is_dark()) {
1285 header_palette.setColor(QPalette::Background,
1286 QColor(255, 255, 255, ExpansionAreaHeaderAlpha));
1287 selector_palette.setColor(QPalette::Background,
1288 QColor(255, 255, 255, ExpansionAreaAlpha));
1289 } else {
1290 header_palette.setColor(QPalette::Background,
1291 QColor(0, 0, 0, ExpansionAreaHeaderAlpha));
1292 selector_palette.setColor(QPalette::Background,
1293 QColor(0, 0, 0, ExpansionAreaAlpha));
1294 }
1ed73ebd 1295#endif
c6b4e925 1296
6a26fc44
SA
1297 const int w = m.boundingRect(r->decode_row->title()).width() + RowTitleMargin;
1298 r->title_width = w;
1299
adf9e022
SA
1300 // Set up top-level container
1301 connect(r->container, SIGNAL(widgetResized(QWidget*)),
1302 this, SLOT(on_row_container_resized(QWidget*)));
6a26fc44
SA
1303
1304 QVBoxLayout* vlayout = new QVBoxLayout();
1305 r->container->setLayout(vlayout);
1306
65bde9b3 1307 // Add header container
6a26fc44
SA
1308 vlayout->addWidget(r->header_container);
1309 vlayout->setContentsMargins(0, 0, 0, 0);
1310 vlayout->setSpacing(0);
65bde9b3 1311 QHBoxLayout* header_container_layout = new QHBoxLayout();
6a26fc44
SA
1312 r->header_container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
1313 r->header_container->setMinimumSize(0, default_row_height_);
65bde9b3
SA
1314 r->header_container->setLayout(header_container_layout);
1315 r->header_container->layout()->setContentsMargins(10, 2, 10, 2);
6a26fc44
SA
1316
1317 r->header_container->setAutoFillBackground(true);
1318 r->header_container->setPalette(header_palette);
1319
65bde9b3 1320 // Add widgets inside the header container
6a26fc44 1321 QCheckBox* cb = new QCheckBox();
9eaa8343 1322 r->row_visibility_checkbox = cb;
65bde9b3 1323 header_container_layout->addWidget(cb);
6a26fc44
SA
1324 cb->setText(tr("Show this row"));
1325 cb->setChecked(r->decode_row->visible());
1326
1327 row_show_hide_mapper_.setMapping(cb, row_id);
1328 connect(cb, SIGNAL(stateChanged(int)),
1329 &row_show_hide_mapper_, SLOT(map()));
1330
65bde9b3
SA
1331 QPushButton* btn = new QPushButton();
1332 header_container_layout->addWidget(btn);
1333 btn->setFlat(true);
1334 btn->setStyleSheet(":hover { background-color: palette(button); color: palette(button-text); border:0; }");
1335 btn->setText(tr("Show All"));
1336 btn->setProperty("decode_trace_row_ptr", QVariant::fromValue((void*)r));
1337 connect(btn, SIGNAL(clicked(bool)), this, SLOT(on_show_all_classes()));
1338
1339 btn = new QPushButton();
1340 header_container_layout->addWidget(btn);
1341 btn->setFlat(true);
1342 btn->setStyleSheet(":hover { background-color: palette(button); color: palette(button-text); border:0; }");
1343 btn->setText(tr("Hide All"));
1344 btn->setProperty("decode_trace_row_ptr", QVariant::fromValue((void*)r));
1345 connect(btn, SIGNAL(clicked(bool)), this, SLOT(on_hide_all_classes()));
1346
1347 header_container_layout->addStretch(); // To left-align the header widgets
1348
6a26fc44
SA
1349 // Add selector container
1350 vlayout->addWidget(r->selector_container);
adf9e022
SA
1351 r->selector_container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
1352 r->selector_container->setLayout(new FlowLayout(r->selector_container));
6a26fc44
SA
1353
1354 r->selector_container->setAutoFillBackground(true);
1355 r->selector_container->setPalette(selector_palette);
1356
1357 // Add all classes that can be toggled
1358 vector<AnnotationClass*> ann_classes = r->decode_row->ann_classes();
1359
1360 for (const AnnotationClass* ann_class : ann_classes) {
1361 cb = new QCheckBox();
1362 cb->setText(tr(ann_class->description));
02078aa1 1363 cb->setChecked(ann_class->visible());
6a26fc44 1364
ec4f16ff
SA
1365 int dim = ViewItemPaintParams::text_height() - 2;
1366 QPixmap pixmap(dim, dim);
ae30ff42 1367 pixmap.fill(r->decode_row->get_class_color(ann_class->id));
ec4f16ff
SA
1368 cb->setIcon(pixmap);
1369
6a26fc44 1370 r->selector_container->layout()->addWidget(cb);
65bde9b3 1371 r->selectors.push_back(cb);
6a26fc44
SA
1372
1373 cb->setProperty("ann_class_ptr", QVariant::fromValue((void*)ann_class));
81dc0221
SA
1374 cb->setProperty("decode_trace_row_ptr", QVariant::fromValue((void*)r));
1375
6a26fc44
SA
1376 class_show_hide_mapper_.setMapping(cb, cb);
1377 connect(cb, SIGNAL(stateChanged(int)),
1378 &class_show_hide_mapper_, SLOT(map()));
1379 }
1380}
1381
1382void DecodeTrace::update_rows()
1383{
a653b83d
SA
1384 if (!owner_)
1385 return;
1386
6a26fc44
SA
1387 lock_guard<mutex> lock(row_modification_mutex_);
1388
1389 for (DecodeTraceRow& r : rows_)
84002113
SA
1390 r.exists = false;
1391
c6b4e925 1392 unsigned int row_id = 0;
6a26fc44 1393 for (Row* decode_row : decode_signal_->get_rows()) {
84002113
SA
1394 // Find row in our list
1395 auto r_it = find_if(rows_.begin(), rows_.end(),
6a26fc44 1396 [&](DecodeTraceRow& r){ return r.decode_row == decode_row; });
84002113 1397
6a26fc44 1398 DecodeTraceRow* r = nullptr;
84002113
SA
1399 if (r_it == rows_.end()) {
1400 // Row doesn't exist yet, create and append it
6a26fc44 1401 DecodeTraceRow nr;
84002113 1402 nr.decode_row = decode_row;
ae30ff42 1403 nr.decode_row->set_base_color(base_->color());
84002113 1404 nr.height = default_row_height_;
c6b4e925 1405 nr.expanded_height = default_row_height_;
84002113 1406 nr.currently_visible = false;
81dc0221 1407 nr.has_hidden_classes = decode_row->has_hidden_classes();
84002113 1408 nr.expand_marker_highlighted = false;
74bf6666 1409 nr.expanding = false;
84002113 1410 nr.expanded = false;
74bf6666
SA
1411 nr.collapsing = false;
1412 nr.expand_marker_shape = default_marker_shape_;
adf9e022 1413 nr.container = new ContainerWidget(owner_->view()->scrollarea());
c6b4e925
SA
1414 nr.header_container = new QWidget(nr.container);
1415 nr.selector_container = new QWidget(nr.container);
84002113
SA
1416
1417 rows_.push_back(nr);
1418 r = &rows_.back();
6a26fc44 1419 initialize_row_widgets(r, row_id);
84002113
SA
1420 } else
1421 r = &(*r_it);
1422
1423 r->exists = true;
c6b4e925 1424 row_id++;
84002113
SA
1425 }
1426
9eaa8343
SA
1427 // If there's only one row, it must not be hidden or else it can't be un-hidden
1428 if (row_id == 1)
1429 rows_.front().row_visibility_checkbox->setEnabled(false);
1430
84002113
SA
1431 // Remove any rows that no longer exist, obeying that iterators are invalidated
1432 bool any_exists;
1433 do {
1434 any_exists = false;
1435
1436 for (unsigned int i = 0; i < rows_.size(); i++)
1437 if (!rows_[i].exists) {
9eaa8343
SA
1438 delete rows_[i].row_visibility_checkbox;
1439
c6b4e925
SA
1440 for (QCheckBox* cb : rows_[i].selectors)
1441 delete cb;
1442
1443 delete rows_[i].selector_container;
1444 delete rows_[i].header_container;
dca3cbee 1445 delete rows_[i].container;
c6b4e925 1446
84002113
SA
1447 rows_.erase(rows_.begin() + i);
1448 any_exists = true;
1449 break;
1450 }
1451 } while (any_exists);
1452}
1453
6a26fc44 1454void DecodeTrace::set_row_expanded(DecodeTraceRow* r)
c6b4e925
SA
1455{
1456 r->height = r->expanded_height;
1457 r->expanding = false;
1458 r->expanded = true;
1459
1460 // For details on this, see on_animation_timer()
1461 r->expand_marker_shape.setPoint(0, 0, 0);
1462 r->expand_marker_shape.setPoint(1, ArrowSize, ArrowSize);
1463 r->expand_marker_shape.setPoint(2, 2*ArrowSize, 0);
1464
1465 r->container->resize(owner_->view()->viewport()->width() - r->container->pos().x(),
1466 r->height - 2 * default_row_height_);
1467}
1468
6a26fc44 1469void DecodeTrace::set_row_collapsed(DecodeTraceRow* r)
c6b4e925
SA
1470{
1471 r->height = default_row_height_;
1472 r->collapsing = false;
1473 r->expanded = false;
1474 r->expand_marker_shape = default_marker_shape_;
1475 r->container->setVisible(false);
1476
1477 r->container->resize(owner_->view()->viewport()->width() - r->container->pos().x(),
1478 r->height - 2 * default_row_height_);
1479}
1480
1481void DecodeTrace::update_expanded_rows()
1482{
6a26fc44 1483 for (DecodeTraceRow& r : rows_) {
adf9e022
SA
1484 if (r.expanding || r.expanded)
1485 r.expanded_height = 2 * default_row_height_ + r.container->sizeHint().height();
1486
1487 if (r.expanded)
1488 r.height = r.expanded_height;
1489
1490 int x = 2 * ArrowSize;
1491 int y = get_row_y(&r) + default_row_height_;
1492 // Only update the position if it actually changes
1493 if ((x != r.container->pos().x()) || (y != r.container->pos().y()))
1494 r.container->move(x, y);
1495
1496 int w = owner_->view()->viewport()->width() - x;
1497 int h = r.height - 2 * default_row_height_;
1498 // Only update the dimension if they actually change
1499 if ((w != r.container->sizeHint().width()) || (h != r.container->sizeHint().height()))
1500 r.container->resize(w, h);
c6b4e925
SA
1501 }
1502}
1503
ab185f78
SA
1504void DecodeTrace::on_setting_changed(const QString &key, const QVariant &value)
1505{
945238ca
SA
1506 Trace::on_setting_changed(key, value);
1507
a767229e 1508 if (key == GlobalSettings::Key_Dec_AlwaysShowAllRows)
ab185f78 1509 always_show_all_rows_ = value.toBool();
ab185f78
SA
1510}
1511
ae30ff42
SA
1512void DecodeTrace::on_color_changed(const QColor &color)
1513{
1514 for (DecodeTraceRow& r : rows_)
1515 r.decode_row->set_base_color(color);
88a25978
SA
1516
1517 if (owner_)
1518 owner_->row_item_appearance_changed(false, true);
ae30ff42
SA
1519}
1520
ad908057 1521void DecodeTrace::on_new_annotations()
5b6ae103
SA
1522{
1523 if (!delayed_trace_updater_.isActive())
1524 delayed_trace_updater_.start();
1525}
1526
1527void DecodeTrace::on_delayed_trace_update()
9cef9567 1528{
8dbbc7f0 1529 if (owner_)
6e2c3c85 1530 owner_->row_item_appearance_changed(false, true);
9cef9567
JH
1531}
1532
eee3eab9
SA
1533void DecodeTrace::on_decode_reset()
1534{
84002113 1535 update_rows();
eee3eab9
SA
1536
1537 if (owner_)
1538 owner_->row_item_appearance_changed(false, true);
1539}
1540
1b56c646
SA
1541void DecodeTrace::on_decode_finished()
1542{
1543 if (owner_)
1544 owner_->row_item_appearance_changed(false, true);
1545}
1546
556259d2
SA
1547void DecodeTrace::on_pause_decode()
1548{
1549 if (decode_signal_->is_paused())
1550 decode_signal_->resume_decode();
1551 else
1552 decode_signal_->pause_decode();
1553}
1554
b9329558 1555void DecodeTrace::on_delete()
c51482b3 1556{
ad908057 1557 session_.remove_decode_signal(decode_signal_);
c51482b3
JH
1558}
1559
6ac6242b 1560void DecodeTrace::on_channel_selected(int)
4e5a4405 1561{
9f97b357
SA
1562 QComboBox *cb = qobject_cast<QComboBox*>(QObject::sender());
1563
1564 // Determine signal that was selected
cf1541a1
SA
1565 shared_ptr<data::SignalBase> signal =
1566 cb->itemData(cb->currentIndex()).value<shared_ptr<data::SignalBase>>();
9f97b357
SA
1567
1568 // Determine decode channel ID this combo box is the channel selector for
1569 const uint16_t id = channel_id_map_.at(cb);
1570
1571 decode_signal_->assign_signal(id, signal);
1572}
1573
1574void DecodeTrace::on_channels_updated()
1575{
1576 if (owner_)
1577 owner_->row_item_appearance_changed(false, true);
4e5a4405
JH
1578}
1579
9f97b357 1580void DecodeTrace::on_init_state_changed(int)
407c9ebe 1581{
9f97b357
SA
1582 QComboBox *cb = qobject_cast<QComboBox*>(QObject::sender());
1583
1584 // Determine inital pin state that was selected
1585 int init_state = cb->itemData(cb->currentIndex()).value<int>();
1586
1587 // Determine decode channel ID this combo box is the channel selector for
1588 const uint16_t id = init_state_map_.at(cb);
1589
1590 decode_signal_->set_initial_pin_state(id, init_state);
407c9ebe
UH
1591}
1592
7491a29f
JH
1593void DecodeTrace::on_stack_decoder(srd_decoder *decoder)
1594{
ad908057 1595 decode_signal_->stack_decoder(decoder);
84002113 1596 update_rows();
37fd11b1
JH
1597
1598 create_popup_form();
7491a29f
JH
1599}
1600
613d097c
JH
1601void DecodeTrace::on_delete_decoder(int index)
1602{
ad908057 1603 decode_signal_->remove_decoder(index);
84002113 1604 update_rows();
613d097c 1605
ded43869
SA
1606 owner_->extents_changed(false, true);
1607
c063290a 1608 create_popup_form();
613d097c
JH
1609}
1610
dd048a7e
JH
1611void DecodeTrace::on_show_hide_decoder(int index)
1612{
ad908057 1613 const bool state = decode_signal_->toggle_decoder_visibility(index);
dd048a7e 1614
8dbbc7f0 1615 assert(index < (int)decoder_forms_.size());
ad908057 1616 decoder_forms_[index]->set_decoder_visible(state);
dd048a7e 1617
a767229e 1618 if (!state)
ded43869 1619 owner_->extents_changed(false, true);
ded43869 1620
c6b4e925
SA
1621 owner_->row_item_appearance_changed(false, true);
1622}
1623
6a26fc44 1624void DecodeTrace::on_show_hide_row(int row_id)
c6b4e925 1625{
6a26fc44 1626 if (row_id >= (int)rows_.size())
c6b4e925
SA
1627 return;
1628
6a26fc44 1629 rows_[row_id].decode_row->set_visible(!rows_[row_id].decode_row->visible());
c6b4e925 1630
440bdf50
SA
1631 if (!rows_[row_id].decode_row->visible())
1632 set_row_collapsed(&rows_[row_id]);
1633
c6b4e925
SA
1634 owner_->extents_changed(false, true);
1635 owner_->row_item_appearance_changed(false, true);
dd048a7e
JH
1636}
1637
1ed73ebd
VPP
1638#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1639void DecodeTrace::on_show_hide_class(QObject* sender)
1640#else
6a26fc44 1641void DecodeTrace::on_show_hide_class(QWidget* sender)
1ed73ebd 1642#endif
6a26fc44
SA
1643{
1644 void* ann_class_ptr = sender->property("ann_class_ptr").value<void*>();
1645 assert(ann_class_ptr);
6a26fc44 1646 AnnotationClass* ann_class = (AnnotationClass*)ann_class_ptr;
81dc0221 1647
02078aa1 1648 ann_class->set_visible(!ann_class->visible());
6a26fc44 1649
81dc0221
SA
1650 void* row_ptr = sender->property("decode_trace_row_ptr").value<void*>();
1651 assert(row_ptr);
1652 DecodeTraceRow* row = (DecodeTraceRow*)row_ptr;
1653
1654 row->has_hidden_classes = row->decode_row->has_hidden_classes();
1655
6a26fc44
SA
1656 owner_->row_item_appearance_changed(false, true);
1657}
1658
65bde9b3
SA
1659void DecodeTrace::on_show_all_classes()
1660{
1661 void* row_ptr = QObject::sender()->property("decode_trace_row_ptr").value<void*>();
1662 assert(row_ptr);
1663 DecodeTraceRow* row = (DecodeTraceRow*)row_ptr;
1664
1665 for (QCheckBox* cb : row->selectors)
1666 cb->setChecked(true);
1667
1668 row->has_hidden_classes = false;
1669
1670 owner_->row_item_appearance_changed(false, true);
1671}
1672
1673void DecodeTrace::on_hide_all_classes()
1674{
1675 void* row_ptr = QObject::sender()->property("decode_trace_row_ptr").value<void*>();
1676 assert(row_ptr);
1677 DecodeTraceRow* row = (DecodeTraceRow*)row_ptr;
1678
1679 for (QCheckBox* cb : row->selectors)
1680 cb->setChecked(false);
1681
1682 row->has_hidden_classes = true;
1683
1684 owner_->row_item_appearance_changed(false, true);
1685}
1686
adf9e022
SA
1687void DecodeTrace::on_row_container_resized(QWidget* sender)
1688{
1689 sender->update();
1690
1691 owner_->extents_changed(false, true);
1692 owner_->row_item_appearance_changed(false, true);
1693}
1694
c764c995
SA
1695void DecodeTrace::on_copy_annotation_to_clipboard()
1696{
c764c995
SA
1697 if (!selected_row_)
1698 return;
1699
462941e2 1700 deque<const Annotation*> annotations;
c764c995 1701
462941e2 1702 decode_signal_->get_annotation_subset(annotations, selected_row_,
c764c995
SA
1703 current_segment_, selected_sample_range_.first, selected_sample_range_.first);
1704
462941e2 1705 if (annotations.empty())
c764c995
SA
1706 return;
1707
628b45cc 1708 QClipboard *clipboard = QApplication::clipboard();
462941e2 1709 clipboard->setText(annotations.front()->annotations()->front(), QClipboard::Clipboard);
41aaa675
SA
1710
1711 if (clipboard->supportsSelection())
462941e2 1712 clipboard->setText(annotations.front()->annotations()->front(), QClipboard::Selection);
c764c995
SA
1713}
1714
be843692
SA
1715void DecodeTrace::on_export_row()
1716{
99ba5f28 1717 selected_sample_range_ = make_pair(0, numeric_limits<uint64_t>::max());
be843692
SA
1718 on_export_row_from_here();
1719}
1720
5a914348
SA
1721void DecodeTrace::on_export_all_rows()
1722{
99ba5f28
SA
1723 selected_sample_range_ = make_pair(0, numeric_limits<uint64_t>::max());
1724 on_export_all_rows_from_here();
1725}
1726
1727void DecodeTrace::on_export_row_with_cursor()
1728{
1729 const View *view = owner_->view();
1730 assert(view);
1731
1732 if (!view->cursors()->enabled())
1733 return;
1734
1735 const double samplerate = session_.get_samplerate();
1736
1737 const pv::util::Timestamp& start_time = view->cursors()->first()->time();
1738 const pv::util::Timestamp& end_time = view->cursors()->second()->time();
1739
1740 const uint64_t start_sample = (uint64_t)max(
a20c1fcc 1741 0.0, start_time.convert_to<double>() * samplerate);
99ba5f28 1742 const uint64_t end_sample = (uint64_t)max(
a20c1fcc 1743 0.0, end_time.convert_to<double>() * samplerate);
99ba5f28
SA
1744
1745 // Are both cursors negative and thus were clamped to 0?
1746 if ((start_sample == 0) && (end_sample == 0))
1747 return;
1748
1749 selected_sample_range_ = make_pair(start_sample, end_sample);
1750 on_export_row_from_here();
1751}
1752
1753void DecodeTrace::on_export_all_rows_with_cursor()
1754{
1755 const View *view = owner_->view();
1756 assert(view);
1757
1758 if (!view->cursors()->enabled())
1759 return;
1760
1761 const double samplerate = session_.get_samplerate();
1762
1763 const pv::util::Timestamp& start_time = view->cursors()->first()->time();
1764 const pv::util::Timestamp& end_time = view->cursors()->second()->time();
1765
1766 const uint64_t start_sample = (uint64_t)max(
a20c1fcc 1767 0.0, start_time.convert_to<double>() * samplerate);
99ba5f28 1768 const uint64_t end_sample = (uint64_t)max(
a20c1fcc 1769 0.0, end_time.convert_to<double>() * samplerate);
99ba5f28
SA
1770
1771 // Are both cursors negative and thus were clamped to 0?
1772 if ((start_sample == 0) && (end_sample == 0))
1773 return;
1774
1775 selected_sample_range_ = make_pair(start_sample, end_sample);
5a914348
SA
1776 on_export_all_rows_from_here();
1777}
1778
be843692
SA
1779void DecodeTrace::on_export_row_from_here()
1780{
be843692
SA
1781 if (!selected_row_)
1782 return;
1783
462941e2 1784 deque<const Annotation*> annotations;
be843692 1785
462941e2 1786 decode_signal_->get_annotation_subset(annotations, selected_row_,
99ba5f28 1787 current_segment_, selected_sample_range_.first, selected_sample_range_.second);
be843692 1788
462941e2 1789 if (annotations.empty())
be843692
SA
1790 return;
1791
5a914348 1792 export_annotations(annotations);
5a914348 1793}
1ed996b4 1794
5a914348
SA
1795void DecodeTrace::on_export_all_rows_from_here()
1796{
462941e2 1797 deque<const Annotation*> annotations;
1ed996b4 1798
462941e2 1799 decode_signal_->get_annotation_subset(annotations, current_segment_,
99ba5f28 1800 selected_sample_range_.first, selected_sample_range_.second);
be843692 1801
462941e2 1802 if (!annotations.empty())
5a914348 1803 export_annotations(annotations);
be843692
SA
1804}
1805
74bf6666
SA
1806void DecodeTrace::on_animation_timer()
1807{
1808 bool animation_finished = true;
1809
6a26fc44 1810 for (DecodeTraceRow& r : rows_) {
74bf6666
SA
1811 if (!(r.expanding || r.collapsing))
1812 continue;
1813
1814 unsigned int height_delta = r.expanded_height - default_row_height_;
1815
1816 if (r.expanding) {
1817 if (r.height < r.expanded_height) {
1818 r.anim_height += height_delta / (float)AnimationDurationInTicks;
8b24af15 1819 r.height = min((int)r.anim_height, (int)r.expanded_height);
74bf6666
SA
1820 r.anim_shape += ArrowSize / (float)AnimationDurationInTicks;
1821 animation_finished = false;
c6b4e925
SA
1822 } else
1823 set_row_expanded(&r);
74bf6666
SA
1824 }
1825
1826 if (r.collapsing) {
1827 if (r.height > default_row_height_) {
1828 r.anim_height -= height_delta / (float)AnimationDurationInTicks;
8b24af15 1829 r.height = max((int)r.anim_height, (int)0);
74bf6666
SA
1830 r.anim_shape -= ArrowSize / (float)AnimationDurationInTicks;
1831 animation_finished = false;
c6b4e925
SA
1832 } else
1833 set_row_collapsed(&r);
74bf6666
SA
1834 }
1835
1836 // The expansion marker shape switches between
1837 // 0/-A, A/0, 0/A (default state; anim_shape=0) and
1838 // 0/ 0, A/A, 2A/0 (expanded state; anim_shape=ArrowSize)
1839
1840 r.expand_marker_shape.setPoint(0, 0, -ArrowSize + r.anim_shape);
1841 r.expand_marker_shape.setPoint(1, ArrowSize, r.anim_shape);
1842 r.expand_marker_shape.setPoint(2, 2*r.anim_shape, ArrowSize - r.anim_shape);
1843 }
1844
1845 if (animation_finished)
1846 animation_timer_.stop();
1847
c6b4e925
SA
1848 owner_->extents_changed(false, true);
1849 owner_->row_item_appearance_changed(false, true);
74bf6666
SA
1850}
1851
440bdf50
SA
1852void DecodeTrace::on_hide_hidden_rows()
1853{
1854 // Make all hidden traces invisible again unless the user is hovering over a row name
1855 bool any_highlighted = false;
1856
1857 for (DecodeTraceRow& r : rows_)
1858 if (r.expand_marker_highlighted)
1859 any_highlighted = true;
1860
1861 if (!any_highlighted) {
1862 show_hidden_rows_ = false;
1863
440bdf50
SA
1864 owner_->extents_changed(false, true);
1865 owner_->row_item_appearance_changed(false, true);
1866 }
1867}
1868
1573bf16 1869} // namespace trace
f4e57597 1870} // namespace views
55d3603d 1871} // namespace pv