changeset 1261:f683b191dbe6

Merge from branch "scale-ticks"
author Chris Cannam
date Mon, 10 Jul 2017 14:24:25 +0100
parents 14dcdc596baf (current diff) cdaeff1858af (diff)
children a9c2e791ab8d
files
diffstat 2 files changed, 17 insertions(+), 81 deletions(-) [+]
line wrap: on
line diff
--- a/layer/LinearNumericalScale.cpp	Sun Mar 12 13:37:01 2017 +0000
+++ b/layer/LinearNumericalScale.cpp	Mon Jul 10 14:24:25 2017 +0100
@@ -22,6 +22,8 @@
 
 #include "LayerGeometryProvider.h"
 
+#include "base/ScaleTickIntervals.h"
+
 int
 LinearNumericalScale::getWidth(LayerGeometryProvider *,
 			       QPainter &paint)
@@ -38,59 +40,34 @@
 				    double maxf)
 {
     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 });
+    n = int(ticks.size());
 
     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;
 
-    int prevy = -1;
-                
     for (int i = 0; i < n; ++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[i].value;
+        QString label = QString::fromStdString(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;
     }
 }
--- a/layer/LogNumericalScale.cpp	Sun Mar 12 13:37:01 2017 +0000
+++ b/layer/LogNumericalScale.cpp	Mon Jul 10 14:24:25 2017 +0100
@@ -24,7 +24,7 @@
 
 #include "LayerGeometryProvider.h"
 
-//#define DEBUG_TIME_VALUE_LAYER 1
+#include "base/ScaleTickIntervals.h"
 
 int
 LogNumericalScale::getWidth(LayerGeometryProvider *,
@@ -41,36 +41,12 @@
 				 double minlog,
 				 double maxlog)
 {
+    int n = 10;
+    auto ticks = ScaleTickIntervals::logarithmicAlready({ minlog, maxlog, n });
+    n = int(ticks.size());
+
     int w = getWidth(v, paint) + x0;
 
-    int n = 10;
-
-    double val = minlog;
-    double inc = (maxlog - val) / n; // even increments of log scale
-
-    // smallest increment as displayed
-    double minDispInc = LogRange::unmap(minlog + inc) - LogRange::unmap(minlog);
-
-#ifdef DEBUG_TIME_VALUE_LAYER
-    cerr << "min = " << minlog << ", max = " << maxlog << ", inc = " << inc << ", minDispInc = " << minDispInc << endl;
-#endif
-
-    const int buflen = 40;
-    char buffer[buflen];
-
-    double round = 1.f;
-    int dp = 0;
-
-    if (minDispInc > 0) {
-        int prec = int(trunc(log10(minDispInc)));
-        if (prec < 0) dp = -prec;
-        round = pow(10.0, prec);
-        if (dp > 4) dp = 4;
-#ifdef DEBUG_TIME_VALUE_LAYER
-        cerr << "round = " << round << ", prec = " << prec << ", dp = " << dp << endl;
-#endif
-    }
-
     int prevy = -1;
                 
     for (int i = 0; i < n; ++i) {
@@ -83,32 +59,17 @@
 	    if (layer->getScaleUnits() != "") drawText = false;
 	}
 
-        double dispval = LogRange::unmap(val);
-	dispval = floor(dispval / round) * round;
+        double val = ticks[i].value;
+        QString label = QString::fromStdString(ticks[i].label);
 
-#ifdef DEBUG_TIME_VALUE_LAYER
-	cerr << "val = " << val << ", dispval = " << dispval << endl;
-#endif
-
-	y = layer->getYForValue(v, dispval);
+	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;
         }
 
-	int digits = int(trunc(log10(dispval)));
-	int sf = dp + (digits > 0 ? digits : 0);
-	if (sf < 4) sf = 4;
-#ifdef DEBUG_TIME_VALUE_LAYER
-        cerr << "sf = " << sf << endl;
-#endif
-	snprintf(buffer, buflen, "%.*g", sf, dispval);
-
-	QString label = QString(buffer);
-
 	paint.drawLine(w - 5, y, w, y);
 
         if (drawText) {
@@ -117,6 +78,5 @@
         }
 
         prevy = y;
-	val += inc;
     }
 }