annotate layer/LinearNumericalScale.cpp @ 735:1640a7c753cc tonioni

Short-circuit test for enough space for label -- drastically reduces lag on os/x with qt4
author Chris Cannam
date Thu, 06 Mar 2014 09:43:18 +0000
parents 1a1448f7beb2
children 1d526ba11a24
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 "LinearNumericalScale.h"
Chris@698 17 #include "VerticalScaleLayer.h"
Chris@698 18
Chris@698 19 #include <QPainter>
Chris@698 20
Chris@698 21 #include <cmath>
Chris@698 22
Chris@698 23 #include "view/View.h"
Chris@698 24
Chris@698 25 int
Chris@698 26 LinearNumericalScale::getWidth(View *v,
Chris@698 27 QPainter &paint)
Chris@698 28 {
Chris@699 29 return paint.fontMetrics().width("-000.00") + 10;
Chris@698 30 }
Chris@698 31
Chris@698 32 void
Chris@698 33 LinearNumericalScale::paintVertical(View *v,
Chris@698 34 const VerticalScaleLayer *layer,
Chris@698 35 QPainter &paint,
Chris@698 36 int x0,
Chris@698 37 float minf,
Chris@698 38 float maxf)
Chris@698 39 {
Chris@698 40 int h = v->height();
Chris@698 41
Chris@698 42 int n = 10;
Chris@698 43
Chris@698 44 float val = minf;
Chris@698 45 float inc = (maxf - val) / n;
Chris@698 46
Chris@698 47 char buffer[40];
Chris@698 48
Chris@698 49 int w = getWidth(v, paint) + x0;
Chris@698 50
Chris@698 51 float round = 1.f;
Chris@698 52 int dp = 0;
Chris@698 53 if (inc > 0) {
Chris@698 54 int prec = trunc(log10f(inc));
Chris@698 55 prec -= 1;
Chris@698 56 if (prec < 0) dp = -prec;
Chris@698 57 round = powf(10.f, prec);
Chris@698 58 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@698 59 cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl;
Chris@698 60 #endif
Chris@698 61 }
Chris@698 62
Chris@698 63 int prevy = -1;
Chris@698 64
Chris@698 65 for (int i = 0; i < n; ++i) {
Chris@698 66
Chris@698 67 int y, ty;
Chris@698 68 bool drawText = true;
Chris@698 69
Chris@698 70 float dispval = val;
Chris@698 71
Chris@698 72 if (i == n-1 &&
Chris@698 73 v->height() < paint.fontMetrics().height() * (n*2)) {
Chris@698 74 if (layer->getScaleUnits() != "") drawText = false;
Chris@698 75 }
Chris@698 76 dispval = lrintf(val / round) * round;
Chris@698 77
Chris@698 78 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@698 79 cerr << "val = " << val << ", dispval = " << dispval << endl;
Chris@698 80 #endif
Chris@698 81
Chris@698 82 y = layer->getYForValue(v, dispval);
Chris@698 83
Chris@698 84 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
Chris@698 85
Chris@698 86 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
Chris@698 87 val += inc;
Chris@698 88 continue;
Chris@698 89 }
Chris@698 90
Chris@698 91 sprintf(buffer, "%.*f", dp, dispval);
Chris@698 92
Chris@698 93 QString label = QString(buffer);
Chris@698 94
Chris@698 95 paint.drawLine(w - 5, y, w, y);
Chris@698 96
Chris@698 97 if (drawText) {
Chris@699 98 paint.drawText(w - paint.fontMetrics().width(label) - 6,
Chris@698 99 ty, label);
Chris@698 100 }
Chris@698 101
Chris@698 102 prevy = y;
Chris@698 103 val += inc;
Chris@698 104 }
Chris@698 105 }