# HG changeset patch # User Chris Cannam # Date 1386162683 0 # Node ID 1a1448f7beb2267d95fb745770eb8049395b9d3a # Parent ad7623c393965fb049be70d7800665ad2c24b0d6 Pull out colour scale drawing as well diff -r ad7623c39396 -r 1a1448f7beb2 layer/ColourScaleLayer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/layer/ColourScaleLayer.h Wed Dec 04 13:11:23 2013 +0000 @@ -0,0 +1,30 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2006-2013 Chris Cannam and QMUL. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#ifndef COLOUR_SCALE_LAYER_H +#define COLOUR_SCALE_LAYER_H + +#include +#include + +class ColourScaleLayer +{ +public: + virtual QString getScaleUnits() const = 0; + virtual QColor getColourForValue(View *v, float value) const = 0; +}; + +#endif + diff -r ad7623c39396 -r 1a1448f7beb2 layer/LinearColourScale.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/layer/LinearColourScale.cpp Wed Dec 04 13:11:23 2013 +0000 @@ -0,0 +1,97 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2006-2013 Chris Cannam and QMUL. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#include "LinearColourScale.h" +#include "ColourScaleLayer.h" + +#include + +#include + +#include "view/View.h" + +int +LinearColourScale::getWidth(View *v, + QPainter &paint) +{ + return paint.fontMetrics().width("-000.00") + 15; +} + +void +LinearColourScale::paintVertical(View *v, + const ColourScaleLayer *layer, + QPainter &paint, + int x0, + float min, + float max) +{ + int h = v->height(); + + int n = 10; + + float val = min; + float inc = (max - val) / n; + + char buffer[40]; + + int w = getWidth(v, paint) + x0; + + int boxx = 5, boxy = 5; + if (layer->getScaleUnits() != "") { + boxy += paint.fontMetrics().height(); + } + int boxw = 10, boxh = h - boxy - 5; + + int tx = 5 + boxx + boxw; + paint.drawRect(boxx, boxy, boxw, boxh); + + paint.save(); + for (int y = 0; y < boxh; ++y) { + float val = ((boxh - y) * (max - min)) / boxh + min; + paint.setPen(layer->getColourForValue(v, val)); + paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1); + } + paint.restore(); + + float round = 1.f; + int dp = 0; + if (inc > 0) { + int prec = trunc(log10f(inc)); + prec -= 1; + if (prec < 0) dp = -prec; + round = powf(10.f, prec); +#ifdef DEBUG_TIME_VALUE_LAYER + cerr << "inc = " << inc << ", round = " << round << ", dp = " << dp << endl; +#endif + } + + for (int i = 0; i < n; ++i) { + + int y, ty; + + y = boxy + int(boxh - ((val - min) * boxh) / (max - min)); + + ty = y - paint.fontMetrics().height() + + paint.fontMetrics().ascent() + 2; + + sprintf(buffer, "%.*f", dp, val); + QString label = QString(buffer); + + paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y); + paint.drawText(tx, ty, label); + + val += inc; + } +} diff -r ad7623c39396 -r 1a1448f7beb2 layer/LinearColourScale.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/layer/LinearColourScale.h Wed Dec 04 13:11:23 2013 +0000 @@ -0,0 +1,36 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2006-2013 Chris Cannam and QMUL. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#ifndef LINEAR_COLOUR_SCALE_H +#define LINEAR_COLOUR_SCALE_H + +#include + +class QPainter; +class View; +class ColourScaleLayer; + +class LinearColourScale +{ +public: + int getWidth(View *v, QPainter &paint); + + void paintVertical + (View *v, const ColourScaleLayer *layer, QPainter &paint, int x0, + float minf, float maxf); +}; + +#endif + diff -r ad7623c39396 -r 1a1448f7beb2 layer/LinearNumericalScale.cpp --- a/layer/LinearNumericalScale.cpp Wed Dec 04 11:35:08 2013 +0000 +++ b/layer/LinearNumericalScale.cpp Wed Dec 04 13:11:23 2013 +0000 @@ -26,7 +26,7 @@ LinearNumericalScale::getWidth(View *v, QPainter &paint) { - return paint.fontMetrics().width("-000.000"); + return paint.fontMetrics().width("-000.00") + 10; } void @@ -95,7 +95,7 @@ paint.drawLine(w - 5, y, w, y); if (drawText) { - paint.drawText(w - paint.fontMetrics().width(label) - 13, + paint.drawText(w - paint.fontMetrics().width(label) - 6, ty, label); } diff -r ad7623c39396 -r 1a1448f7beb2 layer/LogColourScale.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/layer/LogColourScale.cpp Wed Dec 04 13:11:23 2013 +0000 @@ -0,0 +1,99 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2006-2013 Chris Cannam and QMUL. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#include "LogColourScale.h" +#include "ColourScaleLayer.h" + +#include "base/LogRange.h" + +#include + +#include + +#include "view/View.h" + +int +LogColourScale::getWidth(View *v, + QPainter &paint) +{ + return paint.fontMetrics().width("-000.00") + 15; +} + +void +LogColourScale::paintVertical(View *v, + const ColourScaleLayer *layer, + QPainter &paint, + int x0, + float minlog, + float maxlog) +{ + int h = v->height(); + + int n = 10; + + float val = minlog; + float inc = (maxlog - val) / n; + + char buffer[40]; + + int w = getWidth(v, paint) + x0; + + int boxx = 5, boxy = 5; + if (layer->getScaleUnits() != "") { + boxy += paint.fontMetrics().height(); + } + int boxw = 10, boxh = h - boxy - 5; + + int tx = 5 + boxx + boxw; + paint.drawRect(boxx, boxy, boxw, boxh); + + paint.save(); + for (int y = 0; y < boxh; ++y) { + float val = ((boxh - y) * (maxlog - minlog)) / boxh + minlog; + paint.setPen(layer->getColourForValue(v, LogRange::unmap(val))); + paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1); + } + paint.restore(); + + int dp = 0; + if (inc > 0) { + int prec = trunc(log10f(inc)); + prec -= 1; + if (prec < 0) dp = -prec; + } + + for (int i = 0; i < n; ++i) { + + int y, ty; + + y = boxy + int(boxh - ((val - minlog) * boxh) / (maxlog - minlog)); + + ty = y - paint.fontMetrics().height() + + paint.fontMetrics().ascent() + 2; + + double dv = LogRange::unmap(val); + int digits = trunc(log10f(dv)); + int sf = dp + (digits > 0 ? digits : 0); + if (sf < 2) sf = 2; + sprintf(buffer, "%.*g", sf, dv); + + QString label = QString(buffer); + + paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y); + paint.drawText(tx, ty, label); + + val += inc; + } +} diff -r ad7623c39396 -r 1a1448f7beb2 layer/LogColourScale.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/layer/LogColourScale.h Wed Dec 04 13:11:23 2013 +0000 @@ -0,0 +1,36 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2006-2013 Chris Cannam and QMUL. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#ifndef LOG_COLOUR_SCALE_H +#define LOG_COLOUR_SCALE_H + +#include + +class QPainter; +class View; +class ColourScaleLayer; + +class LogColourScale +{ +public: + int getWidth(View *v, QPainter &paint); + + void paintVertical + (View *v, const ColourScaleLayer *layer, QPainter &paint, int x0, + float minf, float maxf); +}; + +#endif + diff -r ad7623c39396 -r 1a1448f7beb2 layer/LogNumericalScale.cpp --- a/layer/LogNumericalScale.cpp Wed Dec 04 11:35:08 2013 +0000 +++ b/layer/LogNumericalScale.cpp Wed Dec 04 13:11:23 2013 +0000 @@ -30,7 +30,7 @@ LogNumericalScale::getWidth(View *, QPainter &paint) { - return paint.fontMetrics().width("-000.00"); + return paint.fontMetrics().width("-000.00") + 10; } void diff -r ad7623c39396 -r 1a1448f7beb2 layer/TimeValueLayer.cpp --- a/layer/TimeValueLayer.cpp Wed Dec 04 11:35:08 2013 +0000 +++ b/layer/TimeValueLayer.cpp Wed Dec 04 13:11:23 2013 +0000 @@ -33,6 +33,8 @@ #include "PianoScale.h" #include "LinearNumericalScale.h" #include "LogNumericalScale.h" +#include "LinearColourScale.h" +#include "LogColourScale.h" #include #include @@ -1208,14 +1210,19 @@ TimeValueLayer::getVerticalScaleWidth(View *v, bool, QPainter &paint) const { if (!m_model || shouldAutoAlign()) return 0; - int w = 0; - if (m_verticalScale == LogScale) { - w = LogNumericalScale().getWidth(v, paint); + if (m_plotStyle == PlotSegmentation) { + if (m_verticalScale == LogScale) { + return LogColourScale().getWidth(v, paint); + } else { + return LinearColourScale().getWidth(v, paint); + } } else { - w = LinearNumericalScale().getWidth(v, paint); + if (m_verticalScale == LogScale) { + return LogNumericalScale().getWidth(v, paint) + 10; // for piano + } else { + return LinearNumericalScale().getWidth(v, paint); + } } - if (m_plotStyle == PlotSegmentation) return w + 20; - else return w + 10; } void @@ -1351,19 +1358,30 @@ val += inc; } */ + QString unit; float min, max; bool logarithmic; - getScaleExtents(v, min, max, logarithmic); int w = getVerticalScaleWidth(v, false, paint); int h = v->height(); if (m_plotStyle == PlotSegmentation) { - //!!! todo! + getValueExtents(min, max, logarithmic, unit); + + if (logarithmic) { + + LogRange::mapRange(min, max); + LogColourScale().paintVertical(v, this, paint, 0, min, max); + + } else { + LinearColourScale().paintVertical(v, this, paint, 0, min, max); + } } else { + getScaleExtents(v, min, max, logarithmic); + if (logarithmic) { LogNumericalScale().paintVertical(v, this, paint, 0, min, max); } else { diff -r ad7623c39396 -r 1a1448f7beb2 layer/TimeValueLayer.h --- a/layer/TimeValueLayer.h Wed Dec 04 11:35:08 2013 +0000 +++ b/layer/TimeValueLayer.h Wed Dec 04 13:11:23 2013 +0000 @@ -18,6 +18,8 @@ #include "SingleColourLayer.h" #include "VerticalScaleLayer.h" +#include "ColourScaleLayer.h" + #include "data/model/SparseTimeValueModel.h" #include @@ -26,7 +28,9 @@ class View; class QPainter; -class TimeValueLayer : public SingleColourLayer, public VerticalScaleLayer +class TimeValueLayer : public SingleColourLayer, + public VerticalScaleLayer, + public ColourScaleLayer { Q_OBJECT @@ -150,14 +154,14 @@ } } - /// VerticalScaleLayer methods + /// VerticalScaleLayer and ColourScaleLayer methods virtual void getScaleExtents(View *, float &min, float &max, bool &log) const; virtual int getYForValue(View *, float value) const; virtual float getValueForY(View *, int y) const; virtual QString getScaleUnits() const; + virtual QColor getColourForValue(View *v, float value) const; protected: - QColor getColourForValue(View *v, float value) const; bool shouldAutoAlign() const; SparseTimeValueModel::PointList getLocalPoints(View *v, int) const; diff -r ad7623c39396 -r 1a1448f7beb2 svgui.pro --- a/svgui.pro Wed Dec 04 11:35:08 2013 +0000 +++ b/svgui.pro Wed Dec 04 13:11:23 2013 +0000 @@ -30,13 +30,15 @@ HEADERS += layer/Colour3DPlotLayer.h \ layer/ColourDatabase.h \ layer/ColourMapper.h \ - layer/FlexiNoteLayer.h \ + layer/ColourScaleLayer.h \ layer/ImageLayer.h \ layer/ImageRegionFinder.h \ layer/Layer.h \ layer/LayerFactory.h \ layer/LinearNumericalScale.h \ layer/LogNumericalScale.h \ + layer/LinearColourScale.h \ + layer/LogColourScale.h \ layer/NoteLayer.h \ layer/PaintAssistant.h \ layer/PianoScale.h \ @@ -55,13 +57,14 @@ SOURCES += layer/Colour3DPlotLayer.cpp \ layer/ColourDatabase.cpp \ layer/ColourMapper.cpp \ - layer/FlexiNoteLayer.cpp \ layer/ImageLayer.cpp \ layer/ImageRegionFinder.cpp \ layer/Layer.cpp \ layer/LayerFactory.cpp \ layer/LinearNumericalScale.cpp \ layer/LogNumericalScale.cpp \ + layer/LinearColourScale.cpp \ + layer/LogColourScale.cpp \ layer/NoteLayer.cpp \ layer/PaintAssistant.cpp \ layer/PianoScale.cpp \