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 "LinearColourScale.h"
17 #include "ColourScaleLayer.h"
18 
19 #include <QPainter>
20 
21 #include <cmath>
22 
23 #include "LayerGeometryProvider.h"
24 
25 int
getWidth(LayerGeometryProvider *,QPainter & paint)26 LinearColourScale::getWidth(LayerGeometryProvider *,
27                             QPainter &paint)
28 {
29     // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
30     // replacement (horizontalAdvance) was only added in Qt 5.11
31     // which is too new for us
32 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
33 
34     return paint.fontMetrics().width("-000.00") + 15;
35 }
36 
37 void
paintVertical(LayerGeometryProvider * v,const ColourScaleLayer * layer,QPainter & paint,int,double min,double max)38 LinearColourScale::paintVertical(LayerGeometryProvider *v,
39                                  const ColourScaleLayer *layer,
40                                  QPainter &paint,
41                                  int /* x0 */,
42                                  double min,
43                                  double max)
44 {
45     int h = v->getPaintHeight();
46 
47     int n = 10;
48 
49     double val = min;
50     double inc = (max - val) / n;
51 
52     const int buflen = 40;
53     char buffer[buflen];
54 
55     int boxx = 5, boxy = 5;
56     if (layer->getScaleUnits() != "") {
57         boxy += paint.fontMetrics().height();
58     }
59     int boxw = 10, boxh = h - boxy - 5;
60 
61     int tx = 5 + boxx + boxw;
62     paint.drawRect(boxx, boxy, boxw, boxh);
63 
64     paint.save();
65     for (int y = 0; y < boxh; ++y) {
66         double val = ((boxh - y) * (max - min)) / boxh + min;
67         paint.setPen(layer->getColourForValue(v, val));
68         paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
69     }
70     paint.restore();
71 
72 //    double round = 1.f;
73     int dp = 0;
74     if (inc > 0) {
75         int prec = int(trunc(log10(inc)));
76         prec -= 1;
77         if (prec < 0) dp = -prec;
78 //        round = powf(10.f, prec);
79 //#ifdef DEBUG_TIME_VALUE_LAYER
80 //        cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl;
81 //#endif
82     }
83 
84     for (int i = 0; i < n; ++i) {
85 
86         int y, ty;
87 
88         y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
89 
90         ty = y - paint.fontMetrics().height() +
91             paint.fontMetrics().ascent() + 2;
92 
93         snprintf(buffer, buflen, "%.*f", dp, val);
94         QString label = QString(buffer);
95 
96         paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
97         paint.drawText(tx, ty, label);
98 
99         val += inc;
100     }
101 }
102