From: Soeren Apel Date: Mon, 10 Aug 2015 07:26:43 +0000 (+0200) Subject: View/Ruler: Calculate and use tick precision X-Git-Tag: pulseview-0.3.0~146 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d40f4db76fc8120fcd54ace8c6fce63387e025ca;p=pulseview.git View/Ruler: Calculate and use tick precision --- diff --git a/pv/view/ruler.cpp b/pv/view/ruler.cpp index 3ec75fc3..dfe7d1c5 100644 --- a/pv/view/ruler.cpp +++ b/pv/view/ruler.cpp @@ -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)); } diff --git a/pv/view/view.cpp b/pv/view/view.cpp index 0844b6a4..032546db 100644 --- a/pv/view/view.cpp +++ b/pv/view/view.cpp @@ -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() diff --git a/pv/view/view.hpp b/pv/view/view.hpp index 95a02084..ff7795c5 100644 --- a/pv/view/view.hpp +++ b/pv/view/view.hpp @@ -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_;