annotate base/ColourDatabase.cpp @ 384:6f6ab834449d spectrogram-cache-rejig

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