annotate layer/LinearColourScale.cpp @ 699:1a1448f7beb2

Pull out colour scale drawing as well
author Chris Cannam
date Wed, 04 Dec 2013 13:11:23 +0000
parents
children 1d526ba11a24
rev   line source
Chris@699 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@699 2
Chris@699 3 /*
Chris@699 4 Sonic Visualiser
Chris@699 5 An audio file viewer and annotation editor.
Chris@699 6 Centre for Digital Music, Queen Mary, University of London.
Chris@699 7 This file copyright 2006-2013 Chris Cannam and QMUL.
Chris@699 8
Chris@699 9 This program is free software; you can redistribute it and/or
Chris@699 10 modify it under the terms of the GNU General Public License as
Chris@699 11 published by the Free Software Foundation; either version 2 of the
Chris@699 12 License, or (at your option) any later version. See the file
Chris@699 13 COPYING included with this distribution for more information.
Chris@699 14 */
Chris@699 15
Chris@699 16 #include "LinearColourScale.h"
Chris@699 17 #include "ColourScaleLayer.h"
Chris@699 18
Chris@699 19 #include <QPainter>
Chris@699 20
Chris@699 21 #include <cmath>
Chris@699 22
Chris@699 23 #include "view/View.h"
Chris@699 24
Chris@699 25 int
Chris@699 26 LinearColourScale::getWidth(View *v,
Chris@699 27 QPainter &paint)
Chris@699 28 {
Chris@699 29 return paint.fontMetrics().width("-000.00") + 15;
Chris@699 30 }
Chris@699 31
Chris@699 32 void
Chris@699 33 LinearColourScale::paintVertical(View *v,
Chris@699 34 const ColourScaleLayer *layer,
Chris@699 35 QPainter &paint,
Chris@699 36 int x0,
Chris@699 37 float min,
Chris@699 38 float max)
Chris@699 39 {
Chris@699 40 int h = v->height();
Chris@699 41
Chris@699 42 int n = 10;
Chris@699 43
Chris@699 44 float val = min;
Chris@699 45 float inc = (max - val) / n;
Chris@699 46
Chris@699 47 char buffer[40];
Chris@699 48
Chris@699 49 int w = getWidth(v, paint) + x0;
Chris@699 50
Chris@699 51 int boxx = 5, boxy = 5;
Chris@699 52 if (layer->getScaleUnits() != "") {
Chris@699 53 boxy += paint.fontMetrics().height();
Chris@699 54 }
Chris@699 55 int boxw = 10, boxh = h - boxy - 5;
Chris@699 56
Chris@699 57 int tx = 5 + boxx + boxw;
Chris@699 58 paint.drawRect(boxx, boxy, boxw, boxh);
Chris@699 59
Chris@699 60 paint.save();
Chris@699 61 for (int y = 0; y < boxh; ++y) {
Chris@699 62 float val = ((boxh - y) * (max - min)) / boxh + min;
Chris@699 63 paint.setPen(layer->getColourForValue(v, val));
Chris@699 64 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
Chris@699 65 }
Chris@699 66 paint.restore();
Chris@699 67
Chris@699 68 float round = 1.f;
Chris@699 69 int dp = 0;
Chris@699 70 if (inc > 0) {
Chris@699 71 int prec = trunc(log10f(inc));
Chris@699 72 prec -= 1;
Chris@699 73 if (prec < 0) dp = -prec;
Chris@699 74 round = powf(10.f, prec);
Chris@699 75 #ifdef DEBUG_TIME_VALUE_LAYER
Chris@699 76 cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl;
Chris@699 77 #endif
Chris@699 78 }
Chris@699 79
Chris@699 80 for (int i = 0; i < n; ++i) {
Chris@699 81
Chris@699 82 int y, ty;
Chris@699 83
Chris@699 84 y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
Chris@699 85
Chris@699 86 ty = y - paint.fontMetrics().height() +
Chris@699 87 paint.fontMetrics().ascent() + 2;
Chris@699 88
Chris@699 89 sprintf(buffer, "%.*f", dp, val);
Chris@699 90 QString label = QString(buffer);
Chris@699 91
Chris@699 92 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
Chris@699 93 paint.drawText(tx, ty, label);
Chris@699 94
Chris@699 95 val += inc;
Chris@699 96 }
Chris@699 97 }