# HG changeset patch # User Chris Cannam # Date 1493889863 -3600 # Node ID 0cc6ab236b0d8dbbecdb8aac46b08ae07991b0c1 # Parent 14dcdc596baf59adec3f8507c302e0424ed76de1 Make use of ScaleTickIntervals in linear numerical scale (not log yet) diff -r 14dcdc596baf -r 0cc6ab236b0d layer/LinearNumericalScale.cpp --- a/layer/LinearNumericalScale.cpp Sun Mar 12 13:37:01 2017 +0000 +++ b/layer/LinearNumericalScale.cpp Thu May 04 10:24:23 2017 +0100 @@ -22,6 +22,8 @@ #include "LayerGeometryProvider.h" +#include "base/ScaleTickIntervals.h" + int LinearNumericalScale::getWidth(LayerGeometryProvider *, QPainter &paint) @@ -39,58 +41,33 @@ { int n = 10; - double val = minf; - double inc = (maxf - val) / n; - - const int buflen = 40; - char buffer[buflen]; + auto ticks = ScaleTickIntervals::linear({ minf, maxf, n }); int w = getWidth(v, paint) + x0; - double round = 1.0; - int dp = 0; - if (inc > 0) { - int prec = int(trunc(log10(inc))); - prec -= 1; - if (prec < 0) dp = -prec; - round = pow(10.0, prec); -#ifdef DEBUG_TIME_VALUE_LAYER - cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl; -#endif - } - int prevy = -1; - for (int i = 0; i < n; ++i) { + for (int i = 0; i < int(ticks.ticks.size()); ++i) { int y, ty; bool drawText = true; - double dispval = val; - if (i == n-1 && v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) { if (layer->getScaleUnits() != "") drawText = false; } - dispval = int(rint(val / round) * round); -#ifdef DEBUG_TIME_VALUE_LAYER - cerr << "val = " << val << ", dispval = " << dispval << endl; -#endif - - y = layer->getYForValue(v, dispval); + double val = ticks.ticks[i].value; + QString label = QString::fromStdString(ticks.ticks[i].label); + + y = layer->getYForValue(v, val); ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2; if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) { - val += inc; continue; } - snprintf(buffer, buflen, "%.*f", dp, dispval); - - QString label = QString(buffer); - paint.drawLine(w - 5, y, w, y); if (drawText) { @@ -99,6 +76,5 @@ } prevy = y; - val += inc; } }