comparison 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
comparison
equal deleted inserted replaced
433:ac349afdb23f 434:4ceb5264404d
810 paint.setPen(getColourForValue(v, val)); 810 paint.setPen(getColourForValue(v, val));
811 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1); 811 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
812 } 812 }
813 paint.restore(); 813 paint.restore();
814 } 814 }
815 815
816 float round = 1.f;
817 int dp = 0;
818 if (inc > 0) {
819 int prec = trunc(log10f(inc));
820 prec -= 1;
821 if (prec < 0) dp = -prec;
822 round = powf(10.f, prec);
823 // std::cerr << "inc = " << inc << ", round = " << round << std::endl;
824 }
825
826 int prevy = -1;
827
816 for (int i = 0; i < n; ++i) { 828 for (int i = 0; i < n; ++i) {
817 829
818 int y, ty; 830 int y, ty;
819 bool drawText = true; 831 bool drawText = true;
832
833 float dispval = val;
820 834
821 if (m_plotStyle == PlotSegmentation) { 835 if (m_plotStyle == PlotSegmentation) {
822 y = boxy + int(boxh - ((val - min) * boxh) / (max - min)); 836 y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
823 ty = y; 837 ty = y;
824 } else { 838 } else {
825 if (i == n-1) { 839 if (i == n-1) {
826 if (m_model->getScaleUnits() != "") drawText = false; 840 if (m_model->getScaleUnits() != "") drawText = false;
827 } 841 }
828 y = getYForValue(v, val); 842 dispval = lrintf(val / round) * round;
843 // std::cerr << "val = " << val << ", dispval = " << dispval << std::endl;
844 y = getYForValue(v, dispval);
829 ty = y - paint.fontMetrics().height() + 845 ty = y - paint.fontMetrics().height() +
830 paint.fontMetrics().ascent(); 846 paint.fontMetrics().ascent() + 2;
831 } 847
832 848 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
833 sprintf(buffer, "%.3f", val); 849 val += inc;
850 continue;
851 }
852 }
853
854 sprintf(buffer, "%.*f", dp, dispval);
834 QString label = QString(buffer); 855 QString label = QString(buffer);
835 856
836 if (m_plotStyle != PlotSegmentation) { 857 if (m_plotStyle != PlotSegmentation) {
837 paint.drawLine(w - 5, y, w, y); 858 paint.drawLine(w - 5, y, w, y);
838 } else { 859 } else {
839 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y); 860 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
840 } 861 }
841 862
842 if (drawText) paint.drawText(tx, ty, label); 863 if (drawText) {
864 paint.drawText(tx + w - paint.fontMetrics().width(label) - 8,
865 ty, label);
866 }
867
868 prevy = y;
843 val += inc; 869 val += inc;
844 } 870 }
845 871
846 if (m_model->getScaleUnits() != "") { 872 if (m_model->getScaleUnits() != "") {
847 paint.drawText(5, 5 + paint.fontMetrics().ascent(), 873 paint.drawText(5, 5 + paint.fontMetrics().ascent(),