diff layer/TimeValueLayer.cpp @ 434:4ceb5264404d

* Improve numerical display in time-value layer scale
author Chris Cannam
date Thu, 16 Oct 2008 16:10:27 +0000
parents bcaeb2ba9b74
children 6324461340b1
line wrap: on
line diff
--- a/layer/TimeValueLayer.cpp	Thu Oct 16 13:38:33 2008 +0000
+++ b/layer/TimeValueLayer.cpp	Thu Oct 16 16:10:27 2008 +0000
@@ -812,12 +812,26 @@
         }
         paint.restore();
     }
+    
+    float round = 1.f;
+    int dp = 0;
+    if (inc > 0) {
+        int prec = trunc(log10f(inc));
+        prec -= 1;
+        if (prec < 0) dp = -prec;
+        round = powf(10.f, prec);
+//        std::cerr << "inc = " << inc << ", round = " << round << std::endl;
+    }
 
+    int prevy = -1;
+                
     for (int i = 0; i < n; ++i) {
 
 	int y, ty;
         bool drawText = true;
 
+        float dispval = val;
+
         if (m_plotStyle == PlotSegmentation) {
             y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
             ty = y;
@@ -825,12 +839,19 @@
             if (i == n-1) {
                 if (m_model->getScaleUnits() != "") drawText = false;
             }
-            y = getYForValue(v, val);
+            dispval = lrintf(val / round) * round;
+//            std::cerr << "val = " << val << ", dispval = " << dispval << std::endl;
+            y = getYForValue(v, dispval);
             ty = y - paint.fontMetrics().height() +
-                     paint.fontMetrics().ascent();
+                     paint.fontMetrics().ascent() + 2;
+
+            if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
+                val += inc;
+                continue;
+            }
         }
 
-	sprintf(buffer, "%.3f", val);
+	sprintf(buffer, "%.*f", dp, dispval);
 	QString label = QString(buffer);
 
         if (m_plotStyle != PlotSegmentation) {
@@ -839,7 +860,12 @@
             paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
         }
 
-        if (drawText) paint.drawText(tx, ty, label);
+        if (drawText) {
+            paint.drawText(tx + w - paint.fontMetrics().width(label) - 8,
+                           ty, label);
+        }
+
+        prevy = y;
 	val += inc;
     }