annotate layer/ColourDatabase.cpp @ 1367:f5566f7271fe waverevision

Rework waveform renderer to use smooth paths, aiming to get near-pixel-identical results when zoomed out far enough for a single path not to be relevant
author Chris Cannam
date Wed, 31 Oct 2018 15:06:32 +0000
parents a34a2a25907c
children ad86aa712d11
rev   line source
Chris@376 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@376 2
Chris@376 3 /*
Chris@376 4 Sonic Visualiser
Chris@376 5 An audio file viewer and annotation editor.
Chris@376 6 Centre for Digital Music, Queen Mary, University of London.
Chris@376 7 This file copyright 2007 QMUL.
Chris@376 8
Chris@376 9 This program is free software; you can redistribute it and/or
Chris@376 10 modify it under the terms of the GNU General Public License as
Chris@376 11 published by the Free Software Foundation; either version 2 of the
Chris@376 12 License, or (at your option) any later version. See the file
Chris@376 13 COPYING included with this distribution for more information.
Chris@376 14 */
Chris@376 15
Chris@376 16 #include "ColourDatabase.h"
Chris@376 17 #include "base/XmlExportable.h"
Chris@376 18
Chris@376 19 #include <QPainter>
Chris@376 20
Chris@376 21 ColourDatabase
Chris@376 22 ColourDatabase::m_instance;
Chris@376 23
Chris@376 24 ColourDatabase *
Chris@376 25 ColourDatabase::getInstance()
Chris@376 26 {
Chris@376 27 return &m_instance;
Chris@376 28 }
Chris@376 29
Chris@376 30 ColourDatabase::ColourDatabase()
Chris@376 31 {
Chris@376 32 }
Chris@376 33
Chris@376 34 int
Chris@376 35 ColourDatabase::getColourCount() const
Chris@376 36 {
Chris@904 37 return int(m_colours.size());
Chris@376 38 }
Chris@376 39
Chris@376 40 QString
Chris@376 41 ColourDatabase::getColourName(int c) const
Chris@376 42 {
Chris@904 43 if (!in_range_for(m_colours, c)) return "";
Chris@376 44 return m_colours[c].name;
Chris@376 45 }
Chris@376 46
Chris@376 47 QColor
Chris@376 48 ColourDatabase::getColour(int c) const
Chris@376 49 {
Chris@904 50 if (!in_range_for(m_colours, c)) return Qt::black;
Chris@376 51 return m_colours[c].colour;
Chris@376 52 }
Chris@376 53
Chris@376 54 QColor
Chris@376 55 ColourDatabase::getColour(QString name) const
Chris@376 56 {
Chris@904 57 for (auto &c: m_colours) {
Chris@904 58 if (c.name == name) return c.colour;
Chris@376 59 }
Chris@376 60
Chris@376 61 return Qt::black;
Chris@376 62 }
Chris@376 63
Chris@376 64 int
Chris@376 65 ColourDatabase::getColourIndex(QString name) const
Chris@376 66 {
Chris@376 67 int index = 0;
Chris@904 68 for (auto &c: m_colours) {
Chris@904 69 if (c.name == name) return index;
Chris@376 70 ++index;
Chris@376 71 }
Chris@376 72
Chris@376 73 return -1;
Chris@376 74 }
Chris@376 75
Chris@376 76 int
Chris@904 77 ColourDatabase::getColourIndex(QColor col) const
Chris@376 78 {
Chris@376 79 int index = 0;
Chris@904 80 for (auto &c: m_colours) {
Chris@904 81 if (c.colour == col) return index;
Chris@376 82 ++index;
Chris@376 83 }
Chris@376 84
Chris@376 85 return -1;
Chris@376 86 }
Chris@376 87
Chris@1367 88 QColor
Chris@1367 89 ColourDatabase::getContrastingColour(int c) const
Chris@1367 90 {
Chris@1367 91 QColor col = getColour(c);
Chris@1367 92 if (col.red() > col.blue()) {
Chris@1367 93 if (col.green() > col.blue()) {
Chris@1367 94 return Qt::blue;
Chris@1367 95 } else {
Chris@1367 96 return Qt::yellow;
Chris@1367 97 }
Chris@1367 98 } else {
Chris@1367 99 if (col.green() > col.blue()) {
Chris@1367 100 return Qt::yellow;
Chris@1367 101 } else {
Chris@1367 102 return Qt::red;
Chris@1367 103 }
Chris@1367 104 }
Chris@1367 105 return Qt::red;
Chris@1367 106 }
Chris@1367 107
Chris@376 108 bool
Chris@376 109 ColourDatabase::useDarkBackground(int c) const
Chris@376 110 {
Chris@904 111 if (!in_range_for(m_colours, c)) return false;
Chris@376 112 return m_colours[c].darkbg;
Chris@376 113 }
Chris@376 114
Chris@376 115 void
Chris@376 116 ColourDatabase::setUseDarkBackground(int c, bool dark)
Chris@376 117 {
Chris@904 118 if (!in_range_for(m_colours, c)) return;
Chris@376 119 if (m_colours[c].darkbg != dark) {
Chris@376 120 m_colours[c].darkbg = dark;
Chris@376 121 emit colourDatabaseChanged();
Chris@376 122 }
Chris@376 123 }
Chris@376 124
Chris@376 125 int
Chris@376 126 ColourDatabase::addColour(QColor c, QString name)
Chris@376 127 {
Chris@376 128 int index = 0;
Chris@904 129
Chris@376 130 for (ColourList::iterator i = m_colours.begin();
Chris@376 131 i != m_colours.end(); ++i) {
Chris@376 132 if (i->name == name) {
Chris@376 133 i->colour = c;
Chris@376 134 return index;
Chris@376 135 }
Chris@376 136 ++index;
Chris@376 137 }
Chris@376 138
Chris@376 139 ColourRec rec;
Chris@376 140 rec.colour = c;
Chris@376 141 rec.name = name;
Chris@376 142 rec.darkbg = false;
Chris@376 143 m_colours.push_back(rec);
Chris@376 144 emit colourDatabaseChanged();
Chris@376 145 return index;
Chris@376 146 }
Chris@376 147
Chris@376 148 void
Chris@376 149 ColourDatabase::removeColour(QString name)
Chris@376 150 {
Chris@376 151 for (ColourList::iterator i = m_colours.begin();
Chris@376 152 i != m_colours.end(); ++i) {
Chris@376 153 if (i->name == name) {
Chris@376 154 m_colours.erase(i);
Chris@376 155 return;
Chris@376 156 }
Chris@376 157 }
Chris@376 158 }
Chris@376 159
Chris@376 160 void
Chris@376 161 ColourDatabase::getStringValues(int index,
Chris@376 162 QString &colourName,
Chris@376 163 QString &colourSpec,
Chris@376 164 QString &darkbg) const
Chris@376 165 {
Chris@376 166 colourName = "";
Chris@376 167 colourSpec = "";
Chris@904 168 if (!in_range_for(m_colours, index)) return;
Chris@376 169
Chris@376 170 colourName = getColourName(index);
Chris@376 171 QColor c = getColour(index);
Chris@376 172 colourSpec = XmlExportable::encodeColour(c.red(), c.green(), c.blue());
Chris@376 173 darkbg = useDarkBackground(index) ? "true" : "false";
Chris@376 174 }
Chris@376 175
Chris@376 176 int
Chris@376 177 ColourDatabase::putStringValues(QString colourName,
Chris@376 178 QString colourSpec,
Chris@376 179 QString darkbg)
Chris@376 180 {
Chris@376 181 int index = -1;
Chris@376 182 if (colourSpec != "") {
Chris@1266 183 QColor colour(colourSpec);
Chris@376 184 index = getColourIndex(colour);
Chris@376 185 if (index < 0) {
Chris@376 186 index = addColour(colour,
Chris@376 187 colourName == "" ? colourSpec : colourName);
Chris@376 188 }
Chris@376 189 } else if (colourName != "") {
Chris@376 190 index = getColourIndex(colourName);
Chris@376 191 }
Chris@376 192 if (index >= 0) {
Chris@376 193 setUseDarkBackground(index, darkbg == "true");
Chris@376 194 }
Chris@376 195 return index;
Chris@376 196 }
Chris@376 197
Chris@376 198 void
Chris@376 199 ColourDatabase::getColourPropertyRange(int *min, int *max) const
Chris@376 200 {
Chris@376 201 ColourDatabase *db = getInstance();
Chris@376 202 if (min) *min = 0;
Chris@376 203 if (max) {
Chris@376 204 *max = 0;
Chris@376 205 if (db->getColourCount() > 0) *max = db->getColourCount()-1;
Chris@376 206 }
Chris@376 207 }
Chris@376 208
Chris@376 209 QPixmap
Chris@376 210 ColourDatabase::getExamplePixmap(int index, QSize size) const
Chris@376 211 {
Chris@376 212 QPixmap pmap(size);
Chris@376 213 pmap.fill(useDarkBackground(index) ? Qt::black : Qt::white);
Chris@376 214 QPainter paint(&pmap);
Chris@376 215 QColor colour(getColour(index));
Chris@376 216 paint.setPen(colour);
Chris@376 217 paint.setBrush(colour);
Chris@376 218 int margin = 2;
Chris@376 219 if (size.width() < 4 || size.height() < 4) margin = 0;
Chris@376 220 else if (size.width() < 8 || size.height() < 8) margin = 1;
Chris@376 221 paint.drawRect(margin, margin,
Chris@376 222 size.width() - margin*2 - 1, size.height() - margin*2 - 1);
Chris@376 223 return pmap;
Chris@376 224 }
Chris@376 225