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