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@1276: #include "HorizontalScaleProvider.h" Chris@1276: #include "LayerGeometryProvider.h" Chris@696: Chris@696: #include "base/LogRange.h" Chris@1276: #include "base/ScaleTickIntervals.h" Chris@696: Chris@696: #include Chris@696: Chris@696: #include Chris@696: Chris@696: int Chris@918: LogNumericalScale::getWidth(LayerGeometryProvider *, Chris@1276: QPainter &paint, Chris@1276: bool horizontal) Chris@696: { Chris@1276: if (horizontal) { Chris@1276: return paint.fontMetrics().height() + 10; Chris@1276: } else { Chris@1276: return paint.fontMetrics().width("-000.00") + 10; Chris@1276: } Chris@696: } Chris@696: Chris@696: void Chris@918: LogNumericalScale::paintVertical(LayerGeometryProvider *v, Chris@1266: const VerticalScaleLayer *layer, Chris@1266: QPainter &paint, Chris@1266: int x0, Chris@1266: double minlog, Chris@1266: double maxlog) Chris@696: { Chris@1260: int n = 10; Chris@1260: auto ticks = ScaleTickIntervals::logarithmicAlready({ minlog, maxlog, n }); Chris@1260: n = int(ticks.size()); Chris@1260: Chris@696: int w = getWidth(v, paint) + x0; Chris@696: Chris@696: int prevy = -1; Chris@696: Chris@696: for (int i = 0; i < n; ++i) { Chris@696: Chris@1266: int y, ty; Chris@696: bool drawText = true; Chris@696: Chris@1266: if (i == n-1 && Chris@1266: v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) { Chris@1266: if (layer->getScaleUnits() != "") drawText = false; Chris@1266: } Chris@696: Chris@1260: double val = ticks[i].value; Chris@1260: QString label = QString::fromStdString(ticks[i].label); Chris@696: Chris@1266: y = layer->getYForValue(v, val); Chris@696: Chris@1266: ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2; Chris@1266: Chris@1266: if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) { Chris@1266: continue; Chris@696: } Chris@696: Chris@1266: paint.drawLine(w - 5, y, w, y); Chris@696: Chris@696: if (drawText) { Chris@1266: paint.drawText(w - paint.fontMetrics().width(label) - 6, Chris@1266: ty, label); Chris@696: } Chris@696: Chris@696: prevy = y; Chris@696: } Chris@696: } Chris@1276: Chris@1276: void Chris@1276: LogNumericalScale::paintHorizontal(LayerGeometryProvider *v, Chris@1276: const HorizontalScaleProvider *p, Chris@1276: QPainter &paint, Chris@1276: QRect r) Chris@1276: { Chris@1276: int x0 = r.x(), y0 = r.y(), x1 = r.x() + r.width(), y1 = r.y() + r.height(); Chris@1276: Chris@1276: paint.drawLine(x0, y0, x1, y0); Chris@1276: Chris@1277: double f0 = p->getFrequencyForX(v, x0 ? x0 : 1); Chris@1276: double f1 = p->getFrequencyForX(v, x1); Chris@1276: Chris@1277: cerr << "f0 = " << f0 << " at x " << (x0 ? x0 : 1) << endl; Chris@1277: cerr << "f1 = " << f1 << " at x " << x1 << endl; Chris@1277: Chris@1278: int n = 20; Chris@1276: auto ticks = ScaleTickIntervals::logarithmic({ f0, f1, n }); Chris@1276: n = int(ticks.size()); Chris@1276: Chris@1277: int marginx = -1; Chris@1276: Chris@1276: for (int i = 0; i < n; ++i) { Chris@1276: Chris@1276: double val = ticks[i].value; Chris@1276: QString label = QString::fromStdString(ticks[i].label); Chris@1276: int tw = paint.fontMetrics().width(label); Chris@1276: Chris@1276: cerr << "i = " << i << ", value = " << val << ", tw = " << tw << endl; Chris@1276: Chris@1277: int x = int(round(p->getXForFrequency(v, val))); Chris@1276: Chris@1276: cerr << "x = " << x << endl; Chris@1277: Chris@1277: if (x < marginx) continue; Chris@1276: Chris@1277: //!!! todo: pixel scaling (here & elsewhere in these classes) Chris@1277: Chris@1277: paint.drawLine(x, y0, x, y1); Chris@1277: Chris@1277: paint.drawText(x + 5, y0 + paint.fontMetrics().ascent() + 5, label); Chris@1277: Chris@1277: marginx = x + tw + 10; Chris@1276: } Chris@1276: } Chris@1276: