Chris@699: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@699: 
Chris@699: /*
Chris@699:     Sonic Visualiser
Chris@699:     An audio file viewer and annotation editor.
Chris@699:     Centre for Digital Music, Queen Mary, University of London.
Chris@699:     This file copyright 2006-2013 Chris Cannam and QMUL.
Chris@699:     
Chris@699:     This program is free software; you can redistribute it and/or
Chris@699:     modify it under the terms of the GNU General Public License as
Chris@699:     published by the Free Software Foundation; either version 2 of the
Chris@699:     License, or (at your option) any later version.  See the file
Chris@699:     COPYING included with this distribution for more information.
Chris@699: */
Chris@699: 
Chris@699: #include "LinearColourScale.h"
Chris@699: #include "ColourScaleLayer.h"
Chris@699: 
Chris@699: #include <QPainter>
Chris@699: 
Chris@699: #include <cmath>
Chris@699: 
Chris@1077: #include "LayerGeometryProvider.h"
Chris@699: 
Chris@699: int
Chris@918: LinearColourScale::getWidth(LayerGeometryProvider *,
Chris@699: 			    QPainter &paint)
Chris@699: {
Chris@699:     return paint.fontMetrics().width("-000.00") + 15;
Chris@699: }
Chris@699: 
Chris@699: void
Chris@918: LinearColourScale::paintVertical(LayerGeometryProvider *v,
Chris@699: 				 const ColourScaleLayer *layer,
Chris@699: 				 QPainter &paint,
Chris@805: 				 int /* x0 */,
Chris@905: 				 double min,
Chris@905: 				 double max)
Chris@699: {
Chris@918:     int h = v->getPaintHeight();
Chris@699: 
Chris@699:     int n = 10;
Chris@699: 
Chris@905:     double val = min;
Chris@905:     double inc = (max - val) / n;
Chris@864:     
Chris@864:     const int buflen = 40;
Chris@864:     char buffer[buflen];
Chris@699: 
Chris@699:     int boxx = 5, boxy = 5;
Chris@699:     if (layer->getScaleUnits() != "") {
Chris@699:         boxy += paint.fontMetrics().height();
Chris@699:     }
Chris@699:     int boxw = 10, boxh = h - boxy - 5;
Chris@699: 
Chris@699:     int tx = 5 + boxx + boxw;
Chris@699:     paint.drawRect(boxx, boxy, boxw, boxh);
Chris@699: 
Chris@699:     paint.save();
Chris@699:     for (int y = 0; y < boxh; ++y) {
Chris@905: 	double val = ((boxh - y) * (max - min)) / boxh + min;
Chris@699: 	paint.setPen(layer->getColourForValue(v, val));
Chris@699: 	paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
Chris@699:     }
Chris@699:     paint.restore();
Chris@699: 
Chris@905: //    double round = 1.f;
Chris@699:     int dp = 0;
Chris@699:     if (inc > 0) {
Chris@905:         int prec = int(trunc(log10(inc)));
Chris@699:         prec -= 1;
Chris@699:         if (prec < 0) dp = -prec;
Chris@805: //        round = powf(10.f, prec);
Chris@805: //#ifdef DEBUG_TIME_VALUE_LAYER
Chris@805: //        cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl;
Chris@805: //#endif
Chris@699:     }
Chris@699: 
Chris@699:     for (int i = 0; i < n; ++i) {
Chris@699: 
Chris@699: 	int y, ty;
Chris@699: 
Chris@699: 	y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
Chris@699: 
Chris@699: 	ty = y - paint.fontMetrics().height() +
Chris@699: 	    paint.fontMetrics().ascent() + 2;
Chris@699: 
Chris@864: 	snprintf(buffer, buflen, "%.*f", dp, val);
Chris@699: 	QString label = QString(buffer);
Chris@699: 
Chris@699: 	paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
Chris@699: 	paint.drawText(tx, ty, label);
Chris@699: 
Chris@699: 	val += inc;
Chris@699:     }
Chris@699: }