]> sigrok.org Git - pulseview.git/commitdiff
Fixes
authorSoeren Apel <redacted>
Fri, 21 Jun 2019 20:53:17 +0000 (22:53 +0200)
committerSoeren Apel <redacted>
Thu, 4 Jul 2019 21:17:27 +0000 (23:17 +0200)
pv/util.cpp
pv/util.hpp
pv/views/trace/cursorpair.cpp
pv/views/trace/cursorpair.hpp
pv/views/trace/ruler.cpp
pv/views/trace/ruler.hpp

index 237ca7f059f1e589dac4dd9e6f6bc29120a6e3c5..9a9a5065a2e17180eb6cdebb71b4aefc07fb5833 100644 (file)
@@ -139,8 +139,7 @@ QString format_time_si(const Timestamp& v, SIPrefix prefix,
        if (sign && !v.is_zero())
                ts << forcesign;
        ts << qSetRealNumberPrecision(precision) << (v * multiplier);
        if (sign && !v.is_zero())
                ts << forcesign;
        ts << qSetRealNumberPrecision(precision) << (v * multiplier);
-       if (!unit.isNull())
-               ts << ' ' << prefix << unit;
+       ts << ' ' << prefix << unit;
 
        return s;
 }
 
        return s;
 }
@@ -162,6 +161,10 @@ QString format_value_si(double v, SIPrefix prefix, unsigned precision,
                                exp -= 3;
                        }
                }
                                exp -= 3;
                        }
                }
+
+               const int prefix_order = -exponent(prefix);
+               precision = (prefix >= SIPrefix::none) ? max((int)(precision + prefix_order), 0) :
+                       max((int)(precision - prefix_order), 0);
        }
 
        assert(prefix >= SIPrefix::yocto);
        }
 
        assert(prefix >= SIPrefix::yocto);
index dd7be222b073760b2ec5392ee222617d9717f869..e1640c4a374d8a9aed51231ae03a6b1c895486b1 100644 (file)
@@ -38,6 +38,7 @@ namespace pv {
 namespace util {
 
 enum class TimeUnit {
 namespace util {
 
 enum class TimeUnit {
+       None = 0,
        Time = 1,
        Samples = 2
 };
        Time = 1,
        Samples = 2
 };
index 562584c2f903fdde8f93b0bbd32005bc58c1673b..b940090ac8b68861d95ab5e16dec80f630b8cebc 100644 (file)
@@ -133,9 +133,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));
 
        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()) {
        text_size_ = p.boundingRect(QRectF(), 0, text).size();
 
        if (selected()) {
@@ -173,49 +173,47 @@ void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp)
        p.drawRect(l, pp.top(), r - l, pp.height());
 }
 
        p.drawRect(l, pp.top(), r - l, pp.height());
 }
 
-QString CursorPair::format_string(qreal max_width, std::function<qreal(const QString&)> query_size)
+QString CursorPair::format_string(int max_width, std::function<double(const QString&)> 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;
                label_incomplete_ = false;
-               return out;
+               return s;
        }
 
        label_incomplete_ = true;
 
        }
 
        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);
+
+       if (query_size(s) <= max_width)
+               return s;
 
 
-       // Give up.
+       // Give up
        return "...";
 }
 
        return "...";
 }
 
@@ -247,6 +245,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
 }
 
                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<double>(), 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
 } // namespace trace
 } // namespace views
 } // namespace pv
index 1dc66a32b8eef8bd622c199e75f9ddf09872dd65..f506c2305937dd23b62a8531461694e3c9854510 100644 (file)
@@ -102,8 +102,8 @@ public:
        /**
         * Constructs the string to display.
         */
        /**
         * Constructs the string to display.
         */
-       QString format_string(qreal max_width = 0, std::function<qreal(const QString&)> query_size
-                       = [](const QString& s) -> qreal { Q_UNUSED(s); return 0; });
+       QString format_string(int max_width = 0, std::function<double(const QString&)> query_size
+               = [](const QString& s) -> double { (void)s; return 0; });
 
        pair<float, float> get_cursor_offsets() const;
 
 
        pair<float, float> get_cursor_offsets() const;
 
