annotate layer/HorizontalFrequencyScale.cpp @ 1548:bd6af89982d7

Permit getScaleProvidingLayerForUnit to return a dormant layer if there is no visible alternative. This is necessary to avoid the scale disappearing in Tony when the spectrogram is toggled off.
author Chris Cannam
date Thu, 17 Oct 2019 14:44:22 +0100
parents f2525e6cbdf1
children
rev   line source
Chris@1281 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1281 2
Chris@1281 3 /*
Chris@1281 4 Sonic Visualiser
Chris@1281 5 An audio file viewer and annotation editor.
Chris@1281 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1281 7 This file copyright 2006-2018 Chris Cannam and QMUL.
Chris@1281 8
Chris@1281 9 This program is free software; you can redistribute it and/or
Chris@1281 10 modify it under the terms of the GNU General Public License as
Chris@1281 11 published by the Free Software Foundation; either version 2 of the
Chris@1281 12 License, or (at your option) any later version. See the file
Chris@1281 13 COPYING included with this distribution for more information.
Chris@1281 14 */
Chris@1281 15
Chris@1281 16 #include "HorizontalFrequencyScale.h"
Chris@1281 17 #include "HorizontalScaleProvider.h"
Chris@1281 18 #include "LayerGeometryProvider.h"
Chris@1281 19
Chris@1281 20 #include "base/ScaleTickIntervals.h"
Chris@1281 21
Chris@1281 22 #include <QPainter>
Chris@1281 23
Chris@1281 24 #include <cmath>
Chris@1281 25
Chris@1281 26 int
Chris@1281 27 HorizontalFrequencyScale::getHeight(LayerGeometryProvider *,
Chris@1281 28 QPainter &paint)
Chris@1281 29 {
Chris@1281 30 return paint.fontMetrics().height() + 10;
Chris@1281 31 }
Chris@1281 32
Chris@1281 33 void
Chris@1281 34 HorizontalFrequencyScale::paintScale(LayerGeometryProvider *v,
Chris@1281 35 const HorizontalScaleProvider *p,
Chris@1281 36 QPainter &paint,
Chris@1281 37 QRect r,
Chris@1281 38 bool logarithmic)
Chris@1281 39 {
Chris@1281 40 int x0 = r.x(), y0 = r.y(), x1 = r.x() + r.width(), y1 = r.y() + r.height();
Chris@1281 41
Chris@1281 42 paint.drawLine(x0, y0, x1, y0);
Chris@1281 43
Chris@1281 44 double f0 = p->getFrequencyForX(v, x0 ? x0 : 1);
Chris@1281 45 double f1 = p->getFrequencyForX(v, x1);
Chris@1281 46
Chris@1281 47 int n = 20;
Chris@1281 48
Chris@1281 49 auto ticks =
Chris@1281 50 logarithmic ?
Chris@1281 51 ScaleTickIntervals::logarithmic({ f0, f1, n }) :
Chris@1281 52 ScaleTickIntervals::linear({ f0, f1, n });
Chris@1281 53
Chris@1281 54 n = int(ticks.size());
Chris@1281 55
Chris@1281 56 int marginx = -1;
Chris@1281 57
Chris@1281 58 for (int i = 0; i < n; ++i) {
Chris@1471 59
Chris@1471 60 // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
Chris@1471 61 // replacement (horizontalAdvance) was only added in Qt 5.11
Chris@1471 62 // which is too new for us
Chris@1471 63 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Chris@1281 64
Chris@1281 65 double val = ticks[i].value;
Chris@1281 66 QString label = QString::fromStdString(ticks[i].label);
Chris@1281 67 int tw = paint.fontMetrics().width(label);
Chris@1281 68
Chris@1281 69 int x = int(round(p->getXForFrequency(v, val)));
Chris@1281 70
Chris@1281 71 if (x < marginx) continue;
Chris@1281 72
Chris@1281 73 //!!! todo: pixel scaling (here & elsewhere in these classes)
Chris@1281 74
Chris@1281 75 paint.drawLine(x, y0, x, y1);
Chris@1281 76
Chris@1281 77 paint.drawText(x + 5, y0 + paint.fontMetrics().ascent() + 5, label);
Chris@1281 78
Chris@1281 79 marginx = x + tw + 10;
Chris@1281 80 }
Chris@1281 81 }
Chris@1281 82