comparison layer/LinearNumericalScale.cpp @ 696:e77b1673e17e tonioni

Pull out log and linear vertical scales into their own classes, make some improvements to log numbering
author Chris Cannam
date Wed, 04 Dec 2013 11:35:08 +0000
parents
children 1a1448f7beb2
comparison
equal deleted inserted replaced
695:6d9624e0ac55 696:e77b1673e17e
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 "LinearNumericalScale.h"
17 #include "VerticalScaleLayer.h"
18
19 #include <QPainter>
20
21 #include <cmath>
22
23 #include "view/View.h"
24
25 int
26 LinearNumericalScale::getWidth(View *v,
27 QPainter &paint)
28 {
29 return paint.fontMetrics().width("-000.000");
30 }
31
32 void
33 LinearNumericalScale::paintVertical(View *v,
34 const VerticalScaleLayer *layer,
35 QPainter &paint,
36 int x0,
37 float minf,
38 float maxf)
39 {
40 int h = v->height();
41
42 int n = 10;
43
44 float val = minf;
45 float inc = (maxf - val) / n;
46
47 char buffer[40];
48
49 int w = getWidth(v, paint) + x0;
50
51 float round = 1.f;
52 int dp = 0;
53 if (inc > 0) {
54 int prec = trunc(log10f(inc));
55 prec -= 1;
56 if (prec < 0) dp = -prec;
57 round = powf(10.f, prec);
58 #ifdef DEBUG_TIME_VALUE_LAYER
59 cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl;
60 #endif
61 }
62
63 int prevy = -1;
64
65 for (int i = 0; i < n; ++i) {
66
67 int y, ty;
68 bool drawText = true;
69
70 float dispval = val;
71
72 if (i == n-1 &&
73 v->height() < paint.fontMetrics().height() * (n*2)) {
74 if (layer->getScaleUnits() != "") drawText = false;
75 }
76 dispval = lrintf(val / round) * round;
77
78 #ifdef DEBUG_TIME_VALUE_LAYER
79 cerr << "val = " << val << ", dispval = " << dispval << endl;
80 #endif
81
82 y = layer->getYForValue(v, dispval);
83
84 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
85
86 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
87 val += inc;
88 continue;
89 }
90
91 sprintf(buffer, "%.*f", dp, dispval);
92
93 QString label = QString(buffer);
94
95 paint.drawLine(w - 5, y, w, y);
96
97 if (drawText) {
98 paint.drawText(w - paint.fontMetrics().width(label) - 13,
99 ty, label);
100 }
101
102 prevy = y;
103 val += inc;
104 }
105 }