annotate layer/LogNumericalScale.cpp @ 710:c65826b5e980 tonioni

Use sv-dependency-builds on OS/X as well if no config.pri
author Chris Cannam
date Thu, 09 Jan 2014 09:18:17 +0000
parents 1a1448f7beb2
children b81f21f2c4c3
rev   line source
Chris@698 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@698 2
Chris@698 3 /*
Chris@698 4 Sonic Visualiser
Chris@698 5 An audio file viewer and annotation editor.
Chris@698 6 Centre for Digital Music, Queen Mary, University of London.
Chris@698 7 This file copyright 2006-2013 Chris Cannam and QMUL.
Chris@698 8
Chris@698 9 This program is free software; you can redistribute it and/or
Chris@698 10 modify it under the terms of the GNU General Public License as
Chris@698 11 published by the Free Software Foundation; either version 2 of the
Chris@698 12 License, or (at your option) any later version. See the file
Chris@698 13 COPYING included with this distribution for more information.
Chris@698 14 */
Chris@698 15
Chris@698 16 #include "LogNumericalScale.h"
Chris@698 17 #include "VerticalScaleLayer.h"
Chris@698 18
Chris@698 19 #include "base/LogRange.h"
Chris@698 20
Chris@698 21 #include <QPainter>
Chris@698 22
Chris@698 23 #include <cmath>
Chris@698 24
Chris@698 25 #include "view/View.h"
Chris@698 26
Chris@698 27 //#define DEBUG_TIME_VALUE_LAYER 1
Chris@698 28
Chris@698 29 int
Chris@698 30 LogNumericalScale::getWidth(View *,
Chris@698 31 QPainter &paint)
Chris@698 32 {
Chris@699 33 return paint.fontMetrics().width("-000.00") + 10;
Chris@698 34 }
Chris@698 35
Chris@698 36 void
Chris@698 37 LogNumericalScale::paintVertical(View *v,
Chris@698 38 const VerticalScaleLayer *layer,
Chris@698 39 QPainter &paint,
Chris@698 40 int x0,
Chris@698 41 float minlog,
Chris@698 42 float maxlog)
Chris@698 43 {
Chris@698 44 int w = getWidth(v, paint) + x0;
Chris@698 45
Chris@698 46 int n = 10;
Chris@698 47
Chris@698 48 float val = minlog;
Chris@698 49 float inc = (maxlog - val) / n; // even increments of log scale
Chris@698 50
Chris@698 51 // smallest increment as displayed
Chris@698 52 float minDispInc = LogRange::unmap(minlog + inc) - LogRange::unmap(minlog);
Chris@698 53
Chris@698 54 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@698 55 cerr << "min = " << minlog << ", max = " << maxlog << ", inc = " << inc << ", minDispInc = " << minDispInc << endl;
Chris@698 56 #endif
Chris@698 57
Chris@698 58 char buffer[40];
Chris@698 59
Chris@698 60 float round = 1.f;
Chris@698 61 int dp = 0;
Chris@698 62
Chris@698 63 if (minDispInc > 0) {
Chris@698 64 int prec = trunc(log10f(minDispInc));
Chris@698 65 if (prec < 0) dp = -prec;
Chris@698 66 round = powf(10.f, prec);
Chris@698 67 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@698 68 cerr << "round = " << round << ", prec = " << prec << ", dp = " << dp << endl;
Chris@698 69 #endif
Chris@698 70 }
Chris@698 71
Chris@698 72 int prevy = -1;
Chris@698 73
Chris@698 74 for (int i = 0; i < n; ++i) {
Chris@698 75
Chris@698 76 int y, ty;
Chris@698 77 bool drawText = true;
Chris@698 78
Chris@698 79 if (i == n-1 &&
Chris@698 80 v->height() < paint.fontMetrics().height() * (n*2)) {
Chris@698 81 if (layer->getScaleUnits() != "") drawText = false;
Chris@698 82 }
Chris@698 83
Chris@698 84 float dispval = LogRange::unmap(val);
Chris@698 85 dispval = floor(dispval / round) * round;
Chris@698 86
Chris@698 87 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@698 88 cerr << "val = " << val << ", dispval = " << dispval << endl;
Chris@698 89 #endif
Chris@698 90
Chris@698 91 y = layer->getYForValue(v, dispval);
Chris@698 92
Chris@698 93 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
Chris@698 94
Chris@698 95 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
Chris@698 96 val += inc;
Chris@698 97 continue;
Chris@698 98 }
Chris@698 99
Chris@698 100 double dv = dispval;
Chris@698 101 int digits = trunc(log10f(dv));
Chris@698 102 int sf = dp + (digits > 0 ? digits : 0);
Chris@698 103 if (sf < 4) sf = 4;
Chris@698 104 sprintf(buffer, "%.*g", sf, dv);
Chris@698 105
Chris@698 106 QString label = QString(buffer);
Chris@698 107
Chris@698 108 paint.drawLine(w - 5, y, w, y);
Chris@698 109
Chris@698 110 if (drawText) {
Chris@698 111 paint.drawText(w - paint.fontMetrics().width(label) - 6,
Chris@698 112 ty, label);
Chris@698 113 }
Chris@698 114
Chris@698 115 prevy = y;
Chris@698 116 val += inc;
Chris@698 117 }
Chris@698 118 }