annotate layer/ColourDatabase.cpp @ 391:4f0f273c8f82

* Provide a proper implementation of SpectrogramLayer::invalidatePixmapCaches(size_t, size_t) -- if the region is only part of the cache's current valid area, crop the valid area instead of resetting it completely. This makes a big difference when first rendering a spectrogram that is zoomed out a long way when the underlying calculation has not yet completed -- as is a common case in Vect for example.
author Chris Cannam
date Wed, 21 May 2008 11:09:15 +0000
parents e1a9e478b7f2
children e0f08e108064
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@376 37 return 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@376 43 if (c < 0 || size_t(c) >= m_colours.size()) 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@376 50 if (c < 0 || size_t(c) >= m_colours.size()) 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@376 57 for (ColourList::const_iterator i = m_colours.begin();
Chris@376 58 i != m_colours.end(); ++i) {
Chris@376 59 if (i->name == name) return i->colour;
Chris@376 60 }
Chris@376 61
Chris@376 62 return Qt::black;
Chris@376 63 }
Chris@376 64
Chris@376 65 int
Chris@376 66 ColourDatabase::getColourIndex(QString name) const
Chris@376 67 {
Chris@376 68 int index = 0;
Chris@376 69 for (ColourList::const_iterator i = m_colours.begin();
Chris@376 70 i != m_colours.end(); ++i) {
Chris@376 71 if (i->name == name) return index;
Chris@376 72 ++index;
Chris@376 73 }
Chris@376 74
Chris@376 75 return -1;
Chris@376 76 }
Chris@376 77
Chris@376 78 int
Chris@376 79 ColourDatabase::getColourIndex(QColor c) const
Chris@376 80 {
Chris@376 81 int index = 0;
Chris@376 82 for (ColourList::const_iterator i = m_colours.begin();
Chris@376 83 i != m_colours.end(); ++i) {
Chris@376 84 if (i->colour == c) return index;
Chris@376 85 ++index;
Chris@376 86 }
Chris@376 87
Chris@376 88 return -1;
Chris@376 89 }
Chris@376 90
Chris@376 91 bool
Chris@376 92 ColourDatabase::useDarkBackground(int c) const
Chris@376 93 {
Chris@376 94 if (c < 0 || size_t(c) >= m_colours.size()) return false;
Chris@376 95 return m_colours[c].darkbg;
Chris@376 96 }
Chris@376 97
Chris@376 98 void
Chris@376 99 ColourDatabase::setUseDarkBackground(int c, bool dark)
Chris@376 100 {
Chris@376 101 if (c < 0 || size_t(c) >= m_colours.size()) return;
Chris@376 102 if (m_colours[c].darkbg != dark) {
Chris@376 103 m_colours[c].darkbg = dark;
Chris@376 104 emit colourDatabaseChanged();
Chris@376 105 }
Chris@376 106 }
Chris@376 107
Chris@376 108 int
Chris@376 109 ColourDatabase::addColour(QColor c, QString name)
Chris@376 110 {
Chris@376 111 int index = 0;
Chris@376 112 for (ColourList::iterator i = m_colours.begin();
Chris@376 113 i != m_colours.end(); ++i) {
Chris@376 114 if (i->name == name) {
Chris@376 115 i->colour = c;
Chris@376 116 return index;
Chris@376 117 }
Chris@376 118 ++index;
Chris@376 119 }
Chris@376 120
Chris@376 121 ColourRec rec;
Chris@376 122 rec.colour = c;
Chris@376 123 rec.name = name;
Chris@376 124 rec.darkbg = false;
Chris@376 125 m_colours.push_back(rec);
Chris@376 126 emit colourDatabaseChanged();
Chris@376 127 return index;
Chris@376 128 }
Chris@376 129
Chris@376 130 void
Chris@376 131 ColourDatabase::removeColour(QString name)
Chris@376 132 {
Chris@376 133 for (ColourList::iterator i = m_colours.begin();
Chris@376 134 i != m_colours.end(); ++i) {
Chris@376 135 if (i->name == name) {
Chris@376 136 m_colours.erase(i);
Chris@376 137 return;
Chris@376 138 }
Chris@376 139 }
Chris@376 140 }
Chris@376 141
Chris@376 142 void
Chris@376 143 ColourDatabase::getStringValues(int index,
Chris@376 144 QString &colourName,
Chris@376 145 QString &colourSpec,
Chris@376 146 QString &darkbg) const
Chris@376 147 {
Chris@376 148 colourName = "";
Chris@376 149 colourSpec = "";
Chris@376 150 if (index < 0 || size_t(index) >= m_colours.size()) return;
Chris@376 151
Chris@376 152 colourName = getColourName(index);
Chris@376 153 QColor c = getColour(index);
Chris@376 154 colourSpec = XmlExportable::encodeColour(c.red(), c.green(), c.blue());
Chris@376 155 darkbg = useDarkBackground(index) ? "true" : "false";
Chris@376 156 }
Chris@376 157
Chris@376 158 int
Chris@376 159 ColourDatabase::putStringValues(QString colourName,
Chris@376 160 QString colourSpec,
Chris@376 161 QString darkbg)
Chris@376 162 {
Chris@376 163 int index = -1;
Chris@376 164 if (colourSpec != "") {
Chris@376 165 QColor colour(colourSpec);
Chris@376 166 index = getColourIndex(colour);
Chris@376 167 if (index < 0) {
Chris@376 168 index = addColour(colour,
Chris@376 169 colourName == "" ? colourSpec : colourName);
Chris@376 170 }
Chris@376 171 } else if (colourName != "") {
Chris@376 172 index = getColourIndex(colourName);
Chris@376 173 }
Chris@376 174 if (index >= 0) {
Chris@376 175 setUseDarkBackground(index, darkbg == "true");
Chris@376 176 }
Chris@376 177 return index;
Chris@376 178 }
Chris@376 179
Chris@376 180 void
Chris@376 181 ColourDatabase::getColourPropertyRange(int *min, int *max) const
Chris@376 182 {
Chris@376 183 ColourDatabase *db = getInstance();
Chris@376 184 if (min) *min = 0;
Chris@376 185 if (max) {
Chris@376 186 *max = 0;
Chris@376 187 if (db->getColourCount() > 0) *max = db->getColourCount()-1;
Chris@376 188 }
Chris@376 189 }
Chris@376 190
Chris@376 191 QPixmap
Chris@376 192 ColourDatabase::getExamplePixmap(int index, QSize size) const
Chris@376 193 {
Chris@376 194 QPixmap pmap(size);
Chris@376 195 pmap.fill(useDarkBackground(index) ? Qt::black : Qt::white);
Chris@376 196 QPainter paint(&pmap);
Chris@376 197 QColor colour(getColour(index));
Chris@376 198 paint.setPen(colour);
Chris@376 199 paint.setBrush(colour);
Chris@376 200 int margin = 2;
Chris@376 201 if (size.width() < 4 || size.height() < 4) margin = 0;
Chris@376 202 else if (size.width() < 8 || size.height() < 8) margin = 1;
Chris@376 203 paint.drawRect(margin, margin,
Chris@376 204 size.width() - margin*2 - 1, size.height() - margin*2 - 1);
Chris@376 205 return pmap;
Chris@376 206 }
Chris@376 207