annotate layer/LinearColourScale.cpp @ 1511:a473b73fb045 time-frequency-boxes

Introduce time-frequency box layer
author Chris Cannam
date Thu, 19 Sep 2019 15:18:28 +0100
parents f2525e6cbdf1
children
rev   line source
Chris@699 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@699 2
Chris@699 3 /*
Chris@699 4 Sonic Visualiser
Chris@699 5 An audio file viewer and annotation editor.
Chris@699 6 Centre for Digital Music, Queen Mary, University of London.
Chris@699 7 This file copyright 2006-2013 Chris Cannam and QMUL.
Chris@699 8
Chris@699 9 This program is free software; you can redistribute it and/or
Chris@699 10 modify it under the terms of the GNU General Public License as
Chris@699 11 published by the Free Software Foundation; either version 2 of the
Chris@699 12 License, or (at your option) any later version. See the file
Chris@699 13 COPYING included with this distribution for more information.
Chris@699 14 */
Chris@699 15
Chris@699 16 #include "LinearColourScale.h"
Chris@699 17 #include "ColourScaleLayer.h"
Chris@699 18
Chris@699 19 #include <QPainter>
Chris@699 20
Chris@699 21 #include <cmath>
Chris@699 22
Chris@1077 23 #include "LayerGeometryProvider.h"
Chris@699 24
Chris@699 25 int
Chris@918 26 LinearColourScale::getWidth(LayerGeometryProvider *,
Chris@1266 27 QPainter &paint)
Chris@699 28 {
Chris@1471 29 // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
Chris@1471 30 // replacement (horizontalAdvance) was only added in Qt 5.11
Chris@1471 31 // which is too new for us
Chris@1471 32 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Chris@1471 33
Chris@699 34 return paint.fontMetrics().width("-000.00") + 15;
Chris@699 35 }
Chris@699 36
Chris@699 37 void
Chris@918 38 LinearColourScale::paintVertical(LayerGeometryProvider *v,
Chris@1266 39 const ColourScaleLayer *layer,
Chris@1266 40 QPainter &paint,
Chris@1266 41 int /* x0 */,
Chris@1266 42 double min,
Chris@1266 43 double max)
Chris@699 44 {
Chris@918 45 int h = v->getPaintHeight();
Chris@699 46
Chris@699 47 int n = 10;
Chris@699 48
Chris@905 49 double val = min;
Chris@905 50 double inc = (max - val) / n;
Chris@864 51
Chris@864 52 const int buflen = 40;
Chris@864 53 char buffer[buflen];
Chris@699 54
Chris@699 55 int boxx = 5, boxy = 5;
Chris@699 56 if (layer->getScaleUnits() != "") {
Chris@699 57 boxy += paint.fontMetrics().height();
Chris@699 58 }
Chris@699 59 int boxw = 10, boxh = h - boxy - 5;
Chris@699 60
Chris@699 61 int tx = 5 + boxx + boxw;
Chris@699 62 paint.drawRect(boxx, boxy, boxw, boxh);
Chris@699 63
Chris@699 64 paint.save();
Chris@699 65 for (int y = 0; y < boxh; ++y) {
Chris@1266 66 double val = ((boxh - y) * (max - min)) / boxh + min;
Chris@1266 67 paint.setPen(layer->getColourForValue(v, val));
Chris@1266 68 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
Chris@699 69 }
Chris@699 70 paint.restore();
Chris@699 71
Chris@905 72 // double round = 1.f;
Chris@699 73 int dp = 0;
Chris@699 74 if (inc > 0) {
Chris@905 75 int prec = int(trunc(log10(inc)));
Chris@699 76 prec -= 1;
Chris@699 77 if (prec < 0) dp = -prec;
Chris@805 78 // round = powf(10.f, prec);
Chris@805 79 //#ifdef DEBUG_TIME_VALUE_LAYER
Chris@805 80 // cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl;
Chris@805 81 //#endif
Chris@699 82 }
Chris@699 83
Chris@699 84 for (int i = 0; i < n; ++i) {
Chris@699 85
Chris@1266 86 int y, ty;
Chris@699 87
Chris@1266 88 y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
Chris@699 89
Chris@1266 90 ty = y - paint.fontMetrics().height() +
Chris@1266 91 paint.fontMetrics().ascent() + 2;
Chris@699 92
Chris@1266 93 snprintf(buffer, buflen, "%.*f", dp, val);
Chris@1266 94 QString label = QString(buffer);
Chris@699 95
Chris@1266 96 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
Chris@1266 97 paint.drawText(tx, ty, label);
Chris@699 98
Chris@1266 99 val += inc;
Chris@699 100 }
Chris@699 101 }