+pair<double, double> DecodeTrace::get_pixels_offset_samples_per_pixel() const
+{
+ shared_ptr<pv::data::DecoderStack> decoder_stack = base_->decoder_stack();
+
+ 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;
+
+ shared_ptr<pv::data::DecoderStack> decoder_stack = base_->decoder_stack();
+
+ 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);
+}
+