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