annotate layer/SingleColourLayer.cpp @ 366:a55affd7ab6c

* Avoid setting a new colour on layer when adding the same layer more than once
author Chris Cannam
date Mon, 11 Feb 2008 11:06:17 +0000
parents c0b9eec70639
children 06360ed99cd1
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@316 22 #include <QTextStream>
Chris@287 23 #include <QApplication>
Chris@287 24
Chris@293 25 SingleColourLayer::ColourRefCount
Chris@293 26 SingleColourLayer::m_colourRefCount;
Chris@287 27
Chris@287 28 SingleColourLayer::SingleColourLayer() :
Chris@294 29 m_colour(0),
Chris@366 30 m_colourExplicitlySet(false),
Chris@366 31 m_defaultColourSet(false)
Chris@287 32 {
Chris@287 33 setDefaultColourFor(0);
Chris@287 34 }
Chris@287 35
Chris@299 36 QPixmap
Chris@299 37 SingleColourLayer::getLayerPresentationPixmap(QSize size) const
Chris@299 38 {
Chris@299 39 return ColourDatabase::getInstance()->getExamplePixmap(m_colour, size);
Chris@299 40 }
Chris@299 41
Chris@287 42 bool
Chris@287 43 SingleColourLayer::hasLightBackground() const
Chris@287 44 {
Chris@287 45 bool dark = ColourDatabase::getInstance()->useDarkBackground(m_colour);
Chris@287 46 return !dark;
Chris@287 47 }
Chris@287 48
Chris@287 49 Layer::PropertyList
Chris@287 50 SingleColourLayer::getProperties() const
Chris@287 51 {
Chris@287 52 PropertyList list = Layer::getProperties();
Chris@287 53 list.push_back("Colour");
Chris@287 54 return list;
Chris@287 55 }
Chris@287 56
Chris@287 57 QString
Chris@287 58 SingleColourLayer::getPropertyLabel(const PropertyName &name) const
Chris@287 59 {
Chris@287 60 if (name == "Colour") return tr("Colour");
Chris@287 61 return "";
Chris@287 62 }
Chris@287 63
Chris@287 64 Layer::PropertyType
Chris@287 65 SingleColourLayer::getPropertyType(const PropertyName &name) const
Chris@287 66 {
Chris@287 67 if (name == "Colour") return ColourProperty;
Chris@287 68 return InvalidProperty;
Chris@287 69 }
Chris@287 70
Chris@287 71 QString
Chris@287 72 SingleColourLayer::getPropertyGroupName(const PropertyName &) const
Chris@287 73 {
Chris@287 74 return QString();
Chris@287 75 }
Chris@287 76
Chris@287 77 int
Chris@287 78 SingleColourLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@287 79 int *min, int *max, int *deflt) const
Chris@287 80 {
Chris@287 81 int val = 0;
Chris@287 82
Chris@287 83 int garbage0, garbage1, garbage2;
Chris@287 84 if (!min) min = &garbage0;
Chris@287 85 if (!max) max = &garbage1;
Chris@287 86 if (!deflt) deflt = &garbage2;
Chris@287 87
Chris@287 88 if (name == "Colour") {
Chris@287 89
Chris@287 90 ColourDatabase::getInstance()->getColourPropertyRange(min, max);
Chris@287 91 *deflt = 0; //!!!
Chris@287 92
Chris@287 93 val = m_colour;
Chris@287 94
Chris@287 95 } else {
Chris@287 96 val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@287 97 }
Chris@287 98
Chris@287 99 return val;
Chris@287 100 }
Chris@287 101
Chris@287 102 QString
Chris@287 103 SingleColourLayer::getPropertyValueLabel(const PropertyName &name,
Chris@287 104 int value) const
Chris@287 105 {
Chris@287 106 if (name == "Colour") {
Chris@287 107 return Layer::getPropertyValueLabel(name, value);
Chris@287 108 }
Chris@287 109 return tr("<unknown>");
Chris@287 110 }
Chris@287 111
Chris@287 112 RangeMapper *
Chris@287 113 SingleColourLayer::getNewPropertyRangeMapper(const PropertyName &) const
Chris@287 114 {
Chris@287 115 return 0;
Chris@287 116 }
Chris@287 117
Chris@287 118 void
Chris@287 119 SingleColourLayer::setProperty(const PropertyName &name, int value)
Chris@287 120 {
Chris@287 121 if (name == "Colour") {
Chris@287 122 setBaseColour(value);
Chris@287 123 }
Chris@287 124 }
Chris@287 125
Chris@287 126 void
Chris@287 127 SingleColourLayer::setDefaultColourFor(View *v)
Chris@287 128 {
Chris@366 129 std::cerr << "SingleColourLayer::setDefaultColourFor: m_colourExplicitlySet = " << m_colourExplicitlySet << ", m_defaultColourSet " << m_defaultColourSet << std::endl;
Chris@366 130
Chris@366 131 if (m_colourExplicitlySet || m_defaultColourSet) return;
Chris@294 132
Chris@287 133 bool dark = false;
Chris@287 134 if (v) {
Chris@287 135 dark = !v->hasLightBackground();
Chris@287 136 } else {
Chris@287 137 QColor bg = QApplication::palette().color(QPalette::Window);
Chris@287 138 if (bg.red() + bg.green() + bg.blue() < 384) dark = true;
Chris@287 139 }
Chris@287 140
Chris@287 141 ColourDatabase *cdb = ColourDatabase::getInstance();
Chris@287 142
Chris@287 143 int hint = -1;
Chris@287 144 bool impose = false;
Chris@287 145 if (v) {
Chris@293 146 if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
Chris@293 147 m_colourRefCount[m_colour] > 0) {
Chris@293 148 m_colourRefCount[m_colour]--;
Chris@293 149 }
Chris@287 150 // We don't want to call this if !v because that probably
Chris@287 151 // means we're being called from the constructor, and this is
Chris@287 152 // a virtual function
Chris@287 153 hint = getDefaultColourHint(dark, impose);
Chris@297 154 // std::cerr << "hint = " << hint << ", impose = " << impose << std::endl;
Chris@293 155 } else {
Chris@297 156 // std::cerr << "(from ctor)" << std::endl;
Chris@287 157 }
Chris@287 158
Chris@287 159 if (hint >= 0 && impose) {
Chris@293 160 setBaseColour(hint);
Chris@366 161 m_defaultColourSet = true;
Chris@287 162 return;
Chris@287 163 }
Chris@287 164
Chris@293 165 int bestCount = 0, bestColour = -1;
Chris@293 166
Chris@287 167 for (int i = 0; i < cdb->getColourCount(); ++i) {
Chris@293 168
Chris@287 169 int index = i;
Chris@287 170 if (hint > 0) index = (index + hint) % cdb->getColourCount();
Chris@287 171 if (cdb->useDarkBackground(index) != dark) continue;
Chris@293 172
Chris@293 173 int count = 0;
Chris@293 174 if (m_colourRefCount.find(index) != m_colourRefCount.end()) {
Chris@293 175 count = m_colourRefCount[index];
Chris@287 176 }
Chris@293 177
Chris@297 178 // std::cerr << "index = " << index << ", count = " << count;
Chris@293 179
Chris@293 180 if (bestColour < 0 || count < bestCount) {
Chris@293 181 bestColour = index;
Chris@293 182 bestCount = count;
Chris@297 183 // std::cerr << " *";
Chris@293 184 }
Chris@293 185
Chris@297 186 // std::cerr << std::endl;
Chris@287 187 }
Chris@293 188
Chris@293 189 if (bestColour < 0) m_colour = 0;
Chris@293 190 else m_colour = bestColour;
Chris@287 191
Chris@293 192 if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
Chris@293 193 m_colourRefCount[m_colour] = 1;
Chris@293 194 } else {
Chris@293 195 m_colourRefCount[m_colour]++;
Chris@293 196 }
Chris@366 197
Chris@366 198 m_defaultColourSet = true;
Chris@287 199 }
Chris@287 200
Chris@287 201 void
Chris@287 202 SingleColourLayer::setBaseColour(int colour)
Chris@287 203 {
Chris@294 204 m_colourExplicitlySet = true;
Chris@294 205
Chris@287 206 if (m_colour == colour) return;
Chris@293 207
Chris@293 208 if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
Chris@293 209 m_colourRefCount[m_colour] > 0) {
Chris@293 210 m_colourRefCount[m_colour]--;
Chris@293 211 }
Chris@293 212
Chris@287 213 m_colour = colour;
Chris@293 214
Chris@293 215 if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
Chris@293 216 m_colourRefCount[m_colour] = 1;
Chris@293 217 } else {
Chris@293 218 m_colourRefCount[m_colour]++;
Chris@293 219 }
Chris@293 220
Chris@287 221 flagBaseColourChanged();
Chris@287 222 emit layerParametersChanged();
Chris@287 223 }
Chris@287 224
Chris@287 225 int
Chris@287 226 SingleColourLayer::getBaseColour() const
Chris@287 227 {
Chris@287 228 return m_colour;
Chris@287 229 }
Chris@287 230
Chris@287 231 QColor
Chris@287 232 SingleColourLayer::getBaseQColor() const
Chris@287 233 {
Chris@287 234 return ColourDatabase::getInstance()->getColour(m_colour);
Chris@287 235 }
Chris@287 236
Chris@287 237 QColor
Chris@287 238 SingleColourLayer::getBackgroundQColor(View *v) const
Chris@287 239 {
Chris@287 240 return v->getBackground();
Chris@287 241 }
Chris@287 242
Chris@287 243 QColor
Chris@287 244 SingleColourLayer::getForegroundQColor(View *v) const
Chris@287 245 {
Chris@287 246 return v->getForeground();
Chris@287 247 }
Chris@287 248
Chris@287 249 std::vector<QColor>
Chris@287 250 SingleColourLayer::getPartialShades(View *v) const
Chris@287 251 {
Chris@287 252 std::vector<QColor> s;
Chris@287 253 QColor base = getBaseQColor();
Chris@287 254 QColor bg = getBackgroundQColor(v);
Chris@287 255 for (int i = 0; i < 3; ++i) {
Chris@287 256 int red = base.red() + ((bg.red() - base.red()) * (i + 1)) / 4;
Chris@287 257 int green = base.green() + ((bg.green() - base.green()) * (i + 1)) / 4;
Chris@287 258 int blue = base.blue() + ((bg.blue() - base.blue()) * (i + 1)) / 4;
Chris@287 259 s.push_back(QColor(red, green, blue));
Chris@287 260 }
Chris@287 261 return s;
Chris@287 262 }
Chris@287 263
Chris@316 264 void
Chris@316 265 SingleColourLayer::toXml(QTextStream &stream,
Chris@316 266 QString indent, QString extraAttributes) const
Chris@287 267 {
Chris@287 268 QString s;
Chris@287 269
Chris@287 270 QString colourName, colourSpec, darkbg;
Chris@287 271 ColourDatabase::getInstance()->getStringValues
Chris@287 272 (m_colour, colourName, colourSpec, darkbg);
Chris@287 273
Chris@287 274 s += QString("colourName=\"%1\" "
Chris@287 275 "colour=\"%2\" "
Chris@287 276 "darkBackground=\"%3\" ")
Chris@287 277 .arg(colourName)
Chris@287 278 .arg(colourSpec)
Chris@287 279 .arg(darkbg);
Chris@287 280
Chris@316 281 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@287 282 }
Chris@287 283
Chris@287 284 void
Chris@287 285 SingleColourLayer::setProperties(const QXmlAttributes &attributes)
Chris@287 286 {
Chris@287 287 QString colourName = attributes.value("colourName");
Chris@287 288 QString colourSpec = attributes.value("colour");
Chris@287 289 QString darkbg = attributes.value("darkBackground");
Chris@296 290
Chris@296 291 int colour = ColourDatabase::getInstance()->putStringValues
Chris@287 292 (colourName, colourSpec, darkbg);
Chris@296 293
Chris@296 294 m_colourExplicitlySet = true;
Chris@296 295
Chris@296 296 if (m_colour != colour) {
Chris@296 297
Chris@296 298 std::cerr << "SingleColourLayer::setProperties: changing colour from " << m_colour << " to " << colour << std::endl;
Chris@296 299
Chris@296 300 if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
Chris@296 301 m_colourRefCount[m_colour] > 0) {
Chris@296 302 m_colourRefCount[m_colour]--;
Chris@296 303 }
Chris@296 304
Chris@296 305 m_colour = colour;
Chris@296 306
Chris@296 307 if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
Chris@296 308 m_colourRefCount[m_colour] = 1;
Chris@296 309 } else {
Chris@296 310 m_colourRefCount[m_colour]++;
Chris@296 311 }
Chris@296 312
Chris@296 313 flagBaseColourChanged();
Chris@294 314 }
Chris@287 315 }
Chris@287 316