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