comparison base/ColourDatabase.cpp @ 277:3b8008d09541

* Add a colour database, and Add New Colour function to the colour combo in property box. The colour property is only correctly handled in the waveform layer so far. * Add en_GB translation, to translate those annoying Color texts in the Qt colour picker dialog.
author Chris Cannam
date Wed, 11 Jul 2007 17:21:37 +0000
parents
children 9a13687c078b
comparison
equal deleted inserted replaced
276:657825878970 277:3b8008d09541
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 "XmlExportable.h"
18
19 ColourDatabase
20 ColourDatabase::m_instance;
21
22 ColourDatabase *
23 ColourDatabase::getInstance()
24 {
25 return &m_instance;
26 }
27
28 ColourDatabase::ColourDatabase()
29 {
30 }
31
32 int
33 ColourDatabase::getColourCount() const
34 {
35 return m_colours.size();
36 }
37
38 QString
39 ColourDatabase::getColourName(int c) const
40 {
41 if (c < 0 || size_t(c) >= m_colours.size()) return "";
42 return m_colours[c].name;
43 }
44
45 QColor
46 ColourDatabase::getColour(int c) const
47 {
48 if (c < 0 || size_t(c) >= m_colours.size()) return Qt::black;
49 return m_colours[c].colour;
50 }
51
52 QColor
53 ColourDatabase::getColour(QString name) const
54 {
55 for (ColourList::const_iterator i = m_colours.begin();
56 i != m_colours.end(); ++i) {
57 if (i->name == name) return i->colour;
58 }
59
60 return Qt::black;
61 }
62
63 int
64 ColourDatabase::getColourIndex(QString name) const
65 {
66 int index = 0;
67 for (ColourList::const_iterator i = m_colours.begin();
68 i != m_colours.end(); ++i) {
69 if (i->name == name) return index;
70 ++index;
71 }
72
73 return -1;
74 }
75
76 int
77 ColourDatabase::getColourIndex(QColor c) const
78 {
79 int index = 0;
80 for (ColourList::const_iterator i = m_colours.begin();
81 i != m_colours.end(); ++i) {
82 if (i->colour == c) return index;
83 ++index;
84 }
85
86 return -1;
87 }
88
89 bool
90 ColourDatabase::useDarkBackground(int c) const
91 {
92 if (c < 0 || size_t(c) >= m_colours.size()) return false;
93 return m_colours[c].darkbg;
94 }
95
96 void
97 ColourDatabase::setUseDarkBackground(int c, bool dark)
98 {
99 if (c < 0 || size_t(c) >= m_colours.size()) return;
100 m_colours[c].darkbg = dark;
101 }
102
103 int
104 ColourDatabase::addColour(QColor c, QString name)
105 {
106 int index = 0;
107 for (ColourList::iterator i = m_colours.begin();
108 i != m_colours.end(); ++i) {
109 if (i->name == name) {
110 i->colour = c;
111 return index;
112 }
113 ++index;
114 }
115
116 ColourRec rec;
117 rec.colour = c;
118 rec.name = name;
119 rec.darkbg = false;
120 m_colours.push_back(rec);
121 emit colourDatabaseChanged();
122 return index;
123 }
124
125 void
126 ColourDatabase::removeColour(QString name)
127 {
128 for (ColourList::iterator i = m_colours.begin();
129 i != m_colours.end(); ++i) {
130 if (i->name == name) {
131 m_colours.erase(i);
132 return;
133 }
134 }
135 }
136
137 void
138 ColourDatabase::getStringValues(int index,
139 QString &colourName,
140 QString &colourSpec,
141 QString &darkbg) const
142 {
143 colourName = "";
144 colourSpec = "";
145 if (index < 0 || size_t(index) >= m_colours.size()) return;
146
147 colourName = getColourName(index);
148 colourSpec = XmlExportable::encodeColour(getColour(index));
149 darkbg = useDarkBackground(index) ? "true" : "false";
150 }
151
152 int
153 ColourDatabase::putStringValues(QString colourName,
154 QString colourSpec,
155 QString darkbg)
156 {
157 int index = -1;
158 if (colourSpec != "") {
159 QColor colour(colourSpec);
160 index = getColourIndex(colour);
161 if (index < 0) {
162 index = addColour(colour,
163 colourName == "" ? colourSpec : colourName);
164 }
165 } else if (colourName != "") {
166 index = getColourIndex(colourName);
167 }
168 if (index >= 0) {
169 setUseDarkBackground(index, darkbg == "true");
170 }
171 return index;
172 }
173
174 void
175 ColourDatabase::getColourPropertyRange(int *min, int *max) const
176 {
177 ColourDatabase *db = getInstance();
178 if (min) *min = 0;
179 if (max) {
180 *max = 0;
181 if (db->getColourCount() > 0) *max = db->getColourCount()-1;
182 }
183 }
184