1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Rosegarden
5 A MIDI and audio sequencer and musical notation editor.
6 Copyright 2000-2021 the Rosegarden development team.
7
8 This file is Copyright 2003
9 Mark Hymers <markh@linuxfromscratch.org>
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version. See the file
15 COPYING included with this distribution for more information.
16 */
17
18 #include "ColourMap.h"
19
20 #include "XmlExportable.h"
21
22 #include <sstream>
23
24 namespace Rosegarden
25 {
26
27
28 const QColor ColourMap::defaultSegmentColour(255, 234, 182);
29
ColourMap()30 ColourMap::ColourMap()
31 {
32 // Set up entry 0 as the default colour.
33 // ??? Does this really matter? I suspect this is clobbered by
34 // RoseXmlHandler, so it's not needed. Might be worth doing
35 // some comprehensive testing to see if removing this would
36 // make any difference.
37 colours[0] = Entry();
38 }
39
40 QColor
getColour(unsigned colourID) const41 ColourMap::getColour(unsigned colourID) const
42 {
43 // If the map is empty, return the default.
44 if (colours.empty())
45 return defaultSegmentColour;
46
47 // Find the colourID in the map.
48 MapType::const_iterator colourIter = colours.find(colourID);
49
50 // Not found? Return the first entry.
51 if (colourIter == colours.end())
52 return colours.begin()->second.colour;
53
54 return colourIter->second.colour;
55 }
56
57 std::string
getName(unsigned colourID) const58 ColourMap::getName(unsigned colourID) const
59 {
60 // If the map is empty, return the default.
61 if (colours.empty())
62 return "";
63
64 // Find the colourID in the map.
65 MapType::const_iterator colourIter = colours.find(colourID);
66
67 // Not found? Return the first entry.
68 if (colourIter == colours.end())
69 return colours.begin()->second.name;
70
71 return colourIter->second.name;
72 }
73
74 void
addEntry(QColor colour,std::string name)75 ColourMap::addEntry(QColor colour, std::string name)
76 {
77 // Find the first unused ID.
78
79 // ??? It would be easier and faster to keep track of the largest/next
80 // ID we can assign in a member variable, and use/increment that.
81 // It's perfectly OK to have gaps in the IDs.
82 // However, since there is no way to test this routine, I'm leaving
83 // it as-is for now.
84
85 unsigned highest = 0;
86
87 // For each colour in the map
88 for (MapType::const_iterator colourIter = colours.begin();
89 colourIter != colours.end();
90 ++colourIter)
91 {
92 if (colourIter->first != highest)
93 break;
94
95 ++highest;
96 }
97
98 colours[highest] = Entry(colour, name);
99 }
100
101 void
modifyName(unsigned colourID,std::string name)102 ColourMap::modifyName(unsigned colourID, std::string name)
103 {
104 // We don't allow a name to be given to the default colour
105 if (colourID == 0)
106 return;
107
108 // Find the colourID in the map.
109 MapType::iterator colourIter = colours.find(colourID);
110
111 // Not found? Bail.
112 if (colourIter == colours.end())
113 return;
114
115 colourIter->second.name = name;
116 }
117
118 void
modifyColour(unsigned colourID,QColor colour)119 ColourMap::modifyColour(unsigned colourID, QColor colour)
120 {
121 // Find the colourID in the map.
122 MapType::iterator colourIter = colours.find(colourID);
123
124 // Not found? Bail.
125 if (colourIter == colours.end())
126 return;
127
128 colourIter->second.colour = colour;
129 }
130
131 void
deleteEntry(unsigned colourID)132 ColourMap::deleteEntry(unsigned colourID)
133 {
134 // We explicitly refuse to delete the default colour
135 if (colourID == 0)
136 return;
137
138 colours.erase(colourID);
139 }
140
141 std::string
toXmlString(std::string name) const142 ColourMap::toXmlString(std::string name) const
143 {
144 std::stringstream output;
145
146 output << " <colourmap name=\"" << XmlExportable::encode(name)
147 << "\">" << std::endl;
148
149 // For each colour in the map
150 for (MapType::const_iterator colourIter = colours.begin();
151 colourIter != colours.end();
152 ++colourIter) {
153
154 const QColor &color = colourIter->second.colour;
155
156 output << " " << " <colourpair id=\"" << colourIter->first
157 << "\" name=\"" << XmlExportable::encode(colourIter->second.name)
158 << "\" "
159 << "red=\"" << color.red()
160 << "\" green=\"" << color.green()
161 << "\" blue=\"" << color.blue()
162 << "\""
163 << "/>" << std::endl;
164 }
165
166 output << " </colourmap>" << std::endl;
167
168 return output.str();
169
170 }
171
172
173 }
174