svgui  1.9
LogColourScale.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 "LogColourScale.h"
00017 #include "ColourScaleLayer.h"
00018 
00019 #include "base/LogRange.h"
00020 
00021 #include <QPainter>
00022 
00023 #include <cmath>
00024 
00025 #include "view/View.h"
00026 
00027 int
00028 LogColourScale::getWidth(View *,
00029                             QPainter &paint)
00030 {
00031     return paint.fontMetrics().width("-000.00") + 15;
00032 }
00033 
00034 void
00035 LogColourScale::paintVertical(View *v,
00036                               const ColourScaleLayer *layer,
00037                               QPainter &paint,
00038                               int /* x0 */,
00039                               float minlog,
00040                               float maxlog)
00041 {
00042     int h = v->height();
00043 
00044     int n = 10;
00045 
00046     float val = minlog;
00047     float inc = (maxlog - val) / n;
00048 
00049     char buffer[40];
00050 
00051     int boxx = 5, boxy = 5;
00052     if (layer->getScaleUnits() != "") {
00053         boxy += paint.fontMetrics().height();
00054     }
00055     int boxw = 10, boxh = h - boxy - 5;
00056 
00057     int tx = 5 + boxx + boxw;
00058     paint.drawRect(boxx, boxy, boxw, boxh);
00059 
00060     paint.save();
00061     for (int y = 0; y < boxh; ++y) {
00062         float val = ((boxh - y) * (maxlog - minlog)) / boxh + minlog;
00063         paint.setPen(layer->getColourForValue(v, LogRange::unmap(val)));
00064         paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
00065     }
00066     paint.restore();
00067 
00068     int dp = 0;
00069     if (inc > 0) {
00070         int prec = trunc(log10f(inc));
00071         prec -= 1;
00072         if (prec < 0) dp = -prec;
00073     }
00074 
00075     for (int i = 0; i < n; ++i) {
00076 
00077         int y, ty;
00078 
00079         y = boxy + int(boxh - ((val - minlog) * boxh) / (maxlog - minlog));
00080 
00081         ty = y - paint.fontMetrics().height() +
00082             paint.fontMetrics().ascent() + 2;
00083 
00084         double dv = LogRange::unmap(val);
00085         int digits = trunc(log10f(dv));
00086         int sf = dp + (digits > 0 ? digits : 0);
00087         if (sf < 2) sf = 2;
00088         sprintf(buffer, "%.*g", sf, dv);
00089 
00090         QString label = QString(buffer);
00091 
00092         paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
00093         paint.drawText(tx, ty, label);
00094 
00095         val += inc;
00096     }
00097 }