Chris@698: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@698: Chris@698: /* Chris@698: Sonic Visualiser Chris@698: An audio file viewer and annotation editor. Chris@698: Centre for Digital Music, Queen Mary, University of London. Chris@698: This file copyright 2006-2013 Chris Cannam and QMUL. Chris@698: Chris@698: This program is free software; you can redistribute it and/or Chris@698: modify it under the terms of the GNU General Public License as Chris@698: published by the Free Software Foundation; either version 2 of the Chris@698: License, or (at your option) any later version. See the file Chris@698: COPYING included with this distribution for more information. Chris@698: */ Chris@698: Chris@698: #include "LinearNumericalScale.h" Chris@698: #include "VerticalScaleLayer.h" Chris@698: Chris@698: #include Chris@698: Chris@698: #include Chris@698: Chris@698: #include "view/View.h" Chris@698: Chris@698: int Chris@698: LinearNumericalScale::getWidth(View *v, Chris@698: QPainter &paint) Chris@698: { Chris@699: return paint.fontMetrics().width("-000.00") + 10; Chris@698: } Chris@698: Chris@698: void Chris@698: LinearNumericalScale::paintVertical(View *v, Chris@698: const VerticalScaleLayer *layer, Chris@698: QPainter &paint, Chris@698: int x0, Chris@698: float minf, Chris@698: float maxf) Chris@698: { Chris@698: int h = v->height(); Chris@698: Chris@698: int n = 10; Chris@698: Chris@698: float val = minf; Chris@698: float inc = (maxf - val) / n; Chris@698: Chris@698: char buffer[40]; Chris@698: Chris@698: int w = getWidth(v, paint) + x0; Chris@698: Chris@698: float round = 1.f; Chris@698: int dp = 0; Chris@698: if (inc > 0) { Chris@698: int prec = trunc(log10f(inc)); Chris@698: prec -= 1; Chris@698: if (prec < 0) dp = -prec; Chris@698: round = powf(10.f, prec); Chris@698: #ifdef DEBUG_TIME_VALUE_LAYER Chris@698: cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl; Chris@698: #endif Chris@698: } Chris@698: Chris@698: int prevy = -1; Chris@698: Chris@698: for (int i = 0; i < n; ++i) { Chris@698: Chris@698: int y, ty; Chris@698: bool drawText = true; Chris@698: Chris@698: float dispval = val; Chris@698: Chris@698: if (i == n-1 && Chris@698: v->height() < paint.fontMetrics().height() * (n*2)) { Chris@698: if (layer->getScaleUnits() != "") drawText = false; Chris@698: } Chris@698: dispval = lrintf(val / round) * round; Chris@698: Chris@698: #ifdef DEBUG_TIME_VALUE_LAYER Chris@698: cerr << "val = " << val << ", dispval = " << dispval << endl; Chris@698: #endif Chris@698: Chris@698: y = layer->getYForValue(v, dispval); Chris@698: Chris@698: ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2; Chris@698: Chris@698: if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) { Chris@698: val += inc; Chris@698: continue; Chris@698: } Chris@698: Chris@698: sprintf(buffer, "%.*f", dp, dispval); Chris@698: Chris@698: QString label = QString(buffer); Chris@698: Chris@698: paint.drawLine(w - 5, y, w, y); Chris@698: Chris@698: if (drawText) { Chris@699: paint.drawText(w - paint.fontMetrics().width(label) - 6, Chris@698: ty, label); Chris@698: } Chris@698: Chris@698: prevy = y; Chris@698: val += inc; Chris@698: } Chris@698: }