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