X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=pv%2Fview%2Fruler.cpp;h=3cc2eb173404e0213c87d6161c198f2b00ad94c1;hb=18f7104f875a71bceeb152398b682737528a4ad7;hp=86f73f95e457c7306cbc51e6e55b51e4de939417;hpb=84a0d458a4b04141dd45af6dec44d36782ee163e;p=pulseview.git 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