annotate layer/ColourDatabase.cpp @ 1185:f32828ea63d9 3.0-integration

Minor debug tweaks
author Chris Cannam
date Wed, 14 Dec 2016 11:56:47 +0000
parents e0f08e108064
children a34a2a25907c
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@904 37 return int(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@904 43 if (!in_range_for(m_colours, c)) 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@904 50 if (!in_range_for(m_colours, c)) 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@904 57 for (auto &c: m_colours) {
Chris@904 58 if (c.name == name) return c.colour;
Chris@376 59 }
Chris@376 60
Chris@376 61 return Qt::black;
Chris@376 62 }
Chris@376 63
Chris@376 64 int
Chris@376 65 ColourDatabase::getColourIndex(QString name) const
Chris@376 66 {
Chris@376 67 int index = 0;
Chris@904 68 for (auto &c: m_colours) {
Chris@904 69 if (c.name == name) return index;
Chris@376 70 ++index;
Chris@376 71 }
Chris@376 72
Chris@376 73 return -1;
Chris@376 74 }
Chris@376 75
Chris@376 76 int
Chris@904 77 ColourDatabase::getColourIndex(QColor col) const
Chris@376 78 {
Chris@376 79 int index = 0;
Chris@904 80 for (auto &c: m_colours) {
Chris@904 81 if (c.colour == col) return index;
Chris@376 82 ++index;
Chris@376 83 }
Chris@376 84
Chris@376 85 return -1;
Chris@376 86 }
Chris@376 87
Chris@376 88 bool
Chris@376 89 ColourDatabase::useDarkBackground(int c) const
Chris@376 90 {
Chris@904 91 if (!in_range_for(m_colours, c)) return false;
Chris@376 92 return m_colours[c].darkbg;
Chris@376 93 }
Chris@376 94
Chris@376 95 void
Chris@376 96 ColourDatabase::setUseDarkBackground(int c, bool dark)
Chris@376 97 {
Chris@904 98 if (!in_range_for(m_colours, c)) return;
Chris@376 99 if (m_colours[c].darkbg != dark) {
Chris@376 100 m_colours[c].darkbg = dark;
Chris@376 101 emit colourDatabaseChanged();
Chris@376 102 }
Chris@376 103 }
Chris@376 104
Chris@376 105 int
Chris@376 106 ColourDatabase::addColour(QColor c, QString name)
Chris@376 107 {
Chris@376 108 int index = 0;
Chris@904 109
Chris@376 110 for (ColourList::iterator i = m_colours.begin();
Chris@376 111 i != m_colours.end(); ++i) {
Chris@376 112 if (i->name == name) {
Chris@376 113 i->colour = c;
Chris@376 114 return index;
Chris@376 115 }
Chris@376 116 ++index;
Chris@376 117 }
Chris@376 118
Chris@376 119 ColourRec rec;
Chris@376 120 rec.colour = c;
Chris@376 121 rec.name = name;
Chris@376 122 rec.darkbg = false;
Chris@376 123 m_colours.push_back(rec);
Chris@376 124 emit colourDatabaseChanged();
Chris@376 125 return index;
Chris@376 126 }
Chris@376 127
Chris@376 128 void
Chris@376 129 ColourDatabase::removeColour(QString name)
Chris@376 130 {
Chris@376 131 for (ColourList::iterator i = m_colours.begin();
Chris@376 132 i != m_colours.end(); ++i) {
Chris@376 133 if (i->name == name) {
Chris@376 134 m_colours.erase(i);
Chris@376 135 return;
Chris@376 136 }
Chris@376 137 }
Chris@376 138 }
Chris@376 139
Chris@376 140 void
Chris@376 141 ColourDatabase::getStringValues(int index,
Chris@376 142 QString &colourName,
Chris@376 143 QString &colourSpec,
Chris@376 144 QString &darkbg) const
Chris@376 145 {
Chris@376 146 colourName = "";
Chris@376 147 colourSpec = "";
Chris@904 148 if (!in_range_for(m_colours, index)) return;
Chris@376 149
Chris@376 150 colourName = getColourName(index);
Chris@376 151 QColor c = getColour(index);
Chris@376 152 colourSpec = XmlExportable::encodeColour(c.red(), c.green(), c.blue());
Chris@376 153 darkbg = useDarkBackground(index) ? "true" : "false";
Chris@376 154 }
Chris@376 155
Chris@376 156 int
Chris@376 157 ColourDatabase::putStringValues(QString colourName,
Chris@376 158 QString colourSpec,
Chris@376 159 QString darkbg)
Chris@376 160 {
Chris@376 161 int index = -1;
Chris@376 162 if (colourSpec != "") {
Chris@376 163 QColor colour(colourSpec);
Chris@376 164 index = getColourIndex(colour);
Chris@376 165 if (index < 0) {
Chris@376 166 index = addColour(colour,
Chris@376 167 colourName == "" ? colourSpec : colourName);
Chris@376 168 }
Chris@376 169 } else if (colourName != "") {
Chris@376 170 index = getColourIndex(colourName);
Chris@376 171 }
Chris@376 172 if (index >= 0) {
Chris@376 173 setUseDarkBackground(index, darkbg == "true");
Chris@376 174 }
Chris@376 175 return index;
Chris@376 176 }
Chris@376 177
Chris@376 178 void
Chris@376 179 ColourDatabase::getColourPropertyRange(int *min, int *max) const
Chris@376 180 {
Chris@376 181 ColourDatabase *db = getInstance();
Chris@376 182 if (min) *min = 0;
Chris@376 183 if (max) {
Chris@376 184 *max = 0;
Chris@376 185 if (db->getColourCount() > 0) *max = db->getColourCount()-1;
Chris@376 186 }
Chris@376 187 }
Chris@376 188
Chris@376 189 QPixmap
Chris@376 190 ColourDatabase::getExamplePixmap(int index, QSize size) const
Chris@376 191 {
Chris@376 192 QPixmap pmap(size);
Chris@376 193 pmap.fill(useDarkBackground(index) ? Qt::black : Qt::white);
Chris@376 194 QPainter paint(&pmap);
Chris@376 195 QColor colour(getColour(index));
Chris@376 196 paint.setPen(colour);
Chris@376 197 paint.setBrush(colour);
Chris@376 198 int margin = 2;
Chris@376 199 if (size.width() < 4 || size.height() < 4) margin = 0;
Chris@376 200 else if (size.width() < 8 || size.height() < 8) margin = 1;
Chris@376 201 paint.drawRect(margin, margin,
Chris@376 202 size.width() - margin*2 - 1, size.height() - margin*2 - 1);
Chris@376 203 return pmap;
Chris@376 204 }
Chris@376 205