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