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