annotate layer/LogNumericalScale.cpp @ 1280:34394e8c2942 horizontal-scale

Merge
author Chris Cannam
date Wed, 02 May 2018 14:27:17 +0100
parents b6fc0970cd66
children fc9d9f1103fa
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@696 7 This file copyright 2006-2013 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 "HorizontalScaleProvider.h"
Chris@1276 19 #include "LayerGeometryProvider.h"
Chris@696 20
Chris@696 21 #include "base/LogRange.h"
Chris@1276 22 #include "base/ScaleTickIntervals.h"
Chris@696 23
Chris@696 24 #include <QPainter>
Chris@696 25
Chris@696 26 #include <cmath>
Chris@696 27
Chris@696 28 int
Chris@918 29 LogNumericalScale::getWidth(LayerGeometryProvider *,
Chris@1276 30 QPainter &paint,
Chris@1276 31 bool horizontal)
Chris@696 32 {
Chris@1276 33 if (horizontal) {
Chris@1276 34 return paint.fontMetrics().height() + 10;
Chris@1276 35 } else {
Chris@1276 36 return paint.fontMetrics().width("-000.00") + 10;
Chris@1276 37 }
Chris@696 38 }
Chris@696 39
Chris@696 40 void
Chris@918 41 LogNumericalScale::paintVertical(LayerGeometryProvider *v,
Chris@1266 42 const VerticalScaleLayer *layer,
Chris@1266 43 QPainter &paint,
Chris@1266 44 int x0,
Chris@1266 45 double minlog,
Chris@1266 46 double maxlog)
Chris@696 47 {
Chris@1260 48 int n = 10;
Chris@1260 49 auto ticks = ScaleTickIntervals::logarithmicAlready({ minlog, maxlog, n });
Chris@1260 50 n = int(ticks.size());
Chris@1260 51
Chris@696 52 int w = getWidth(v, paint) + x0;
Chris@696 53
Chris@696 54 int prevy = -1;
Chris@696 55
Chris@696 56 for (int i = 0; i < n; ++i) {
Chris@696 57
Chris@1266 58 int y, ty;
Chris@696 59 bool drawText = true;
Chris@696 60
Chris@1266 61 if (i == n-1 &&
Chris@1266 62 v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) {
Chris@1266 63 if (layer->getScaleUnits() != "") drawText = false;
Chris@1266 64 }
Chris@696 65
Chris@1260 66 double val = ticks[i].value;
Chris@1260 67 QString label = QString::fromStdString(ticks[i].label);
Chris@696 68
Chris@1266 69 y = layer->getYForValue(v, val);
Chris@696 70
Chris@1266 71 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
Chris@1266 72
Chris@1266 73 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
Chris@1266 74 continue;
Chris@696 75 }
Chris@696 76
Chris@1266 77 paint.drawLine(w - 5, y, w, y);
Chris@696 78
Chris@696 79 if (drawText) {
Chris@1266 80 paint.drawText(w - paint.fontMetrics().width(label) - 6,
Chris@1266 81 ty, label);
Chris@696 82 }
Chris@696 83
Chris@696 84 prevy = y;
Chris@696 85 }
Chris@696 86 }
Chris@1276 87
Chris@1276 88 void
Chris@1276 89 LogNumericalScale::paintHorizontal(LayerGeometryProvider *v,
Chris@1276 90 const HorizontalScaleProvider *p,
Chris@1276 91 QPainter &paint,
Chris@1276 92 QRect r)
Chris@1276 93 {
Chris@1276 94 int x0 = r.x(), y0 = r.y(), x1 = r.x() + r.width(), y1 = r.y() + r.height();
Chris@1276 95
Chris@1276 96 paint.drawLine(x0, y0, x1, y0);
Chris@1276 97
Chris@1277 98 double f0 = p->getFrequencyForX(v, x0 ? x0 : 1);
Chris@1276 99 double f1 = p->getFrequencyForX(v, x1);
Chris@1276 100
Chris@1277 101 cerr << "f0 = " << f0 << " at x " << (x0 ? x0 : 1) << endl;
Chris@1277 102 cerr << "f1 = " << f1 << " at x " << x1 << endl;
Chris@1277 103
Chris@1278 104 int n = 20;
Chris@1276 105 auto ticks = ScaleTickIntervals::logarithmic({ f0, f1, n });
Chris@1276 106 n = int(ticks.size());
Chris@1276 107
Chris@1277 108 int marginx = -1;
Chris@1276 109
Chris@1276 110 for (int i = 0; i < n; ++i) {
Chris@1276 111
Chris@1276 112 double val = ticks[i].value;
Chris@1276 113 QString label = QString::fromStdString(ticks[i].label);
Chris@1276 114 int tw = paint.fontMetrics().width(label);
Chris@1276 115
Chris@1276 116 cerr << "i = " << i << ", value = " << val << ", tw = " << tw << endl;
Chris@1276 117
Chris@1277 118 int x = int(round(p->getXForFrequency(v, val)));
Chris@1276 119
Chris@1276 120 cerr << "x = " << x << endl;
Chris@1277 121
Chris@1277 122 if (x < marginx) continue;
Chris@1276 123
Chris@1277 124 //!!! todo: pixel scaling (here & elsewhere in these classes)
Chris@1277 125
Chris@1277 126 paint.drawLine(x, y0, x, y1);
Chris@1277 127
Chris@1277 128 paint.drawText(x + 5, y0 + paint.fontMetrics().ascent() + 5, label);
Chris@1277 129
Chris@1277 130 marginx = x + tw + 10;
Chris@1276 131 }
Chris@1276 132 }
Chris@1276 133