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