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-2018 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 #include "LayerGeometryProvider.h"
19 
20 #include <QPainter>
21 
22 #include <cmath>
23 
24 #include "base/ScaleTickIntervals.h"
25 
26 int
28  QPainter &paint)
29 {
30  // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
31  // replacement (horizontalAdvance) was only added in Qt 5.11
32  // which is too new for us
33 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
34 
35  return paint.fontMetrics().width("-000.00") + 10;
36 }
37 
38 void
40  const VerticalScaleLayer *layer,
41  QPainter &paint,
42  int x0,
43  double minf,
44  double maxf)
45 {
46  int n = 10;
47  auto ticks = ScaleTickIntervals::linear({ minf, maxf, n });
48  n = int(ticks.size());
49 
50  int w = getWidth(v, paint) + x0;
51 
52  int prevy = -1;
53 
54  for (int i = 0; i < n; ++i) {
55 
56  int y, ty;
57  bool drawText = true;
58 
59  if (i == n-1 &&
60  v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) {
61  if (layer->getScaleUnits() != "") drawText = false;
62  }
63 
64  double val = ticks[i].value;
65  QString label = QString::fromStdString(ticks[i].label);
66 
67  y = layer->getYForValue(v, val);
68 
69  ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
70 
71  if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
72  continue;
73  }
74 
75  paint.drawLine(w - 5, y, w, y);
76 
77  if (drawText) {
78  paint.drawText(w - paint.fontMetrics().width(label) - 6,
79  ty, label);
80  }
81 
82  prevy = y;
83  }
84 }
int getWidth(LayerGeometryProvider *v, QPainter &paint)
Interface for classes that provide geometry information (such as size, start frame, and a large number of other properties) about the disposition of a layer.
Interface for layers in which the Y axis represents (or can sometimes represent, depending on the dis...
virtual int getYForValue(LayerGeometryProvider *, double value) const =0
void paintVertical(LayerGeometryProvider *v, const VerticalScaleLayer *layer, QPainter &paint, int x0, double minf, double maxf)
virtual int getPaintHeight() const
virtual QString getScaleUnits() const =0