]> sigrok.org Git - pulseview.git/blame - pv/views/trace/decodetrace.cpp
DecodeTrace: Add annotation class to hover text
[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
641574bc 85const QColor DecodeTrace::NoDecodeColor = QColor(0x88, 0x8A, 0x85);
81dc0221 86const QColor DecodeTrace::ExpandMarkerWarnColor = QColor(0xFF, 0xA5, 0x00); // QColorConstants::Svg::orange
440bdf50 87const QColor DecodeTrace::ExpandMarkerHiddenColor = QColor(0x69, 0x69, 0x69); // QColorConstants::Svg::dimgray
c6b4e925
SA
88const uint8_t DecodeTrace::ExpansionAreaHeaderAlpha = 10 * 255 / 100;
89const uint8_t DecodeTrace::ExpansionAreaAlpha = 5 * 255 / 100;
ad50ac1a 90
74bf6666 91const int DecodeTrace::ArrowSize = 6;
06e810f2 92const double DecodeTrace::EndCapWidth = 5;
74bf6666 93const int DecodeTrace::RowTitleMargin = 7;
06e810f2
JH
94const int DecodeTrace::DrawPadding = 100;
95
5b6ae103 96const int DecodeTrace::MaxTraceUpdateRate = 1; // No more than 1 Hz
440bdf50
SA
97const int DecodeTrace::AnimationDurationInTicks = 7;
98const int DecodeTrace::HiddenRowHideDelay = 1000; // 1 second
adf9e022
SA
99
100/**
101 * Helper function for forceUpdate()
102 */
103void invalidateLayout(QLayout* layout)
104{
105 // Recompute the given layout and all its child layouts recursively
106 for (int i = 0; i < layout->count(); i++) {
107 QLayoutItem *item = layout->itemAt(i);
108
109 if (item->layout())
110 invalidateLayout(item->layout());
111 else
112 item->invalidate();
113 }
114
115 layout->invalidate();
116 layout->activate();
117}
118
119void forceUpdate(QWidget* widget)
120{
121 // Update all child widgets recursively
122 for (QObject* child : widget->children())
123 if (child->isWidgetType())
124 forceUpdate((QWidget*)child);
125
126 // Invalidate the layout of the widget itself
127 if (widget->layout())
128 invalidateLayout(widget->layout());
129}
130
131
132ContainerWidget::ContainerWidget(QWidget *parent) :
133 QWidget(parent)
134{
135}
136
137void ContainerWidget::resizeEvent(QResizeEvent* event)
138{
139 QWidget::resizeEvent(event);
140
141 widgetResized(this);
142}
143
144
2b81ae46 145DecodeTrace::DecodeTrace(pv::Session &session,
bb7dd726 146 shared_ptr<data::SignalBase> signalbase, int index) :
bf0edd2b 147 Trace(signalbase),
8dbbc7f0 148 session_(session),
440bdf50 149 show_hidden_rows_(false),
8dbbc7f0 150 delete_mapper_(this),
c6b4e925
SA
151 show_hide_mapper_(this),
152 row_show_hide_mapper_(this)
55d3603d 153{
ad908057 154 decode_signal_ = dynamic_pointer_cast<data::DecodeSignal>(base_);
e0fc5810 155
ab185f78
SA
156 GlobalSettings settings;
157 always_show_all_rows_ = settings.value(GlobalSettings::Key_Dec_AlwaysShowAllRows).toBool();
158
159 GlobalSettings::add_change_handler(this);
160
752281db
SA
161 // Determine shortest string we want to see displayed in full
162 QFontMetrics m(QApplication::font());
ae726b70
VO
163 // e.g. two hex characters
164 min_useful_label_width_ = util::text_width(m, "XX");
752281db 165
84002113 166 default_row_height_ = (ViewItemPaintParams::text_height() * 6) / 4;
9741d6e0 167 annotation_height_ = (ViewItemPaintParams::text_height() * 5) / 4;
84002113 168
9ba13f5e
SA
169 // For the base color, we want to start at a very different color for
170 // every decoder stack, so multiply the index with a number that is
171 // rather close to 180 degrees of the color circle but not a dividend of 360
172 // Note: The offset equals the color of the first annotation
173 QColor color;
174 const int h = (120 + 160 * index) % 360;
ae30ff42
SA
175 const int s = DECODE_COLOR_SATURATION;
176 const int v = DECODE_COLOR_VALUE;
9ba13f5e
SA
177 color.setHsv(h, s, v);
178 base_->set_color(color);
9cef9567 179
ae30ff42
SA
180 connect(decode_signal_.get(), SIGNAL(color_changed(QColor)),
181 this, SLOT(on_color_changed(QColor)));
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 338
f6a93932 339 const QString err = base_->get_error_message();
1ae18301 340 if (!err.isEmpty())
f6a93932 341 paint_error(p, 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) {
f14dee77
SA
615 const pair<int, int> extents = v_extents();
616 const int trace_top = get_visual_y() + extents.first;
617 const int trace_btm = get_visual_y() + extents.second;
618
41293691 619 if ((hp.x() > 0) && (hp.x() < (int)(ArrowSize + 3 + hover_row->title_width)) &&
f14dee77 620 (hp.y() > trace_top) && (hp.y() < trace_btm)) {
440bdf50 621
74bf6666 622 hover_row->expand_marker_highlighted = true;
440bdf50
SA
623 show_hidden_rows_ = true;
624 delayed_hidden_row_hider_.start();
625 }
74bf6666
SA
626 }
627
628 // Tooltip handling
629 if (hp.x() > 0) {
630 QString ann = get_annotation_at_point(hp);
631
632 if (!ann.isEmpty()) {
633 QFontMetrics m(QToolTip::font());
634 const QRect text_size = m.boundingRect(QRect(), 0, ann);
635
636 // This is OS-specific and unfortunately we can't query it, so
637 // use an approximation to at least try to minimize the error.
638 const int padding = default_row_height_ + 8;
639
640 // Make sure the tool tip doesn't overlap with the mouse cursor.
641 // If it did, the tool tip would constantly hide and re-appear.
642 // We also push it up by one row so that it appears above the
643 // decode trace, not below.
644 QPoint p = hp;
645 p.setX(hp.x() - (text_size.width() / 2) - padding);
646
647 p.setY(get_row_y(hover_row) - default_row_height_ -
648 text_size.height() - padding);
649
650 const View *const view = owner_->view();
651 assert(view);
652 QToolTip::showText(view->viewport()->mapToGlobal(p), ann);
653
654 } else
655 QToolTip::hideText();
656
657 } else
658 QToolTip::hideText();
659}
660
661void DecodeTrace::mouse_left_press_event(const QMouseEvent* event)
662{
adf9e022
SA
663 // Update container widths which depend on the scrollarea's current width
664 update_expanded_rows();
665
74bf6666 666 // Handle row expansion marker
6a26fc44 667 for (DecodeTraceRow& r : rows_) {
74bf6666
SA
668 if (!r.expand_marker_highlighted)
669 continue;
670
671 unsigned int y = get_row_y(&r);
41293691 672 if ((event->x() > 0) && (event->x() <= (int)(ArrowSize + 3 + r.title_width)) &&
74bf6666
SA
673 (event->y() > (int)(y - (default_row_height_ / 2))) &&
674 (event->y() <= (int)(y + (default_row_height_ / 2)))) {
675
676 if (r.expanded) {
677 r.collapsing = true;
678 r.expanded = false;
679 r.anim_shape = ArrowSize;
680 } else {
681 r.expanding = true;
682 r.anim_shape = 0;
adf9e022
SA
683
684 // Force geometry update of the widget container to get
685 // an up-to-date height (which also depends on the width)
686 forceUpdate(r.container);
687
dca3cbee 688 r.container->setVisible(true);
adf9e022 689 r.expanded_height = 2 * default_row_height_ + r.container->sizeHint().height();
74bf6666
SA
690 }
691
692 r.animation_step = 0;
693 r.anim_height = r.height;
dca3cbee 694
74bf6666
SA
695 animation_timer_.start();
696 }
697 }
698}
699
462941e2 700void DecodeTrace::draw_annotations(deque<const Annotation*>& annotations,
bdb97140 701 QPainter &p, const ViewItemPaintParams &pp, int y, const DecodeTraceRow& row)
50631798 702{
1e948182 703 uint32_t block_class = 0;
20d90d22 704 bool block_class_uniform = true;
5c48ce32 705 qreal block_start = 0;
20d90d22
SA
706 int block_ann_count = 0;
707
5a9146ac 708 const Annotation* prev_ann;
5c48ce32 709 qreal prev_end = INT_MIN;
20d90d22 710
5c48ce32 711 qreal a_end;
50631798
SA
712
713 double samples_per_pixel, pixels_offset;
714 tie(pixels_offset, samples_per_pixel) =
715 get_pixels_offset_samples_per_pixel();
716
717 // Gather all annotations that form a visual "block" and draw them as such
5a9146ac 718 for (const Annotation* a : annotations) {
50631798 719
5a9146ac
SA
720 const qreal abs_a_start = a->start_sample() / samples_per_pixel;
721 const qreal abs_a_end = a->end_sample() / samples_per_pixel;
bdc2a99b 722
5c48ce32 723 const qreal a_start = abs_a_start - pixels_offset;
20d90d22
SA
724 a_end = abs_a_end - pixels_offset;
725
5c48ce32
SA
726 const qreal a_width = a_end - a_start;
727 const qreal delta = a_end - prev_end;
bdc2a99b
SA
728
729 bool a_is_separate = false;
730
731 // Annotation wider than the threshold for a useful label width?
752281db 732 if (a_width >= min_useful_label_width_) {
462941e2 733 for (const QString &ann_text : *(a->annotations())) {
5c48ce32 734 const qreal w = p.boundingRect(QRectF(), 0, ann_text).width();
bdc2a99b
SA
735 // Annotation wide enough to fit a label? Don't put it in a block then
736 if (w <= a_width) {
737 a_is_separate = true;
738 break;
739 }
740 }
741 }
50631798 742
bdc2a99b
SA
743 // Were the previous and this annotation more than a pixel apart?
744 if ((abs(delta) > 1) || a_is_separate) {
745 // Block was broken, draw annotations that form the current block
20d90d22 746 if (block_ann_count == 1)
bdb97140 747 draw_annotation(prev_ann, p, pp, y, row);
20d90d22
SA
748 else if (block_ann_count > 0)
749 draw_annotation_block(block_start, prev_end, block_class,
bdb97140 750 block_class_uniform, p, y, row);
50631798 751
20d90d22 752 block_ann_count = 0;
50631798
SA
753 }
754
bdc2a99b 755 if (a_is_separate) {
bdb97140 756 draw_annotation(a, p, pp, y, row);
bdc2a99b 757 // Next annotation must start a new block. delta will be > 1
20d90d22
SA
758 // because we set prev_end to INT_MIN but that's okay since
759 // block_ann_count will be 0 and nothing will be drawn
760 prev_end = INT_MIN;
5c48ce32 761 block_ann_count = 0;
bdc2a99b 762 } else {
20d90d22 763 prev_end = a_end;
5a9146ac 764 prev_ann = a;
20d90d22
SA
765
766 if (block_ann_count == 0) {
767 block_start = a_start;
462941e2 768 block_class = a->ann_class_id();
20d90d22
SA
769 block_class_uniform = true;
770 } else
462941e2 771 if (a->ann_class_id() != block_class)
20d90d22
SA
772 block_class_uniform = false;
773
774 block_ann_count++;
bdc2a99b 775 }
50631798
SA
776 }
777
20d90d22 778 if (block_ann_count == 1)
bdb97140 779 draw_annotation(prev_ann, p, pp, y, row);
20d90d22
SA
780 else if (block_ann_count > 0)
781 draw_annotation_block(block_start, prev_end, block_class,
bdb97140 782 block_class_uniform, p, y, row);
50631798
SA
783}
784
bdb97140
SA
785void DecodeTrace::draw_annotation(const Annotation* a, QPainter &p,
786 const ViewItemPaintParams &pp, int y, const DecodeTraceRow& row) const
06e810f2 787{
53e35b2d
JH
788 double samples_per_pixel, pixels_offset;
789 tie(pixels_offset, samples_per_pixel) =
790 get_pixels_offset_samples_per_pixel();
7f8517f6 791
bdb97140 792 const double start = a->start_sample() / samples_per_pixel - pixels_offset;
5a9146ac 793 const double end = a->end_sample() / samples_per_pixel - pixels_offset;
287d607f 794
ae30ff42
SA
795 p.setPen(a->dark_color());
796 p.setBrush(a->color());
06e810f2 797
462941e2 798 if ((start > (pp.right() + DrawPadding)) || (end < (pp.left() - DrawPadding)))
06e810f2
JH
799 return;
800
5a9146ac 801 if (a->start_sample() == a->end_sample())
bdb97140 802 draw_instant(a, p, start, y);
06e810f2 803 else
bdb97140 804 draw_range(a, p, start, end, y, pp, row.title_width);
06e810f2
JH
805}
806
5c48ce32 807void DecodeTrace::draw_annotation_block(qreal start, qreal end,
1e948182 808 uint32_t ann_class, bool use_ann_format, QPainter &p, int y,
bdb97140 809 const DecodeTraceRow& row) const
50631798 810{
bdb97140
SA
811 const double top = y + .5 - annotation_height_ / 2;
812 const double bottom = y + .5 + annotation_height_ / 2;
462941e2 813 const double width = end - start;
3082ee93 814
20d90d22
SA
815 // If all annotations in this block are of the same type, we can use the
816 // one format that all of these annotations have. Otherwise, we should use
817 // a neutral color (i.e. gray)
818 if (use_ann_format) {
ae30ff42
SA
819 p.setPen(row.decode_row->get_dark_class_color(ann_class));
820 p.setBrush(QBrush(row.decode_row->get_class_color(ann_class), Qt::Dense4Pattern));
20d90d22 821 } else {
462941e2 822 p.setPen(QColor(Qt::darkGray));
20d90d22
SA
823 p.setBrush(QBrush(Qt::gray, Qt::Dense4Pattern));
824 }
825
462941e2
SA
826 if (width <= 1)
827 p.drawLine(QPointF(start, top), QPointF(start, bottom));
828 else {
829 const QRectF rect(start, top, width, bottom - top);
830 const int r = annotation_height_ / 4;
831 p.drawRoundedRect(rect, r, r);
832 }
50631798
SA
833}
834
bdb97140 835void DecodeTrace::draw_instant(const Annotation* a, QPainter &p, qreal x, int y) const
06e810f2 836{
462941e2
SA
837 const QString text = a->annotations()->empty() ?
838 QString() : a->annotations()->back();
5c48ce32 839 const qreal w = min((qreal)p.boundingRect(QRectF(), 0, text).width(),
bdb97140
SA
840 0.0) + annotation_height_;
841 const QRectF rect(x - w / 2, y - annotation_height_ / 2, w, annotation_height_);
06e810f2 842
bdb97140 843 p.drawRoundedRect(rect, annotation_height_ / 2, annotation_height_ / 2);
06e810f2 844
2a56e448 845 p.setPen(Qt::black);
06e810f2
JH
846 p.drawText(rect, Qt::AlignCenter | Qt::AlignVCenter, text);
847}
848
bdb97140 849void DecodeTrace::draw_range(const Annotation* a, QPainter &p,
5a9146ac 850 qreal start, qreal end, int y, const ViewItemPaintParams &pp,
f765c3db 851 int row_title_width) const
06e810f2 852{
bdb97140
SA
853 const qreal top = y + .5 - annotation_height_ / 2;
854 const qreal bottom = y + .5 + annotation_height_ / 2;
462941e2 855 const vector<QString>* annotations = a->annotations();
06e810f2 856
06e810f2 857 // If the two ends are within 1 pixel, draw a vertical line
2ad82c2e 858 if (start + 1.0 > end) {
06e810f2
JH
859 p.drawLine(QPointF(start, top), QPointF(start, bottom));
860 return;
861 }
862
5c48ce32 863 const qreal cap_width = min((end - start) / 4, EndCapWidth);
06e810f2
JH
864
865 QPointF pts[] = {
866 QPointF(start, y + .5f),
867 QPointF(start + cap_width, top),
868 QPointF(end - cap_width, top),
869 QPointF(end, y + .5f),
870 QPointF(end - cap_width, bottom),
871 QPointF(start + cap_width, bottom)
872 };
873
874 p.drawConvexPolygon(pts, countof(pts));
875
462941e2 876 if (annotations->empty())
06e810f2
JH
877 return;
878
7352be72
SA
879 const int ann_start = start + cap_width;
880 const int ann_end = end - cap_width;
881
74bf6666 882 const int real_start = max(ann_start, pp.left() + ArrowSize + row_title_width);
6f925ba9 883 const int real_end = min(ann_end, pp.right());
7352be72
SA
884 const int real_width = real_end - real_start;
885
bdb97140 886 QRectF rect(real_start, y - annotation_height_ / 2, real_width, annotation_height_);
0f290e9b
JH
887 if (rect.width() <= 4)
888 return;
889
2a56e448 890 p.setPen(Qt::black);
06e810f2
JH
891
892 // Try to find an annotation that will fit
893 QString best_annotation;
894 int best_width = 0;
895
462941e2 896 for (const QString &s : *annotations) {
5a9146ac 897 const int w = p.boundingRect(QRectF(), 0, s).width();
06e810f2 898 if (w <= rect.width() && w > best_width)
5a9146ac 899 best_annotation = s, best_width = w;
06e810f2
JH
900 }
901
902 if (best_annotation.isEmpty())
462941e2 903 best_annotation = annotations->back();
06e810f2
JH
904
905 // If not ellide the last in the list
906 p.drawText(rect, Qt::AlignCenter, p.fontMetrics().elidedText(
907 best_annotation, Qt::ElideRight, rect.width()));
908}
909
bdb97140 910void DecodeTrace::draw_unresolved_period(QPainter &p, int left, int right) const
5dfeb70f 911{
53e35b2d
JH
912 double samples_per_pixel, pixels_offset;
913
5ecf957f 914 const int64_t sample_count = decode_signal_->get_working_sample_count(current_segment_);
5dfeb70f
JH
915 if (sample_count == 0)
916 return;
917
b82908ab 918 const int64_t samples_decoded = decode_signal_->get_decoded_sample_count(current_segment_, true);
5dfeb70f
JH
919 if (sample_count == samples_decoded)
920 return;
921
be9e7b4b 922 const int y = get_visual_y();
7f8517f6 923
ff83d980 924 tie(pixels_offset, samples_per_pixel) = get_pixels_offset_samples_per_pixel();
7f8517f6 925
5dfeb70f
JH
926 const double start = max(samples_decoded /
927 samples_per_pixel - pixels_offset, left - 1.0);
928 const double end = min(sample_count / samples_per_pixel -
929 pixels_offset, right + 1.0);
bdb97140
SA
930 const QRectF no_decode_rect(start, y - (annotation_height_ / 2) - 0.5,
931 end - start, annotation_height_);
5dfeb70f
JH
932
933 p.setPen(QPen(Qt::NoPen));
934 p.setBrush(Qt::white);
935 p.drawRect(no_decode_rect);
936
641574bc
SA
937 p.setPen(NoDecodeColor);
938 p.setBrush(QBrush(NoDecodeColor, Qt::Dense6Pattern));
5dfeb70f
JH
939 p.drawRect(no_decode_rect);
940}
941
53e35b2d 942pair<double, double> DecodeTrace::get_pixels_offset_samples_per_pixel() const
7f8517f6 943{
8dbbc7f0 944 assert(owner_);
7f8517f6 945
8dbbc7f0 946 const View *view = owner_->view();
eae6e30a
JH
947 assert(view);
948
949 const double scale = view->scale();
7f8517f6
SA
950 assert(scale > 0);
951
53e35b2d 952 const double pixels_offset =
ff83d980 953 ((view->offset() - decode_signal_->start_time()) / scale).convert_to<double>();
7f8517f6 954
85125b0f 955 double samplerate = decode_signal_->get_samplerate();
7f8517f6
SA
956
957 // Show sample rate as 1Hz when it is unknown
958 if (samplerate == 0.0)
959 samplerate = 1.0;
960
53e35b2d 961 return make_pair(pixels_offset, samplerate * scale);
7f8517f6
SA
962}
963
67fb15bf 964pair<uint64_t, uint64_t> DecodeTrace::get_view_sample_range(
db1bf6bf 965 int x_start, int x_end) const
7f8517f6 966{
53e35b2d
JH
967 double samples_per_pixel, pixels_offset;
968 tie(pixels_offset, samples_per_pixel) =
969 get_pixels_offset_samples_per_pixel();
7f8517f6 970
db1bf6bf
JH
971 const uint64_t start = (uint64_t)max(
972 (x_start + pixels_offset) * samples_per_pixel, 0.0);
973 const uint64_t end = (uint64_t)max(
974 (x_end + pixels_offset) * samples_per_pixel, 0.0);
7f8517f6
SA
975
976 return make_pair(start, end);
977}
978
6a26fc44 979unsigned int DecodeTrace::get_row_y(const DecodeTraceRow* row) const
e2f90c50 980{
84002113 981 assert(row);
e2f90c50 982
84002113 983 unsigned int y = get_visual_y();
99029fda 984
6a26fc44 985 for (const DecodeTraceRow& r : rows_) {
9741d6e0
SA
986 if (!r.currently_visible)
987 continue;
988
989 if (row->decode_row == r.decode_row)
84002113 990 break;
9741d6e0
SA
991 else
992 y += r.height;
993 }
99029fda 994
84002113
SA
995 return y;
996}
99029fda 997
6a26fc44 998DecodeTraceRow* DecodeTrace::get_row_at_point(const QPoint &point)
84002113 999{
9741d6e0 1000 int y = get_visual_y() - (default_row_height_ / 2);
84002113 1001
6a26fc44 1002 for (DecodeTraceRow& r : rows_) {
84002113
SA
1003 if (!r.currently_visible)
1004 continue;
1005
1006 if ((point.y() >= y) && (point.y() < (int)(y + r.height)))
1007 return &r;
1008
1009 y += r.height;
1010 }
e2f90c50 1011
84002113 1012 return nullptr;
e2f90c50
SA
1013}
1014
117cdea3 1015const QString DecodeTrace::get_annotation_at_point(const QPoint &point)
e2f90c50 1016{
117cdea3
JH
1017 if (!enabled())
1018 return QString();
e2f90c50 1019
117cdea3 1020 const pair<uint64_t, uint64_t> sample_range =
67fb15bf 1021 get_view_sample_range(point.x(), point.x() + 1);
6a26fc44 1022 const DecodeTraceRow* r = get_row_at_point(point);
84002113
SA
1023
1024 if (!r)
117cdea3 1025 return QString();
e2f90c50 1026
9741d6e0
SA
1027 if (point.y() > (int)(get_row_y(r) + (annotation_height_ / 2)))
1028 return QString();
1029
462941e2 1030 deque<const Annotation*> annotations;
e2f90c50 1031
84002113 1032 decode_signal_->get_annotation_subset(annotations, r->decode_row,
72435789 1033 current_segment_, sample_range.first, sample_range.second);
e2f90c50 1034
c583ea05
SA
1035 const Annotation* a = annotations.empty() ? nullptr : annotations[0];
1036
1037 // Create a string of the format "CLASS: VALUE"
1038 QString s;
1039 if (a) {
1040 if (!a->ann_class_description().isEmpty())
1041 s = a->ann_class_description() + ": ";
1042 s += a->annotations()->front();
1043 }
1044
1045 return s;
e2f90c50
SA
1046}
1047
5a9146ac
SA
1048void DecodeTrace::create_decoder_form(int index, shared_ptr<Decoder> &dec,
1049 QWidget *parent, QFormLayout *form)
7491a29f 1050{
1cc1c8de 1051 GlobalSettings settings;
7491a29f
JH
1052
1053 assert(dec);
6a26fc44 1054 const srd_decoder *const decoder = dec->get_srd_decoder();
7491a29f
JH
1055 assert(decoder);
1056
ff59fa2c
SA
1057 const bool decoder_deletable = index > 0;
1058
204bae45 1059 pv::widgets::DecoderGroupBox *const group =
27e8df22 1060 new pv::widgets::DecoderGroupBox(
580b4f25
UH
1061 QString::fromUtf8(decoder->name),
1062 tr("%1:\n%2").arg(QString::fromUtf8(decoder->longname),
1063 QString::fromUtf8(decoder->desc)),
1064 nullptr, decoder_deletable);
5d3ca591 1065 group->set_decoder_visible(dec->visible());
613d097c 1066
ff59fa2c
SA
1067 if (decoder_deletable) {
1068 delete_mapper_.setMapping(group, index);
1069 connect(group, SIGNAL(delete_decoder()), &delete_mapper_, SLOT(map()));
1070 }
613d097c 1071
8dbbc7f0 1072 show_hide_mapper_.setMapping(group, index);
dd048a7e 1073 connect(group, SIGNAL(show_hide_decoder()),
8dbbc7f0 1074 &show_hide_mapper_, SLOT(map()));
dd048a7e 1075
204bae45
JH
1076 QFormLayout *const decoder_form = new QFormLayout;
1077 group->add_layout(decoder_form);
7491a29f 1078
47747218 1079 const vector<DecodeChannel> channels = decode_signal_->get_channels();
407c9ebe 1080
9f97b357 1081 // Add the channels
7a0d99e6 1082 for (const DecodeChannel& ch : channels) {
9f97b357
SA
1083 // Ignore channels not part of the decoder we create the form for
1084 if (ch.decoder_ != dec)
1085 continue;
407c9ebe 1086
9f97b357
SA
1087 QComboBox *const combo = create_channel_selector(parent, &ch);
1088 QComboBox *const combo_init_state = create_channel_selector_init_state(parent, &ch);
407c9ebe 1089
9f97b357
SA
1090 channel_id_map_[combo] = ch.id;
1091 init_state_map_[combo_init_state] = ch.id;
407c9ebe 1092
7491a29f 1093 connect(combo, SIGNAL(currentIndexChanged(int)),
6ac6242b 1094 this, SLOT(on_channel_selected(int)));
9f97b357
SA
1095 connect(combo_init_state, SIGNAL(currentIndexChanged(int)),
1096 this, SLOT(on_init_state_changed(int)));
407c9ebe
UH
1097
1098 QHBoxLayout *const hlayout = new QHBoxLayout;
1099 hlayout->addWidget(combo);
9f97b357 1100 hlayout->addWidget(combo_init_state);
407c9ebe 1101
1cc1c8de 1102 if (!settings.value(GlobalSettings::Key_Dec_InitialStateConfigurable).toBool())
9f97b357 1103 combo_init_state->hide();
7491a29f 1104
9f97b357
SA
1105 const QString required_flag = ch.is_optional ? QString() : QString("*");
1106 decoder_form->addRow(tr("<b>%1</b> (%2) %3")
1107 .arg(ch.name, ch.desc, required_flag), hlayout);
7491a29f
JH
1108 }
1109
1110 // Add the options
3cc9ad7b 1111 shared_ptr<binding::Decoder> binding(
946b52e1 1112 new binding::Decoder(decode_signal_, dec));
204bae45 1113 binding->add_properties_to_form(decoder_form, true);
7491a29f 1114
8dbbc7f0 1115 bindings_.push_back(binding);
204bae45
JH
1116
1117 form->addRow(group);
8dbbc7f0 1118 decoder_forms_.push_back(group);
7491a29f
JH
1119}
1120
9f97b357 1121QComboBox* DecodeTrace::create_channel_selector(QWidget *parent, const DecodeChannel *ch)
4e5a4405 1122{
47e9e7bb 1123 const auto sigs(session_.signalbases());
78b0af3e 1124
9f97b357 1125 // Sort signals in natural order
47e9e7bb 1126 vector< shared_ptr<data::SignalBase> > sig_list(sigs.begin(), sigs.end());
6f925ba9 1127 sort(sig_list.begin(), sig_list.end(),
47e9e7bb
SA
1128 [](const shared_ptr<data::SignalBase> &a,
1129 const shared_ptr<data::SignalBase> &b) {
1130 return strnatcasecmp(a->name().toStdString(),
1131 b->name().toStdString()) < 0; });
4e5a4405 1132
4e5a4405
JH
1133 QComboBox *selector = new QComboBox(parent);
1134
009fc9ae 1135 selector->addItem("-", QVariant::fromValue((void*)nullptr));
4e5a4405 1136
9f97b357 1137 if (!ch->assigned_signal)
4e5a4405
JH
1138 selector->setCurrentIndex(0);
1139
47e9e7bb
SA
1140 for (const shared_ptr<data::SignalBase> &b : sig_list) {
1141 assert(b);
79c4a9c8 1142 if (b->logic_data() && b->enabled()) {
cf1541a1 1143 selector->addItem(b->name(), QVariant::fromValue(b));
5da5d081 1144
cf1541a1 1145 if (ch->assigned_signal == b)
9f97b357 1146 selector->setCurrentIndex(selector->count() - 1);
4e5a4405
JH
1147 }
1148 }
1149
1150 return selector;
1151}
1152
9f97b357
SA
1153QComboBox* DecodeTrace::create_channel_selector_init_state(QWidget *parent,
1154 const DecodeChannel *ch)
407c9ebe
UH
1155{
1156 QComboBox *selector = new QComboBox(parent);
1157
009fc9ae
SA
1158 selector->addItem("0", QVariant::fromValue((int)SRD_INITIAL_PIN_LOW));
1159 selector->addItem("1", QVariant::fromValue((int)SRD_INITIAL_PIN_HIGH));
1160 selector->addItem("X", QVariant::fromValue((int)SRD_INITIAL_PIN_SAME_AS_SAMPLE0));
407c9ebe 1161
9f97b357 1162 selector->setCurrentIndex(ch->initial_pin_state);
407c9ebe
UH
1163
1164 selector->setToolTip("Initial (assumed) pin value before the first sample");
1165
1166 return selector;
1167}
1168
462941e2 1169void DecodeTrace::export_annotations(deque<const Annotation*>& annotations) const
5a914348 1170{
5a914348
SA
1171 GlobalSettings settings;
1172 const QString dir = settings.value("MainWindow/SaveDirectory").toString();
1173
1174 const QString file_name = QFileDialog::getSaveFileName(
1175 owner_->view(), tr("Export annotations"), dir, tr("Text Files (*.txt);;All Files (*)"));
1176
1177 if (file_name.isEmpty())
1178 return;
1179
1180 QString format = settings.value(GlobalSettings::Key_Dec_ExportFormat).toString();
1181 const QString quote = format.contains("%q") ? "\"" : "";
1182 format = format.remove("%q");
1183
761f8302
SA
1184 const bool has_sample_range = format.contains("%s");
1185 const bool has_row_name = format.contains("%r");
1186 const bool has_dec_name = format.contains("%d");
1187 const bool has_class_name = format.contains("%c");
1188 const bool has_first_ann_text = format.contains("%1");
1189 const bool has_all_ann_text = format.contains("%a");
1190
5a914348
SA
1191 QFile file(file_name);
1192 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
1193 QTextStream out_stream(&file);
1194
462941e2 1195 for (const Annotation* ann : annotations) {
761f8302 1196 QString out_text = format;
5a914348 1197
761f8302
SA
1198 if (has_sample_range) {
1199 const QString sample_range = QString("%1-%2") \
1200 .arg(QString::number(ann->start_sample()), QString::number(ann->end_sample()));
1201 out_text = out_text.replace("%s", sample_range);
1202 }
5a914348 1203
761f8302
SA
1204 if (has_dec_name)
1205 out_text = out_text.replace("%d",
1206 quote + QString::fromUtf8(ann->row()->decoder()->name()) + quote);
5a914348 1207
761f8302
SA
1208 if (has_row_name) {
1209 const QString row_name = quote + ann->row()->description() + quote;
1210 out_text = out_text.replace("%r", row_name);
1211 }
1212
1213 if (has_class_name) {
1214 const QString class_name = quote + ann->ann_class_name() + quote;
1215 out_text = out_text.replace("%c", class_name);
1216 }
1217
1218 if (has_first_ann_text) {
1219 const QString first_ann_text = quote + ann->annotations()->front() + quote;
1220 out_text = out_text.replace("%1", first_ann_text);
1221 }
1222
1223 if (has_all_ann_text) {
1224 QString all_ann_text;
1225 for (const QString &s : *(ann->annotations()))
1226 all_ann_text = all_ann_text + quote + s + quote + ",";
1227 all_ann_text.chop(1);
1228
1229 out_text = out_text.replace("%a", all_ann_text);
1230 }
5a914348 1231
5a914348
SA
1232 out_stream << out_text << '\n';
1233 }
1234
1235 if (out_stream.status() == QTextStream::Ok)
1236 return;
1237 }
1238
1239 QMessageBox msg(owner_->view());
970fca0d 1240 msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
5a914348
SA
1241 msg.setStandardButtons(QMessageBox::Ok);
1242 msg.setIcon(QMessageBox::Warning);
1243 msg.exec();
1244}
1245
6a26fc44 1246void DecodeTrace::initialize_row_widgets(DecodeTraceRow* r, unsigned int row_id)
84002113 1247{
adf9e022 1248 // Set colors and fixed widths
84002113
SA
1249 QFontMetrics m(QApplication::font());
1250
c6b4e925
SA
1251 QPalette header_palette = owner_->view()->palette();
1252 QPalette selector_palette = owner_->view()->palette();
1253
1254 if (GlobalSettings::current_theme_is_dark()) {
1255 header_palette.setColor(QPalette::Background,
1256 QColor(255, 255, 255, ExpansionAreaHeaderAlpha));
1257 selector_palette.setColor(QPalette::Background,
1258 QColor(255, 255, 255, ExpansionAreaAlpha));
1259 } else {
1260 header_palette.setColor(QPalette::Background,
1261 QColor(0, 0, 0, ExpansionAreaHeaderAlpha));
1262 selector_palette.setColor(QPalette::Background,
1263 QColor(0, 0, 0, ExpansionAreaAlpha));
1264 }
1265
6a26fc44
SA
1266 const int w = m.boundingRect(r->decode_row->title()).width() + RowTitleMargin;
1267 r->title_width = w;
1268
adf9e022
SA
1269 // Set up top-level container
1270 connect(r->container, SIGNAL(widgetResized(QWidget*)),
1271 this, SLOT(on_row_container_resized(QWidget*)));
6a26fc44
SA
1272
1273 QVBoxLayout* vlayout = new QVBoxLayout();
1274 r->container->setLayout(vlayout);
1275
65bde9b3 1276 // Add header container
6a26fc44
SA
1277 vlayout->addWidget(r->header_container);
1278 vlayout->setContentsMargins(0, 0, 0, 0);
1279 vlayout->setSpacing(0);
65bde9b3 1280 QHBoxLayout* header_container_layout = new QHBoxLayout();
6a26fc44
SA
1281 r->header_container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
1282 r->header_container->setMinimumSize(0, default_row_height_);
65bde9b3
SA
1283 r->header_container->setLayout(header_container_layout);
1284 r->header_container->layout()->setContentsMargins(10, 2, 10, 2);
6a26fc44
SA
1285
1286 r->header_container->setAutoFillBackground(true);
1287 r->header_container->setPalette(header_palette);
1288
65bde9b3 1289 // Add widgets inside the header container
6a26fc44 1290 QCheckBox* cb = new QCheckBox();
9eaa8343 1291 r->row_visibility_checkbox = cb;
65bde9b3 1292 header_container_layout->addWidget(cb);
6a26fc44
SA
1293 cb->setText(tr("Show this row"));
1294 cb->setChecked(r->decode_row->visible());
1295
1296 row_show_hide_mapper_.setMapping(cb, row_id);
1297 connect(cb, SIGNAL(stateChanged(int)),
1298 &row_show_hide_mapper_, SLOT(map()));
1299
65bde9b3
SA
1300 QPushButton* btn = new QPushButton();
1301 header_container_layout->addWidget(btn);
1302 btn->setFlat(true);
1303 btn->setStyleSheet(":hover { background-color: palette(button); color: palette(button-text); border:0; }");
1304 btn->setText(tr("Show All"));
1305 btn->setProperty("decode_trace_row_ptr", QVariant::fromValue((void*)r));
1306 connect(btn, SIGNAL(clicked(bool)), this, SLOT(on_show_all_classes()));
1307
1308 btn = new QPushButton();
1309 header_container_layout->addWidget(btn);
1310 btn->setFlat(true);
1311 btn->setStyleSheet(":hover { background-color: palette(button); color: palette(button-text); border:0; }");
1312 btn->setText(tr("Hide All"));
1313 btn->setProperty("decode_trace_row_ptr", QVariant::fromValue((void*)r));
1314 connect(btn, SIGNAL(clicked(bool)), this, SLOT(on_hide_all_classes()));
1315
1316 header_container_layout->addStretch(); // To left-align the header widgets
1317
6a26fc44
SA
1318 // Add selector container
1319 vlayout->addWidget(r->selector_container);
adf9e022
SA
1320 r->selector_container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
1321 r->selector_container->setLayout(new FlowLayout(r->selector_container));
6a26fc44
SA
1322
1323 r->selector_container->setAutoFillBackground(true);
1324 r->selector_container->setPalette(selector_palette);
1325
1326 // Add all classes that can be toggled
1327 vector<AnnotationClass*> ann_classes = r->decode_row->ann_classes();
1328
1329 for (const AnnotationClass* ann_class : ann_classes) {
1330 cb = new QCheckBox();
1331 cb->setText(tr(ann_class->description));
02078aa1 1332 cb->setChecked(ann_class->visible());
6a26fc44 1333
ec4f16ff
SA
1334 int dim = ViewItemPaintParams::text_height() - 2;
1335 QPixmap pixmap(dim, dim);
ae30ff42 1336 pixmap.fill(r->decode_row->get_class_color(ann_class->id));
ec4f16ff
SA
1337 cb->setIcon(pixmap);
1338
6a26fc44 1339 r->selector_container->layout()->addWidget(cb);
65bde9b3 1340 r->selectors.push_back(cb);
6a26fc44
SA
1341
1342 cb->setProperty("ann_class_ptr", QVariant::fromValue((void*)ann_class));
81dc0221
SA
1343 cb->setProperty("decode_trace_row_ptr", QVariant::fromValue((void*)r));
1344
6a26fc44
SA
1345 class_show_hide_mapper_.setMapping(cb, cb);
1346 connect(cb, SIGNAL(stateChanged(int)),
1347 &class_show_hide_mapper_, SLOT(map()));
1348 }
1349}
1350
1351void DecodeTrace::update_rows()
1352{
a653b83d
SA
1353 if (!owner_)
1354 return;
1355
6a26fc44
SA
1356 lock_guard<mutex> lock(row_modification_mutex_);
1357
1358 for (DecodeTraceRow& r : rows_)
84002113
SA
1359 r.exists = false;
1360
c6b4e925 1361 unsigned int row_id = 0;
6a26fc44 1362 for (Row* decode_row : decode_signal_->get_rows()) {
84002113
SA
1363 // Find row in our list
1364 auto r_it = find_if(rows_.begin(), rows_.end(),
6a26fc44 1365 [&](DecodeTraceRow& r){ return r.decode_row == decode_row; });
84002113 1366
6a26fc44 1367 DecodeTraceRow* r = nullptr;
84002113
SA
1368 if (r_it == rows_.end()) {
1369 // Row doesn't exist yet, create and append it
6a26fc44 1370 DecodeTraceRow nr;
84002113 1371 nr.decode_row = decode_row;
ae30ff42 1372 nr.decode_row->set_base_color(base_->color());
84002113 1373 nr.height = default_row_height_;
c6b4e925 1374 nr.expanded_height = default_row_height_;
84002113 1375 nr.currently_visible = false;
81dc0221 1376 nr.has_hidden_classes = decode_row->has_hidden_classes();
84002113 1377 nr.expand_marker_highlighted = false;
74bf6666 1378 nr.expanding = false;
84002113 1379 nr.expanded = false;
74bf6666
SA
1380 nr.collapsing = false;
1381 nr.expand_marker_shape = default_marker_shape_;
adf9e022 1382 nr.container = new ContainerWidget(owner_->view()->scrollarea());
c6b4e925
SA
1383 nr.header_container = new QWidget(nr.container);
1384 nr.selector_container = new QWidget(nr.container);
84002113
SA
1385
1386 rows_.push_back(nr);
1387 r = &rows_.back();
6a26fc44 1388 initialize_row_widgets(r, row_id);
84002113
SA
1389 } else
1390 r = &(*r_it);
1391
1392 r->exists = true;
c6b4e925 1393 row_id++;
84002113
SA
1394 }
1395
9eaa8343
SA
1396 // If there's only one row, it must not be hidden or else it can't be un-hidden
1397 if (row_id == 1)
1398 rows_.front().row_visibility_checkbox->setEnabled(false);
1399
84002113
SA
1400 // Remove any rows that no longer exist, obeying that iterators are invalidated
1401 bool any_exists;
1402 do {
1403 any_exists = false;
1404
1405 for (unsigned int i = 0; i < rows_.size(); i++)
1406 if (!rows_[i].exists) {
9eaa8343
SA
1407 delete rows_[i].row_visibility_checkbox;
1408
c6b4e925
SA
1409 for (QCheckBox* cb : rows_[i].selectors)
1410 delete cb;
1411
1412 delete rows_[i].selector_container;
1413 delete rows_[i].header_container;
dca3cbee 1414 delete rows_[i].container;
c6b4e925 1415
84002113
SA
1416 rows_.erase(rows_.begin() + i);
1417 any_exists = true;
1418 break;
1419 }
1420 } while (any_exists);
1421}
1422
6a26fc44 1423void DecodeTrace::set_row_expanded(DecodeTraceRow* r)
c6b4e925
SA
1424{
1425 r->height = r->expanded_height;
1426 r->expanding = false;
1427 r->expanded = true;
1428
1429 // For details on this, see on_animation_timer()
1430 r->expand_marker_shape.setPoint(0, 0, 0);
1431 r->expand_marker_shape.setPoint(1, ArrowSize, ArrowSize);
1432 r->expand_marker_shape.setPoint(2, 2*ArrowSize, 0);
1433
1434 r->container->resize(owner_->view()->viewport()->width() - r->container->pos().x(),
1435 r->height - 2 * default_row_height_);
1436}
1437
6a26fc44 1438void DecodeTrace::set_row_collapsed(DecodeTraceRow* r)
c6b4e925
SA
1439{
1440 r->height = default_row_height_;
1441 r->collapsing = false;
1442 r->expanded = false;
1443 r->expand_marker_shape = default_marker_shape_;
1444 r->container->setVisible(false);
1445
1446 r->container->resize(owner_->view()->viewport()->width() - r->container->pos().x(),
1447 r->height - 2 * default_row_height_);
1448}
1449
1450void DecodeTrace::update_expanded_rows()
1451{
6a26fc44 1452 for (DecodeTraceRow& r : rows_) {
adf9e022
SA
1453 if (r.expanding || r.expanded)
1454 r.expanded_height = 2 * default_row_height_ + r.container->sizeHint().height();
1455
1456 if (r.expanded)
1457 r.height = r.expanded_height;
1458
1459 int x = 2 * ArrowSize;
1460 int y = get_row_y(&r) + default_row_height_;
1461 // Only update the position if it actually changes
1462 if ((x != r.container->pos().x()) || (y != r.container->pos().y()))
1463 r.container->move(x, y);
1464
1465 int w = owner_->view()->viewport()->width() - x;
1466 int h = r.height - 2 * default_row_height_;
1467 // Only update the dimension if they actually change
1468 if ((w != r.container->sizeHint().width()) || (h != r.container->sizeHint().height()))
1469 r.container->resize(w, h);
c6b4e925
SA
1470 }
1471}
1472
ab185f78
SA
1473void DecodeTrace::on_setting_changed(const QString &key, const QVariant &value)
1474{
945238ca
SA
1475 Trace::on_setting_changed(key, value);
1476
a767229e 1477 if (key == GlobalSettings::Key_Dec_AlwaysShowAllRows)
ab185f78 1478 always_show_all_rows_ = value.toBool();
ab185f78
SA
1479}
1480
ae30ff42
SA
1481void DecodeTrace::on_color_changed(const QColor &color)
1482{
1483 for (DecodeTraceRow& r : rows_)
1484 r.decode_row->set_base_color(color);
88a25978
SA
1485
1486 if (owner_)
1487 owner_->row_item_appearance_changed(false, true);
ae30ff42
SA
1488}
1489
ad908057 1490void DecodeTrace::on_new_annotations()
5b6ae103
SA
1491{
1492 if (!delayed_trace_updater_.isActive())
1493 delayed_trace_updater_.start();
1494}
1495
1496void DecodeTrace::on_delayed_trace_update()
9cef9567 1497{
8dbbc7f0 1498 if (owner_)
6e2c3c85 1499 owner_->row_item_appearance_changed(false, true);
9cef9567
JH
1500}
1501
eee3eab9
SA
1502void DecodeTrace::on_decode_reset()
1503{
84002113 1504 update_rows();
eee3eab9
SA
1505
1506 if (owner_)
1507 owner_->row_item_appearance_changed(false, true);
1508}
1509
1b56c646
SA
1510void DecodeTrace::on_decode_finished()
1511{
1512 if (owner_)
1513 owner_->row_item_appearance_changed(false, true);
1514}
1515
556259d2
SA
1516void DecodeTrace::on_pause_decode()
1517{
1518 if (decode_signal_->is_paused())
1519 decode_signal_->resume_decode();
1520 else
1521 decode_signal_->pause_decode();
1522}
1523
b9329558 1524void DecodeTrace::on_delete()
c51482b3 1525{
ad908057 1526 session_.remove_decode_signal(decode_signal_);
c51482b3
JH
1527}
1528
6ac6242b 1529void DecodeTrace::on_channel_selected(int)
4e5a4405 1530{
9f97b357
SA
1531 QComboBox *cb = qobject_cast<QComboBox*>(QObject::sender());
1532
1533 // Determine signal that was selected
cf1541a1
SA
1534 shared_ptr<data::SignalBase> signal =
1535 cb->itemData(cb->currentIndex()).value<shared_ptr<data::SignalBase>>();
9f97b357
SA
1536
1537 // Determine decode channel ID this combo box is the channel selector for
1538 const uint16_t id = channel_id_map_.at(cb);
1539
1540 decode_signal_->assign_signal(id, signal);
1541}
1542
1543void DecodeTrace::on_channels_updated()
1544{
1545 if (owner_)
1546 owner_->row_item_appearance_changed(false, true);
4e5a4405
JH
1547}
1548
9f97b357 1549void DecodeTrace::on_init_state_changed(int)
407c9ebe 1550{
9f97b357
SA
1551 QComboBox *cb = qobject_cast<QComboBox*>(QObject::sender());
1552
1553 // Determine inital pin state that was selected
1554 int init_state = cb->itemData(cb->currentIndex()).value<int>();
1555
1556 // Determine decode channel ID this combo box is the channel selector for
1557 const uint16_t id = init_state_map_.at(cb);
1558
1559 decode_signal_->set_initial_pin_state(id, init_state);
407c9ebe
UH
1560}
1561
7491a29f
JH
1562void DecodeTrace::on_stack_decoder(srd_decoder *decoder)
1563{
ad908057 1564 decode_signal_->stack_decoder(decoder);
84002113 1565 update_rows();
37fd11b1
JH
1566
1567 create_popup_form();
7491a29f
JH
1568}
1569
613d097c
JH
1570void DecodeTrace::on_delete_decoder(int index)
1571{
ad908057 1572 decode_signal_->remove_decoder(index);
84002113 1573 update_rows();
613d097c 1574
ded43869
SA
1575 owner_->extents_changed(false, true);
1576
c063290a 1577 create_popup_form();
613d097c
JH
1578}
1579
dd048a7e
JH
1580void DecodeTrace::on_show_hide_decoder(int index)
1581{
ad908057 1582 const bool state = decode_signal_->toggle_decoder_visibility(index);
dd048a7e 1583
8dbbc7f0 1584 assert(index < (int)decoder_forms_.size());
ad908057 1585 decoder_forms_[index]->set_decoder_visible(state);
dd048a7e 1586
a767229e 1587 if (!state)
ded43869 1588 owner_->extents_changed(false, true);
ded43869 1589
c6b4e925
SA
1590 owner_->row_item_appearance_changed(false, true);
1591}
1592
6a26fc44 1593void DecodeTrace::on_show_hide_row(int row_id)
c6b4e925 1594{
6a26fc44 1595 if (row_id >= (int)rows_.size())
c6b4e925
SA
1596 return;
1597
6a26fc44 1598 rows_[row_id].decode_row->set_visible(!rows_[row_id].decode_row->visible());
c6b4e925 1599
440bdf50
SA
1600 if (!rows_[row_id].decode_row->visible())
1601 set_row_collapsed(&rows_[row_id]);
1602
c6b4e925
SA
1603 owner_->extents_changed(false, true);
1604 owner_->row_item_appearance_changed(false, true);
dd048a7e
JH
1605}
1606
6a26fc44
SA
1607void DecodeTrace::on_show_hide_class(QWidget* sender)
1608{
1609 void* ann_class_ptr = sender->property("ann_class_ptr").value<void*>();
1610 assert(ann_class_ptr);
6a26fc44 1611 AnnotationClass* ann_class = (AnnotationClass*)ann_class_ptr;
81dc0221 1612
02078aa1 1613 ann_class->set_visible(!ann_class->visible());
6a26fc44 1614
81dc0221
SA
1615 void* row_ptr = sender->property("decode_trace_row_ptr").value<void*>();
1616 assert(row_ptr);
1617 DecodeTraceRow* row = (DecodeTraceRow*)row_ptr;
1618
1619 row->has_hidden_classes = row->decode_row->has_hidden_classes();
1620
6a26fc44
SA
1621 owner_->row_item_appearance_changed(false, true);
1622}
1623
65bde9b3
SA
1624void DecodeTrace::on_show_all_classes()
1625{
1626 void* row_ptr = QObject::sender()->property("decode_trace_row_ptr").value<void*>();
1627 assert(row_ptr);
1628 DecodeTraceRow* row = (DecodeTraceRow*)row_ptr;
1629
1630 for (QCheckBox* cb : row->selectors)
1631 cb->setChecked(true);
1632
1633 row->has_hidden_classes = false;
1634
1635 owner_->row_item_appearance_changed(false, true);
1636}
1637
1638void DecodeTrace::on_hide_all_classes()
1639{
1640 void* row_ptr = QObject::sender()->property("decode_trace_row_ptr").value<void*>();
1641 assert(row_ptr);
1642 DecodeTraceRow* row = (DecodeTraceRow*)row_ptr;
1643
1644 for (QCheckBox* cb : row->selectors)
1645 cb->setChecked(false);
1646
1647 row->has_hidden_classes = true;
1648
1649 owner_->row_item_appearance_changed(false, true);
1650}
1651
adf9e022
SA
1652void DecodeTrace::on_row_container_resized(QWidget* sender)
1653{
1654 sender->update();
1655
1656 owner_->extents_changed(false, true);
1657 owner_->row_item_appearance_changed(false, true);
1658}
1659
c764c995
SA
1660void DecodeTrace::on_copy_annotation_to_clipboard()
1661{
c764c995
SA
1662 if (!selected_row_)
1663 return;
1664
462941e2 1665 deque<const Annotation*> annotations;
c764c995 1666
462941e2 1667 decode_signal_->get_annotation_subset(annotations, selected_row_,
c764c995
SA
1668 current_segment_, selected_sample_range_.first, selected_sample_range_.first);
1669
462941e2 1670 if (annotations.empty())
c764c995
SA
1671 return;
1672
628b45cc 1673 QClipboard *clipboard = QApplication::clipboard();
462941e2 1674 clipboard->setText(annotations.front()->annotations()->front(), QClipboard::Clipboard);
41aaa675
SA
1675
1676 if (clipboard->supportsSelection())
462941e2 1677 clipboard->setText(annotations.front()->annotations()->front(), QClipboard::Selection);
c764c995
SA
1678}
1679
be843692
SA
1680void DecodeTrace::on_export_row()
1681{
99ba5f28 1682 selected_sample_range_ = make_pair(0, numeric_limits<uint64_t>::max());
be843692
SA
1683 on_export_row_from_here();
1684}
1685
5a914348
SA
1686void DecodeTrace::on_export_all_rows()
1687{
99ba5f28
SA
1688 selected_sample_range_ = make_pair(0, numeric_limits<uint64_t>::max());
1689 on_export_all_rows_from_here();
1690}
1691
1692void DecodeTrace::on_export_row_with_cursor()
1693{
1694 const View *view = owner_->view();
1695 assert(view);
1696
1697 if (!view->cursors()->enabled())
1698 return;
1699
1700 const double samplerate = session_.get_samplerate();
1701
1702 const pv::util::Timestamp& start_time = view->cursors()->first()->time();
1703 const pv::util::Timestamp& end_time = view->cursors()->second()->time();
1704
1705 const uint64_t start_sample = (uint64_t)max(
a20c1fcc 1706 0.0, start_time.convert_to<double>() * samplerate);
99ba5f28 1707 const uint64_t end_sample = (uint64_t)max(
a20c1fcc 1708 0.0, end_time.convert_to<double>() * samplerate);
99ba5f28
SA
1709
1710 // Are both cursors negative and thus were clamped to 0?
1711 if ((start_sample == 0) && (end_sample == 0))
1712 return;
1713
1714 selected_sample_range_ = make_pair(start_sample, end_sample);
1715 on_export_row_from_here();
1716}
1717
1718void DecodeTrace::on_export_all_rows_with_cursor()
1719{
1720 const View *view = owner_->view();
1721 assert(view);
1722
1723 if (!view->cursors()->enabled())
1724 return;
1725
1726 const double samplerate = session_.get_samplerate();
1727
1728 const pv::util::Timestamp& start_time = view->cursors()->first()->time();
1729 const pv::util::Timestamp& end_time = view->cursors()->second()->time();
1730
1731 const uint64_t start_sample = (uint64_t)max(
a20c1fcc 1732 0.0, start_time.convert_to<double>() * samplerate);
99ba5f28 1733 const uint64_t end_sample = (uint64_t)max(
a20c1fcc 1734 0.0, end_time.convert_to<double>() * samplerate);
99ba5f28
SA
1735
1736 // Are both cursors negative and thus were clamped to 0?
1737 if ((start_sample == 0) && (end_sample == 0))
1738 return;
1739
1740 selected_sample_range_ = make_pair(start_sample, end_sample);
5a914348
SA
1741 on_export_all_rows_from_here();
1742}
1743
be843692
SA
1744void DecodeTrace::on_export_row_from_here()
1745{
be843692
SA
1746 if (!selected_row_)
1747 return;
1748
462941e2 1749 deque<const Annotation*> annotations;
be843692 1750
462941e2 1751 decode_signal_->get_annotation_subset(annotations, selected_row_,
99ba5f28 1752 current_segment_, selected_sample_range_.first, selected_sample_range_.second);
be843692 1753
462941e2 1754 if (annotations.empty())
be843692
SA
1755 return;
1756
5a914348 1757 export_annotations(annotations);
5a914348 1758}
1ed996b4 1759
5a914348
SA
1760void DecodeTrace::on_export_all_rows_from_here()
1761{
462941e2 1762 deque<const Annotation*> annotations;
1ed996b4 1763
462941e2 1764 decode_signal_->get_annotation_subset(annotations, current_segment_,
99ba5f28 1765 selected_sample_range_.first, selected_sample_range_.second);
be843692 1766
462941e2 1767 if (!annotations.empty())
5a914348 1768 export_annotations(annotations);
be843692
SA
1769}
1770
74bf6666
SA
1771void DecodeTrace::on_animation_timer()
1772{
1773 bool animation_finished = true;
1774
6a26fc44 1775 for (DecodeTraceRow& r : rows_) {
74bf6666
SA
1776 if (!(r.expanding || r.collapsing))
1777 continue;
1778
1779 unsigned int height_delta = r.expanded_height - default_row_height_;
1780
1781 if (r.expanding) {
1782 if (r.height < r.expanded_height) {
1783 r.anim_height += height_delta / (float)AnimationDurationInTicks;
8b24af15 1784 r.height = min((int)r.anim_height, (int)r.expanded_height);
74bf6666
SA
1785 r.anim_shape += ArrowSize / (float)AnimationDurationInTicks;
1786 animation_finished = false;
c6b4e925
SA
1787 } else
1788 set_row_expanded(&r);
74bf6666
SA
1789 }
1790
1791 if (r.collapsing) {
1792 if (r.height > default_row_height_) {
1793 r.anim_height -= height_delta / (float)AnimationDurationInTicks;
8b24af15 1794 r.height = max((int)r.anim_height, (int)0);
74bf6666
SA
1795 r.anim_shape -= ArrowSize / (float)AnimationDurationInTicks;
1796 animation_finished = false;
c6b4e925
SA
1797 } else
1798 set_row_collapsed(&r);
74bf6666
SA
1799 }
1800
1801 // The expansion marker shape switches between
1802 // 0/-A, A/0, 0/A (default state; anim_shape=0) and
1803 // 0/ 0, A/A, 2A/0 (expanded state; anim_shape=ArrowSize)
1804
1805 r.expand_marker_shape.setPoint(0, 0, -ArrowSize + r.anim_shape);
1806 r.expand_marker_shape.setPoint(1, ArrowSize, r.anim_shape);
1807 r.expand_marker_shape.setPoint(2, 2*r.anim_shape, ArrowSize - r.anim_shape);
1808 }
1809
1810 if (animation_finished)
1811 animation_timer_.stop();
1812
c6b4e925
SA
1813 owner_->extents_changed(false, true);
1814 owner_->row_item_appearance_changed(false, true);
74bf6666
SA
1815}
1816
440bdf50
SA
1817void DecodeTrace::on_hide_hidden_rows()
1818{
1819 // Make all hidden traces invisible again unless the user is hovering over a row name
1820 bool any_highlighted = false;
1821
1822 for (DecodeTraceRow& r : rows_)
1823 if (r.expand_marker_highlighted)
1824 any_highlighted = true;
1825
1826 if (!any_highlighted) {
1827 show_hidden_rows_ = false;
1828
440bdf50
SA
1829 owner_->extents_changed(false, true);
1830 owner_->row_item_appearance_changed(false, true);
1831 }
1832}
1833
1573bf16 1834} // namespace trace
f4e57597 1835} // namespace views
55d3603d 1836} // namespace pv