X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=pv%2Fviews%2Ftrace%2Fcursorpair.cpp;h=3e3a6b8e467e3b3fe1e7087f7c2b9e681b7ecedb;hb=2bdc5796c866b9494b2c40051e973e4385d9c46f;hp=562584c2f903fdde8f93b0bbd32005bc58c1673b;hpb=ef85cfa4599d3741e7007921e39a44490e97cfaf;p=pulseview.git diff --git a/pv/views/trace/cursorpair.cpp b/pv/views/trace/cursorpair.cpp index 562584c2..3e3a6b8e 100644 --- a/pv/views/trace/cursorpair.cpp +++ b/pv/views/trace/cursorpair.cpp @@ -45,7 +45,8 @@ const int CursorPair::DeltaPadding = 8; CursorPair::CursorPair(View &view) : TimeItem(view), first_(new Cursor(view, 0.0)), - second_(new Cursor(view, 1.0)) + second_(new Cursor(view, 1.0)), + label_incomplete_(true) { GlobalSettings::add_change_handler(this); @@ -84,11 +85,24 @@ void CursorPair::set_time(const pv::util::Timestamp& time) second_->set_time(time + delta); } +const pv::util::Timestamp CursorPair::time() const +{ + return 0; +} + float CursorPair::get_x() const { return (first_->get_x() + second_->get_x()) / 2.0f; } +const pv::util::Timestamp CursorPair::delta(const pv::util::Timestamp& other) const +{ + if (other < second_->time()) + return other - first_->time(); + else + return other - second_->time(); +} + QPoint CursorPair::drag_point(const QRect &rect) const { return first_->drag_point(rect); @@ -133,9 +147,9 @@ void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover) const int radius = delta_rect.height() / 2; QRectF text_rect(delta_rect.intersected(rect).adjusted(radius, 0, -radius, 0)); - QString text = format_string(text_rect.width(), [&p](const QString& s) -> qreal { - return p.boundingRect(QRectF(), 0, s).width(); - }); + QString text = format_string(text_rect.width(), + [&p](const QString& s) -> double { return p.boundingRect(QRectF(), 0, s).width(); }); + text_size_ = p.boundingRect(QRectF(), 0, text).size(); if (selected()) { @@ -173,49 +187,47 @@ void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp) p.drawRect(l, pp.top(), r - l, pp.height()); } -QString CursorPair::format_string(qreal max_width, std::function query_size) +QString CursorPair::format_string(int max_width, std::function query_size) { - constexpr int time_precision = 12; - constexpr int freq_precision = 4; - - const pv::util::SIPrefix prefix = view_.tick_prefix(); - const pv::util::Timestamp diff = abs(second_->time() - first_->time()); + int time_precision = 12; + int freq_precision = 12; - const QString time = Ruler::format_time_with_distance( - diff, diff, prefix, view_.time_unit(), time_precision, false); - const QString freq = util::format_time_si( - 1 / diff, pv::util::SIPrefix::unspecified, freq_precision, "Hz", false); - const QString out = QString("%1 / %2").arg(time, freq); + QString s = format_string_sub(time_precision, freq_precision); - // Try full "{time} ms / {freq} Hz" format - if (max_width <= 0 || query_size(out) <= max_width) { + // Try full "{time} s / {freq} Hz" format + if ((max_width <= 0) || (query_size(s) <= max_width)) { label_incomplete_ = false; - return out; + return s; } label_incomplete_ = true; - // Try just "{time}ms" format and gradually reduce time precision down to zero - for (int shrinkage=0; shrinkage <= time_precision; shrinkage++) { - int prec = time_precision - shrinkage ; + // Gradually reduce time precision to match frequency precision + while (time_precision > freq_precision) { + time_precision--; + + s = format_string_sub(time_precision, freq_precision); + if (query_size(s) <= max_width) + return s; + } - const QString time = Ruler::format_time_with_distance( - diff, diff, prefix, view_.time_unit(), - prec, false); + // Gradually reduce both precisions down to zero + while (time_precision > 0) { + time_precision--; + freq_precision--; - if (query_size(time) <= max_width) - return time; + s = format_string_sub(time_precision, freq_precision); + if (query_size(s) <= max_width) + return s; } - // Try no trailing digits and drop the unit to at least display something. The unit should be obvious from the ruler - // anyway. - const QString bare_number = Ruler::format_time_with_distance( - diff, diff, prefix, view_.time_unit(), - 0, false, false); - if (query_size(bare_number) <= max_width) - return bare_number; + // Try no trailing digits and drop the unit to at least display something + s = format_string_sub(0, 0, false); - // Give up. + if (query_size(s) <= max_width) + return s; + + // Give up return "..."; } @@ -247,6 +259,27 @@ void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp) QToolTip::hideText(); // TODO Will break other tooltips when there can be others } +QString CursorPair::format_string_sub(int time_precision, int freq_precision, bool show_unit) +{ + const pv::util::SIPrefix prefix = view_.tick_prefix(); + const pv::util::Timestamp diff = abs(second_->time() - first_->time()); + + const QString time = Ruler::format_time_with_distance( + diff, diff, prefix, (show_unit ? view_.time_unit() : pv::util::TimeUnit::None), + time_precision, false); + + // We can only show a frequency when there's a time base + if (view_.time_unit() == pv::util::TimeUnit::Time) { + const QString freq = util::format_value_si( + 1 / diff.convert_to(), pv::util::SIPrefix::unspecified, + freq_precision, (show_unit ? "Hz" : nullptr), false); + + return QString("%1 / %2").arg(time, freq); + } else + // In this case, we return the number of samples, really + return time; +} + } // namespace trace } // namespace views } // namespace pv