Mercurial > hg > svgui
comparison layer/PaintAssistant.cpp @ 1324:13d9b422f7fe zoom
Merge from default branch
author | Chris Cannam |
---|---|
date | Mon, 17 Sep 2018 13:51:31 +0100 |
parents | 6e35062fc10a |
children | cca66ce390e0 |
comparison
equal
deleted
inserted
replaced
1183:57d192e26331 | 1324:13d9b422f7fe |
---|---|
17 | 17 |
18 #include "LayerGeometryProvider.h" | 18 #include "LayerGeometryProvider.h" |
19 | 19 |
20 #include "base/AudioLevel.h" | 20 #include "base/AudioLevel.h" |
21 #include "base/Strings.h" | 21 #include "base/Strings.h" |
22 #include "base/Debug.h" | |
22 | 23 |
23 #include <QPaintDevice> | 24 #include <QPaintDevice> |
24 #include <QPainter> | 25 #include <QPainter> |
25 | 26 |
26 #include <iostream> | 27 #include <iostream> |
27 #include <cmath> | 28 #include <cmath> |
28 | 29 |
29 void | 30 void |
30 PaintAssistant::paintVerticalLevelScale(QPainter &paint, QRect rect, | 31 PaintAssistant::paintVerticalLevelScale(QPainter &paint, QRect rect, |
31 double minVal, double maxVal, | 32 double minVal, double maxVal, |
32 Scale scale, int &mult, | 33 Scale scale, int &mult, |
33 std::vector<int> *vy) | 34 std::vector<int> *vy) |
34 { | 35 { |
35 static double meterdbs[] = { -40, -30, -20, -15, -10, | 36 static double meterdbs[] = { -40, -30, -20, -15, -10, |
36 -5, -3, -2, -1, -0.5, 0 }; | 37 -5, -3, -2, -1, -0.5, 0 }; |
53 round = int(minVal + step * mult); | 54 round = int(minVal + step * mult); |
54 mult *= 10; | 55 mult *= 10; |
55 } while (!round && mult < limit); | 56 } while (!round && mult < limit); |
56 if (round) { | 57 if (round) { |
57 mult /= 10; | 58 mult /= 10; |
58 // cerr << "\n\nstep goes from " << step; | |
59 step = double(round) / mult; | 59 step = double(round) / mult; |
60 n = int(lrint((maxVal - minVal) / step)); | 60 n = int(lrint((maxVal - minVal) / step)); |
61 if (mult > 1) { | 61 if (mult > 1) { |
62 mult /= 10; | 62 mult /= 10; |
63 } | 63 } |
64 // cerr << " to " << step << " (n = " << n << ")" << endl; | |
65 } | 64 } |
66 } | 65 } |
67 | 66 |
68 for (int i = 0; i <= n; ++i) { | 67 for (int i = 0; i <= n; ++i) { |
69 | 68 |
114 abs(y - lastLabelledY) >= textHeight - 1); | 113 abs(y - lastLabelledY) >= textHeight - 1); |
115 | 114 |
116 if (spaceForLabel) { | 115 if (spaceForLabel) { |
117 | 116 |
118 int tx = 3; | 117 int tx = 3; |
119 // if (scale != LinearScale) { | |
120 if (paint.fontMetrics().width(text) < w - 10) { | 118 if (paint.fontMetrics().width(text) < w - 10) { |
121 tx = w - 10 - paint.fontMetrics().width(text); | 119 tx = w - 10 - paint.fontMetrics().width(text); |
122 } | 120 } |
123 | 121 |
124 int ty = y; | 122 int ty = y; |
125 | 123 |
126 if (ty < paint.fontMetrics().ascent()) { | 124 if (ty < paint.fontMetrics().ascent()) { |
127 ty = paint.fontMetrics().ascent(); | 125 ty = paint.fontMetrics().ascent(); |
128 // } else if (ty > rect.y() + h - paint.fontMetrics().descent()) { | |
129 // ty = rect.y() + h - paint.fontMetrics().descent(); | |
130 } else { | 126 } else { |
131 ty += toff; | 127 ty += toff; |
132 } | 128 } |
133 | 129 |
134 paint.drawText(tx, ty, text); | 130 paint.drawText(tx, ty, text); |
135 | 131 |
136 lastLabelledY = ty - toff; | 132 lastLabelledY = ty - toff; |
137 /* | 133 |
138 if (ny != y) { | |
139 ty = ny; | |
140 if (ty < paint.fontMetrics().ascent()) { | |
141 ty = paint.fontMetrics().ascent(); | |
142 } else if (ty > h - paint.fontMetrics().descent()) { | |
143 ty = h - paint.fontMetrics().descent(); | |
144 } else { | |
145 ty += toff; | |
146 } | |
147 paint.drawText(tx, ty, text); | |
148 } | |
149 */ | |
150 paint.drawLine(w - 7, y, w, y); | 134 paint.drawLine(w - 7, y, w, y); |
151 if (vy) vy->push_back(y); | 135 if (vy) vy->push_back(y); |
152 | 136 |
153 if (ny != y) { | 137 if (ny != y) { |
154 paint.drawLine(w - 7, ny, w, ny); | 138 paint.drawLine(w - 7, ny, w, ny); |
185 double minVal, double maxVal, | 169 double minVal, double maxVal, |
186 int minY, int height) | 170 int minY, int height) |
187 { | 171 { |
188 int vy = 0; | 172 int vy = 0; |
189 | 173 |
190 // int m = height/2; | |
191 // int my = minY + m; | |
192 | |
193 switch (scale) { | 174 switch (scale) { |
194 | 175 |
195 case LinearScale: | 176 case LinearScale: |
196 // vy = my - int(m * value); | |
197 vy = minY + height - int(((value - minVal) / (maxVal - minVal)) * height); | 177 vy = minY + height - int(((value - minVal) / (maxVal - minVal)) * height); |
198 break; | 178 break; |
199 | 179 |
200 case MeterScale: | 180 case MeterScale: |
201 // vy = my - AudioLevel::multiplier_to_preview(value, m); | |
202 vy = minY + height - AudioLevel::multiplier_to_preview | 181 vy = minY + height - AudioLevel::multiplier_to_preview |
203 ((value - minVal) / (maxVal - minVal), height); | 182 ((value - minVal) / (maxVal - minVal), height); |
204 break; | 183 break; |
205 | 184 |
206 case dBScale: | 185 case dBScale: |
236 paint.setPen(Qt::NoPen); | 215 paint.setPen(Qt::NoPen); |
237 paint.setBrush(boxColour); | 216 paint.setBrush(boxColour); |
238 | 217 |
239 QRect r = paint.fontMetrics().boundingRect(text); | 218 QRect r = paint.fontMetrics().boundingRect(text); |
240 r.translate(QPoint(x, y)); | 219 r.translate(QPoint(x, y)); |
241 // cerr << "drawVisibleText: r = " << r.x() << "," <<r.y() << " " << r.width() << "x" << r.height() << endl; | |
242 paint.drawRect(r); | 220 paint.drawRect(r); |
243 paint.setBrush(Qt::NoBrush); | 221 paint.setBrush(Qt::NoBrush); |
244 | 222 |
245 paint.setPen(surroundColour); | 223 paint.setPen(surroundColour); |
246 | 224 |
247 for (int dx = -1; dx <= 1; ++dx) { | 225 for (int dx = -1; dx <= 1; ++dx) { |
248 for (int dy = -1; dy <= 1; ++dy) { | 226 for (int dy = -1; dy <= 1; ++dy) { |
249 if (!(dx || dy)) continue; | 227 if (!(dx || dy)) continue; |
250 paint.drawText(x + dx, y + dy, text); | 228 paint.drawText(x + dx, y + dy, text); |
251 } | 229 } |
252 } | 230 } |
253 | 231 |
254 paint.setPen(penColour); | 232 paint.setPen(penColour); |
255 | 233 |
256 paint.drawText(x, y, text); | 234 paint.drawText(x, y, text); |
257 | 235 |
258 paint.restore(); | 236 paint.restore(); |
259 | 237 |
260 } else { | 238 } else { |
261 | 239 |
262 std::cerr << "ERROR: PaintAssistant::drawVisibleText: Boxed style not yet implemented!" << std::endl; | 240 std::cerr << "ERROR: PaintAssistant::drawVisibleText: Boxed style not yet implemented!" << std::endl; |
263 } | 241 } |
264 } | 242 } |
243 | |
244 double | |
245 PaintAssistant::scalePenWidth(double width) | |
246 { | |
247 static double ratio = 0.0; | |
248 if (ratio == 0.0) { | |
249 double baseEm; | |
250 #ifdef Q_OS_MAC | |
251 baseEm = 17.0; | |
252 #else | |
253 baseEm = 15.0; | |
254 #endif | |
255 double em = QFontMetrics(QFont()).height(); | |
256 ratio = em / baseEm; | |
257 | |
258 SVDEBUG << "PaintAssistant::scalePenWidth: ratio is " << ratio | |
259 << " (em = " << em << ")" << endl; | |
260 } | |
261 | |
262 if (ratio <= 1.0) { | |
263 // we only ever scale up in this method | |
264 return width; | |
265 } | |
266 | |
267 if (width <= 0) { | |
268 // zero-width pen, produce a scaled one-pixel pen | |
269 return ratio; | |
270 } | |
271 | |
272 return width * ratio; | |
273 } | |
274 | |
275 QPen | |
276 PaintAssistant::scalePen(QPen pen) | |
277 { | |
278 return QPen(pen.color(), scalePenWidth(pen.width())); | |
279 } | |
280 | |
281 |