svgui  1.9
LinearNumericalScale.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 "LinearNumericalScale.h"
17 #include "VerticalScaleLayer.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") + 10;
30 }
31 
32 void
34  const VerticalScaleLayer *layer,
35  QPainter &paint,
36  int x0,
37  float minf,
38  float maxf)
39 {
40  int n = 10;
41 
42  float val = minf;
43  float inc = (maxf - val) / n;
44 
45  char buffer[40];
46 
47  int w = getWidth(v, paint) + x0;
48 
49  float round = 1.f;
50  int dp = 0;
51  if (inc > 0) {
52  int prec = trunc(log10f(inc));
53  prec -= 1;
54  if (prec < 0) dp = -prec;
55  round = powf(10.f, prec);
56 #ifdef DEBUG_TIME_VALUE_LAYER
57  cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl;
58 #endif
59  }
60 
61  int prevy = -1;
62 
63  for (int i = 0; i < n; ++i) {
64 
65  int y, ty;
66  bool drawText = true;
67 
68  float dispval = val;
69 
70  if (i == n-1 &&
71  v->height() < paint.fontMetrics().height() * (n*2)) {
72  if (layer->getScaleUnits() != "") drawText = false;
73  }
74  dispval = lrintf(val / round) * round;
75 
76 #ifdef DEBUG_TIME_VALUE_LAYER
77  cerr << "val = " << val << ", dispval = " << dispval << endl;
78 #endif
79 
80  y = layer->getYForValue(v, dispval);
81 
82  ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
83 
84  if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
85  val += inc;
86  continue;
87  }
88 
89  sprintf(buffer, "%.*f", dp, dispval);
90 
91  QString label = QString(buffer);
92 
93  paint.drawLine(w - 5, y, w, y);
94 
95  if (drawText) {
96  paint.drawText(w - paint.fontMetrics().width(label) - 6,
97  ty, label);
98  }
99 
100  prevy = y;
101  val += inc;
102  }
103 }
int getWidth(View *v, QPainter &paint)
void paintVertical(View *v, const VerticalScaleLayer *layer, QPainter &paint, int x0, float minf, float maxf)
virtual int getYForValue(View *, 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
virtual QString getScaleUnits() const =0