1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 Santeri Piippo |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #include "document.h" |
|
20 #include "ldconfig.h" |
|
21 #include "gui.h" |
|
22 #include "misc.h" |
|
23 #include "colors.h" |
|
24 |
|
25 // ============================================================================= |
|
26 // Helper function for parseLDConfig |
|
27 // ----------------------------------------------------------------------------- |
|
28 static bool parseLDConfigTag (LDConfigParser& pars, char const* tag, str& val) |
|
29 { int pos; |
|
30 |
|
31 // Try find the token and get its position |
|
32 if (!pars.findToken (pos, tag, 1)) |
|
33 return false; |
|
34 |
|
35 // Get the token after it and store it into val |
|
36 return pars.getToken (val, pos + 1); |
|
37 } |
|
38 |
|
39 // ============================================================================= |
|
40 // ----------------------------------------------------------------------------- |
|
41 void parseLDConfig() |
|
42 { File* f = openLDrawFile ("LDConfig.ldr", false); |
|
43 |
|
44 if (!f) |
|
45 { critical (fmt (QObject::tr ("Unable to open LDConfig.ldr for parsing: %1"), |
|
46 strerror (errno))); |
|
47 return; |
|
48 } |
|
49 |
|
50 // Read in the lines |
|
51 for (str line : *f) |
|
52 { if (line.length() == 0 || line[0] != '0') |
|
53 continue; // empty or illogical |
|
54 |
|
55 line.remove ('\r'); |
|
56 line.remove ('\n'); |
|
57 |
|
58 // Parse the line |
|
59 LDConfigParser pars (line, ' '); |
|
60 |
|
61 int code = 0, alpha = 255; |
|
62 str name, facename, edgename, valuestr; |
|
63 |
|
64 // Check 0 !COLOUR, parse the name |
|
65 if (!pars.tokenCompare (0, "0") || !pars.tokenCompare (1, "!COLOUR") || !pars.getToken (name, 2)) |
|
66 continue; |
|
67 |
|
68 // Replace underscores in the name with spaces for readability |
|
69 name.replace ("_", " "); |
|
70 |
|
71 // Get the CODE tag |
|
72 if (!parseLDConfigTag (pars, "CODE", valuestr)) |
|
73 continue; |
|
74 |
|
75 if (!numeric (valuestr)) |
|
76 continue; // not a number |
|
77 |
|
78 // Ensure that the code is within [0 - 511] |
|
79 bool ok; |
|
80 code = valuestr.toShort (&ok); |
|
81 |
|
82 if (!ok || code < 0 || code >= 512) |
|
83 continue; |
|
84 |
|
85 // VALUE and EDGE tags |
|
86 if (!parseLDConfigTag (pars, "VALUE", facename) || !parseLDConfigTag (pars, "EDGE", edgename)) |
|
87 continue; |
|
88 |
|
89 // Ensure that our colors are correct |
|
90 QColor faceColor (facename), |
|
91 edgeColor (edgename); |
|
92 |
|
93 if (!faceColor.isValid() || !edgeColor.isValid()) |
|
94 continue; |
|
95 |
|
96 // Parse alpha if given. |
|
97 if (parseLDConfigTag (pars, "ALPHA", valuestr)) |
|
98 alpha = clamp (valuestr.toInt(), 0, 255); |
|
99 |
|
100 LDColor* col = new LDColor; |
|
101 col->name = name; |
|
102 col->faceColor = faceColor; |
|
103 col->edgeColor = edgeColor; |
|
104 col->hexcode = facename; |
|
105 col->faceColor.setAlpha (alpha); |
|
106 col->index = code; |
|
107 setColor (code, col); |
|
108 } |
|
109 |
|
110 delete f; |
|
111 } |
|
112 |
|
113 // ============================================================================= |
|
114 // ----------------------------------------------------------------------------- |
|
115 LDConfigParser::LDConfigParser (str inText, char sep) |
|
116 { m_tokens = inText.split (sep, QString::SkipEmptyParts); |
|
117 m_pos = -1; |
|
118 } |
|
119 |
|
120 // ============================================================================= |
|
121 // ----------------------------------------------------------------------------- |
|
122 bool LDConfigParser::isAtBeginning() |
|
123 { return m_pos == -1; |
|
124 } |
|
125 |
|
126 // ============================================================================= |
|
127 // ----------------------------------------------------------------------------- |
|
128 bool LDConfigParser::isAtEnd() |
|
129 { return m_pos == m_tokens.size() - 1; |
|
130 } |
|
131 |
|
132 // ============================================================================= |
|
133 // ----------------------------------------------------------------------------- |
|
134 bool LDConfigParser::getToken (str& val, const int pos) |
|
135 { if (pos >= m_tokens.size()) |
|
136 return false; |
|
137 |
|
138 val = m_tokens[pos]; |
|
139 return true; |
|
140 } |
|
141 |
|
142 // ============================================================================= |
|
143 // ----------------------------------------------------------------------------- |
|
144 bool LDConfigParser::getNextToken (str& val) |
|
145 { return getToken (val, ++m_pos); |
|
146 } |
|
147 |
|
148 // ============================================================================= |
|
149 // ----------------------------------------------------------------------------- |
|
150 bool LDConfigParser::peekNextToken (str& val) |
|
151 { return getToken (val, m_pos + 1); |
|
152 } |
|
153 |
|
154 // ============================================================================= |
|
155 // ----------------------------------------------------------------------------- |
|
156 bool LDConfigParser::findToken (int& result, char const* needle, int args) |
|
157 { for (int i = 0; i < (m_tokens.size() - args); ++i) |
|
158 { if (m_tokens[i] == needle) |
|
159 { result = i; |
|
160 return true; |
|
161 } |
|
162 } |
|
163 |
|
164 return false; |
|
165 } |
|
166 |
|
167 // ============================================================================= |
|
168 // ----------------------------------------------------------------------------- |
|
169 void LDConfigParser::rewind() |
|
170 { m_pos = -1; |
|
171 } |
|
172 |
|
173 // ============================================================================= |
|
174 // ----------------------------------------------------------------------------- |
|
175 void LDConfigParser::seek (int amount, bool rel) |
|
176 { m_pos = (rel ? m_pos : 0) + amount; |
|
177 } |
|
178 |
|
179 // ============================================================================= |
|
180 // ----------------------------------------------------------------------------- |
|
181 int LDConfigParser::getSize() |
|
182 { return m_tokens.size(); |
|
183 } |
|
184 |
|
185 // ============================================================================= |
|
186 // ----------------------------------------------------------------------------- |
|
187 bool LDConfigParser::tokenCompare (int inPos, const char* sOther) |
|
188 { str tok; |
|
189 |
|
190 if (!getToken (tok, inPos)) |
|
191 return false; |
|
192 |
|
193 return (tok == sOther); |
|
194 } |
|