]> sigrok.org Git - pulseview.git/commitdiff
View/Ruler: Calculate and use tick precision
authorSoeren Apel <redacted>
Mon, 10 Aug 2015 07:26:43 +0000 (09:26 +0200)
committerUwe Hermann <redacted>
Sun, 16 Aug 2015 16:54:18 +0000 (18:54 +0200)
pv/view/ruler.cpp
pv/view/view.cpp
pv/view/view.hpp

index 3ec75fc307ed7df0ec6d0b2e9a132510e32d2996..dfe7d1c555161d6d1d9ca589bfca099695cfc5ab 100644 (file)
@@ -91,7 +91,6 @@ void Ruler::paintEvent(QPaintEvent*)
        p.setRenderHint(QPainter::Antialiasing);
 
        const double tick_period = view_.tick_period();
-       const unsigned int prefix = view_.tick_prefix();
 
        // Draw the tick marks
        p.setPen(palette().color(foregroundRole()));
@@ -122,7 +121,8 @@ void Ruler::paintEvent(QPaintEvent*)
                        // Draw a major tick
                        p.drawText(x, ValueMargin, 0, text_height,
                                AlignCenter | AlignTop | TextDontClip,
-                               util::format_time(t, prefix, view_.time_unit()));
+                               util::format_time(t, view_.tick_prefix(), view_.time_unit(),
+                                       view_.tick_precision()));
                        p.drawLine(QPointF(x, major_tick_y1),
                                QPointF(x, ruler_height));
                }
index 0844b6a47e6fd13c16d34ba85eddfc7850ec3974..032546dba034eda4cea63cc3f60489127cd28d9d 100644 (file)
@@ -104,6 +104,7 @@ View::View(Session &session, QWidget *parent) :
        always_zoom_to_fit_(false),
        tick_period_(0.0),
        tick_prefix_(0),
+       tick_precision_(0),
        time_unit_(util::Time),
        show_cursors_(false),
        cursors_(new CursorPair(*this)),
@@ -236,6 +237,11 @@ unsigned int View::tick_prefix() const
        return tick_prefix_;
 }
 
+unsigned int View::tick_precision() const
+{
+       return tick_precision_;
+}
+
 double View::tick_period() const
 {
        return tick_period_;
@@ -490,7 +496,8 @@ void View::calculate_tick_spacing()
        const double SpacingIncrement = 32.0f;
        const double MinValueSpacing = 32.0f;
 
-       double min_width = SpacingIncrement, typical_width;
+       double min_width = SpacingIncrement;
+       double label_width, tick_period_width;
 
        QFontMetrics m(QApplication::font());
 
@@ -509,14 +516,22 @@ void View::calculate_tick_spacing()
 
                tick_prefix_ = (order - pv::util::FirstSIPrefixPower) / 3;
 
-               typical_width = m.boundingRect(0, 0, INT_MAX, INT_MAX,
-                       Qt::AlignLeft | Qt::AlignTop,
-                       format_time(offset_, tick_prefix_, time_unit_)).width() +
+               // Precision is the number of fractional digits required, not
+               // taking the prefix into account (and it must never be negative)
+               tick_precision_ = std::max((int)ceil(log10f(1 / tick_period_)), 0);
+
+               tick_period_width = tick_period_ / scale_;
+
+               const QString label_text =
+                       format_time(offset_, tick_prefix_, time_unit_, tick_precision_);
+
+               label_width = m.boundingRect(0, 0, INT_MAX, INT_MAX,
+                       Qt::AlignLeft | Qt::AlignTop, label_text).width() +
                                MinValueSpacing;
 
                min_width += SpacingIncrement;
 
-       } while (typical_width > tick_period_ / scale_);
+       } while (tick_period_width < label_width);
 }
 
 void View::update_scroll()
index 95a02084dca1d95cafa28949b24dccd5441d4930..ff7795c5a68dbd55b92204d7236817e2dce35182 100644 (file)
@@ -125,6 +125,11 @@ public:
         */
        unsigned int tick_prefix() const;
 
+       /**
+        * Returns the number of fractional digits shown for the time markings.
+        */
+       unsigned int tick_precision() const;
+
        /**
         * Returns period of the graticule time markings.
         */
@@ -315,6 +320,7 @@ private:
 
        double tick_period_;
        unsigned int tick_prefix_;
+       unsigned int tick_precision_;
        util::TimeUnit time_unit_;
 
        bool show_cursors_;