]> sigrok.org Git - pulseview.git/blobdiff - pv/view/ruler.cpp
LogicSignal: Added an icon cache
[pulseview.git] / pv / view / ruler.cpp
index 86f73f95e457c7306cbc51e6e55b51e4de939417..56651169afee06f96813d2625475e0d717add254 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#include "ruler.h"
+#include "ruler.hpp"
 
-#include "view.h"
-#include "pv/util.h"
+#include "view.hpp"
+#include "pv/util.hpp"
 
 #include <extdef.h>
 
@@ -41,7 +41,7 @@ Ruler::Ruler(View &parent) :
 {
        setMouseTracking(true);
 
-       connect(&_view, SIGNAL(hover_point_changed()),
+       connect(&view_, SIGNAL(hover_point_changed()),
                this, SLOT(hover_point_changed()));
 }
 
@@ -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<double, unsigned int> 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();
@@ -97,9 +71,9 @@ void Ruler::paintEvent(QPaintEvent*)
 
        const double minor_tick_period = tick_period / MinorTickSubdivision;
        const double first_major_division =
-               floor(_view.offset() / tick_period);
+               floor(view_.offset() / tick_period);
        const double first_minor_division =
-               ceil(_view.offset() / minor_tick_period);
+               ceil(view_.offset() / minor_tick_period);
        const double t0 = first_major_division * tick_period;
 
        int division = (int)round(first_minor_division -
@@ -113,7 +87,7 @@ void Ruler::paintEvent(QPaintEvent*)
 
        do {
                const double t = t0 + division * minor_tick_period;
-               x = (t - _view.offset()) / _view.scale();
+               x = (t - view_.offset()) / view_.scale();
 
                if (division % MinorTickSubdivision == 0)
                {
@@ -141,7 +115,7 @@ void Ruler::paintEvent(QPaintEvent*)
 
 void Ruler::draw_hover_mark(QPainter &p)
 {
-       const int x = _view.hover_point().x();
+       const int x = view_.hover_point().x();
 
        if (x == -1)
                return;
@@ -163,5 +137,41 @@ void Ruler::hover_point_changed()
        update();
 }
 
+std::pair<double, unsigned int> 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