comparison layer/LogNumericalScale.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 "LogNumericalScale.h" 16 #include "LogNumericalScale.h"
17 #include "VerticalScaleLayer.h" 17 #include "VerticalScaleLayer.h"
18 #include "LayerGeometryProvider.h"
18 19
19 #include "base/LogRange.h" 20 #include "base/LogRange.h"
21 #include "base/ScaleTickIntervals.h"
20 22
21 #include <QPainter> 23 #include <QPainter>
22 24
23 #include <cmath> 25 #include <cmath>
24 26
25 #include "LayerGeometryProvider.h"
26
27 //#define DEBUG_TIME_VALUE_LAYER 1
28
29 int 27 int
30 LogNumericalScale::getWidth(LayerGeometryProvider *, 28 LogNumericalScale::getWidth(LayerGeometryProvider *,
31 QPainter &paint) 29 QPainter &paint)
32 { 30 {
33 return paint.fontMetrics().width("-000.00") + 10; 31 return paint.fontMetrics().width("-000.00") + 10;
34 } 32 }
35 33
36 void 34 void
37 LogNumericalScale::paintVertical(LayerGeometryProvider *v, 35 LogNumericalScale::paintVertical(LayerGeometryProvider *v,
38 const VerticalScaleLayer *layer, 36 const VerticalScaleLayer *layer,
39 QPainter &paint, 37 QPainter &paint,
40 int x0, 38 int x0,
41 double minlog, 39 double minlog,
42 double maxlog) 40 double maxlog)
43 { 41 {
42 int n = 10;
43 auto ticks = ScaleTickIntervals::logarithmicAlready({ minlog, maxlog, n });
44 n = int(ticks.size());
45
44 int w = getWidth(v, paint) + x0; 46 int w = getWidth(v, paint) + x0;
45
46 int n = 10;
47
48 double val = minlog;
49 double inc = (maxlog - val) / n; // even increments of log scale
50
51 // smallest increment as displayed
52 double minDispInc = LogRange::unmap(minlog + inc) - LogRange::unmap(minlog);
53
54 #ifdef DEBUG_TIME_VALUE_LAYER
55 cerr << "min = " << minlog << ", max = " << maxlog << ", inc = " << inc << ", minDispInc = " << minDispInc << endl;
56 #endif
57
58 const int buflen = 40;
59 char buffer[buflen];
60
61 double round = 1.f;
62 int dp = 0;
63
64 if (minDispInc > 0) {
65 int prec = int(trunc(log10(minDispInc)));
66 if (prec < 0) dp = -prec;
67 round = pow(10.0, prec);
68 if (dp > 4) dp = 4;
69 #ifdef DEBUG_TIME_VALUE_LAYER
70 cerr << "round = " << round << ", prec = " << prec << ", dp = " << dp << endl;
71 #endif
72 }
73 47
74 int prevy = -1; 48 int prevy = -1;
75 49
76 for (int i = 0; i < n; ++i) { 50 for (int i = 0; i < n; ++i) {
77 51
78 int y, ty; 52 int y, ty;
79 bool drawText = true; 53 bool drawText = true;
80 54
81 if (i == n-1 && 55 if (i == n-1 &&
82 v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) { 56 v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) {
83 if (layer->getScaleUnits() != "") drawText = false; 57 if (layer->getScaleUnits() != "") drawText = false;
84 }
85
86 double dispval = LogRange::unmap(val);
87 dispval = floor(dispval / round) * round;
88
89 #ifdef DEBUG_TIME_VALUE_LAYER
90 cerr << "val = " << val << ", dispval = " << dispval << endl;
91 #endif
92
93 y = layer->getYForValue(v, dispval);
94
95 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
96
97 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
98 val += inc;
99 continue;
100 } 58 }
101 59
102 int digits = int(trunc(log10(dispval))); 60 double val = ticks[i].value;
103 int sf = dp + (digits > 0 ? digits : 0); 61 QString label = QString::fromStdString(ticks[i].label);
104 if (sf < 4) sf = 4;
105 #ifdef DEBUG_TIME_VALUE_LAYER
106 cerr << "sf = " << sf << endl;
107 #endif
108 snprintf(buffer, buflen, "%.*g", sf, dispval);
109 62
110 QString label = QString(buffer); 63 y = layer->getYForValue(v, val);
111 64
112 paint.drawLine(w - 5, y, w, y); 65 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
66
67 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
68 continue;
69 }
70
71 paint.drawLine(w - 5, y, w, y);
113 72
114 if (drawText) { 73 if (drawText) {
115 paint.drawText(w - paint.fontMetrics().width(label) - 6, 74 paint.drawText(w - paint.fontMetrics().width(label) - 6,
116 ty, label); 75 ty, label);
117 } 76 }
118 77
119 prevy = y; 78 prevy = y;
120 val += inc;
121 } 79 }
122 } 80 }
81