Thu, 23 Feb 2017 23:36:59 +0200
Changed QByteArray to QVector to avoid that ugly reinterpret_cast.
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | /* |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | * LDForge: LDraw parts authoring CAD |
1072 | 3 | * Copyright (C) 2013 - 2017 Teemu Piippo |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | * |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | * This program is free software: you can redistribute it and/or modify |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | * it under the terms of the GNU General Public License as published by |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | * the Free Software Foundation, either version 3 of the License, or |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | * (at your option) any later version. |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | * |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | * This program is distributed in the hope that it will be useful, |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | * GNU General Public License for more details. |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | * |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | * You should have received a copy of the GNU General Public License |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | */ |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | |
998 | 19 | #include <QMessageBox> |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | #include "colors.h" |
1012 | 21 | #include "ldpaths.h" |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | |
1114
ffd49a28f49e
Moved some global constants into appropriate namespaces.
Teemu Piippo <teemu@hecknology.net>
parents:
1086
diff
changeset
|
23 | ColorData* LDColor::colorData = nullptr; |
1153 | 24 | const LDColor LDColor::nullColor = -1; |
946 | 25 | |
1044 | 26 | /* |
27 | * Initializes the color information module. | |
28 | */ | |
1114
ffd49a28f49e
Moved some global constants into appropriate namespaces.
Teemu Piippo <teemu@hecknology.net>
parents:
1086
diff
changeset
|
29 | void LDColor::initColors() |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | { |
998 | 31 | static ColorData colors; |
1114
ffd49a28f49e
Moved some global constants into appropriate namespaces.
Teemu Piippo <teemu@hecknology.net>
parents:
1086
diff
changeset
|
32 | LDColor::colorData = &colors; |
946 | 33 | } |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | |
1044 | 35 | /* |
1153 | 36 | * Default-constructs an LDColor to 0(black). |
1044 | 37 | */ |
38 | LDColor::LDColor() : | |
1153 | 39 | m_index {0} {} |
1044 | 40 | |
41 | /* | |
42 | * Constructs an LDColor by index. | |
43 | */ | |
1153 | 44 | LDColor::LDColor(qint32 index) : |
45 | m_index {index} {} | |
1044 | 46 | |
47 | /* | |
48 | * Returns whether or not the color is valid. | |
49 | */ | |
946 | 50 | bool LDColor::isValid() const |
51 | { | |
998 | 52 | if (isLDConfigColor() and data().name.isEmpty()) |
946 | 53 | return false; // Unknown LDConfig color |
1044 | 54 | else |
55 | return m_index != -1; | |
795
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
56 | } |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | |
1044 | 58 | /* |
59 | * Returns whether or not this color is defined in LDConfig.ldr. | |
60 | * This is false for e.g. direct colors. | |
61 | */ | |
946 | 62 | bool LDColor::isLDConfigColor() const |
795
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
63 | { |
1044 | 64 | return colorData->contains(index()); |
998 | 65 | } |
66 | ||
1044 | 67 | /* |
68 | * Returns the ColorData entry for this color. | |
69 | */ | |
998 | 70 | const ColorData::Entry& LDColor::data() const |
71 | { | |
1044 | 72 | return colorData->get(index()); |
795
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
73 | } |
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
74 | |
1044 | 75 | /* |
76 | * Returns the name of this color. | |
77 | */ | |
946 | 78 | QString LDColor::name() const |
795
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
79 | { |
946 | 80 | if (isDirect()) |
1153 | 81 | return "0x" + QString::number(index(), 16).toUpper(); |
946 | 82 | else if (isLDConfigColor()) |
998 | 83 | return data().name; |
946 | 84 | else if (index() == -1) |
85 | return "null color"; | |
86 | else | |
1044 | 87 | return "unknown"; |
946 | 88 | } |
89 | ||
1044 | 90 | /* |
91 | * Returns the hexadecimal code of this color. | |
92 | */ | |
946 | 93 | QString LDColor::hexcode() const |
94 | { | |
95 | return faceColor().name(); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
97 | |
1044 | 98 | /* |
99 | * Returns the color used for surfaces. | |
100 | */ | |
946 | 101 | QColor LDColor::faceColor() const |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
102 | { |
946 | 103 | if (isDirect()) |
104 | { | |
1044 | 105 | // Direct color -- compute from the index. |
946 | 106 | QColor color; |
1044 | 107 | color.setRed((index() & 0x0FF0000) >> 16); |
108 | color.setGreen((index() & 0x000FF00) >> 8); | |
109 | color.setBlue(index() & 0x00000FF); | |
946 | 110 | |
111 | if (index() >= 0x3000000) | |
1044 | 112 | color.setAlpha(128); |
946 | 113 | |
114 | return color; | |
115 | } | |
116 | else if (isLDConfigColor()) | |
117 | { | |
998 | 118 | return data().faceColor; |
946 | 119 | } |
120 | else | |
121 | { | |
122 | return Qt::black; | |
123 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
124 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
125 | |
1044 | 126 | /* |
127 | * Returns the color used for edge lines. | |
128 | */ | |
946 | 129 | QColor LDColor::edgeColor() const |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
130 | { |
946 | 131 | if (isDirect()) |
1044 | 132 | return luma(faceColor()) < 48 ? Qt::white : Qt::black; |
946 | 133 | else if (isLDConfigColor()) |
998 | 134 | return data().edgeColor; |
946 | 135 | else |
136 | return Qt::black; | |
137 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
138 | |
1044 | 139 | /* |
140 | * Returns the index number of this color. | |
141 | */ | |
946 | 142 | qint32 LDColor::index() const |
143 | { | |
144 | return m_index; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
145 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
146 | |
1044 | 147 | /* |
148 | * Returns a string containing the preferred representation of the index. | |
149 | */ | |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
150 | QString LDColor::indexString() const |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
151 | { |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
152 | if (isDirect()) |
1044 | 153 | { |
154 | // Use hexadecimal notation for direct colors. | |
155 | return "0x" + QString::number(index(), 16).toUpper(); | |
156 | } | |
157 | else | |
158 | { | |
159 | return QString::number(index()); | |
160 | } | |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
161 | } |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
162 | |
1044 | 163 | /* |
164 | * Returns whether or not this color is a direct color. | |
165 | * Direct colors are picked by RGB value and are not defined in LDConfig.ldr. | |
166 | */ | |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
167 | bool LDColor::isDirect() const |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
168 | { |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
169 | return index() >= 0x02000000; |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
170 | } |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
171 | |
1044 | 172 | /* |
173 | * LDColors are hashed by their index. | |
174 | */ | |
1031
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
175 | uint qHash(LDColor color) |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
176 | { |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
177 | return color.index(); |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
178 | } |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
179 | |
1044 | 180 | /* |
181 | * Calculates the luma-value for the given color. | |
182 | * c.f. https://en.wikipedia.org/wiki/Luma_(video) | |
183 | */ | |
1153 | 184 | int luma(const QColor& color) |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
185 | { |
1153 | 186 | return static_cast<int>(round(0.2126 * color.red() + 0.7152 * color.green() + 0.0722 * color.blue())); |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
187 | } |
795
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
188 | |
1044 | 189 | /* |
190 | * Constructs the color data array. | |
191 | */ | |
998 | 192 | ColorData::ColorData() |
795
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
193 | { |
998 | 194 | // Initialize main and edge colors, they're special like that. |
1044 | 195 | m_data[MainColor].faceColor = "#AAAAAA"; |
998 | 196 | m_data[MainColor].edgeColor = Qt::black; |
197 | m_data[MainColor].name = "Main color"; | |
1153 | 198 | m_data[EdgeColor].faceColor = Qt::black; |
199 | m_data[EdgeColor].edgeColor = Qt::black; | |
998 | 200 | m_data[EdgeColor].name = "Edge color"; |
1044 | 201 | |
202 | // Load the rest from LDConfig.ldr. | |
998 | 203 | loadFromLdconfig(); |
204 | } | |
205 | ||
1044 | 206 | /* |
207 | * ColorData :: contains | |
208 | * | |
209 | * Returns whether or not the given color index is present in the array. | |
210 | */ | |
211 | bool ColorData::contains(int code) const | |
946 | 212 | { |
1065
c8ecddbd99e9
Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents:
1063
diff
changeset
|
213 | return code >= 0 and code < countof(m_data); |
998 | 214 | } |
215 | ||
1044 | 216 | /* |
217 | * Returns an entry in the color array. | |
218 | */ | |
219 | const ColorData::Entry& ColorData::get(int code) const | |
998 | 220 | { |
1044 | 221 | if (not contains(code)) |
222 | throw std::runtime_error {"Attempted to get non-existant color information"}; | |
946 | 223 | |
998 | 224 | return m_data[code]; |
225 | } | |
226 | ||
1044 | 227 | /* |
228 | * Loads color information from LDConfig.ldr. | |
229 | */ | |
998 | 230 | void ColorData::loadFromLdconfig() |
231 | { | |
1012 | 232 | QString path = LDPaths::ldConfigPath(); |
1044 | 233 | QFile file {path}; |
998 | 234 | |
1153 | 235 | if (not file.open(QIODevice::ReadOnly)) |
946 | 236 | { |
1044 | 237 | QMessageBox::critical(nullptr, "Error", "Unable to open LDConfig.ldr for parsing: " + file.errorString()); |
946 | 238 | return; |
239 | } | |
240 | ||
998 | 241 | // TODO: maybe LDConfig can be loaded as a Document? Or would that be overkill? |
1044 | 242 | while (not file.atEnd()) |
946 | 243 | { |
1153 | 244 | QString line = QString::fromUtf8(file.readLine()); |
946 | 245 | |
246 | if (line.isEmpty() or line[0] != '0') | |
247 | continue; // empty or illogical | |
248 | ||
1044 | 249 | line.remove('\r'); |
250 | line.remove('\n'); | |
946 | 251 | |
252 | // Parse the line | |
1044 | 253 | LDConfigParser parser = {line}; |
998 | 254 | QString name; |
255 | QString facename; | |
256 | QString edgename; | |
257 | QString codestring; | |
946 | 258 | |
259 | // Check 0 !COLOUR, parse the name | |
1044 | 260 | if (not parser.compareToken(0, "0") or not parser.compareToken(1, "!COLOUR") or not parser.getToken(name, 2)) |
946 | 261 | continue; |
262 | ||
263 | // Replace underscores in the name with spaces for readability | |
1044 | 264 | name.replace("_", " "); |
946 | 265 | |
1153 | 266 | if (not parser.parseTag("CODE", codestring)) |
946 | 267 | continue; |
268 | ||
269 | bool ok; | |
1044 | 270 | int code = codestring.toShort(&ok); |
946 | 271 | |
1044 | 272 | if (not ok or not contains(code)) |
946 | 273 | continue; |
274 | ||
1044 | 275 | if (not parser.parseTag("VALUE", facename) or not parser.parseTag("EDGE", edgename)) |
946 | 276 | continue; |
277 | ||
278 | // Ensure that our colors are correct | |
1044 | 279 | QColor faceColor = {facename}; |
280 | QColor edgeColor = {edgename}; | |
946 | 281 | |
282 | if (not faceColor.isValid() or not edgeColor.isValid()) | |
283 | continue; | |
284 | ||
1044 | 285 | // Fill in the entry now. |
998 | 286 | Entry& entry = m_data[code]; |
946 | 287 | entry.name = name; |
288 | entry.faceColor = faceColor; | |
289 | entry.edgeColor = edgeColor; | |
998 | 290 | |
1044 | 291 | // If the alpha tag is present, fill in that too. |
292 | if (parser.parseTag("ALPHA", codestring)) | |
293 | entry.faceColor.setAlpha(qBound(0, codestring.toInt(), 255)); | |
946 | 294 | } |
295 | } | |
296 | ||
1044 | 297 | /* |
298 | * Constructs the LDConfig.ldr parser. | |
299 | */ | |
300 | LDConfigParser::LDConfigParser(QString inputText) | |
946 | 301 | { |
1153 | 302 | m_tokens = inputText.split(' ', QString::SkipEmptyParts); |
946 | 303 | } |
304 | ||
1044 | 305 | /* |
306 | * Returns whether or not there is a token at the given position. | |
307 | * If there is, fills in the value parameter with it. | |
308 | */ | |
309 | bool LDConfigParser::getToken(QString& tokenText, int position) | |
946 | 310 | { |
1065
c8ecddbd99e9
Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents:
1063
diff
changeset
|
311 | if (position >= countof(m_tokens)) |
1044 | 312 | { |
946 | 313 | return false; |
1044 | 314 | } |
315 | else | |
316 | { | |
317 | tokenText = m_tokens[position]; | |
318 | return true; | |
319 | } | |
946 | 320 | } |
321 | ||
1044 | 322 | /* |
323 | * Attempts to find the provided token in the parsed LDConfig.ldr line. | |
324 | * If found, fills in the first parameter with the position of the token. | |
325 | * | |
326 | * The args parameter specifies how many arguments (i.e. following tokens) the token needs to have. | |
327 | */ | |
328 | bool LDConfigParser::findToken(int& tokenPosition, QString needle, int args) | |
946 | 329 | { |
1065
c8ecddbd99e9
Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents:
1063
diff
changeset
|
330 | for (int i = 0; i < (countof(m_tokens) - args); ++i) |
946 | 331 | { |
332 | if (m_tokens[i] == needle) | |
333 | { | |
1044 | 334 | tokenPosition = i; |
946 | 335 | return true; |
336 | } | |
337 | } | |
338 | ||
339 | return false; | |
340 | } | |
341 | ||
1044 | 342 | /* |
343 | * Returns whether or not the token at the given position has the given text value. | |
344 | */ | |
1153 | 345 | bool LDConfigParser::compareToken(int position, QString text) |
946 | 346 | { |
1044 | 347 | QString token; |
1153 | 348 | return getToken(token, position) and (token == text); |
946 | 349 | } |
350 | ||
1044 | 351 | /* |
352 | * Finds an attribute in the line, and fills in its value. | |
353 | * For instance, if the line contains "ALPHA 128", this function can find the "128" for "ALPHA". | |
354 | * Returns whether or not the attribute was found. | |
355 | */ | |
1153 | 356 | bool LDConfigParser::parseTag(QString key, QString& value) |
946 | 357 | { |
1044 | 358 | int position; |
946 | 359 | |
360 | // Try find the token and get its position | |
1153 | 361 | if (not findToken(position, key, 1)) |
1044 | 362 | { |
946 | 363 | return false; |
1044 | 364 | } |
365 | else | |
366 | { | |
367 | // Get the token after it and store it in. | |
1153 | 368 | return getToken(value, position + 1); |
1044 | 369 | } |
370 | } |