comparison layer/LinearNumericalScale.cpp @ 1261:f683b191dbe6

Merge from branch "scale-ticks"
author Chris Cannam
date Mon, 10 Jul 2017 14:24:25 +0100
parents cdaeff1858af
children a34a2a25907c
comparison
equal deleted inserted replaced
1257:14dcdc596baf 1261:f683b191dbe6
20 20
21 #include <cmath> 21 #include <cmath>
22 22
23 #include "LayerGeometryProvider.h" 23 #include "LayerGeometryProvider.h"
24 24
25 #include "base/ScaleTickIntervals.h"
26
25 int 27 int
26 LinearNumericalScale::getWidth(LayerGeometryProvider *, 28 LinearNumericalScale::getWidth(LayerGeometryProvider *,
27 QPainter &paint) 29 QPainter &paint)
28 { 30 {
29 return paint.fontMetrics().width("-000.00") + 10; 31 return paint.fontMetrics().width("-000.00") + 10;
36 int x0, 38 int x0,
37 double minf, 39 double minf,
38 double maxf) 40 double maxf)
39 { 41 {
40 int n = 10; 42 int n = 10;
41 43 auto ticks = ScaleTickIntervals::linear({ minf, maxf, n });
42 double val = minf; 44 n = int(ticks.size());
43 double inc = (maxf - val) / n;
44
45 const int buflen = 40;
46 char buffer[buflen];
47 45
48 int w = getWidth(v, paint) + x0; 46 int w = getWidth(v, paint) + x0;
49 47
50 double round = 1.0; 48 int prevy = -1;
51 int dp = 0;
52 if (inc > 0) {
53 int prec = int(trunc(log10(inc)));
54 prec -= 1;
55 if (prec < 0) dp = -prec;
56 round = pow(10.0, prec);
57 #ifdef DEBUG_TIME_VALUE_LAYER
58 cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl;
59 #endif
60 }
61 49
62 int prevy = -1;
63
64 for (int i = 0; i < n; ++i) { 50 for (int i = 0; i < n; ++i) {
65 51
66 int y, ty; 52 int y, ty;
67 bool drawText = true; 53 bool drawText = true;
68 54
69 double dispval = val;
70
71 if (i == n-1 && 55 if (i == n-1 &&
72 v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) { 56 v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) {
73 if (layer->getScaleUnits() != "") drawText = false; 57 if (layer->getScaleUnits() != "") drawText = false;
74 } 58 }
75 dispval = int(rint(val / round) * round);
76 59
77 #ifdef DEBUG_TIME_VALUE_LAYER 60 double val = ticks[i].value;
78 cerr << "val = " << val << ", dispval = " << dispval << endl; 61 QString label = QString::fromStdString(ticks[i].label);
79 #endif 62
80 63 y = layer->getYForValue(v, val);
81 y = layer->getYForValue(v, dispval);
82 64
83 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2; 65 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
84 66
85 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) { 67 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
86 val += inc;
87 continue; 68 continue;
88 } 69 }
89
90 snprintf(buffer, buflen, "%.*f", dp, dispval);
91
92 QString label = QString(buffer);
93 70
94 paint.drawLine(w - 5, y, w, y); 71 paint.drawLine(w - 5, y, w, y);
95 72
96 if (drawText) { 73 if (drawText) {
97 paint.drawText(w - paint.fontMetrics().width(label) - 6, 74 paint.drawText(w - paint.fontMetrics().width(label) - 6,
98 ty, label); 75 ty, label);
99 } 76 }
100 77
101 prevy = y; 78 prevy = y;
102 val += inc;
103 } 79 }
104 } 80 }