Chris@1281: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1281: Chris@1281: /* Chris@1281: Sonic Visualiser Chris@1281: An audio file viewer and annotation editor. Chris@1281: Centre for Digital Music, Queen Mary, University of London. Chris@1281: This file copyright 2006-2018 Chris Cannam and QMUL. Chris@1281: Chris@1281: This program is free software; you can redistribute it and/or Chris@1281: modify it under the terms of the GNU General Public License as Chris@1281: published by the Free Software Foundation; either version 2 of the Chris@1281: License, or (at your option) any later version. See the file Chris@1281: COPYING included with this distribution for more information. Chris@1281: */ Chris@1281: Chris@1281: #include "HorizontalFrequencyScale.h" Chris@1281: #include "HorizontalScaleProvider.h" Chris@1281: #include "LayerGeometryProvider.h" Chris@1281: Chris@1281: #include "base/ScaleTickIntervals.h" Chris@1281: Chris@1281: #include Chris@1281: Chris@1281: #include Chris@1281: Chris@1281: int Chris@1281: HorizontalFrequencyScale::getHeight(LayerGeometryProvider *, Chris@1281: QPainter &paint) Chris@1281: { Chris@1281: return paint.fontMetrics().height() + 10; Chris@1281: } Chris@1281: Chris@1281: void Chris@1281: HorizontalFrequencyScale::paintScale(LayerGeometryProvider *v, Chris@1281: const HorizontalScaleProvider *p, Chris@1281: QPainter &paint, Chris@1281: QRect r, Chris@1281: bool logarithmic) Chris@1281: { Chris@1281: int x0 = r.x(), y0 = r.y(), x1 = r.x() + r.width(), y1 = r.y() + r.height(); Chris@1281: Chris@1281: paint.drawLine(x0, y0, x1, y0); Chris@1281: Chris@1281: double f0 = p->getFrequencyForX(v, x0 ? x0 : 1); Chris@1281: double f1 = p->getFrequencyForX(v, x1); Chris@1281: Chris@1281: int n = 20; Chris@1281: Chris@1281: auto ticks = Chris@1281: logarithmic ? Chris@1281: ScaleTickIntervals::logarithmic({ f0, f1, n }) : Chris@1281: ScaleTickIntervals::linear({ f0, f1, n }); Chris@1281: Chris@1281: n = int(ticks.size()); Chris@1281: Chris@1281: int marginx = -1; Chris@1281: Chris@1281: for (int i = 0; i < n; ++i) { Chris@1471: Chris@1471: // Qt 5.13 deprecates QFontMetrics::width(), but its suggested Chris@1471: // replacement (horizontalAdvance) was only added in Qt 5.11 Chris@1471: // which is too new for us Chris@1471: #pragma GCC diagnostic ignored "-Wdeprecated-declarations" Chris@1281: Chris@1281: double val = ticks[i].value; Chris@1281: QString label = QString::fromStdString(ticks[i].label); Chris@1281: int tw = paint.fontMetrics().width(label); Chris@1281: Chris@1281: int x = int(round(p->getXForFrequency(v, val))); Chris@1281: Chris@1281: if (x < marginx) continue; Chris@1281: Chris@1281: //!!! todo: pixel scaling (here & elsewhere in these classes) Chris@1281: Chris@1281: paint.drawLine(x, y0, x, y1); Chris@1281: Chris@1281: paint.drawText(x + 5, y0 + paint.fontMetrics().ascent() + 5, label); Chris@1281: Chris@1281: marginx = x + tw + 10; Chris@1281: } Chris@1281: } Chris@1281: