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