annotate layer/LinearNumericalScale.cpp @ 696:e77b1673e17e tonioni

Pull out log and linear vertical scales into their own classes, make some improvements to log numbering
author Chris Cannam
date Wed, 04 Dec 2013 11:35:08 +0000
parents
children 1a1448f7beb2
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@696 26 LinearNumericalScale::getWidth(View *v,
Chris@696 27 QPainter &paint)
Chris@696 28 {
Chris@696 29 return paint.fontMetrics().width("-000.000");
Chris@696 30 }
Chris@696 31
Chris@696 32 void
Chris@696 33 LinearNumericalScale::paintVertical(View *v,
Chris@696 34 const VerticalScaleLayer *layer,
Chris@696 35 QPainter &paint,
Chris@696 36 int x0,
Chris@696 37 float minf,
Chris@696 38 float maxf)
Chris@696 39 {
Chris@696 40 int h = v->height();
Chris@696 41
Chris@696 42 int n = 10;
Chris@696 43
Chris@696 44 float val = minf;
Chris@696 45 float inc = (maxf - val) / n;
Chris@696 46
Chris@696 47 char buffer[40];
Chris@696 48
Chris@696 49 int w = getWidth(v, paint) + x0;
Chris@696 50
Chris@696 51 float round = 1.f;
Chris@696 52 int dp = 0;
Chris@696 53 if (inc > 0) {
Chris@696 54 int prec = trunc(log10f(inc));
Chris@696 55 prec -= 1;
Chris@696 56 if (prec < 0) dp = -prec;
Chris@696 57 round = powf(10.f, prec);
Chris@696 58 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@696 59 cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl;
Chris@696 60 #endif
Chris@696 61 }
Chris@696 62
Chris@696 63 int prevy = -1;
Chris@696 64
Chris@696 65 for (int i = 0; i < n; ++i) {
Chris@696 66
Chris@696 67 int y, ty;
Chris@696 68 bool drawText = true;
Chris@696 69
Chris@696 70 float dispval = val;
Chris@696 71
Chris@696 72 if (i == n-1 &&
Chris@696 73 v->height() < paint.fontMetrics().height() * (n*2)) {
Chris@696 74 if (layer->getScaleUnits() != "") drawText = false;
Chris@696 75 }
Chris@696 76 dispval = lrintf(val / round) * round;
Chris@696 77
Chris@696 78 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@696 79 cerr << "val = " << val << ", dispval = " << dispval << endl;
Chris@696 80 #endif
Chris@696 81
Chris@696 82 y = layer->getYForValue(v, dispval);
Chris@696 83
Chris@696 84 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
Chris@696 85
Chris@696 86 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
Chris@696 87 val += inc;
Chris@696 88 continue;
Chris@696 89 }
Chris@696 90
Chris@696 91 sprintf(buffer, "%.*f", dp, dispval);
Chris@696 92
Chris@696 93 QString label = QString(buffer);
Chris@696 94
Chris@696 95 paint.drawLine(w - 5, y, w, y);
Chris@696 96
Chris@696 97 if (drawText) {
Chris@696 98 paint.drawText(w - paint.fontMetrics().width(label) - 13,
Chris@696 99 ty, label);
Chris@696 100 }
Chris@696 101
Chris@696 102 prevy = y;
Chris@696 103 val += inc;
Chris@696 104 }
Chris@696 105 }