svgui  1.9
LogColourScale.cpp
Go to the documentation of this file.
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
29  QPainter &paint)
30 {
31  return paint.fontMetrics().width("-000.00") + 15;
32 }
33 
34 void
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 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) * (maxlog - minlog)) / boxh + minlog;
63  paint.setPen(layer->getColourForValue(v, LogRange::unmap(val)));
64  paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
65  }
66  paint.restore();
67 
68  int dp = 0;
69  if (inc > 0) {
70  int prec = trunc(log10f(inc));
71  prec -= 1;
72  if (prec < 0) dp = -prec;
73  }
74 
75  for (int i = 0; i < n; ++i) {
76 
77  int y, ty;
78 
79  y = boxy + int(boxh - ((val - minlog) * boxh) / (maxlog - minlog));
80 
81  ty = y - paint.fontMetrics().height() +
82  paint.fontMetrics().ascent() + 2;
83 
84  double dv = LogRange::unmap(val);
85  int digits = trunc(log10f(dv));
86  int sf = dp + (digits > 0 ? digits : 0);
87  if (sf < 2) sf = 2;
88  sprintf(buffer, "%.*g", sf, dv);
89 
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 }
int getWidth(View *v, QPainter &paint)
virtual QColor getColourForValue(View *v, float value) const =0
View is the base class of widgets that display one or more overlaid views of data against a horizonta...
Definition: View.h:50
void paintVertical(View *v, const ColourScaleLayer *layer, QPainter &paint, int x0, float minf, float maxf)
virtual QString getScaleUnits() const =0