annotate layer/LogNumericalScale.cpp @ 1551:e79731086b0f

Fixes to NoteLayer, particularly to calculation of vertical scale when model unit is not Hz. To avoid inconsistency we now behave as if the unit is always Hz from the point of view of the external API and display, converting at the point where we obtain values from the events themselves. Also various fixes to editing.
author Chris Cannam
date Thu, 21 Nov 2019 14:02:57 +0000
parents f2525e6cbdf1
children
rev   line source
Chris@696 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@696 2
Chris@696 3 /*
Chris@696 4 Sonic Visualiser
Chris@696 5 An audio file viewer and annotation editor.
Chris@696 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1281 7 This file copyright 2006-2018 Chris Cannam and QMUL.
Chris@696 8
Chris@696 9 This program is free software; you can redistribute it and/or
Chris@696 10 modify it under the terms of the GNU General Public License as
Chris@696 11 published by the Free Software Foundation; either version 2 of the
Chris@696 12 License, or (at your option) any later version. See the file
Chris@696 13 COPYING included with this distribution for more information.
Chris@696 14 */
Chris@696 15
Chris@696 16 #include "LogNumericalScale.h"
Chris@696 17 #include "VerticalScaleLayer.h"
Chris@1276 18 #include "LayerGeometryProvider.h"
Chris@696 19
Chris@696 20 #include "base/LogRange.h"
Chris@1276 21 #include "base/ScaleTickIntervals.h"
Chris@696 22
Chris@696 23 #include <QPainter>
Chris@696 24
Chris@696 25 #include <cmath>
Chris@696 26
Chris@696 27 int
Chris@918 28 LogNumericalScale::getWidth(LayerGeometryProvider *,
Chris@1281 29 QPainter &paint)
Chris@696 30 {
Chris@1471 31 // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
Chris@1471 32 // replacement (horizontalAdvance) was only added in Qt 5.11
Chris@1471 33 // which is too new for us
Chris@1471 34 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Chris@1471 35
Chris@1281 36 return paint.fontMetrics().width("-000.00") + 10;
Chris@696 37 }
Chris@696 38
Chris@696 39 void
Chris@918 40 LogNumericalScale::paintVertical(LayerGeometryProvider *v,
Chris@1266 41 const VerticalScaleLayer *layer,
Chris@1266 42 QPainter &paint,
Chris@1266 43 int x0,
Chris@1266 44 double minlog,
Chris@1266 45 double maxlog)
Chris@696 46 {
Chris@1260 47 int n = 10;
Chris@1260 48 auto ticks = ScaleTickIntervals::logarithmicAlready({ minlog, maxlog, n });
Chris@1260 49 n = int(ticks.size());
Chris@1260 50
Chris@696 51 int w = getWidth(v, paint) + x0;
Chris@696 52
Chris@696 53 int prevy = -1;
Chris@696 54
Chris@696 55 for (int i = 0; i < n; ++i) {
Chris@696 56
Chris@1266 57 int y, ty;
Chris@696 58 bool drawText = true;
Chris@696 59
Chris@1266 60 if (i == n-1 &&
Chris@1266 61 v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) {
Chris@1266 62 if (layer->getScaleUnits() != "") drawText = false;
Chris@1266 63 }
Chris@696 64
Chris@1260 65 double val = ticks[i].value;
Chris@1260 66 QString label = QString::fromStdString(ticks[i].label);
Chris@696 67
Chris@1266 68 y = layer->getYForValue(v, val);
Chris@696 69
Chris@1266 70 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
Chris@1266 71
Chris@1266 72 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
Chris@1266 73 continue;
Chris@696 74 }
Chris@696 75
Chris@1266 76 paint.drawLine(w - 5, y, w, y);
Chris@696 77
Chris@696 78 if (drawText) {
Chris@1266 79 paint.drawText(w - paint.fontMetrics().width(label) - 6,
Chris@1266 80 ty, label);
Chris@696 81 }
Chris@696 82
Chris@696 83 prevy = y;
Chris@696 84 }
Chris@696 85 }
Chris@1276 86