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