HorizontalFrequencyScale.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 
18 #include "LayerGeometryProvider.h"
19 
20 #include "base/ScaleTickIntervals.h"
21 
22 #include <QPainter>
23 
24 #include <cmath>
25 
26 int
28  QPainter &paint)
29 {
30  return paint.fontMetrics().height() + 10;
31 }
32 
33 void
35  const HorizontalScaleProvider *p,
36  QPainter &paint,
37  QRect r,
38  bool logarithmic)
39 {
40  int x0 = r.x(), y0 = r.y(), x1 = r.x() + r.width(), y1 = r.y() + r.height();
41 
42  paint.drawLine(x0, y0, x1, y0);
43 
44  double f0 = p->getFrequencyForX(v, x0 ? x0 : 1);
45  double f1 = p->getFrequencyForX(v, x1);
46 
47  int n = 20;
48 
49  auto ticks =
50  logarithmic ?
51  ScaleTickIntervals::logarithmic({ f0, f1, n }) :
52  ScaleTickIntervals::linear({ f0, f1, n });
53 
54  n = int(ticks.size());
55 
56  int marginx = -1;
57 
58  for (int i = 0; i < n; ++i) {
59 
60  // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
61  // replacement (horizontalAdvance) was only added in Qt 5.11
62  // which is too new for us
63 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
64 
65  double val = ticks[i].value;
66  QString label = QString::fromStdString(ticks[i].label);
67  int tw = paint.fontMetrics().width(label);
68 
69  int x = int(round(p->getXForFrequency(v, val)));
70 
71  if (x < marginx) continue;
72 
74 
75  paint.drawLine(x, y0, x, y1);
76 
77  paint.drawText(x + 5, y0 + paint.fontMetrics().ascent() + 5, label);
78 
79  marginx = x + tw + 10;
80  }
81 }
82 
void paintScale(LayerGeometryProvider *v, const HorizontalScaleProvider *provider, QPainter &paint, QRect r, bool logarithmic)
virtual double getFrequencyForX(const LayerGeometryProvider *, double x) const =0
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.
int getHeight(LayerGeometryProvider *v, QPainter &paint)
virtual double getXForFrequency(const LayerGeometryProvider *, double freq) const =0
Interface to be implemented by objects, such as layers or objects they delegate to, in which the X axis corresponds to frequency.