LogNumericalScale.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 "LogNumericalScale.h"
17 #include "VerticalScaleLayer.h"
18 #include "LayerGeometryProvider.h"
19 
20 #include "base/LogRange.h"
21 #include "base/ScaleTickIntervals.h"
22 
23 #include <QPainter>
24 
25 #include <cmath>
26 
27 int
29  QPainter &paint)
30 {
31  // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
32  // replacement (horizontalAdvance) was only added in Qt 5.11
33  // which is too new for us
34 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
35 
36  return paint.fontMetrics().width("-000.00") + 10;
37 }
38 
39 void
41  const VerticalScaleLayer *layer,
42  QPainter &paint,
43  int x0,
44  double minlog,
45  double maxlog)
46 {
47  int n = 10;
48  auto ticks = ScaleTickIntervals::logarithmicAlready({ minlog, maxlog, n });
49  n = int(ticks.size());
50 
51  int w = getWidth(v, paint) + x0;
52 
53  int prevy = -1;
54 
55  for (int i = 0; i < n; ++i) {
56 
57  int y, ty;
58  bool drawText = true;
59 
60  if (i == n-1 &&
61  v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) {
62  if (layer->getScaleUnits() != "") drawText = false;
63  }
64 
65  double val = ticks[i].value;
66  QString label = QString::fromStdString(ticks[i].label);
67 
68  y = layer->getYForValue(v, val);
69 
70  ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
71 
72  if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
73  continue;
74  }
75 
76  paint.drawLine(w - 5, y, w, y);
77 
78  if (drawText) {
79  paint.drawText(w - paint.fontMetrics().width(label) - 6,
80  ty, label);
81  }
82 
83  prevy = y;
84  }
85 }
86 
void paintVertical(LayerGeometryProvider *v, const VerticalScaleLayer *layer, QPainter &paint, int x0, double minlog, double maxlog)
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
virtual int getPaintHeight() const
virtual QString getScaleUnits() const =0