svgui  1.9
LinearNumericalScale.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006-2013 Chris Cannam and QMUL.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "LinearNumericalScale.h"
00017 #include "VerticalScaleLayer.h"
00018 
00019 #include <QPainter>
00020 
00021 #include <cmath>
00022 
00023 #include "view/View.h"
00024 
00025 int
00026 LinearNumericalScale::getWidth(View *,
00027                                QPainter &paint)
00028 {
00029     return paint.fontMetrics().width("-000.00") + 10;
00030 }
00031 
00032 void
00033 LinearNumericalScale::paintVertical(View *v,
00034                                     const VerticalScaleLayer *layer,
00035                                     QPainter &paint,
00036                                     int x0,
00037                                     float minf,
00038                                     float maxf)
00039 {
00040     int n = 10;
00041 
00042     float val = minf;
00043     float inc = (maxf - val) / n;
00044 
00045     char buffer[40];
00046 
00047     int w = getWidth(v, paint) + x0;
00048 
00049     float round = 1.f;
00050     int dp = 0;
00051     if (inc > 0) {
00052         int prec = trunc(log10f(inc));
00053         prec -= 1;
00054         if (prec < 0) dp = -prec;
00055         round = powf(10.f, prec);
00056 #ifdef DEBUG_TIME_VALUE_LAYER
00057         cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl;
00058 #endif
00059     }
00060 
00061     int prevy = -1;
00062                 
00063     for (int i = 0; i < n; ++i) {
00064 
00065         int y, ty;
00066         bool drawText = true;
00067 
00068         float dispval = val;
00069 
00070         if (i == n-1 &&
00071             v->height() < paint.fontMetrics().height() * (n*2)) {
00072             if (layer->getScaleUnits() != "") drawText = false;
00073         }
00074         dispval = lrintf(val / round) * round;
00075 
00076 #ifdef DEBUG_TIME_VALUE_LAYER
00077         cerr << "val = " << val << ", dispval = " << dispval << endl;
00078 #endif
00079 
00080         y = layer->getYForValue(v, dispval);
00081 
00082         ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
00083         
00084         if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
00085             val += inc;
00086             continue;
00087         }
00088 
00089         sprintf(buffer, "%.*f", dp, dispval);
00090 
00091         QString label = QString(buffer);
00092 
00093         paint.drawLine(w - 5, y, w, y);
00094 
00095         if (drawText) {
00096             paint.drawText(w - paint.fontMetrics().width(label) - 6,
00097                            ty, label);
00098         }
00099 
00100         prevy = y;
00101         val += inc;
00102     }
00103 }