From abad24e2d61c0c84ac8495c22d29a8a3a49ee9fa Mon Sep 17 00:00:00 2001 From: Jens Steinhauser Date: Wed, 21 May 2014 15:02:13 +0200 Subject: [PATCH] CursorHeader: Use the same number format as the ruler. --- pv/view/cursorheader.cpp | 6 +++- pv/view/ruler.cpp | 70 +++++++++++++++++++++++----------------- pv/view/ruler.h | 12 +++++++ 3 files changed, 57 insertions(+), 31 deletions(-) diff --git a/pv/view/cursorheader.cpp b/pv/view/cursorheader.cpp index 861cd70f..6592acd4 100644 --- a/pv/view/cursorheader.cpp +++ b/pv/view/cursorheader.cpp @@ -20,6 +20,7 @@ #include "cursorheader.h" +#include "ruler.h" #include "view.h" #include @@ -68,9 +69,12 @@ void CursorHeader::paintEvent(QPaintEvent*) QPainter p(this); p.setRenderHint(QPainter::Antialiasing); + unsigned int prefix = pv::view::Ruler::calculate_tick_spacing( + p, _view.scale(), _view.offset()).second; + // Draw the cursors if (_view.cursors_shown()) { - _view.cursors().draw_markers(p, rect(), 0); //prefix); + _view.cursors().draw_markers(p, rect(), prefix); } } diff --git a/pv/view/ruler.cpp b/pv/view/ruler.cpp index 86f73f95..3cc2eb17 100644 --- a/pv/view/ruler.cpp +++ b/pv/view/ruler.cpp @@ -52,42 +52,16 @@ QSize Ruler::sizeHint() const void Ruler::paintEvent(QPaintEvent*) { - const double SpacingIncrement = 32.0f; - const double MinValueSpacing = 32.0f; const int ValueMargin = 3; QPainter p(this); p.setRenderHint(QPainter::Antialiasing); - double min_width = SpacingIncrement, typical_width; - double tick_period; - unsigned int prefix; - - // Find tick spacing, and number formatting that does not cause - // value to collide. - do - { - const double min_period = _view.scale() * min_width; - - const int order = (int)floorf(log10f(min_period)); - const double order_decimal = pow(10.0, order); - - unsigned int unit = 0; - - do - { - tick_period = order_decimal * ScaleUnits[unit++]; - } while (tick_period < min_period && unit < countof(ScaleUnits)); - - prefix = (order - pv::util::FirstSIPrefixPower) / 3; + std::pair spacing = + calculate_tick_spacing(p, _view.scale(), _view.offset()); - typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX, - AlignLeft | AlignTop, pv::util::format_time(_view.offset(), - prefix)).width() + MinValueSpacing; - - min_width += SpacingIncrement; - - } while(typical_width > tick_period / _view.scale()); + double tick_period = spacing.first; + unsigned int prefix = spacing.second; const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX, AlignLeft | AlignTop, "8").height(); @@ -163,5 +137,41 @@ void Ruler::hover_point_changed() update(); } +std::pair Ruler::calculate_tick_spacing( + QPainter& p, double scale, double offset) +{ + const double SpacingIncrement = 32.0f; + const double MinValueSpacing = 32.0f; + + double min_width = SpacingIncrement, typical_width; + + double tick_period; + unsigned int prefix; + + do { + const double min_period = scale * min_width; + + const int order = (int)floorf(log10f(min_period)); + const double order_decimal = pow(10.0, order); + + unsigned int unit = 0; + + do { + tick_period = order_decimal * ScaleUnits[unit++]; + } while (tick_period < min_period && unit < countof(ScaleUnits)); + + prefix = (order - pv::util::FirstSIPrefixPower) / 3; + + typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX, + AlignLeft | AlignTop, pv::util::format_time(offset, + prefix)).width() + MinValueSpacing; + + min_width += SpacingIncrement; + + } while(typical_width > tick_period / scale); + + return std::make_pair(tick_period, prefix); +} + } // namespace view } // namespace pv diff --git a/pv/view/ruler.h b/pv/view/ruler.h index cee76c68..08a9c0e1 100644 --- a/pv/view/ruler.h +++ b/pv/view/ruler.h @@ -42,6 +42,18 @@ private: public: Ruler(View &parent); + /** + * Find a tick spacing and number formatting that does not cause + * the values to collide. + * @param p A QPainter used to determine the needed space for the values. + * @param scale A pv::view::View's scale. + * @param offset A pv::view::View's offset. + * + * @return The tick period to use in 'first' and the prefix in 'second'. + */ + static std::pair calculate_tick_spacing( + QPainter& p, double scale, double offset); + public: QSize sizeHint() const; -- 2.30.2