comparison layer/LogColourScale.cpp @ 699:1a1448f7beb2

Pull out colour scale drawing as well
author Chris Cannam
date Wed, 04 Dec 2013 13:11:23 +0000
parents
children 1d526ba11a24
comparison
equal deleted inserted replaced
698:ad7623c39396 699:1a1448f7beb2
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 "view/View.h"
26
27 int
28 LogColourScale::getWidth(View *v,
29 QPainter &paint)
30 {
31 return paint.fontMetrics().width("-000.00") + 15;
32 }
33
34 void
35 LogColourScale::paintVertical(View *v,
36 const ColourScaleLayer *layer,
37 QPainter &paint,
38 int x0,
39 float minlog,
40 float maxlog)
41 {
42 int h = v->height();
43
44 int n = 10;
45
46 float val = minlog;
47 float inc = (maxlog - val) / n;
48
49 char buffer[40];
50
51 int w = getWidth(v, paint) + x0;
52
53 int boxx = 5, boxy = 5;
54 if (layer->getScaleUnits() != "") {
55 boxy += paint.fontMetrics().height();
56 }
57 int boxw = 10, boxh = h - boxy - 5;
58
59 int tx = 5 + boxx + boxw;
60 paint.drawRect(boxx, boxy, boxw, boxh);
61
62 paint.save();
63 for (int y = 0; y < boxh; ++y) {
64 float val = ((boxh - y) * (maxlog - minlog)) / boxh + minlog;
65 paint.setPen(layer->getColourForValue(v, LogRange::unmap(val)));
66 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
67 }
68 paint.restore();
69
70 int dp = 0;
71 if (inc > 0) {
72 int prec = trunc(log10f(inc));
73 prec -= 1;
74 if (prec < 0) dp = -prec;
75 }
76
77 for (int i = 0; i < n; ++i) {
78
79 int y, ty;
80
81 y = boxy + int(boxh - ((val - minlog) * boxh) / (maxlog - minlog));
82
83 ty = y - paint.fontMetrics().height() +
84 paint.fontMetrics().ascent() + 2;
85
86 double dv = LogRange::unmap(val);
87 int digits = trunc(log10f(dv));
88 int sf = dp + (digits > 0 ? digits : 0);
89 if (sf < 2) sf = 2;
90 sprintf(buffer, "%.*g", sf, dv);
91
92 QString label = QString(buffer);
93
94 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
95 paint.drawText(tx, ty, label);
96
97 val += inc;
98 }
99 }