# HG changeset patch # User Chris Cannam # Date 1224173427 0 # Node ID 4ceb5264404d5f042decbd5946318b2cf52de4c6 # Parent ac349afdb23f876ca2034b86dde52465576066ee * Improve numerical display in time-value layer scale diff -r ac349afdb23f -r 4ceb5264404d layer/TimeValueLayer.cpp --- 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; }