1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This file copyright 2007 QMUL.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "SingleColourLayer.h"
#include "ColourDatabase.h"
#include "view/View.h"
#include <iostream>
#include <QTextStream>
#include <QApplication>
//#define DEBUG_COLOUR_SELECTION 1
namespace sv {
SingleColourLayer::ColourRefCount
SingleColourLayer::m_colourRefCount;
SingleColourLayer::SingleColourLayer() :
m_colour(0),
m_colourExplicitlySet(false),
m_defaultColourSet(false)
{
// Reference current colour because setDefaulColourFor
// will unreference it before (possibly) changing it.
refColor();
setDefaultColourFor(nullptr);
}
SingleColourLayer::~SingleColourLayer()
{
unrefColor();
}
QPixmap
SingleColourLayer::getLayerPresentationPixmap(QSize size) const
{
return ColourDatabase::getInstance()->getExamplePixmap(m_colour, size);
}
bool
SingleColourLayer::hasLightBackground() const
{
bool dark = ColourDatabase::getInstance()->useDarkBackground(m_colour);
return !dark;
}
Layer::PropertyList
SingleColourLayer::getProperties() const
{
PropertyList list = Layer::getProperties();
list.push_back("Colour");
return list;
}
QString
SingleColourLayer::getPropertyLabel(const PropertyName &name) const
{
if (name == "Colour") return tr("Colour");
return "";
}
Layer::PropertyType
SingleColourLayer::getPropertyType(const PropertyName &name) const
{
if (name == "Colour") return ColourProperty;
return InvalidProperty;
}
QString
SingleColourLayer::getPropertyGroupName(const PropertyName &) const
{
return QString();
}
int
SingleColourLayer::getPropertyRangeAndValue(const PropertyName &name,
int *min, int *max, int *deflt) const
{
int val = 0;
int garbage0, garbage1, garbage2;
if (!min) min = &garbage0;
if (!max) max = &garbage1;
if (!deflt) deflt = &garbage2;
if (name == "Colour") {
ColourDatabase::getInstance()->getColourPropertyRange(min, max);
*deflt = 0; //!!!
val = m_colour;
} else {
val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
}
return val;
}
QString
SingleColourLayer::getPropertyValueLabel(const PropertyName &name,
int value) const
{
if (name == "Colour") {
ColourDatabase *db = ColourDatabase::getInstance();
if (value >= 0 && value < db->getColourCount()) {
return db->getColourName(value);
}
}
return tr("<unknown>");
}
RangeMapper *
SingleColourLayer::getNewPropertyRangeMapper(const PropertyName &) const
{
return nullptr;
}
void
SingleColourLayer::setProperty(const PropertyName &name, int value)
{
if (name == "Colour") {
setBaseColour(value);
}
}
void
SingleColourLayer::setDefaultColourFor(LayerGeometryProvider *v)
{
#ifdef DEBUG_COLOUR_SELECTION
SVDEBUG << "SingleColourLayer::setDefaultColourFor: m_colourExplicitlySet = " << m_colourExplicitlySet << ", m_defaultColourSet " << m_defaultColourSet << endl;
#endif
if (m_colourExplicitlySet || m_defaultColourSet) return;
if (v) m_defaultColourSet = true; // v==0 case doesn't really count
bool dark = false;
if (v) {
dark = !v->hasLightBackground();
} else {
QColor bg = QApplication::palette().color(QPalette::Window);
if (bg.red() + bg.green() + bg.blue() < 384) dark = true;
}
ColourDatabase *cdb = ColourDatabase::getInstance();
int hint = -1;
bool impose = false;
if (v) {
// We don't want to call this if !v because that probably
// means we're being called from the constructor, and this is
// a virtual function
hint = getDefaultColourHint(dark, impose);
#ifdef DEBUG_COLOUR_SELECTION
cerr << "hint = " << hint << ", impose = " << impose << endl;
#endif
} else {
#ifdef DEBUG_COLOUR_SELECTION
cerr << "(from ctor)" << endl;
#endif
}
if (hint >= 0 && impose) {
setBaseColour(hint);
return;
}
unrefColor();
int bestCount = 0, bestColour = -1;
for (int i = 0; i < cdb->getColourCount(); ++i) {
int index = i;
if (hint > 0) index = (index + hint) % cdb->getColourCount();
if (cdb->useDarkBackground(index) != dark) continue;
int count = 0;
if (m_colourRefCount.find(index) != m_colourRefCount.end()) {
count = m_colourRefCount[index];
}
#ifdef DEBUG_COLOUR_SELECTION
cerr << "index = " << index << ", count = " << count;
#endif
if (bestColour < 0 || count < bestCount) {
bestColour = index;
bestCount = count;
#ifdef DEBUG_COLOUR_SELECTION
cerr << " *";
#endif
}
#ifdef DEBUG_COLOUR_SELECTION
cerr << endl;
#endif
}
if (bestColour < 0) m_colour = 0;
else m_colour = bestColour;
refColor();
}
void
SingleColourLayer::setBaseColour(int colour)
{
m_colourExplicitlySet = true;
if (m_colour == colour) return;
refColor();
m_colour = colour;
unrefColor();
flagBaseColourChanged();
emit layerParametersChanged();
}
int
SingleColourLayer::getBaseColour() const
{
return m_colour;
}
QColor
SingleColourLayer::getBaseQColor() const
{
return ColourDatabase::getInstance()->getColour(m_colour);
}
QColor
SingleColourLayer::getBackgroundQColor(LayerGeometryProvider *v) const
{
return v->getBackground();
}
QColor
SingleColourLayer::getForegroundQColor(LayerGeometryProvider *v) const
{
return v->getForeground();
}
std::vector<QColor>
SingleColourLayer::getPartialShades(LayerGeometryProvider *v) const
{
std::vector<QColor> s;
QColor base = getBaseQColor();
QColor bg = getBackgroundQColor(v);
for (int i = 0; i < 3; ++i) {
int red = base.red() + ((bg.red() - base.red()) * (i + 1)) / 4;
int green = base.green() + ((bg.green() - base.green()) * (i + 1)) / 4;
int blue = base.blue() + ((bg.blue() - base.blue()) * (i + 1)) / 4;
s.push_back(QColor(red, green, blue));
}
return s;
}
void
SingleColourLayer::toXml(QTextStream &stream,
QString indent, QString extraAttributes) const
{
QString s;
QString colourName, colourSpec, darkbg;
ColourDatabase::getInstance()->getStringValues
(m_colour, colourName, colourSpec, darkbg);
s += QString("colourName=\"%1\" "
"colour=\"%2\" "
"darkBackground=\"%3\" ")
.arg(colourName)
.arg(colourSpec)
.arg(darkbg);
Layer::toXml(stream, indent, extraAttributes + " " + s);
}
void
SingleColourLayer::setProperties(const LayerAttributes &attributes)
{
QString colourName = attributes.value("colourName");
QString colourSpec = attributes.value("colour");
QString darkbg = attributes.value("darkBackground");
int colour = ColourDatabase::getInstance()->putStringValues
(colourName, colourSpec, darkbg);
if (colour == -1)
return;
m_colourExplicitlySet = true;
if (m_colour != colour) {
#ifdef DEBUG_COLOUR_SELECTION
SVDEBUG << "SingleColourLayer::setProperties: changing colour from " << m_colour << " to " << colour << endl;
#endif
unrefColor();
m_colour = colour;
refColor();
flagBaseColourChanged();
}
}
void SingleColourLayer::refColor()
{
if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
m_colourRefCount[m_colour] = 1;
} else {
m_colourRefCount[m_colour]++;
}
}
void SingleColourLayer::unrefColor()
{
if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
m_colourRefCount[m_colour] > 0) {
m_colourRefCount[m_colour]--;
}
}
} // end namespace sv
|