]> sigrok.org Git - pulseview.git/blame_incremental - pv/views/trace/decodetrace.cpp
DecodeTrace: Add widget container
[pulseview.git] / pv / views / trace / decodetrace.cpp
... / ...
CommitLineData
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
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20extern "C" {
21#include <libsigrokdecode/libsigrokdecode.h>
22}
23
24#include <limits>
25#include <mutex>
26#include <tuple>
27
28#include <extdef.h>
29
30#include <boost/functional/hash.hpp>
31
32#include <QAction>
33#include <QApplication>
34#include <QClipboard>
35#include <QComboBox>
36#include <QFileDialog>
37#include <QFormLayout>
38#include <QLabel>
39#include <QMenu>
40#include <QMessageBox>
41#include <QPushButton>
42#include <QTextStream>
43#include <QToolTip>
44
45#include "decodetrace.hpp"
46#include "view.hpp"
47#include "viewport.hpp"
48
49#include <pv/globalsettings.hpp>
50#include <pv/session.hpp>
51#include <pv/strnatcmp.hpp>
52#include <pv/data/decodesignal.hpp>
53#include <pv/data/decode/annotation.hpp>
54#include <pv/data/decode/decoder.hpp>
55#include <pv/data/logic.hpp>
56#include <pv/data/logicsegment.hpp>
57#include <pv/widgets/decodergroupbox.hpp>
58#include <pv/widgets/decodermenu.hpp>
59
60using std::abs;
61using std::find_if;
62using std::lock_guard;
63using std::make_pair;
64using std::max;
65using std::min;
66using std::numeric_limits;
67using std::out_of_range;
68using std::pair;
69using std::shared_ptr;
70using std::tie;
71using std::vector;
72
73using pv::data::decode::Annotation;
74using pv::data::decode::Row;
75using pv::data::decode::DecodeChannel;
76using pv::data::DecodeSignal;
77
78namespace pv {
79namespace views {
80namespace trace {
81
82
83#define DECODETRACE_COLOR_SATURATION (180) /* 0-255 */
84#define DECODETRACE_COLOR_VALUE (170) /* 0-255 */
85
86const QColor DecodeTrace::ErrorBgColor = QColor(0xEF, 0x29, 0x29);
87const QColor DecodeTrace::NoDecodeColor = QColor(0x88, 0x8A, 0x85);
88
89const int DecodeTrace::ArrowSize = 6;
90const double DecodeTrace::EndCapWidth = 5;
91const int DecodeTrace::RowTitleMargin = 7;
92const int DecodeTrace::DrawPadding = 100;
93
94const int DecodeTrace::MaxTraceUpdateRate = 1; // No more than 1 Hz
95const unsigned int DecodeTrace::AnimationDurationInTicks = 7;
96
97DecodeTrace::DecodeTrace(pv::Session &session,
98 shared_ptr<data::SignalBase> signalbase, int index) :
99 Trace(signalbase),
100 session_(session),
101 max_visible_rows_(0),
102 delete_mapper_(this),
103 show_hide_mapper_(this)
104{
105 decode_signal_ = dynamic_pointer_cast<data::DecodeSignal>(base_);
106
107 GlobalSettings settings;
108 always_show_all_rows_ = settings.value(GlobalSettings::Key_Dec_AlwaysShowAllRows).toBool();
109
110 GlobalSettings::add_change_handler(this);
111
112 // Determine shortest string we want to see displayed in full
113 QFontMetrics m(QApplication::font());
114 min_useful_label_width_ = m.width("XX"); // e.g. two hex characters
115
116 default_row_height_ = (ViewItemPaintParams::text_height() * 6) / 4;
117 annotation_height_ = (ViewItemPaintParams::text_height() * 5) / 4;
118
119 // For the base color, we want to start at a very different color for
120 // every decoder stack, so multiply the index with a number that is
121 // rather close to 180 degrees of the color circle but not a dividend of 360
122 // Note: The offset equals the color of the first annotation
123 QColor color;
124 const int h = (120 + 160 * index) % 360;
125 const int s = DECODETRACE_COLOR_SATURATION;
126 const int v = DECODETRACE_COLOR_VALUE;
127 color.setHsv(h, s, v);
128 base_->set_color(color);
129
130 connect(decode_signal_.get(), SIGNAL(new_annotations()),
131 this, SLOT(on_new_annotations()));
132 connect(decode_signal_.get(), SIGNAL(decode_reset()),
133 this, SLOT(on_decode_reset()));
134 connect(decode_signal_.get(), SIGNAL(decode_finished()),
135 this, SLOT(on_decode_finished()));
136 connect(decode_signal_.get(), SIGNAL(channels_updated()),
137 this, SLOT(on_channels_updated()));
138
139 connect(&delete_mapper_, SIGNAL(mapped(int)),
140 this, SLOT(on_delete_decoder(int)));
141 connect(&show_hide_mapper_, SIGNAL(mapped(int)),
142 this, SLOT(on_show_hide_decoder(int)));
143
144 connect(&delayed_trace_updater_, SIGNAL(timeout()),
145 this, SLOT(on_delayed_trace_update()));
146 delayed_trace_updater_.setSingleShot(true);
147 delayed_trace_updater_.setInterval(1000 / MaxTraceUpdateRate);
148
149 connect(&animation_timer_, SIGNAL(timeout()),
150 this, SLOT(on_animation_timer()));
151 animation_timer_.setInterval(1000 / 50);
152
153 default_marker_shape_ << QPoint(0, -ArrowSize);
154 default_marker_shape_ << QPoint(ArrowSize, 0);
155 default_marker_shape_ << QPoint(0, ArrowSize);
156}
157
158DecodeTrace::~DecodeTrace()
159{
160 GlobalSettings::remove_change_handler(this);
161
162 for (RowData& r : rows_)
163 delete r.container;
164}
165
166bool DecodeTrace::enabled() const
167{
168 return true;
169}
170
171shared_ptr<data::SignalBase> DecodeTrace::base() const
172{
173 return base_;
174}
175
176pair<int, int> DecodeTrace::v_extents() const
177{
178 // Make an empty decode trace appear symmetrical
179 if (max_visible_rows_ == 0)
180 return make_pair(-default_row_height_, default_row_height_);
181
182 unsigned int height = 0;
183 for (const RowData& r : rows_)
184 if (r.currently_visible)
185 height += r.height;
186
187 return make_pair(-default_row_height_, height);
188}
189
190void DecodeTrace::paint_back(QPainter &p, ViewItemPaintParams &pp)
191{
192 Trace::paint_back(p, pp);
193 paint_axis(p, pp, get_visual_y());
194}
195
196void DecodeTrace::paint_mid(QPainter &p, ViewItemPaintParams &pp)
197{
198 lock_guard<mutex> lock(row_modification_mutex_);
199
200 // Set default pen to allow for text width calculation
201 p.setPen(Qt::black);
202
203 pair<uint64_t, uint64_t> sample_range = get_view_sample_range(pp.left(), pp.right());
204
205 // Just because the view says we see a certain sample range it
206 // doesn't mean we have this many decoded samples, too, so crop
207 // the range to what has been decoded already
208 sample_range.second = min((int64_t)sample_range.second,
209 decode_signal_->get_decoded_sample_count(current_segment_, false));
210
211 for (RowData& r : rows_)
212 r.currently_visible = false;
213 visible_rows_ = 0;
214 int y = get_visual_y();
215
216 for (RowData& r : rows_) {
217 // If an entire decoder is hidden, we don't want to fetch annotations
218 if (!r.decode_row.decoder()->shown()) {
219 r.currently_visible = false;
220 continue;
221 }
222
223 vector<Annotation> annotations;
224 decode_signal_->get_annotation_subset(annotations, r.decode_row,
225 current_segment_, sample_range.first, sample_range.second);
226
227 // Show row if there are visible annotations or when user wants to see
228 // all rows that have annotations somewhere and this one is one of them
229 size_t ann_count = decode_signal_->get_annotation_count(r.decode_row, current_segment_);
230 r.currently_visible = !annotations.empty() || (always_show_all_rows_ && (ann_count > 0));
231
232 if (r.currently_visible) {
233 draw_annotations(annotations, p, annotation_height_, pp, y,
234 get_row_color(r.decode_row.index()), r.title_width);
235 y += r.height;
236 visible_rows_++;
237 }
238 }
239
240 draw_unresolved_period(p, annotation_height_, pp.left(), pp.right());
241
242 if (visible_rows_ > max_visible_rows_) {
243 max_visible_rows_ = visible_rows_;
244
245 // Call order is important, otherwise the lazy event handler won't work
246 owner_->extents_changed(false, true);
247 owner_->row_item_appearance_changed(false, true);
248 }
249
250 const QString err = decode_signal_->error_message();
251 if (!err.isEmpty())
252 draw_error(p, err, pp);
253}
254
255void DecodeTrace::paint_fore(QPainter &p, ViewItemPaintParams &pp)
256{
257 unsigned int y = get_visual_y();
258
259 for (const RowData& r : rows_) {
260 if (!r.currently_visible)
261 continue;
262
263 p.setPen(QPen(Qt::NoPen));
264
265 if (r.expand_marker_highlighted)
266 p.setBrush(QApplication::palette().brush(QPalette::Highlight));
267 else
268 p.setBrush(QApplication::palette().brush(QPalette::WindowText));
269
270 // Draw expansion marker
271 QPolygon marker(r.expand_marker_shape);
272 marker.translate(pp.left(), y);
273 p.drawPolygon(marker);
274
275 p.setBrush(QApplication::palette().brush(QPalette::WindowText));
276
277 const QRect text_rect(pp.left() + ArrowSize * 2, y - r.height / 2,
278 pp.right() - pp.left(), r.height);
279 const QString h(r.decode_row.title());
280 const int f = Qt::AlignLeft | Qt::AlignVCenter |
281 Qt::TextDontClip;
282
283 // Draw the outline
284 p.setPen(QApplication::palette().color(QPalette::Base));
285 for (int dx = -1; dx <= 1; dx++)
286 for (int dy = -1; dy <= 1; dy++)
287 if (dx != 0 && dy != 0)
288 p.drawText(text_rect.translated(dx, dy), f, h);
289
290 // Draw the text
291 p.setPen(QApplication::palette().color(QPalette::WindowText));
292 p.drawText(text_rect, f, h);
293
294 y += r.height;
295 }
296
297 if (show_hover_marker_)
298 paint_hover_marker(p);
299}
300
301void DecodeTrace::update_stack_button()
302{
303 const vector< shared_ptr<data::decode::Decoder> > &stack = decode_signal_->decoder_stack();
304
305 // Only show decoders in the menu that can be stacked onto the last one in the stack
306 if (!stack.empty()) {
307 const srd_decoder* d = stack.back()->decoder();
308
309 if (d->outputs) {
310 pv::widgets::DecoderMenu *const decoder_menu =
311 new pv::widgets::DecoderMenu(stack_button_, (const char*)(d->outputs->data));
312 connect(decoder_menu, SIGNAL(decoder_selected(srd_decoder*)),
313 this, SLOT(on_stack_decoder(srd_decoder*)));
314
315 decoder_menu->setStyleSheet("QMenu { menu-scrollable: 1; }");
316
317 stack_button_->setMenu(decoder_menu);
318 stack_button_->show();
319 return;
320 }
321 }
322
323 // No decoders available for stacking
324 stack_button_->setMenu(nullptr);
325 stack_button_->hide();
326}
327
328void DecodeTrace::populate_popup_form(QWidget *parent, QFormLayout *form)
329{
330 using pv::data::decode::Decoder;
331
332 assert(form);
333
334 // Add the standard options
335 Trace::populate_popup_form(parent, form);
336
337 // Add the decoder options
338 bindings_.clear();
339 channel_id_map_.clear();
340 init_state_map_.clear();
341 decoder_forms_.clear();
342
343 const vector< shared_ptr<Decoder> > &stack = decode_signal_->decoder_stack();
344
345 if (stack.empty()) {
346 QLabel *const l = new QLabel(
347 tr("<p><i>No decoders in the stack</i></p>"));
348 l->setAlignment(Qt::AlignCenter);
349 form->addRow(l);
350 } else {
351 auto iter = stack.cbegin();
352 for (int i = 0; i < (int)stack.size(); i++, iter++) {
353 shared_ptr<Decoder> dec(*iter);
354 create_decoder_form(i, dec, parent, form);
355 }
356
357 form->addRow(new QLabel(
358 tr("<i>* Required channels</i>"), parent));
359 }
360
361 // Add stacking button
362 stack_button_ = new QPushButton(tr("Stack Decoder"), parent);
363 stack_button_->setToolTip(tr("Stack a higher-level decoder on top of this one"));
364 update_stack_button();
365
366 QHBoxLayout *stack_button_box = new QHBoxLayout;
367 stack_button_box->addWidget(stack_button_, 0, Qt::AlignRight);
368 form->addRow(stack_button_box);
369}
370
371QMenu* DecodeTrace::create_header_context_menu(QWidget *parent)
372{
373 QMenu *const menu = Trace::create_header_context_menu(parent);
374
375 menu->addSeparator();
376
377 QAction *const del = new QAction(tr("Delete"), this);
378 del->setShortcuts(QKeySequence::Delete);
379 connect(del, SIGNAL(triggered()), this, SLOT(on_delete()));
380 menu->addAction(del);
381
382 return menu;
383}
384
385QMenu* DecodeTrace::create_view_context_menu(QWidget *parent, QPoint &click_pos)
386{
387 // Get entries from default menu before adding our own
388 QMenu *const menu = new QMenu(parent);
389
390 QMenu* default_menu = Trace::create_view_context_menu(parent, click_pos);
391 if (default_menu) {
392 for (QAction *action : default_menu->actions()) { // clazy:exclude=range-loop
393 menu->addAction(action);
394 if (action->parent() == default_menu)
395 action->setParent(menu);
396 }
397 delete default_menu;
398
399 // Add separator if needed
400 if (menu->actions().length() > 0)
401 menu->addSeparator();
402 }
403
404 selected_row_ = nullptr;
405 const RowData* r = get_row_at_point(click_pos);
406 if (r)
407 selected_row_ = &(r->decode_row);
408
409 const View *const view = owner_->view();
410 assert(view);
411 QPoint pos = view->viewport()->mapFrom(parent, click_pos);
412
413 // Default sample range is "from here"
414 const pair<uint64_t, uint64_t> sample_range = get_view_sample_range(pos.x(), pos.x() + 1);
415 selected_sample_range_ = make_pair(sample_range.first, numeric_limits<uint64_t>::max());
416
417 if (decode_signal_->is_paused()) {
418 QAction *const resume =
419 new QAction(tr("Resume decoding"), this);
420 resume->setIcon(QIcon::fromTheme("media-playback-start",
421 QIcon(":/icons/media-playback-start.png")));
422 connect(resume, SIGNAL(triggered()), this, SLOT(on_pause_decode()));
423 menu->addAction(resume);
424 } else {
425 QAction *const pause =
426 new QAction(tr("Pause decoding"), this);
427 pause->setIcon(QIcon::fromTheme("media-playback-pause",
428 QIcon(":/icons/media-playback-pause.png")));
429 connect(pause, SIGNAL(triggered()), this, SLOT(on_pause_decode()));
430 menu->addAction(pause);
431 }
432
433 QAction *const copy_annotation_to_clipboard =
434 new QAction(tr("Copy annotation text to clipboard"), this);
435 copy_annotation_to_clipboard->setIcon(QIcon::fromTheme("edit-paste",
436 QIcon(":/icons/edit-paste.svg")));
437 connect(copy_annotation_to_clipboard, SIGNAL(triggered()), this, SLOT(on_copy_annotation_to_clipboard()));
438 menu->addAction(copy_annotation_to_clipboard);
439
440 menu->addSeparator();
441
442 QAction *const export_all_rows =
443 new QAction(tr("Export all annotations"), this);
444 export_all_rows->setIcon(QIcon::fromTheme("document-save-as",
445 QIcon(":/icons/document-save-as.png")));
446 connect(export_all_rows, SIGNAL(triggered()), this, SLOT(on_export_all_rows()));
447 menu->addAction(export_all_rows);
448
449 QAction *const export_row =
450 new QAction(tr("Export all annotations for this row"), this);
451 export_row->setIcon(QIcon::fromTheme("document-save-as",
452 QIcon(":/icons/document-save-as.png")));
453 connect(export_row, SIGNAL(triggered()), this, SLOT(on_export_row()));
454 menu->addAction(export_row);
455
456 menu->addSeparator();
457
458 QAction *const export_all_rows_from_here =
459 new QAction(tr("Export all annotations, starting here"), this);
460 export_all_rows_from_here->setIcon(QIcon::fromTheme("document-save-as",
461 QIcon(":/icons/document-save-as.png")));
462 connect(export_all_rows_from_here, SIGNAL(triggered()), this, SLOT(on_export_all_rows_from_here()));
463 menu->addAction(export_all_rows_from_here);
464
465 QAction *const export_row_from_here =
466 new QAction(tr("Export annotations for this row, starting here"), this);
467 export_row_from_here->setIcon(QIcon::fromTheme("document-save-as",
468 QIcon(":/icons/document-save-as.png")));
469 connect(export_row_from_here, SIGNAL(triggered()), this, SLOT(on_export_row_from_here()));
470 menu->addAction(export_row_from_here);
471
472 menu->addSeparator();
473
474 QAction *const export_all_rows_with_cursor =
475 new QAction(tr("Export all annotations within cursor range"), this);
476 export_all_rows_with_cursor->setIcon(QIcon::fromTheme("document-save-as",
477 QIcon(":/icons/document-save-as.png")));
478 connect(export_all_rows_with_cursor, SIGNAL(triggered()), this, SLOT(on_export_all_rows_with_cursor()));
479 menu->addAction(export_all_rows_with_cursor);
480
481 QAction *const export_row_with_cursor =
482 new QAction(tr("Export annotations for this row within cursor range"), this);
483 export_row_with_cursor->setIcon(QIcon::fromTheme("document-save-as",
484 QIcon(":/icons/document-save-as.png")));
485 connect(export_row_with_cursor, SIGNAL(triggered()), this, SLOT(on_export_row_with_cursor()));
486 menu->addAction(export_row_with_cursor);
487
488 if (!view->cursors()->enabled()) {
489 export_all_rows_with_cursor->setEnabled(false);
490 export_row_with_cursor->setEnabled(false);
491 }
492
493 return menu;
494}
495
496void DecodeTrace::delete_pressed()
497{
498 on_delete();
499}
500
501void DecodeTrace::hover_point_changed(const QPoint &hp)
502{
503 Trace::hover_point_changed(hp);
504
505 assert(owner_);
506
507 RowData* hover_row = get_row_at_point(hp);
508
509 // Row expansion marker handling
510 for (RowData& r : rows_)
511 r.expand_marker_highlighted = false;
512
513 if (hover_row) {
514 int row_y = get_row_y(hover_row);
515 if ((hp.x() > 0) && (hp.x() < 2 * ArrowSize) &&
516 (hp.y() > (int)(row_y - ArrowSize)) && (hp.y() < (int)(row_y + ArrowSize)))
517 hover_row->expand_marker_highlighted = true;
518 }
519
520 // Tooltip handling
521 if (hp.x() > 0) {
522 QString ann = get_annotation_at_point(hp);
523
524 if (!ann.isEmpty()) {
525 QFontMetrics m(QToolTip::font());
526 const QRect text_size = m.boundingRect(QRect(), 0, ann);
527
528 // This is OS-specific and unfortunately we can't query it, so
529 // use an approximation to at least try to minimize the error.
530 const int padding = default_row_height_ + 8;
531
532 // Make sure the tool tip doesn't overlap with the mouse cursor.
533 // If it did, the tool tip would constantly hide and re-appear.
534 // We also push it up by one row so that it appears above the
535 // decode trace, not below.
536 QPoint p = hp;
537 p.setX(hp.x() - (text_size.width() / 2) - padding);
538
539 p.setY(get_row_y(hover_row) - default_row_height_ -
540 text_size.height() - padding);
541
542 const View *const view = owner_->view();
543 assert(view);
544 QToolTip::showText(view->viewport()->mapToGlobal(p), ann);
545
546 } else
547 QToolTip::hideText();
548
549 } else
550 QToolTip::hideText();
551}
552
553void DecodeTrace::mouse_left_press_event(const QMouseEvent* event)
554{
555 // Handle row expansion marker
556 for (RowData& r : rows_) {
557 if (!r.expand_marker_highlighted)
558 continue;
559
560 unsigned int y = get_row_y(&r);
561 if ((event->x() > 0) && (event->x() <= (int)(ArrowSize + 3)) &&
562 (event->y() > (int)(y - (default_row_height_ / 2))) &&
563 (event->y() <= (int)(y + (default_row_height_ / 2)))) {
564
565 if (r.expanded) {
566 r.collapsing = true;
567 r.expanded = false;
568 r.anim_shape = ArrowSize;
569 } else {
570 r.expanding = true;
571 r.anim_shape = 0;
572 r.container->setVisible(true);
573 }
574
575 r.animation_step = 0;
576 r.anim_height = r.height;
577
578 r.container->move(2 * ArrowSize,
579 get_row_y(&r) + default_row_height_);
580
581 animation_timer_.start();
582 }
583 }
584}
585
586void DecodeTrace::draw_annotations(vector<pv::data::decode::Annotation> annotations,
587 QPainter &p, int h, const ViewItemPaintParams &pp, int y,
588 QColor row_color, int row_title_width)
589{
590 using namespace pv::data::decode;
591
592 Annotation::Class block_class = 0;
593 bool block_class_uniform = true;
594 qreal block_start = 0;
595 int block_ann_count = 0;
596
597 const Annotation *prev_ann;
598 qreal prev_end = INT_MIN;
599
600 qreal a_end;
601
602 double samples_per_pixel, pixels_offset;
603 tie(pixels_offset, samples_per_pixel) =
604 get_pixels_offset_samples_per_pixel();
605
606 // Sort the annotations by start sample so that decoders
607 // can't confuse us by creating annotations out of order
608 stable_sort(annotations.begin(), annotations.end(),
609 [](const Annotation &a, const Annotation &b) {
610 return a.start_sample() < b.start_sample(); });
611
612 // Gather all annotations that form a visual "block" and draw them as such
613 for (const Annotation &a : annotations) {
614
615 const qreal abs_a_start = a.start_sample() / samples_per_pixel;
616 const qreal abs_a_end = a.end_sample() / samples_per_pixel;
617
618 const qreal a_start = abs_a_start - pixels_offset;
619 a_end = abs_a_end - pixels_offset;
620
621 const qreal a_width = a_end - a_start;
622 const qreal delta = a_end - prev_end;
623
624 bool a_is_separate = false;
625
626 // Annotation wider than the threshold for a useful label width?
627 if (a_width >= min_useful_label_width_) {
628 for (const QString &ann_text : a.annotations()) {
629 const qreal w = p.boundingRect(QRectF(), 0, ann_text).width();
630 // Annotation wide enough to fit a label? Don't put it in a block then
631 if (w <= a_width) {
632 a_is_separate = true;
633 break;
634 }
635 }
636 }
637
638 // Were the previous and this annotation more than a pixel apart?
639 if ((abs(delta) > 1) || a_is_separate) {
640 // Block was broken, draw annotations that form the current block
641 if (block_ann_count == 1)
642 draw_annotation(*prev_ann, p, h, pp, y, row_color,
643 row_title_width);
644 else if (block_ann_count > 0)
645 draw_annotation_block(block_start, prev_end, block_class,
646 block_class_uniform, p, h, y, row_color);
647
648 block_ann_count = 0;
649 }
650
651 if (a_is_separate) {
652 draw_annotation(a, p, h, pp, y, row_color, row_title_width);
653 // Next annotation must start a new block. delta will be > 1
654 // because we set prev_end to INT_MIN but that's okay since
655 // block_ann_count will be 0 and nothing will be drawn
656 prev_end = INT_MIN;
657 block_ann_count = 0;
658 } else {
659 prev_end = a_end;
660 prev_ann = &a;
661
662 if (block_ann_count == 0) {
663 block_start = a_start;
664 block_class = a.ann_class();
665 block_class_uniform = true;
666 } else
667 if (a.ann_class() != block_class)
668 block_class_uniform = false;
669
670 block_ann_count++;
671 }
672 }
673
674 if (block_ann_count == 1)
675 draw_annotation(*prev_ann, p, h, pp, y, row_color, row_title_width);
676 else if (block_ann_count > 0)
677 draw_annotation_block(block_start, prev_end, block_class,
678 block_class_uniform, p, h, y, row_color);
679}
680
681void DecodeTrace::draw_annotation(const pv::data::decode::Annotation &a,
682 QPainter &p, int h, const ViewItemPaintParams &pp, int y,
683 QColor row_color, int row_title_width) const
684{
685 double samples_per_pixel, pixels_offset;
686 tie(pixels_offset, samples_per_pixel) =
687 get_pixels_offset_samples_per_pixel();
688
689 const double start = a.start_sample() / samples_per_pixel -
690 pixels_offset;
691 const double end = a.end_sample() / samples_per_pixel - pixels_offset;
692
693 QColor color = get_annotation_color(row_color, a.ann_class());
694 p.setPen(color.darker());
695 p.setBrush(color);
696
697 if (start > pp.right() + DrawPadding || end < pp.left() - DrawPadding)
698 return;
699
700 if (a.start_sample() == a.end_sample())
701 draw_instant(a, p, h, start, y);
702 else
703 draw_range(a, p, h, start, end, y, pp, row_title_width);
704}
705
706void DecodeTrace::draw_annotation_block(qreal start, qreal end,
707 Annotation::Class ann_class, bool use_ann_format, QPainter &p, int h,
708 int y, QColor row_color) const
709{
710 const double top = y + .5 - h / 2;
711 const double bottom = y + .5 + h / 2;
712
713 const QRectF rect(start, top, end - start, bottom - top);
714 const int r = h / 4;
715
716 p.setPen(QPen(Qt::NoPen));
717 p.setBrush(Qt::white);
718 p.drawRoundedRect(rect, r, r);
719
720 // If all annotations in this block are of the same type, we can use the
721 // one format that all of these annotations have. Otherwise, we should use
722 // a neutral color (i.e. gray)
723 if (use_ann_format) {
724 const QColor color = get_annotation_color(row_color, ann_class);
725 p.setPen(color.darker());
726 p.setBrush(QBrush(color, Qt::Dense4Pattern));
727 } else {
728 p.setPen(Qt::gray);
729 p.setBrush(QBrush(Qt::gray, Qt::Dense4Pattern));
730 }
731
732 p.drawRoundedRect(rect, r, r);
733}
734
735void DecodeTrace::draw_instant(const pv::data::decode::Annotation &a, QPainter &p,
736 int h, qreal x, int y) const
737{
738 const QString text = a.annotations().empty() ?
739 QString() : a.annotations().back();
740 const qreal w = min((qreal)p.boundingRect(QRectF(), 0, text).width(),
741 0.0) + h;
742 const QRectF rect(x - w / 2, y - h / 2, w, h);
743
744 p.drawRoundedRect(rect, h / 2, h / 2);
745
746 p.setPen(Qt::black);
747 p.drawText(rect, Qt::AlignCenter | Qt::AlignVCenter, text);
748}
749
750void DecodeTrace::draw_range(const pv::data::decode::Annotation &a, QPainter &p,
751 int h, qreal start, qreal end, int y, const ViewItemPaintParams &pp,
752 int row_title_width) const
753{
754 const qreal top = y + .5 - h / 2;
755 const qreal bottom = y + .5 + h / 2;
756 const vector<QString> annotations = a.annotations();
757
758 // If the two ends are within 1 pixel, draw a vertical line
759 if (start + 1.0 > end) {
760 p.drawLine(QPointF(start, top), QPointF(start, bottom));
761 return;
762 }
763
764 const qreal cap_width = min((end - start) / 4, EndCapWidth);
765
766 QPointF pts[] = {
767 QPointF(start, y + .5f),
768 QPointF(start + cap_width, top),
769 QPointF(end - cap_width, top),
770 QPointF(end, y + .5f),
771 QPointF(end - cap_width, bottom),
772 QPointF(start + cap_width, bottom)
773 };
774
775 p.drawConvexPolygon(pts, countof(pts));
776
777 if (annotations.empty())
778 return;
779
780 const int ann_start = start + cap_width;
781 const int ann_end = end - cap_width;
782
783 const int real_start = max(ann_start, pp.left() + ArrowSize + row_title_width);
784 const int real_end = min(ann_end, pp.right());
785 const int real_width = real_end - real_start;
786
787 QRectF rect(real_start, y - h / 2, real_width, h);
788 if (rect.width() <= 4)
789 return;
790
791 p.setPen(Qt::black);
792
793 // Try to find an annotation that will fit
794 QString best_annotation;
795 int best_width = 0;
796
797 for (const QString &a : annotations) {
798 const int w = p.boundingRect(QRectF(), 0, a).width();
799 if (w <= rect.width() && w > best_width)
800 best_annotation = a, best_width = w;
801 }
802
803 if (best_annotation.isEmpty())
804 best_annotation = annotations.back();
805
806 // If not ellide the last in the list
807 p.drawText(rect, Qt::AlignCenter, p.fontMetrics().elidedText(
808 best_annotation, Qt::ElideRight, rect.width()));
809}
810
811void DecodeTrace::draw_error(QPainter &p, const QString &message,
812 const ViewItemPaintParams &pp)
813{
814 const int y = get_visual_y();
815
816 double samples_per_pixel, pixels_offset;
817 tie(pixels_offset, samples_per_pixel) = get_pixels_offset_samples_per_pixel();
818
819 p.setPen(ErrorBgColor.darker());
820 p.setBrush(ErrorBgColor);
821
822 const QRectF bounding_rect = QRectF(pp.left(), INT_MIN / 2 + y, pp.right(), INT_MAX);
823
824 const QRectF text_rect = p.boundingRect(bounding_rect, Qt::AlignCenter, message);
825 const qreal r = text_rect.height() / 4;
826
827 p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r, Qt::AbsoluteSize);
828
829 p.setPen(Qt::black);
830 p.drawText(text_rect, message);
831}
832
833void DecodeTrace::draw_unresolved_period(QPainter &p, int h, int left, int right) const
834{
835 using namespace pv::data;
836 using pv::data::decode::Decoder;
837
838 double samples_per_pixel, pixels_offset;
839
840 const int64_t sample_count = decode_signal_->get_working_sample_count(current_segment_);
841 if (sample_count == 0)
842 return;
843
844 const int64_t samples_decoded = decode_signal_->get_decoded_sample_count(current_segment_, true);
845 if (sample_count == samples_decoded)
846 return;
847
848 const int y = get_visual_y();
849
850 tie(pixels_offset, samples_per_pixel) = get_pixels_offset_samples_per_pixel();
851
852 const double start = max(samples_decoded /
853 samples_per_pixel - pixels_offset, left - 1.0);
854 const double end = min(sample_count / samples_per_pixel -
855 pixels_offset, right + 1.0);
856 const QRectF no_decode_rect(start, y - (h / 2) - 0.5, end - start, h);
857
858 p.setPen(QPen(Qt::NoPen));
859 p.setBrush(Qt::white);
860 p.drawRect(no_decode_rect);
861
862 p.setPen(NoDecodeColor);
863 p.setBrush(QBrush(NoDecodeColor, Qt::Dense6Pattern));
864 p.drawRect(no_decode_rect);
865}
866
867pair<double, double> DecodeTrace::get_pixels_offset_samples_per_pixel() const
868{
869 assert(owner_);
870
871 const View *view = owner_->view();
872 assert(view);
873
874 const double scale = view->scale();
875 assert(scale > 0);
876
877 const double pixels_offset =
878 ((view->offset() - decode_signal_->start_time()) / scale).convert_to<double>();
879
880 double samplerate = decode_signal_->samplerate();
881
882 // Show sample rate as 1Hz when it is unknown
883 if (samplerate == 0.0)
884 samplerate = 1.0;
885
886 return make_pair(pixels_offset, samplerate * scale);
887}
888
889pair<uint64_t, uint64_t> DecodeTrace::get_view_sample_range(
890 int x_start, int x_end) const
891{
892 double samples_per_pixel, pixels_offset;
893 tie(pixels_offset, samples_per_pixel) =
894 get_pixels_offset_samples_per_pixel();
895
896 const uint64_t start = (uint64_t)max(
897 (x_start + pixels_offset) * samples_per_pixel, 0.0);
898 const uint64_t end = (uint64_t)max(
899 (x_end + pixels_offset) * samples_per_pixel, 0.0);
900
901 return make_pair(start, end);
902}
903
904QColor DecodeTrace::get_row_color(int row_index) const
905{
906 // For each row color, use the base color hue and add an offset that's
907 // not a dividend of 360
908
909 QColor color;
910 const int h = (base_->color().toHsv().hue() + 20 * row_index) % 360;
911 const int s = DECODETRACE_COLOR_SATURATION;
912 const int v = DECODETRACE_COLOR_VALUE;
913 color.setHsl(h, s, v);
914
915 return color;
916}
917
918QColor DecodeTrace::get_annotation_color(QColor row_color, int annotation_index) const
919{
920 // For each row color, use the base color hue and add an offset that's
921 // not a dividend of 360 and not a multiple of the row offset
922
923 QColor color(row_color);
924 const int h = (color.toHsv().hue() + 55 * annotation_index) % 360;
925 const int s = DECODETRACE_COLOR_SATURATION;
926 const int v = DECODETRACE_COLOR_VALUE;
927 color.setHsl(h, s, v);
928
929 return color;
930}
931
932unsigned int DecodeTrace::get_row_y(const RowData* row) const
933{
934 assert(row);
935
936 unsigned int y = get_visual_y();
937
938 for (const RowData& r : rows_) {
939 if (!r.currently_visible)
940 continue;
941
942 if (row->decode_row == r.decode_row)
943 break;
944 else
945 y += r.height;
946 }
947
948 return y;
949}
950
951RowData* DecodeTrace::get_row_at_point(const QPoint &point)
952{
953 int y = get_visual_y() - (default_row_height_ / 2);
954
955 for (RowData& r : rows_) {
956 if (!r.currently_visible)
957 continue;
958
959 if ((point.y() >= y) && (point.y() < (int)(y + r.height)))
960 return &r;
961
962 y += r.height;
963 }
964
965 return nullptr;
966}
967
968const QString DecodeTrace::get_annotation_at_point(const QPoint &point)
969{
970 using namespace pv::data::decode;
971
972 if (!enabled())
973 return QString();
974
975 const pair<uint64_t, uint64_t> sample_range =
976 get_view_sample_range(point.x(), point.x() + 1);
977 const RowData* r = get_row_at_point(point);
978
979 if (!r)
980 return QString();
981
982 if (point.y() > (int)(get_row_y(r) + (annotation_height_ / 2)))
983 return QString();
984
985 vector<Annotation> annotations;
986
987 decode_signal_->get_annotation_subset(annotations, r->decode_row,
988 current_segment_, sample_range.first, sample_range.second);
989
990 return (annotations.empty()) ?
991 QString() : annotations[0].annotations().front();
992}
993
994void DecodeTrace::create_decoder_form(int index,
995 shared_ptr<data::decode::Decoder> &dec, QWidget *parent,
996 QFormLayout *form)
997{
998 GlobalSettings settings;
999
1000 assert(dec);
1001 const srd_decoder *const decoder = dec->decoder();
1002 assert(decoder);
1003
1004 const bool decoder_deletable = index > 0;
1005
1006 pv::widgets::DecoderGroupBox *const group =
1007 new pv::widgets::DecoderGroupBox(
1008 QString::fromUtf8(decoder->name),
1009 tr("%1:\n%2").arg(QString::fromUtf8(decoder->longname),
1010 QString::fromUtf8(decoder->desc)),
1011 nullptr, decoder_deletable);
1012 group->set_decoder_visible(dec->shown());
1013
1014 if (decoder_deletable) {
1015 delete_mapper_.setMapping(group, index);
1016 connect(group, SIGNAL(delete_decoder()), &delete_mapper_, SLOT(map()));
1017 }
1018
1019 show_hide_mapper_.setMapping(group, index);
1020 connect(group, SIGNAL(show_hide_decoder()),
1021 &show_hide_mapper_, SLOT(map()));
1022
1023 QFormLayout *const decoder_form = new QFormLayout;
1024 group->add_layout(decoder_form);
1025
1026 const vector<DecodeChannel> channels = decode_signal_->get_channels();
1027
1028 // Add the channels
1029 for (const DecodeChannel& ch : channels) {
1030 // Ignore channels not part of the decoder we create the form for
1031 if (ch.decoder_ != dec)
1032 continue;
1033
1034 QComboBox *const combo = create_channel_selector(parent, &ch);
1035 QComboBox *const combo_init_state = create_channel_selector_init_state(parent, &ch);
1036
1037 channel_id_map_[combo] = ch.id;
1038 init_state_map_[combo_init_state] = ch.id;
1039
1040 connect(combo, SIGNAL(currentIndexChanged(int)),
1041 this, SLOT(on_channel_selected(int)));
1042 connect(combo_init_state, SIGNAL(currentIndexChanged(int)),
1043 this, SLOT(on_init_state_changed(int)));
1044
1045 QHBoxLayout *const hlayout = new QHBoxLayout;
1046 hlayout->addWidget(combo);
1047 hlayout->addWidget(combo_init_state);
1048
1049 if (!settings.value(GlobalSettings::Key_Dec_InitialStateConfigurable).toBool())
1050 combo_init_state->hide();
1051
1052 const QString required_flag = ch.is_optional ? QString() : QString("*");
1053 decoder_form->addRow(tr("<b>%1</b> (%2) %3")
1054 .arg(ch.name, ch.desc, required_flag), hlayout);
1055 }
1056
1057 // Add the options
1058 shared_ptr<binding::Decoder> binding(
1059 new binding::Decoder(decode_signal_, dec));
1060 binding->add_properties_to_form(decoder_form, true);
1061
1062 bindings_.push_back(binding);
1063
1064 form->addRow(group);
1065 decoder_forms_.push_back(group);
1066}
1067
1068QComboBox* DecodeTrace::create_channel_selector(QWidget *parent, const DecodeChannel *ch)
1069{
1070 const auto sigs(session_.signalbases());
1071
1072 // Sort signals in natural order
1073 vector< shared_ptr<data::SignalBase> > sig_list(sigs.begin(), sigs.end());
1074 sort(sig_list.begin(), sig_list.end(),
1075 [](const shared_ptr<data::SignalBase> &a,
1076 const shared_ptr<data::SignalBase> &b) {
1077 return strnatcasecmp(a->name().toStdString(),
1078 b->name().toStdString()) < 0; });
1079
1080 QComboBox *selector = new QComboBox(parent);
1081
1082 selector->addItem("-", qVariantFromValue((void*)nullptr));
1083
1084 if (!ch->assigned_signal)
1085 selector->setCurrentIndex(0);
1086
1087 for (const shared_ptr<data::SignalBase> &b : sig_list) {
1088 assert(b);
1089 if (b->logic_data() && b->enabled()) {
1090 selector->addItem(b->name(),
1091 qVariantFromValue((void*)b.get()));
1092
1093 if (ch->assigned_signal == b.get())
1094 selector->setCurrentIndex(selector->count() - 1);
1095 }
1096 }
1097
1098 return selector;
1099}
1100
1101QComboBox* DecodeTrace::create_channel_selector_init_state(QWidget *parent,
1102 const DecodeChannel *ch)
1103{
1104 QComboBox *selector = new QComboBox(parent);
1105
1106 selector->addItem("0", qVariantFromValue((int)SRD_INITIAL_PIN_LOW));
1107 selector->addItem("1", qVariantFromValue((int)SRD_INITIAL_PIN_HIGH));
1108 selector->addItem("X", qVariantFromValue((int)SRD_INITIAL_PIN_SAME_AS_SAMPLE0));
1109
1110 selector->setCurrentIndex(ch->initial_pin_state);
1111
1112 selector->setToolTip("Initial (assumed) pin value before the first sample");
1113
1114 return selector;
1115}
1116
1117void DecodeTrace::export_annotations(vector<Annotation> *annotations) const
1118{
1119 using namespace pv::data::decode;
1120
1121 GlobalSettings settings;
1122 const QString dir = settings.value("MainWindow/SaveDirectory").toString();
1123
1124 const QString file_name = QFileDialog::getSaveFileName(
1125 owner_->view(), tr("Export annotations"), dir, tr("Text Files (*.txt);;All Files (*)"));
1126
1127 if (file_name.isEmpty())
1128 return;
1129
1130 QString format = settings.value(GlobalSettings::Key_Dec_ExportFormat).toString();
1131 const QString quote = format.contains("%q") ? "\"" : "";
1132 format = format.remove("%q");
1133
1134 QFile file(file_name);
1135 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
1136 QTextStream out_stream(&file);
1137
1138 for (Annotation &ann : *annotations) {
1139 const QString sample_range = QString("%1-%2") \
1140 .arg(QString::number(ann.start_sample()), QString::number(ann.end_sample()));
1141
1142 const QString class_name = quote + ann.row()->class_name() + quote;
1143
1144 QString all_ann_text;
1145 for (const QString &s : ann.annotations())
1146 all_ann_text = all_ann_text + quote + s + quote + ",";
1147 all_ann_text.chop(1);
1148
1149 const QString first_ann_text = quote + ann.annotations().front() + quote;
1150
1151 QString out_text = format;
1152 out_text = out_text.replace("%s", sample_range);
1153 out_text = out_text.replace("%d",
1154 quote + QString::fromUtf8(ann.row()->decoder()->name()) + quote);
1155 out_text = out_text.replace("%c", class_name);
1156 out_text = out_text.replace("%1", first_ann_text);
1157 out_text = out_text.replace("%a", all_ann_text);
1158 out_stream << out_text << '\n';
1159 }
1160
1161 if (out_stream.status() == QTextStream::Ok)
1162 return;
1163 }
1164
1165 QMessageBox msg(owner_->view());
1166 msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
1167 msg.setStandardButtons(QMessageBox::Ok);
1168 msg.setIcon(QMessageBox::Warning);
1169 msg.exec();
1170}
1171
1172void DecodeTrace::update_rows()
1173{
1174 lock_guard<mutex> lock(row_modification_mutex_);
1175
1176 QFontMetrics m(QApplication::font());
1177
1178 for (RowData& r : rows_)
1179 r.exists = false;
1180
1181 for (const Row& decode_row : decode_signal_->get_rows()) {
1182 // Find row in our list
1183 auto r_it = find_if(rows_.begin(), rows_.end(),
1184 [&](RowData& r){ return r.decode_row == decode_row; });
1185
1186 RowData* r = nullptr;
1187 if (r_it == rows_.end()) {
1188 // Row doesn't exist yet, create and append it
1189 RowData nr;
1190 nr.decode_row = decode_row;
1191 nr.height = default_row_height_;
1192 nr.expanded_height = 5*default_row_height_;
1193 nr.currently_visible = false;
1194 nr.expand_marker_highlighted = false;
1195 nr.expanding = false;
1196 nr.expanded = false;
1197 nr.collapsing = false;
1198 nr.expand_marker_shape = default_marker_shape_;
1199 nr.container = new QWidget(owner_->view()->viewport());
1200
1201 rows_.push_back(nr);
1202 r = &rows_.back();
1203 } else
1204 r = &(*r_it);
1205
1206 r->exists = true;
1207
1208 const int w = m.boundingRect(r->decode_row.title()).width() + RowTitleMargin;
1209 r->title_width = w;
1210
1211 r->container->resize(owner_->view()->viewport()->width() - r->container->pos().x(),
1212 r->expanded_height - 2 * default_row_height_);
1213 r->container->setVisible(false);
1214 }
1215
1216 // Remove any rows that no longer exist, obeying that iterators are invalidated
1217 bool any_exists;
1218 do {
1219 any_exists = false;
1220
1221 for (unsigned int i = 0; i < rows_.size(); i++)
1222 if (!rows_[i].exists) {
1223 delete rows_[i].container;
1224 rows_.erase(rows_.begin() + i);
1225 any_exists = true;
1226 break;
1227 }
1228 } while (any_exists);
1229}
1230
1231void DecodeTrace::on_setting_changed(const QString &key, const QVariant &value)
1232{
1233 Trace::on_setting_changed(key, value);
1234
1235 if (key == GlobalSettings::Key_Dec_AlwaysShowAllRows) {
1236 max_visible_rows_ = 0;
1237 always_show_all_rows_ = value.toBool();
1238 }
1239}
1240
1241void DecodeTrace::on_new_annotations()
1242{
1243 if (!delayed_trace_updater_.isActive())
1244 delayed_trace_updater_.start();
1245}
1246
1247void DecodeTrace::on_delayed_trace_update()
1248{
1249 if (owner_)
1250 owner_->row_item_appearance_changed(false, true);
1251}
1252
1253void DecodeTrace::on_decode_reset()
1254{
1255 max_visible_rows_ = 0;
1256 update_rows();
1257
1258 if (owner_)
1259 owner_->row_item_appearance_changed(false, true);
1260}
1261
1262void DecodeTrace::on_decode_finished()
1263{
1264 if (owner_)
1265 owner_->row_item_appearance_changed(false, true);
1266}
1267
1268void DecodeTrace::on_pause_decode()
1269{
1270 if (decode_signal_->is_paused())
1271 decode_signal_->resume_decode();
1272 else
1273 decode_signal_->pause_decode();
1274}
1275
1276void DecodeTrace::on_delete()
1277{
1278 session_.remove_decode_signal(decode_signal_);
1279}
1280
1281void DecodeTrace::on_channel_selected(int)
1282{
1283 QComboBox *cb = qobject_cast<QComboBox*>(QObject::sender());
1284
1285 // Determine signal that was selected
1286 const data::SignalBase *signal =
1287 (data::SignalBase*)cb->itemData(cb->currentIndex()).value<void*>();
1288
1289 // Determine decode channel ID this combo box is the channel selector for
1290 const uint16_t id = channel_id_map_.at(cb);
1291
1292 decode_signal_->assign_signal(id, signal);
1293}
1294
1295void DecodeTrace::on_channels_updated()
1296{
1297 if (owner_)
1298 owner_->row_item_appearance_changed(false, true);
1299}
1300
1301void DecodeTrace::on_init_state_changed(int)
1302{
1303 QComboBox *cb = qobject_cast<QComboBox*>(QObject::sender());
1304
1305 // Determine inital pin state that was selected
1306 int init_state = cb->itemData(cb->currentIndex()).value<int>();
1307
1308 // Determine decode channel ID this combo box is the channel selector for
1309 const uint16_t id = init_state_map_.at(cb);
1310
1311 decode_signal_->set_initial_pin_state(id, init_state);
1312}
1313
1314void DecodeTrace::on_stack_decoder(srd_decoder *decoder)
1315{
1316 decode_signal_->stack_decoder(decoder);
1317 update_rows();
1318
1319 create_popup_form();
1320}
1321
1322void DecodeTrace::on_delete_decoder(int index)
1323{
1324 decode_signal_->remove_decoder(index);
1325 update_rows();
1326
1327 // Force re-calculation of the trace height
1328 max_visible_rows_ = 0;
1329 owner_->extents_changed(false, true);
1330
1331 create_popup_form();
1332}
1333
1334void DecodeTrace::on_show_hide_decoder(int index)
1335{
1336 const bool state = decode_signal_->toggle_decoder_visibility(index);
1337
1338 assert(index < (int)decoder_forms_.size());
1339 decoder_forms_[index]->set_decoder_visible(state);
1340
1341 if (!state) {
1342 // Force re-calculation of the trace height, see paint_mid()
1343 max_visible_rows_ = 0;
1344 owner_->extents_changed(false, true);
1345 }
1346
1347 if (owner_)
1348 owner_->row_item_appearance_changed(false, true);
1349}
1350
1351void DecodeTrace::on_copy_annotation_to_clipboard()
1352{
1353 using namespace pv::data::decode;
1354
1355 if (!selected_row_)
1356 return;
1357
1358 vector<Annotation> *annotations = new vector<Annotation>();
1359
1360 decode_signal_->get_annotation_subset(*annotations, *selected_row_,
1361 current_segment_, selected_sample_range_.first, selected_sample_range_.first);
1362
1363 if (annotations->empty())
1364 return;
1365
1366 QClipboard *clipboard = QApplication::clipboard();
1367 clipboard->setText(annotations->front().annotations().front(), QClipboard::Clipboard);
1368
1369 if (clipboard->supportsSelection())
1370 clipboard->setText(annotations->front().annotations().front(), QClipboard::Selection);
1371
1372 delete annotations;
1373}
1374
1375void DecodeTrace::on_export_row()
1376{
1377 selected_sample_range_ = make_pair(0, numeric_limits<uint64_t>::max());
1378 on_export_row_from_here();
1379}
1380
1381void DecodeTrace::on_export_all_rows()
1382{
1383 selected_sample_range_ = make_pair(0, numeric_limits<uint64_t>::max());
1384 on_export_all_rows_from_here();
1385}
1386
1387void DecodeTrace::on_export_row_with_cursor()
1388{
1389 const View *view = owner_->view();
1390 assert(view);
1391
1392 if (!view->cursors()->enabled())
1393 return;
1394
1395 const double samplerate = session_.get_samplerate();
1396
1397 const pv::util::Timestamp& start_time = view->cursors()->first()->time();
1398 const pv::util::Timestamp& end_time = view->cursors()->second()->time();
1399
1400 const uint64_t start_sample = (uint64_t)max(
1401 0.0, start_time.convert_to<double>() * samplerate);
1402 const uint64_t end_sample = (uint64_t)max(
1403 0.0, end_time.convert_to<double>() * samplerate);
1404
1405 // Are both cursors negative and thus were clamped to 0?
1406 if ((start_sample == 0) && (end_sample == 0))
1407 return;
1408
1409 selected_sample_range_ = make_pair(start_sample, end_sample);
1410 on_export_row_from_here();
1411}
1412
1413void DecodeTrace::on_export_all_rows_with_cursor()
1414{
1415 const View *view = owner_->view();
1416 assert(view);
1417
1418 if (!view->cursors()->enabled())
1419 return;
1420
1421 const double samplerate = session_.get_samplerate();
1422
1423 const pv::util::Timestamp& start_time = view->cursors()->first()->time();
1424 const pv::util::Timestamp& end_time = view->cursors()->second()->time();
1425
1426 const uint64_t start_sample = (uint64_t)max(
1427 0.0, start_time.convert_to<double>() * samplerate);
1428 const uint64_t end_sample = (uint64_t)max(
1429 0.0, end_time.convert_to<double>() * samplerate);
1430
1431 // Are both cursors negative and thus were clamped to 0?
1432 if ((start_sample == 0) && (end_sample == 0))
1433 return;
1434
1435 selected_sample_range_ = make_pair(start_sample, end_sample);
1436 on_export_all_rows_from_here();
1437}
1438
1439void DecodeTrace::on_export_row_from_here()
1440{
1441 using namespace pv::data::decode;
1442
1443 if (!selected_row_)
1444 return;
1445
1446 vector<Annotation> *annotations = new vector<Annotation>();
1447
1448 decode_signal_->get_annotation_subset(*annotations, *selected_row_,
1449 current_segment_, selected_sample_range_.first, selected_sample_range_.second);
1450
1451 if (annotations->empty())
1452 return;
1453
1454 export_annotations(annotations);
1455 delete annotations;
1456}
1457
1458void DecodeTrace::on_export_all_rows_from_here()
1459{
1460 using namespace pv::data::decode;
1461
1462 vector<Annotation> *annotations = new vector<Annotation>();
1463
1464 decode_signal_->get_annotation_subset(*annotations, current_segment_,
1465 selected_sample_range_.first, selected_sample_range_.second);
1466
1467 if (!annotations->empty())
1468 export_annotations(annotations);
1469
1470 delete annotations;
1471}
1472
1473void DecodeTrace::on_animation_timer()
1474{
1475 bool animation_finished = true;
1476
1477 for (RowData& r : rows_) {
1478 if (!(r.expanding || r.collapsing))
1479 continue;
1480
1481 unsigned int height_delta = r.expanded_height - default_row_height_;
1482
1483 if (r.expanding) {
1484 if (r.height < r.expanded_height) {
1485 r.anim_height += height_delta / (float)AnimationDurationInTicks;
1486 r.height = r.anim_height;
1487 r.anim_shape += ArrowSize / (float)AnimationDurationInTicks;
1488 animation_finished = false;
1489 } else {
1490 r.height = std::min(r.height, r.expanded_height);
1491 r.expanding = false;
1492 r.expanded = true;
1493 }
1494 }
1495
1496 if (r.collapsing) {
1497 if (r.height > default_row_height_) {
1498 r.anim_height -= height_delta / (float)AnimationDurationInTicks;
1499 r.height = r.anim_height;
1500 r.anim_shape -= ArrowSize / (float)AnimationDurationInTicks;
1501 animation_finished = false;
1502 } else {
1503 r.height = std::max(r.height, default_row_height_);
1504 r.collapsing = false;
1505 r.expanded = false;
1506 r.expand_marker_shape = default_marker_shape_;
1507 r.container->setVisible(false);
1508 }
1509 }
1510
1511 // The expansion marker shape switches between
1512 // 0/-A, A/0, 0/A (default state; anim_shape=0) and
1513 // 0/ 0, A/A, 2A/0 (expanded state; anim_shape=ArrowSize)
1514
1515 r.expand_marker_shape.setPoint(0, 0, -ArrowSize + r.anim_shape);
1516 r.expand_marker_shape.setPoint(1, ArrowSize, r.anim_shape);
1517 r.expand_marker_shape.setPoint(2, 2*r.anim_shape, ArrowSize - r.anim_shape);
1518
1519 r.container->resize(owner_->view()->viewport()->width() - r.container->pos().x(),
1520 r.height - 2 * default_row_height_);
1521 }
1522
1523 if (animation_finished)
1524 animation_timer_.stop();
1525
1526 if (owner_)
1527 owner_->row_item_appearance_changed(false, true);
1528}
1529
1530} // namespace trace
1531} // namespace views
1532} // namespace pv