@@ -112,6 +112,9 @@ public:
 public Q_SLOTS:
        void on_hover_point_changed(const QWidget* widget, const QPoint &hp);
 
 public Q_SLOTS:
        void on_hover_point_changed(const QWidget* widget, const QPoint &hp);
 
+private:
+       QString format_string_sub(int time_precision, int freq_precision, bool show_unit = true);
+
 private:
        shared_ptr<Cursor> first_, second_;
        QColor fill_color_;
 private:
        shared_ptr<Cursor> first_, second_;
        QColor fill_color_;
index 98f55ddb06fb72ebac091263abb65c062e87bfc5..401fc3da28e86e81d2c1ba3858bd1d4cb866a0e3 100644 (file)
@@ -88,8 +88,7 @@ QString Ruler::format_time_with_distance(
        pv::util::SIPrefix prefix,
        pv::util::TimeUnit unit,
        unsigned precision,
        pv::util::SIPrefix prefix,
        pv::util::TimeUnit unit,
        unsigned precision,
-       bool sign,
-       bool show_unit)
+       bool sign)
 {
        const unsigned limit = 60;
 
 {
        const unsigned limit = 60;
 
@@ -98,7 +97,12 @@ QString Ruler::format_time_with_distance(
 
        // If we have to use samples then we have no alternative formats
        if (unit == pv::util::TimeUnit::Samples)
 
        // If we have to use samples then we have no alternative formats
        if (unit == pv::util::TimeUnit::Samples)
-               return pv::util::format_time_si_adjusted(t, prefix, precision, show_unit ? "sa" : NULL, sign);
+               return pv::util::format_time_si_adjusted(t, prefix, precision, "sa", sign);
+
+       QString unit_string;
+       if (unit == pv::util::TimeUnit::Time)
+               unit_string = "s";
+       // Note: In case of pv::util::TimeUnit::None, unit_string remains empty
 
        // View zoomed way out -> low precision (0), big distance (>=60s)
        // -> DD:HH:MM
 
        // View zoomed way out -> low precision (0), big distance (>=60s)
        // -> DD:HH:MM
@@ -110,7 +114,7 @@ QString Ruler::format_time_with_distance(
        // View zoomed way in -> high precision (>3), low step size (<1s)
        // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds
        if (abs(t) < limit)
        // View zoomed way in -> high precision (>3), low step size (<1s)
        // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds
        if (abs(t) < limit)
-               return pv::util::format_time_si_adjusted(t, prefix, precision, show_unit ? "s" : NULL, sign);
+               return pv::util::format_time_si_adjusted(t, prefix, precision, unit_string, sign);
        else
                return pv::util::format_time_minutes(t, precision, sign);
 }
        else
                return pv::util::format_time_minutes(t, precision, sign);
 }
index 3a9fea910d26febacb20cba1d156fc2486210cbc..5035bfc83bffc9962f8a7d06d782c1fae5dcd232 100644 (file)
@@ -115,8 +115,7 @@ public:
                pv::util::SIPrefix prefix = pv::util::SIPrefix::unspecified,
                pv::util::TimeUnit unit = pv::util::TimeUnit::Time,
                unsigned precision = 0,
                pv::util::SIPrefix prefix = pv::util::SIPrefix::unspecified,
                pv::util::TimeUnit unit = pv::util::TimeUnit::Time,
                unsigned precision = 0,
-               bool sign = true,
-               bool show_unit = true);
+               bool sign = true);
 
        pv::util::Timestamp get_absolute_time_from_x_pos(uint32_t x) const;
        pv::util::Timestamp get_ruler_time_from_x_pos(uint32_t x) const;
 
        pv::util::Timestamp get_absolute_time_from_x_pos(uint32_t x) const;
        pv::util::Timestamp get_ruler_time_from_x_pos(uint32_t x) const;