annotate layer/SingleColourLayer.cpp @ 293:15b8a4bfe855

* continue to pick "new" colours for coloured layers even when all colours have been used at least once, rather than sticking on the last one * some messing about with application palette settings * when replacing an audio file, retain the previous playback settings for any layers that depended on the old file * re-check plugin program setting when a parameter changes -- so a plugin can decide to reset the program if the parameters no longer match those for the current program * fix failure to update check-boxes for toggled plugin parameters when their parameters are changed by program changes
author Chris Cannam
date Thu, 09 Aug 2007 14:40:03 +0000
parents cd2492c5fe45
children 919740b20cc9
rev   line source
Chris@287 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@287 2
Chris@287 3 /*
Chris@287 4 Sonic Visualiser
Chris@287 5 An audio file viewer and annotation editor.
Chris@287 6 Centre for Digital Music, Queen Mary, University of London.
Chris@287 7 This file copyright 2007 QMUL.
Chris@287 8
Chris@287 9 This program is free software; you can redistribute it and/or
Chris@287 10 modify it under the terms of the GNU General Public License as
Chris@287 11 published by the Free Software Foundation; either version 2 of the
Chris@287 12 License, or (at your option) any later version. See the file
Chris@287 13 COPYING included with this distribution for more information.
Chris@287 14 */
Chris@287 15
Chris@287 16 #include "SingleColourLayer.h"
Chris@287 17 #include "base/ColourDatabase.h"
Chris@287 18 #include "view/View.h"
Chris@287 19
Chris@287 20 #include <iostream>
Chris@287 21
Chris@287 22 #include <QApplication>
Chris@287 23
Chris@293 24 SingleColourLayer::ColourRefCount
Chris@293 25 SingleColourLayer::m_colourRefCount;
Chris@287 26
Chris@287 27 SingleColourLayer::SingleColourLayer() :
Chris@287 28 m_colour(0)
Chris@287 29 {
Chris@287 30 setDefaultColourFor(0);
Chris@287 31 }
Chris@287 32
Chris@287 33 bool
Chris@287 34 SingleColourLayer::hasLightBackground() const
Chris@287 35 {
Chris@287 36 bool dark = ColourDatabase::getInstance()->useDarkBackground(m_colour);
Chris@287 37 return !dark;
Chris@287 38 }
Chris@287 39
Chris@287 40 Layer::PropertyList
Chris@287 41 SingleColourLayer::getProperties() const
Chris@287 42 {
Chris@287 43 PropertyList list = Layer::getProperties();
Chris@287 44 list.push_back("Colour");
Chris@287 45 return list;
Chris@287 46 }
Chris@287 47
Chris@287 48 QString
Chris@287 49 SingleColourLayer::getPropertyLabel(const PropertyName &name) const
Chris@287 50 {
Chris@287 51 if (name == "Colour") return tr("Colour");
Chris@287 52 return "";
Chris@287 53 }
Chris@287 54
Chris@287 55 Layer::PropertyType
Chris@287 56 SingleColourLayer::getPropertyType(const PropertyName &name) const
Chris@287 57 {
Chris@287 58 if (name == "Colour") return ColourProperty;
Chris@287 59 return InvalidProperty;
Chris@287 60 }
Chris@287 61
Chris@287 62 QString
Chris@287 63 SingleColourLayer::getPropertyGroupName(const PropertyName &) const
Chris@287 64 {
Chris@287 65 return QString();
Chris@287 66 }
Chris@287 67
Chris@287 68 int
Chris@287 69 SingleColourLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@287 70 int *min, int *max, int *deflt) const
Chris@287 71 {
Chris@287 72 int val = 0;
Chris@287 73
Chris@287 74 int garbage0, garbage1, garbage2;
Chris@287 75 if (!min) min = &garbage0;
Chris@287 76 if (!max) max = &garbage1;
Chris@287 77 if (!deflt) deflt = &garbage2;
Chris@287 78
Chris@287 79 if (name == "Colour") {
Chris@287 80
Chris@287 81 ColourDatabase::getInstance()->getColourPropertyRange(min, max);
Chris@287 82 *deflt = 0; //!!!
Chris@287 83
Chris@287 84 val = m_colour;
Chris@287 85
Chris@287 86 } else {
Chris@287 87 val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@287 88 }
Chris@287 89
Chris@287 90 return val;
Chris@287 91 }
Chris@287 92
Chris@287 93 QString
Chris@287 94 SingleColourLayer::getPropertyValueLabel(const PropertyName &name,
Chris@287 95 int value) const
Chris@287 96 {
Chris@287 97 if (name == "Colour") {
Chris@287 98 return Layer::getPropertyValueLabel(name, value);
Chris@287 99 }
Chris@287 100 return tr("<unknown>");
Chris@287 101 }
Chris@287 102
Chris@287 103 RangeMapper *
Chris@287 104 SingleColourLayer::getNewPropertyRangeMapper(const PropertyName &) const
Chris@287 105 {
Chris@287 106 return 0;
Chris@287 107 }
Chris@287 108
Chris@287 109 void
Chris@287 110 SingleColourLayer::setProperty(const PropertyName &name, int value)
Chris@287 111 {
Chris@287 112 if (name == "Colour") {
Chris@287 113 setBaseColour(value);
Chris@287 114 }
Chris@287 115 }
Chris@287 116
Chris@287 117 void
Chris@287 118 SingleColourLayer::setDefaultColourFor(View *v)
Chris@287 119 {
Chris@287 120 bool dark = false;
Chris@287 121 if (v) {
Chris@287 122 dark = !v->hasLightBackground();
Chris@287 123 } else {
Chris@287 124 QColor bg = QApplication::palette().color(QPalette::Window);
Chris@287 125 if (bg.red() + bg.green() + bg.blue() < 384) dark = true;
Chris@287 126 }
Chris@287 127
Chris@287 128 ColourDatabase *cdb = ColourDatabase::getInstance();
Chris@287 129
Chris@287 130 int hint = -1;
Chris@287 131 bool impose = false;
Chris@287 132 if (v) {
Chris@293 133 if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
Chris@293 134 m_colourRefCount[m_colour] > 0) {
Chris@293 135 m_colourRefCount[m_colour]--;
Chris@293 136 }
Chris@287 137 // We don't want to call this if !v because that probably
Chris@287 138 // means we're being called from the constructor, and this is
Chris@287 139 // a virtual function
Chris@287 140 hint = getDefaultColourHint(dark, impose);
Chris@293 141 // std::cerr << "hint = " << hint << ", impose = " << impose << std::endl;
Chris@293 142 } else {
Chris@293 143 // std::cerr << "(from ctor)" << std::endl;
Chris@287 144 }
Chris@287 145
Chris@287 146 if (hint >= 0 && impose) {
Chris@293 147 setBaseColour(hint);
Chris@287 148 return;
Chris@287 149 }
Chris@287 150
Chris@293 151 int bestCount = 0, bestColour = -1;
Chris@293 152
Chris@287 153 for (int i = 0; i < cdb->getColourCount(); ++i) {
Chris@293 154
Chris@287 155 int index = i;
Chris@287 156 if (hint > 0) index = (index + hint) % cdb->getColourCount();
Chris@287 157 if (cdb->useDarkBackground(index) != dark) continue;
Chris@293 158
Chris@293 159 int count = 0;
Chris@293 160 if (m_colourRefCount.find(index) != m_colourRefCount.end()) {
Chris@293 161 count = m_colourRefCount[index];
Chris@287 162 }
Chris@293 163
Chris@293 164 // std::cerr << "index = " << index << ", count = " << count;
Chris@293 165
Chris@293 166 if (bestColour < 0 || count < bestCount) {
Chris@293 167 bestColour = index;
Chris@293 168 bestCount = count;
Chris@293 169 // std::cerr << " *";
Chris@293 170 }
Chris@293 171
Chris@293 172 // std::cerr << std::endl;
Chris@287 173 }
Chris@293 174
Chris@293 175 if (bestColour < 0) m_colour = 0;
Chris@293 176 else m_colour = bestColour;
Chris@287 177
Chris@293 178 if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
Chris@293 179 m_colourRefCount[m_colour] = 1;
Chris@293 180 } else {
Chris@293 181 m_colourRefCount[m_colour]++;
Chris@293 182 }
Chris@287 183 }
Chris@287 184
Chris@287 185 void
Chris@287 186 SingleColourLayer::setBaseColour(int colour)
Chris@287 187 {
Chris@287 188 if (m_colour == colour) return;
Chris@293 189
Chris@293 190 if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
Chris@293 191 m_colourRefCount[m_colour] > 0) {
Chris@293 192 m_colourRefCount[m_colour]--;
Chris@293 193 }
Chris@293 194
Chris@287 195 m_colour = colour;
Chris@293 196
Chris@293 197 if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
Chris@293 198 m_colourRefCount[m_colour] = 1;
Chris@293 199 } else {
Chris@293 200 m_colourRefCount[m_colour]++;
Chris@293 201 }
Chris@293 202
Chris@287 203 flagBaseColourChanged();
Chris@287 204 emit layerParametersChanged();
Chris@287 205 }
Chris@287 206
Chris@287 207 int
Chris@287 208 SingleColourLayer::getBaseColour() const
Chris@287 209 {
Chris@287 210 return m_colour;
Chris@287 211 }
Chris@287 212
Chris@287 213 QColor
Chris@287 214 SingleColourLayer::getBaseQColor() const
Chris@287 215 {
Chris@287 216 return ColourDatabase::getInstance()->getColour(m_colour);
Chris@287 217 }
Chris@287 218
Chris@287 219 QColor
Chris@287 220 SingleColourLayer::getBackgroundQColor(View *v) const
Chris@287 221 {
Chris@287 222 return v->getBackground();
Chris@287 223 }
Chris@287 224
Chris@287 225 QColor
Chris@287 226 SingleColourLayer::getForegroundQColor(View *v) const
Chris@287 227 {
Chris@287 228 return v->getForeground();
Chris@287 229 }
Chris@287 230
Chris@287 231 std::vector<QColor>
Chris@287 232 SingleColourLayer::getPartialShades(View *v) const
Chris@287 233 {
Chris@287 234 std::vector<QColor> s;
Chris@287 235 QColor base = getBaseQColor();
Chris@287 236 QColor bg = getBackgroundQColor(v);
Chris@287 237 for (int i = 0; i < 3; ++i) {
Chris@287 238 int red = base.red() + ((bg.red() - base.red()) * (i + 1)) / 4;
Chris@287 239 int green = base.green() + ((bg.green() - base.green()) * (i + 1)) / 4;
Chris@287 240 int blue = base.blue() + ((bg.blue() - base.blue()) * (i + 1)) / 4;
Chris@287 241 s.push_back(QColor(red, green, blue));
Chris@287 242 }
Chris@287 243 return s;
Chris@287 244 }
Chris@287 245
Chris@287 246 QString
Chris@287 247 SingleColourLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@287 248 {
Chris@287 249 QString s;
Chris@287 250
Chris@287 251 QString colourName, colourSpec, darkbg;
Chris@287 252 ColourDatabase::getInstance()->getStringValues
Chris@287 253 (m_colour, colourName, colourSpec, darkbg);
Chris@287 254
Chris@287 255 s += QString("colourName=\"%1\" "
Chris@287 256 "colour=\"%2\" "
Chris@287 257 "darkBackground=\"%3\" ")
Chris@287 258 .arg(colourName)
Chris@287 259 .arg(colourSpec)
Chris@287 260 .arg(darkbg);
Chris@287 261
Chris@287 262 return Layer::toXmlString(indent, extraAttributes + " " + s);
Chris@287 263 }
Chris@287 264
Chris@287 265 void
Chris@287 266 SingleColourLayer::setProperties(const QXmlAttributes &attributes)
Chris@287 267 {
Chris@287 268 QString colourName = attributes.value("colourName");
Chris@287 269 QString colourSpec = attributes.value("colour");
Chris@287 270 QString darkbg = attributes.value("darkBackground");
Chris@287 271 m_colour = ColourDatabase::getInstance()->putStringValues
Chris@287 272 (colourName, colourSpec, darkbg);
Chris@287 273 }
Chris@287 274