Wed, 17 Feb 2016 19:54:21 +0200
removed removeDuplicates in favor of QSet, and the unused ObjectList class
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 |
1014
f0a8ecb6a357
Happy new year 2016!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1012
diff
changeset
|
3 | * Copyright (C) 2013 - 2016 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 | |
946 | 19 | #include <QFile> |
998 | 20 | #include <QMessageBox> |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | #include "main.h" |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | #include "colors.h" |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | #include "ldDocument.h" |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | #include "miscallenous.h" |
962
a4b463a7ee82
Rename MainWindow files
Teemu Piippo <crimsondusk64@gmail.com>
parents:
952
diff
changeset
|
25 | #include "mainwindow.h" |
998 | 26 | #include "documentmanager.h" |
1012 | 27 | #include "ldpaths.h" |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | |
998 | 29 | static ColorData* colorData = nullptr; |
946 | 30 | |
998 | 31 | void initColors() |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | { |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | print ("Initializing color information.\n"); |
998 | 34 | static ColorData colors; |
35 | colorData = &colors; | |
946 | 36 | } |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | |
946 | 38 | bool LDColor::isValid() const |
39 | { | |
998 | 40 | if (isLDConfigColor() and data().name.isEmpty()) |
946 | 41 | return false; // Unknown LDConfig color |
42 | ||
43 | 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
|
44 | } |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
45 | |
946 | 46 | 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
|
47 | { |
998 | 48 | return colorData->contains (index()); |
49 | } | |
50 | ||
51 | const ColorData::Entry& LDColor::data() const | |
52 | { | |
53 | 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
|
54 | } |
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
|
55 | |
946 | 56 | 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
|
57 | { |
946 | 58 | if (isDirect()) |
59 | return "0x" + QString::number (index(), 16).toUpper(); | |
60 | else if (isLDConfigColor()) | |
998 | 61 | return data().name; |
946 | 62 | else if (index() == -1) |
63 | return "null color"; | |
64 | else | |
65 | return ""; | |
66 | } | |
67 | ||
68 | QString LDColor::hexcode() const | |
69 | { | |
70 | return faceColor().name(); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
71 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
72 | |
946 | 73 | QColor LDColor::faceColor() const |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | { |
946 | 75 | if (isDirect()) |
76 | { | |
77 | QColor color; | |
78 | color.setRed ((index() & 0x0FF0000) >> 16); | |
79 | color.setGreen ((index() & 0x000FF00) >> 8); | |
80 | color.setBlue (index() & 0x00000FF); | |
81 | ||
82 | if (index() >= 0x3000000) | |
83 | color.setAlpha (128); | |
84 | ||
85 | return color; | |
86 | } | |
87 | else if (isLDConfigColor()) | |
88 | { | |
998 | 89 | return data().faceColor; |
946 | 90 | } |
91 | else | |
92 | { | |
93 | return Qt::black; | |
94 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
95 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | |
946 | 97 | QColor LDColor::edgeColor() const |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
98 | { |
946 | 99 | if (isDirect()) |
998 | 100 | return ::luma (faceColor()) < 48 ? Qt::white : Qt::black; |
946 | 101 | else if (isLDConfigColor()) |
998 | 102 | return data().edgeColor; |
946 | 103 | else |
104 | return Qt::black; | |
105 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | |
946 | 107 | int LDColor::luma() const |
108 | { | |
998 | 109 | return ::luma (faceColor()); |
946 | 110 | } |
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
|
111 | |
946 | 112 | int LDColor::edgeLuma() const |
113 | { | |
998 | 114 | return ::luma (edgeColor()); |
946 | 115 | } |
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
|
116 | |
946 | 117 | qint32 LDColor::index() const |
118 | { | |
119 | return m_index; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
120 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
121 | |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
122 | QString LDColor::indexString() const |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
123 | { |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
124 | if (isDirect()) |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
125 | return "0x" + QString::number (index(), 16).toUpper(); |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
126 | |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
127 | return QString::number (index()); |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
128 | } |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
129 | |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
130 | bool LDColor::isDirect() const |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
131 | { |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
132 | return index() >= 0x02000000; |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
133 | } |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
134 | |
1031
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
135 | 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
|
136 | { |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
137 | return color.index(); |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
138 | } |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
139 | |
998 | 140 | int luma (const QColor& col) |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
141 | { |
946 | 142 | return (0.2126f * col.red()) + (0.7152f * col.green()) + (0.0722f * col.blue()); |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
143 | } |
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
|
144 | |
998 | 145 | 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
|
146 | { |
998 | 147 | if (colorData == nullptr) |
148 | colorData = this; | |
149 | ||
150 | // Initialize main and edge colors, they're special like that. | |
151 | m_data[MainColor].faceColor = | |
152 | m_data[MainColor].hexcode = "#AAAAAA"; | |
153 | m_data[MainColor].edgeColor = Qt::black; | |
154 | m_data[MainColor].name = "Main color"; | |
155 | m_data[EdgeColor].faceColor = | |
156 | m_data[EdgeColor].edgeColor = | |
157 | m_data[EdgeColor].hexcode = "#000000"; | |
158 | m_data[EdgeColor].name = "Edge color"; | |
159 | loadFromLdconfig(); | |
160 | } | |
161 | ||
162 | ColorData::~ColorData() | |
163 | { | |
164 | if (colorData == this) | |
165 | colorData = nullptr; | |
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
|
166 | } |
946 | 167 | |
998 | 168 | bool ColorData::contains (int code) const |
946 | 169 | { |
998 | 170 | return code >= 0 and code < EntryCount; |
171 | } | |
172 | ||
173 | const ColorData::Entry& ColorData::get (int code) const | |
174 | { | |
175 | if (not contains (code)) | |
176 | throw std::runtime_error ("Attempted to get non-existant color information"); | |
946 | 177 | |
998 | 178 | return m_data[code]; |
179 | } | |
180 | ||
181 | void ColorData::loadFromLdconfig() | |
182 | { | |
1012 | 183 | QString path = LDPaths::ldConfigPath(); |
998 | 184 | QFile fp (path); |
185 | ||
186 | if (not fp.open (QIODevice::ReadOnly)) | |
946 | 187 | { |
998 | 188 | QMessageBox::critical (nullptr, "Error", "Unable to open LDConfig.ldr for parsing: " + fp.errorString()); |
946 | 189 | return; |
190 | } | |
191 | ||
998 | 192 | // TODO: maybe LDConfig can be loaded as a Document? Or would that be overkill? |
193 | while (not fp.atEnd()) | |
946 | 194 | { |
998 | 195 | QString line = QString::fromUtf8 (fp.readLine()); |
946 | 196 | |
197 | if (line.isEmpty() or line[0] != '0') | |
198 | continue; // empty or illogical | |
199 | ||
200 | line.remove ('\r'); | |
201 | line.remove ('\n'); | |
202 | ||
203 | // Parse the line | |
998 | 204 | LDConfigParser parser (line, ' '); |
205 | QString name; | |
206 | QString facename; | |
207 | QString edgename; | |
208 | QString codestring; | |
946 | 209 | |
210 | // Check 0 !COLOUR, parse the name | |
998 | 211 | if (not parser.compareToken (0, "0") or not parser.compareToken (1, "!COLOUR") or not parser.getToken (name, 2)) |
946 | 212 | continue; |
213 | ||
214 | // Replace underscores in the name with spaces for readability | |
215 | name.replace ("_", " "); | |
216 | ||
998 | 217 | if (not parser.parseTag ("CODE", codestring)) |
946 | 218 | continue; |
219 | ||
220 | bool ok; | |
998 | 221 | int code = codestring.toShort (&ok); |
946 | 222 | |
998 | 223 | if (not ok or not contains (code)) |
946 | 224 | continue; |
225 | ||
998 | 226 | if (not parser.parseTag ("VALUE", facename) or not parser.parseTag ("EDGE", edgename)) |
946 | 227 | continue; |
228 | ||
229 | // Ensure that our colors are correct | |
998 | 230 | QColor faceColor (facename); |
231 | QColor edgeColor (edgename); | |
946 | 232 | |
233 | if (not faceColor.isValid() or not edgeColor.isValid()) | |
234 | continue; | |
235 | ||
998 | 236 | Entry& entry = m_data[code]; |
946 | 237 | entry.name = name; |
238 | entry.faceColor = faceColor; | |
239 | entry.edgeColor = edgeColor; | |
240 | entry.hexcode = facename; | |
998 | 241 | |
242 | if (parser.parseTag ("ALPHA", codestring)) | |
243 | entry.faceColor.setAlpha (qBound (0, codestring.toInt(), 255)); | |
946 | 244 | } |
245 | } | |
246 | ||
247 | // ============================================================================= | |
248 | // | |
249 | LDConfigParser::LDConfigParser (QString inText, char sep) | |
250 | { | |
251 | m_tokens = inText.split (sep, QString::SkipEmptyParts); | |
252 | m_pos = -1; | |
253 | } | |
254 | ||
255 | // ============================================================================= | |
256 | // | |
257 | bool LDConfigParser::getToken (QString& val, const int pos) | |
258 | { | |
259 | if (pos >= m_tokens.size()) | |
260 | return false; | |
261 | ||
262 | val = m_tokens[pos]; | |
263 | return true; | |
264 | } | |
265 | ||
266 | // ============================================================================= | |
267 | // | |
268 | bool LDConfigParser::findToken (int& result, char const* needle, int args) | |
269 | { | |
270 | for (int i = 0; i < (m_tokens.size() - args); ++i) | |
271 | { | |
272 | if (m_tokens[i] == needle) | |
273 | { | |
274 | result = i; | |
275 | return true; | |
276 | } | |
277 | } | |
278 | ||
279 | return false; | |
280 | } | |
281 | ||
282 | // ============================================================================= | |
283 | // | |
284 | bool LDConfigParser::compareToken (int inPos, QString text) | |
285 | { | |
286 | QString tok; | |
287 | ||
288 | if (not getToken (tok, inPos)) | |
289 | return false; | |
290 | ||
291 | return (tok == text); | |
292 | } | |
293 | ||
294 | // ============================================================================= | |
295 | // | |
296 | // Helper function for parseLDConfig | |
297 | // | |
998 | 298 | bool LDConfigParser::parseTag (char const* tag, QString& val) |
946 | 299 | { |
300 | int pos; | |
301 | ||
302 | // Try find the token and get its position | |
303 | if (not findToken (pos, tag, 1)) | |
304 | return false; | |
305 | ||
306 | // Get the token after it and store it into val | |
307 | return getToken (val, pos + 1); | |
308 | } |