Tue, 22 Sep 2015 00:59:21 +0300
LDVertexObject stuff (ultimately not worth it)
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 |
968 | 3 | * Copyright (C) 2013 - 2015 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> |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | #include "main.h" |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | #include "colors.h" |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | #include "ldDocument.h" |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | #include "miscallenous.h" |
962
a4b463a7ee82
Rename MainWindow files
Teemu Piippo <crimsondusk64@gmail.com>
parents:
952
diff
changeset
|
24 | #include "mainwindow.h" |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
25 | |
946 | 26 | struct ColorDataEntry |
27 | { | |
28 | QString name; | |
29 | QString hexcode; | |
30 | QColor faceColor; | |
31 | QColor edgeColor; | |
32 | }; | |
33 | ||
34 | static ColorDataEntry ColorData[512]; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | |
996
9ecc878c7dea
LDVertexObject stuff (ultimately not worth it)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
968
diff
changeset
|
36 | void initColors() |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | { |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | print ("Initializing color information.\n"); |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
39 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | // Always make sure there's 16 and 24 available. They're special like that. |
946 | 41 | ColorData[MainColor].faceColor = |
42 | ColorData[MainColor].hexcode = "#AAAAAA"; | |
43 | ColorData[MainColor].edgeColor = Qt::black; | |
44 | ColorData[MainColor].name = "Main color"; | |
45 | ||
46 | ColorData[EdgeColor].faceColor = | |
47 | ColorData[EdgeColor].edgeColor = | |
48 | ColorData[EdgeColor].hexcode = "#000000"; | |
49 | ColorData[EdgeColor].name = "Edge color"; | |
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
|
50 | |
946 | 51 | parseLDConfig(); |
52 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | |
946 | 54 | bool LDColor::isValid() const |
55 | { | |
56 | if (isLDConfigColor() and ColorData[index()].name.isEmpty()) | |
57 | return false; // Unknown LDConfig color | |
58 | ||
59 | 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
|
60 | } |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
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 | { |
946 | 64 | return index() >= 0 and index() < countof (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
|
65 | } |
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
|
66 | |
946 | 67 | 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
|
68 | { |
946 | 69 | if (isDirect()) |
70 | return "0x" + QString::number (index(), 16).toUpper(); | |
71 | else if (isLDConfigColor()) | |
72 | return ColorData[index()].name; | |
73 | else if (index() == -1) | |
74 | return "null color"; | |
75 | else | |
76 | return ""; | |
77 | } | |
78 | ||
79 | QString LDColor::hexcode() const | |
80 | { | |
81 | return faceColor().name(); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
83 | |
946 | 84 | QColor LDColor::faceColor() const |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
85 | { |
946 | 86 | if (isDirect()) |
87 | { | |
88 | QColor color; | |
89 | color.setRed ((index() & 0x0FF0000) >> 16); | |
90 | color.setGreen ((index() & 0x000FF00) >> 8); | |
91 | color.setBlue (index() & 0x00000FF); | |
92 | ||
93 | if (index() >= 0x3000000) | |
94 | color.setAlpha (128); | |
95 | ||
96 | return color; | |
97 | } | |
98 | else if (isLDConfigColor()) | |
99 | { | |
100 | return ColorData[index()].faceColor; | |
101 | } | |
102 | else | |
103 | { | |
104 | return Qt::black; | |
105 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
107 | |
946 | 108 | QColor LDColor::edgeColor() const |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
109 | { |
946 | 110 | if (isDirect()) |
996
9ecc878c7dea
LDVertexObject stuff (ultimately not worth it)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
968
diff
changeset
|
111 | return luma() < 48 ? Qt::white : Qt::black; |
946 | 112 | else if (isLDConfigColor()) |
113 | return ColorData[index()].edgeColor; | |
114 | else | |
115 | return Qt::black; | |
116 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
117 | |
946 | 118 | int LDColor::luma() const |
119 | { | |
996
9ecc878c7dea
LDVertexObject stuff (ultimately not worth it)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
968
diff
changeset
|
120 | return ::luma (faceColor()); |
946 | 121 | } |
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
|
122 | |
946 | 123 | int LDColor::edgeLuma() const |
124 | { | |
996
9ecc878c7dea
LDVertexObject stuff (ultimately not worth it)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
968
diff
changeset
|
125 | return ::luma (edgeColor()); |
946 | 126 | } |
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
|
127 | |
946 | 128 | qint32 LDColor::index() const |
129 | { | |
130 | return m_index; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
131 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
132 | |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
133 | QString LDColor::indexString() const |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
134 | { |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
135 | if (isDirect()) |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
136 | return "0x" + QString::number (index(), 16).toUpper(); |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
137 | |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
138 | return QString::number (index()); |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
139 | } |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
140 | |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
141 | bool LDColor::isDirect() const |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
142 | { |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
143 | return index() >= 0x02000000; |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
144 | } |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
145 | |
996
9ecc878c7dea
LDVertexObject stuff (ultimately not worth it)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
968
diff
changeset
|
146 | int luma (const QColor& col) |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
147 | { |
946 | 148 | 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
|
149 | } |
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
|
150 | |
946 | 151 | void parseLDConfig() |
152 | { | |
153 | QFile* fp = OpenLDrawFile ("LDConfig.ldr", false); | |
154 | ||
155 | if (fp == nullptr) | |
156 | { | |
157 | Critical (QObject::tr ("Unable to open LDConfig.ldr for parsing.")); | |
158 | return; | |
159 | } | |
160 | ||
161 | // Read in the lines | |
162 | while (not fp->atEnd()) | |
163 | { | |
164 | QString line = QString::fromUtf8 (fp->readLine()); | |
165 | ||
166 | if (line.isEmpty() or line[0] != '0') | |
167 | continue; // empty or illogical | |
168 | ||
169 | line.remove ('\r'); | |
170 | line.remove ('\n'); | |
171 | ||
172 | // Parse the line | |
173 | LDConfigParser pars (line, ' '); | |
174 | ||
175 | int code = 0, alpha = 255; | |
176 | QString name, facename, edgename, valuestr; | |
177 | ||
178 | // Check 0 !COLOUR, parse the name | |
179 | if (not pars.compareToken (0, "0") or | |
180 | not pars.compareToken (1, "!COLOUR") or | |
181 | not pars.getToken (name, 2)) | |
182 | { | |
183 | continue; | |
184 | } | |
185 | ||
186 | // Replace underscores in the name with spaces for readability | |
187 | name.replace ("_", " "); | |
188 | ||
189 | // Get the CODE tag | |
190 | if (not pars.parseLDConfigTag ("CODE", valuestr)) | |
191 | continue; | |
192 | ||
193 | // Ensure that the code is within [0 - 511] | |
194 | bool ok; | |
195 | code = valuestr.toShort (&ok); | |
196 | ||
197 | if (not ok or code < 0 or code >= 512) | |
198 | continue; | |
199 | ||
200 | // VALUE and EDGE tags | |
201 | if (not pars.parseLDConfigTag ("VALUE", facename) or not pars.parseLDConfigTag ("EDGE", edgename)) | |
202 | continue; | |
203 | ||
204 | // Ensure that our colors are correct | |
205 | QColor faceColor (facename), | |
206 | edgeColor (edgename); | |
207 | ||
208 | if (not faceColor.isValid() or not edgeColor.isValid()) | |
209 | continue; | |
210 | ||
211 | // Parse alpha if given. | |
212 | if (pars.parseLDConfigTag ("ALPHA", valuestr)) | |
966
a834e43a57da
Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
962
diff
changeset
|
213 | alpha = qBound (0, valuestr.toInt(), 255); |
946 | 214 | |
215 | ColorDataEntry& entry = ColorData[code]; | |
216 | entry.name = name; | |
217 | entry.faceColor = faceColor; | |
218 | entry.edgeColor = edgeColor; | |
219 | entry.hexcode = facename; | |
220 | entry.faceColor.setAlpha (alpha); | |
221 | } | |
222 | ||
223 | fp->close(); | |
224 | fp->deleteLater(); | |
225 | } | |
226 | ||
227 | // ============================================================================= | |
228 | // | |
229 | LDConfigParser::LDConfigParser (QString inText, char sep) | |
230 | { | |
231 | m_tokens = inText.split (sep, QString::SkipEmptyParts); | |
232 | m_pos = -1; | |
233 | } | |
234 | ||
235 | // ============================================================================= | |
236 | // | |
237 | bool LDConfigParser::getToken (QString& val, const int pos) | |
238 | { | |
239 | if (pos >= m_tokens.size()) | |
240 | return false; | |
241 | ||
242 | val = m_tokens[pos]; | |
243 | return true; | |
244 | } | |
245 | ||
246 | // ============================================================================= | |
247 | // | |
248 | bool LDConfigParser::findToken (int& result, char const* needle, int args) | |
249 | { | |
250 | for (int i = 0; i < (m_tokens.size() - args); ++i) | |
251 | { | |
252 | if (m_tokens[i] == needle) | |
253 | { | |
254 | result = i; | |
255 | return true; | |
256 | } | |
257 | } | |
258 | ||
259 | return false; | |
260 | } | |
261 | ||
262 | // ============================================================================= | |
263 | // | |
264 | bool LDConfigParser::compareToken (int inPos, QString text) | |
265 | { | |
266 | QString tok; | |
267 | ||
268 | if (not getToken (tok, inPos)) | |
269 | return false; | |
270 | ||
271 | return (tok == text); | |
272 | } | |
273 | ||
274 | // ============================================================================= | |
275 | // | |
276 | // Helper function for parseLDConfig | |
277 | // | |
278 | bool LDConfigParser::parseLDConfigTag (char const* tag, QString& val) | |
279 | { | |
280 | int pos; | |
281 | ||
282 | // Try find the token and get its position | |
283 | if (not findToken (pos, tag, 1)) | |
284 | return false; | |
285 | ||
286 | // Get the token after it and store it into val | |
287 | return getToken (val, pos + 1); | |
288 | } |