+ const list< shared_ptr<Decoder> > &stack = decoder_stack_->stack();
+
+ // We get the logic data of the first channel in the list.
+ // This works because we are currently assuming all
+ // LogicSignals have the same data/segment
+ for (const shared_ptr<Decoder> &dec : stack)
+ if (dec && !dec->channels().empty() &&
+ ((logic_signal = (*dec->channels().begin()).second)) &&
+ ((data = logic_signal->logic_data())))
+ break;
+
+ if (!data || data->logic_segments().empty())
+ return;
+
+ const shared_ptr<LogicSegment> segment =
+ data->logic_segments().front();
+ assert(segment);
+ const int64_t sample_count = (int64_t)segment->get_sample_count();
+ if (sample_count == 0)
+ return;
+
+ const int64_t samples_decoded = decoder_stack_->samples_decoded();
+ if (sample_count == samples_decoded)
+ return;
+
+ const int y = get_visual_y();
+
+ tie(pixels_offset, samples_per_pixel) =
+ get_pixels_offset_samples_per_pixel();
+
+ const double start = max(samples_decoded /
+ samples_per_pixel - pixels_offset, left - 1.0);
+ const double end = min(sample_count / samples_per_pixel -
+ pixels_offset, right + 1.0);
+ const QRectF no_decode_rect(start, y - h/2 + 0.5, end - start, h);
+
+ p.setPen(QPen(Qt::NoPen));
+ p.setBrush(Qt::white);
+ p.drawRect(no_decode_rect);
+
+ p.setPen(NoDecodeColour);
+ p.setBrush(QBrush(NoDecodeColour, Qt::Dense6Pattern));
+ p.drawRect(no_decode_rect);
+}
+
+pair<double, double> DecodeTrace::get_pixels_offset_samples_per_pixel() const
+{
+ assert(owner_);
+ assert(decoder_stack_);
+
+ const View *view = owner_->view();
+ assert(view);
+
+ const double scale = view->scale();
+ assert(scale > 0);
+
+ const double pixels_offset =
+ ((view->offset() - decoder_stack_->start_time()) / scale).convert_to<double>();
+
+ double samplerate = decoder_stack_->samplerate();
+
+ // Show sample rate as 1Hz when it is unknown
+ if (samplerate == 0.0)
+ samplerate = 1.0;
+
+ return make_pair(pixels_offset, samplerate * scale);
+}
+
+pair<uint64_t, uint64_t> DecodeTrace::get_sample_range(
+ int x_start, int x_end) const
+{
+ double samples_per_pixel, pixels_offset;
+ tie(pixels_offset, samples_per_pixel) =
+ get_pixels_offset_samples_per_pixel();
+
+ const uint64_t start = (uint64_t)max(
+ (x_start + pixels_offset) * samples_per_pixel, 0.0);
+ const uint64_t end = (uint64_t)max(
+ (x_end + pixels_offset) * samples_per_pixel, 0.0);
+
+ return make_pair(start, end);
+}
+
+int DecodeTrace::get_row_at_point(const QPoint &point)
+{
+ if (!row_height_)
+ return -1;
+
+ const int y = (point.y() - get_visual_y() + row_height_ / 2);
+
+ /* Integer divison of (x-1)/x would yield 0, so we check for this. */
+ if (y < 0)
+ return -1;
+
+ const int row = y / row_height_;
+
+ if (row >= (int)visible_rows_.size())
+ return -1;
+
+ return row;
+}
+
+const QString DecodeTrace::get_annotation_at_point(const QPoint &point)
+{
+ using namespace pv::data::decode;
+
+ if (!enabled())
+ return QString();
+
+ const pair<uint64_t, uint64_t> sample_range =
+ get_sample_range(point.x(), point.x() + 1);
+ const int row = get_row_at_point(point);
+ if (row < 0)
+ return QString();
+
+ vector<pv::data::decode::Annotation> annotations;
+
+ assert(decoder_stack_);
+ decoder_stack_->get_annotation_subset(annotations, visible_rows_[row],
+ sample_range.first, sample_range.second);
+
+ return (annotations.empty()) ?
+ QString() : annotations[0].annotations().front();
+}
+
+void DecodeTrace::hover_point_changed()
+{
+ assert(owner_);
+
+ const View *const view = owner_->view();
+ assert(view);
+
+ QPoint hp = view->hover_point();
+ QString ann = get_annotation_at_point(hp);
+
+ assert(view);
+
+ if (!row_height_ || ann.isEmpty()) {
+ QToolTip::hideText();
+ return;
+ }
+
+ const int hover_row = get_row_at_point(hp);
+
+ QFontMetrics m(QToolTip::font());
+ const QRect text_size = m.boundingRect(QRect(), 0, ann);
+
+ // This is OS-specific and unfortunately we can't query it, so
+ // use an approximation to at least try to minimize the error.
+ const int padding = 8;
+
+ // Make sure the tool tip doesn't overlap with the mouse cursor.
+ // If it did, the tool tip would constantly hide and re-appear.
+ // We also push it up by one row so that it appears above the
+ // decode trace, not below.
+ hp.setX(hp.x() - (text_size.width() / 2) - padding);
+
+ hp.setY(get_visual_y() - (row_height_ / 2) +
+ (hover_row * row_height_) -
+ row_height_ - text_size.height() - padding);
+
+ QToolTip::showText(view->viewport()->mapToGlobal(hp), ann);
+}
+
+void DecodeTrace::create_decoder_form(int index,
+ shared_ptr<data::decode::Decoder> &dec, QWidget *parent,
+ QFormLayout *form)
+{
+ const GSList *l;
+
+ assert(dec);
+ const srd_decoder *const decoder = dec->decoder();
+ assert(decoder);
+
+ const bool decoder_deletable = index > 0;
+
+ pv::widgets::DecoderGroupBox *const group =
+ new pv::widgets::DecoderGroupBox(
+ QString::fromUtf8(decoder->name), nullptr, decoder_deletable);
+ group->set_decoder_visible(dec->shown());
+
+ if (decoder_deletable) {
+ delete_mapper_.setMapping(group, index);
+ connect(group, SIGNAL(delete_decoder()), &delete_mapper_, SLOT(map()));
+ }
+
+ show_hide_mapper_.setMapping(group, index);
+ connect(group, SIGNAL(show_hide_decoder()),
+ &show_hide_mapper_, SLOT(map()));
+
+ QFormLayout *const decoder_form = new QFormLayout;
+ group->add_layout(decoder_form);
+
+ // Add the mandatory channels
+ for (l = decoder->channels; l; l = l->next) {
+ const struct srd_channel *const pdch =
+ (struct srd_channel *)l->data;
+ QComboBox *const combo = create_channel_selector(parent, dec, pdch);
+ connect(combo, SIGNAL(currentIndexChanged(int)),
+ this, SLOT(on_channel_selected(int)));
+ decoder_form->addRow(tr("<b>%1</b> (%2) *")
+ .arg(QString::fromUtf8(pdch->name))
+ .arg(QString::fromUtf8(pdch->desc)), combo);
+
+ const ChannelSelector s = {combo, dec, pdch};
+ channel_selectors_.push_back(s);
+ }
+
+ // Add the optional channels
+ for (l = decoder->opt_channels; l; l = l->next) {
+ const struct srd_channel *const pdch =
+ (struct srd_channel *)l->data;
+ QComboBox *const combo = create_channel_selector(parent, dec, pdch);
+ connect(combo, SIGNAL(currentIndexChanged(int)),
+ this, SLOT(on_channel_selected(int)));
+ decoder_form->addRow(tr("<b>%1</b> (%2)")
+ .arg(QString::fromUtf8(pdch->name))
+ .arg(QString::fromUtf8(pdch->desc)), combo);
+
+ const ChannelSelector s = {combo, dec, pdch};
+ channel_selectors_.push_back(s);
+ }
+
+ // Add the options
+ shared_ptr<binding::Decoder> binding(
+ new binding::Decoder(decoder_stack_, dec));
+ binding->add_properties_to_form(decoder_form, true);
+
+ bindings_.push_back(binding);
+
+ form->addRow(group);
+ decoder_forms_.push_back(group);
+}
+
+QComboBox* DecodeTrace::create_channel_selector(
+ QWidget *parent, const shared_ptr<data::decode::Decoder> &dec,
+ const srd_channel *const pdch)
+{
+ assert(dec);
+
+ const auto sigs(session_.signals());
+
+ vector< shared_ptr<Signal> > sig_list(sigs.begin(), sigs.end());
+ std::sort(sig_list.begin(), sig_list.end(),
+ [](const shared_ptr<Signal> &a, const shared_ptr<Signal> b) {
+ return a->name().compare(b->name()) < 0; });
+
+ assert(decoder_stack_);
+ const auto channel_iter = dec->channels().find(pdch);