comparison layer/ColourDatabase.cpp @ 376:e1a9e478b7f2

* juggle some files around in order to free audioio, base, and system libraries from dependency on QtGui
author Chris Cannam
date Wed, 12 Mar 2008 17:42:56 +0000
parents
children e0f08e108064
comparison
equal deleted inserted replaced
375:daaf1c435d98 376:e1a9e478b7f2
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2007 QMUL.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "ColourDatabase.h"
17 #include "base/XmlExportable.h"
18
19 #include <QPainter>
20
21 ColourDatabase
22 ColourDatabase::m_instance;
23
24 ColourDatabase *
25 ColourDatabase::getInstance()
26 {
27 return &m_instance;
28 }
29
30 ColourDatabase::ColourDatabase()
31 {
32 }
33
34 int
35 ColourDatabase::getColourCount() const
36 {
37 return m_colours.size();
38 }
39
40 QString
41 ColourDatabase::getColourName(int c) const
42 {
43 if (c < 0 || size_t(c) >= m_colours.size()) return "";
44 return m_colours[c].name;
45 }
46
47 QColor
48 ColourDatabase::getColour(int c) const
49 {
50 if (c < 0 || size_t(c) >= m_colours.size()) return Qt::black;
51 return m_colours[c].colour;
52 }
53
54 QColor
55 ColourDatabase::getColour(QString name) const
56 {
57 for (ColourList::const_iterator i = m_colours.begin();
58 i != m_colours.end(); ++i) {
59 if (i->name == name) return i->colour;
60 }
61
62 return Qt::black;
63 }
64
65 int
66 ColourDatabase::getColourIndex(QString name) const
67 {
68 int index = 0;
69 for (ColourList::const_iterator i = m_colours.begin();
70 i != m_colours.end(); ++i) {
71 if (i->name == name) return index;
72 ++index;
73 }
74
75 return -1;
76 }
77
78 int
79 ColourDatabase::getColourIndex(QColor c) const
80 {
81 int index = 0;
82 for (ColourList::const_iterator i = m_colours.begin();
83 i != m_colours.end(); ++i) {
84 if (i->colour == c) return index;
85 ++index;
86 }
87
88 return -1;
89 }
90
91 bool
92 ColourDatabase::useDarkBackground(int c) const
93 {
94 if (c < 0 || size_t(c) >= m_colours.size()) return false;
95 return m_colours[c].darkbg;
96 }
97
98 void
99 ColourDatabase::setUseDarkBackground(int c, bool dark)
100 {
101 if (c < 0 || size_t(c) >= m_colours.size()) return;
102 if (m_colours[c].darkbg != dark) {
103 m_colours[c].darkbg = dark;
104 emit colourDatabaseChanged();
105 }
106 }
107
108 int
109 ColourDatabase::addColour(QColor c, QString name)
110 {
111 int index = 0;
112 for (ColourList::iterator i = m_colours.begin();
113 i != m_colours.end(); ++i) {
114 if (i->name == name) {
115 i->colour = c;
116 return index;
117 }
118 ++index;
119 }
120
121 ColourRec rec;
122 rec.colour = c;
123 rec.name = name;
124 rec.darkbg = false;
125 m_colours.push_back(rec);
126 emit colourDatabaseChanged();
127 return index;
128 }
129
130 void
131 ColourDatabase::removeColour(QString name)
132 {
133 for (ColourList::iterator i = m_colours.begin();
134 i != m_colours.end(); ++i) {
135 if (i->name == name) {
136 m_colours.erase(i);
137 return;
138 }
139 }
140 }
141
142 void
143 ColourDatabase::getStringValues(int index,
144 QString &colourName,
145 QString &colourSpec,
146 QString &darkbg) const
147 {
148 colourName = "";
149 colourSpec = "";
150 if (index < 0 || size_t(index) >= m_colours.size()) return;
151
152 colourName = getColourName(index);
153 QColor c = getColour(index);
154 colourSpec = XmlExportable::encodeColour(c.red(), c.green(), c.blue());
155 darkbg = useDarkBackground(index) ? "true" : "false";
156 }
157
158 int
159 ColourDatabase::putStringValues(QString colourName,
160 QString colourSpec,
161 QString darkbg)
162 {
163 int index = -1;
164 if (colourSpec != "") {
165 QColor colour(colourSpec);
166 index = getColourIndex(colour);
167 if (index < 0) {
168 index = addColour(colour,
169 colourName == "" ? colourSpec : colourName);
170 }
171 } else if (colourName != "") {
172 index = getColourIndex(colourName);
173 }
174 if (index >= 0) {
175 setUseDarkBackground(index, darkbg == "true");
176 }
177 return index;
178 }
179
180 void
181 ColourDatabase::getColourPropertyRange(int *min, int *max) const
182 {
183 ColourDatabase *db = getInstance();
184 if (min) *min = 0;
185 if (max) {
186 *max = 0;
187 if (db->getColourCount() > 0) *max = db->getColourCount()-1;
188 }
189 }
190
191 QPixmap
192 ColourDatabase::getExamplePixmap(int index, QSize size) const
193 {
194 QPixmap pmap(size);
195 pmap.fill(useDarkBackground(index) ? Qt::black : Qt::white);
196 QPainter paint(&pmap);
197 QColor colour(getColour(index));
198 paint.setPen(colour);
199 paint.setBrush(colour);
200 int margin = 2;
201 if (size.width() < 4 || size.height() < 4) margin = 0;
202 else if (size.width() < 8 || size.height() < 8) margin = 1;
203 paint.drawRect(margin, margin,
204 size.width() - margin*2 - 1, size.height() - margin*2 - 1);
205 return pmap;
206 }
207