comparison layer/LinearNumericalScale.cpp @ 1324:13d9b422f7fe zoom

Merge from default branch
author Chris Cannam
date Mon, 17 Sep 2018 13:51:31 +0100
parents fc9d9f1103fa
children f2525e6cbdf1
comparison
equal deleted inserted replaced
1183:57d192e26331 1324:13d9b422f7fe
2 2
3 /* 3 /*
4 Sonic Visualiser 4 Sonic Visualiser
5 An audio file viewer and annotation editor. 5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London. 6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006-2013 Chris Cannam and QMUL. 7 This file copyright 2006-2018 Chris Cannam and QMUL.
8 8
9 This program is free software; you can redistribute it and/or 9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as 10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the 11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file 12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information. 13 COPYING included with this distribution for more information.
14 */ 14 */
15 15
16 #include "LinearNumericalScale.h" 16 #include "LinearNumericalScale.h"
17 #include "VerticalScaleLayer.h" 17 #include "VerticalScaleLayer.h"
18 #include "LayerGeometryProvider.h"
18 19
19 #include <QPainter> 20 #include <QPainter>
20 21
21 #include <cmath> 22 #include <cmath>
22 23
23 #include "LayerGeometryProvider.h" 24 #include "base/ScaleTickIntervals.h"
24 25
25 int 26 int
26 LinearNumericalScale::getWidth(LayerGeometryProvider *, 27 LinearNumericalScale::getWidth(LayerGeometryProvider *,
27 QPainter &paint) 28 QPainter &paint)
28 { 29 {
29 return paint.fontMetrics().width("-000.00") + 10; 30 return paint.fontMetrics().width("-000.00") + 10;
30 } 31 }
31 32
32 void 33 void
33 LinearNumericalScale::paintVertical(LayerGeometryProvider *v, 34 LinearNumericalScale::paintVertical(LayerGeometryProvider *v,
34 const VerticalScaleLayer *layer, 35 const VerticalScaleLayer *layer,
35 QPainter &paint, 36 QPainter &paint,
36 int x0, 37 int x0,
37 double minf, 38 double minf,
38 double maxf) 39 double maxf)
39 { 40 {
40 int n = 10; 41 int n = 10;
41 42 auto ticks = ScaleTickIntervals::linear({ minf, maxf, n });
42 double val = minf; 43 n = int(ticks.size());
43 double inc = (maxf - val) / n;
44
45 const int buflen = 40;
46 char buffer[buflen];
47 44
48 int w = getWidth(v, paint) + x0; 45 int w = getWidth(v, paint) + x0;
49 46
50 double round = 1.0; 47 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 48
62 int prevy = -1;
63
64 for (int i = 0; i < n; ++i) { 49 for (int i = 0; i < n; ++i) {
65 50
66 int y, ty; 51 int y, ty;
67 bool drawText = true; 52 bool drawText = true;
68 53
69 double dispval = val; 54 if (i == n-1 &&
70 55 v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) {
71 if (i == n-1 && 56 if (layer->getScaleUnits() != "") drawText = false;
72 v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) {
73 if (layer->getScaleUnits() != "") drawText = false;
74 }
75 dispval = int(rint(val / round) * round);
76
77 #ifdef DEBUG_TIME_VALUE_LAYER
78 cerr << "val = " << val << ", dispval = " << dispval << endl;
79 #endif
80
81 y = layer->getYForValue(v, dispval);
82
83 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
84
85 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
86 val += inc;
87 continue;
88 } 57 }
89 58
90 snprintf(buffer, buflen, "%.*f", dp, dispval); 59 double val = ticks[i].value;
60 QString label = QString::fromStdString(ticks[i].label);
61
62 y = layer->getYForValue(v, val);
91 63
92 QString label = QString(buffer); 64 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
65
66 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
67 continue;
68 }
93 69
94 paint.drawLine(w - 5, y, w, y); 70 paint.drawLine(w - 5, y, w, y);
95 71
96 if (drawText) { 72 if (drawText) {
97 paint.drawText(w - paint.fontMetrics().width(label) - 6, 73 paint.drawText(w - paint.fontMetrics().width(label) - 6,
98 ty, label); 74 ty, label);
99 } 75 }
100 76
101 prevy = y; 77 prevy = y;
102 val += inc;
103 } 78 }
104 } 79 }