1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006-2013 Chris Cannam and QMUL.
8
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
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "LogColourScale.h"
17 #include "ColourScaleLayer.h"
18
19 #include "base/LogRange.h"
20
21 #include <QPainter>
22
23 #include <cmath>
24
25 #include "LayerGeometryProvider.h"
26
27 int
getWidth(LayerGeometryProvider *,QPainter & paint)28 LogColourScale::getWidth(LayerGeometryProvider *,
29 QPainter &paint)
30 {
31 // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
32 // replacement (horizontalAdvance) was only added in Qt 5.11
33 // which is too new for us
34 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
35
36 return paint.fontMetrics().width("-000.00") + 15;
37 }
38
39 void
paintVertical(LayerGeometryProvider * v,const ColourScaleLayer * layer,QPainter & paint,int,double minlog,double maxlog)40 LogColourScale::paintVertical(LayerGeometryProvider *v,
41 const ColourScaleLayer *layer,
42 QPainter &paint,
43 int /* x0 */,
44 double minlog,
45 double maxlog)
46 {
47 int h = v->getPaintHeight();
48
49 int n = 10;
50
51 double val = minlog;
52 double inc = (maxlog - val) / n;
53
54 const int buflen = 40;
55 char buffer[buflen];
56
57 int boxx = 5, boxy = 5;
58 if (layer->getScaleUnits() != "") {
59 boxy += paint.fontMetrics().height();
60 }
61 int boxw = 10, boxh = h - boxy - 5;
62
63 int tx = 5 + boxx + boxw;
64 paint.drawRect(boxx, boxy, boxw, boxh);
65
66 paint.save();
67 for (int y = 0; y < boxh; ++y) {
68 double val = ((boxh - y) * (maxlog - minlog)) / boxh + minlog;
69 paint.setPen(layer->getColourForValue(v, LogRange::unmap(val)));
70 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
71 }
72 paint.restore();
73
74 int dp = 0;
75 if (inc > 0) {
76 int prec = int(trunc(log10(inc)));
77 prec -= 1;
78 if (prec < 0) dp = -prec;
79 }
80
81 for (int i = 0; i < n; ++i) {
82
83 int y, ty;
84
85 y = boxy + int(boxh - ((val - minlog) * boxh) / (maxlog - minlog));
86
87 ty = y - paint.fontMetrics().height() +
88 paint.fontMetrics().ascent() + 2;
89
90 double dv = LogRange::unmap(val);
91 int digits = int(trunc(log10(dv)));
92 int sf = dp + (digits > 0 ? digits : 0);
93 if (sf < 2) sf = 2;
94 snprintf(buffer, buflen, "%.*g", sf, dv);
95
96 QString label = QString(buffer);
97
98 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
99 paint.drawText(tx, ty, label);
100
101 val += inc;
102 }
103 }
104