annotate layer/LinearNumericalScale.cpp @ 1024:3bce4c45b681 spectrogram-minor-refactor

Rearrange cache update calculations so as to use the actual painted width returned by paint functions (though they only ever return the same width as requested, at this point)
author Chris Cannam
date Mon, 25 Jan 2016 15:52:26 +0000
parents 4fe7a09be0fe
children 5144d7185fb5
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 "LinearNumericalScale.h"
Chris@696 17 #include "VerticalScaleLayer.h"
Chris@696 18
Chris@696 19 #include <QPainter>
Chris@696 20
Chris@696 21 #include <cmath>
Chris@696 22
Chris@696 23 #include "view/View.h"
Chris@696 24
Chris@696 25 int
Chris@918 26 LinearNumericalScale::getWidth(LayerGeometryProvider *,
Chris@696 27 QPainter &paint)
Chris@696 28 {
Chris@699 29 return paint.fontMetrics().width("-000.00") + 10;
Chris@696 30 }
Chris@696 31
Chris@696 32 void
Chris@918 33 LinearNumericalScale::paintVertical(LayerGeometryProvider *v,
Chris@696 34 const VerticalScaleLayer *layer,
Chris@696 35 QPainter &paint,
Chris@696 36 int x0,
Chris@904 37 double minf,
Chris@904 38 double maxf)
Chris@696 39 {
Chris@696 40 int n = 10;
Chris@696 41
Chris@904 42 double val = minf;
Chris@904 43 double inc = (maxf - val) / n;
Chris@696 44
Chris@864 45 const int buflen = 40;
Chris@864 46 char buffer[buflen];
Chris@696 47
Chris@696 48 int w = getWidth(v, paint) + x0;
Chris@696 49
Chris@905 50 double round = 1.0;
Chris@696 51 int dp = 0;
Chris@696 52 if (inc > 0) {
Chris@905 53 int prec = int(trunc(log10(inc)));
Chris@696 54 prec -= 1;
Chris@696 55 if (prec < 0) dp = -prec;
Chris@905 56 round = pow(10.0, prec);
Chris@696 57 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@696 58 cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl;
Chris@696 59 #endif
Chris@696 60 }
Chris@696 61
Chris@696 62 int prevy = -1;
Chris@696 63
Chris@696 64 for (int i = 0; i < n; ++i) {
Chris@696 65
Chris@696 66 int y, ty;
Chris@696 67 bool drawText = true;
Chris@696 68
Chris@904 69 double dispval = val;
Chris@696 70
Chris@696 71 if (i == n-1 &&
Chris@918 72 v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) {
Chris@696 73 if (layer->getScaleUnits() != "") drawText = false;
Chris@696 74 }
Chris@905 75 dispval = int(rint(val / round) * round);
Chris@696 76
Chris@696 77 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@696 78 cerr << "val = " << val << ", dispval = " << dispval << endl;
Chris@696 79 #endif
Chris@696 80
Chris@696 81 y = layer->getYForValue(v, dispval);
Chris@696 82
Chris@696 83 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
Chris@696 84
Chris@696 85 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
Chris@696 86 val += inc;
Chris@696 87 continue;
Chris@696 88 }
Chris@696 89
Chris@864 90 snprintf(buffer, buflen, "%.*f", dp, dispval);
Chris@696 91
Chris@696 92 QString label = QString(buffer);
Chris@696 93
Chris@696 94 paint.drawLine(w - 5, y, w, y);
Chris@696 95
Chris@696 96 if (drawText) {
Chris@699 97 paint.drawText(w - paint.fontMetrics().width(label) - 6,
Chris@696 98 ty, label);
Chris@696 99 }
Chris@696 100
Chris@696 101 prevy = y;
Chris@696 102 val += inc;
Chris@696 103 }
Chris@696 104 }