src/ldconfig.cc

changeset 667
31540c1f22ea
parent 609
a8dc74a809c6
equal deleted inserted replaced
666:c595cfb4791c 667:31540c1f22ea
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013, 2014 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 <QFile>
20 #include "document.h"
21 #include "ldconfig.h"
22 #include "gui.h"
23 #include "misc.h"
24 #include "colors.h"
25
26 // =============================================================================
27 // Helper function for parseLDConfig
28 // -----------------------------------------------------------------------------
29 static bool parseLDConfigTag (LDConfigParser& pars, char const* tag, QString& val)
30 {
31 int pos;
32
33 // Try find the token and get its position
34 if (!pars.findToken (pos, tag, 1))
35 return false;
36
37 // Get the token after it and store it into val
38 return pars.getToken (val, pos + 1);
39 }
40
41 // =============================================================================
42 // -----------------------------------------------------------------------------
43 void parseLDConfig()
44 {
45 QFile* fp = openLDrawFile ("LDConfig.ldr", false);
46
47 if (!fp)
48 {
49 critical (QObject::tr ("Unable to open LDConfig.ldr for parsing."));
50 return;
51 }
52
53 // Read in the lines
54 while (fp->atEnd() == false)
55 {
56 QString line = QString::fromUtf8 (fp->readLine());
57
58 if (line.isEmpty() || line[0] != '0')
59 continue; // empty or illogical
60
61 line.remove ('\r');
62 line.remove ('\n');
63
64 // Parse the line
65 LDConfigParser pars (line, ' ');
66
67 int code = 0, alpha = 255;
68 QString name, facename, edgename, valuestr;
69
70 // Check 0 !COLOUR, parse the name
71 if (!pars.tokenCompare (0, "0") || !pars.tokenCompare (1, "!COLOUR") || !pars.getToken (name, 2))
72 continue;
73
74 // Replace underscores in the name with spaces for readability
75 name.replace ("_", " ");
76
77 // Get the CODE tag
78 if (!parseLDConfigTag (pars, "CODE", valuestr))
79 continue;
80
81 if (!numeric (valuestr))
82 continue; // not a number
83
84 // Ensure that the code is within [0 - 511]
85 bool ok;
86 code = valuestr.toShort (&ok);
87
88 if (!ok || code < 0 || code >= 512)
89 continue;
90
91 // VALUE and EDGE tags
92 if (!parseLDConfigTag (pars, "VALUE", facename) || !parseLDConfigTag (pars, "EDGE", edgename))
93 continue;
94
95 // Ensure that our colors are correct
96 QColor faceColor (facename),
97 edgeColor (edgename);
98
99 if (!faceColor.isValid() || !edgeColor.isValid())
100 continue;
101
102 // Parse alpha if given.
103 if (parseLDConfigTag (pars, "ALPHA", valuestr))
104 alpha = clamp (valuestr.toInt(), 0, 255);
105
106 LDColor* col = new LDColor;
107 col->name = name;
108 col->faceColor = faceColor;
109 col->edgeColor = edgeColor;
110 col->hexcode = facename;
111 col->faceColor.setAlpha (alpha);
112 col->index = code;
113 setColor (code, col);
114 }
115
116 fp->close();
117 fp->deleteLater();
118 }
119
120 // =============================================================================
121 // -----------------------------------------------------------------------------
122 LDConfigParser::LDConfigParser (QString inText, char sep)
123 {
124 m_tokens = inText.split (sep, QString::SkipEmptyParts);
125 m_pos = -1;
126 }
127
128 // =============================================================================
129 // -----------------------------------------------------------------------------
130 bool LDConfigParser::isAtBeginning()
131 {
132 return m_pos == -1;
133 }
134
135 // =============================================================================
136 // -----------------------------------------------------------------------------
137 bool LDConfigParser::isAtEnd()
138 {
139 return m_pos == m_tokens.size() - 1;
140 }
141
142 // =============================================================================
143 // -----------------------------------------------------------------------------
144 bool LDConfigParser::getToken (QString& val, const int pos)
145 {
146 if (pos >= m_tokens.size())
147 return false;
148
149 val = m_tokens[pos];
150 return true;
151 }
152
153 // =============================================================================
154 // -----------------------------------------------------------------------------
155 bool LDConfigParser::getNextToken (QString& val)
156 {
157 return getToken (val, ++m_pos);
158 }
159
160 // =============================================================================
161 // -----------------------------------------------------------------------------
162 bool LDConfigParser::peekNextToken (QString& val)
163 {
164 return getToken (val, m_pos + 1);
165 }
166
167 // =============================================================================
168 // -----------------------------------------------------------------------------
169 bool LDConfigParser::findToken (int& result, char const* needle, int args)
170 {
171 for (int i = 0; i < (m_tokens.size() - args); ++i)
172 {
173 if (m_tokens[i] == needle)
174 {
175 result = i;
176 return true;
177 }
178 }
179
180 return false;
181 }
182
183 // =============================================================================
184 // -----------------------------------------------------------------------------
185 void LDConfigParser::rewind()
186 {
187 m_pos = -1;
188 }
189
190 // =============================================================================
191 // -----------------------------------------------------------------------------
192 void LDConfigParser::seek (int amount, bool rel)
193 {
194 m_pos = (rel ? m_pos : 0) + amount;
195 }
196
197 // =============================================================================
198 // -----------------------------------------------------------------------------
199 int LDConfigParser::getSize()
200 {
201 return m_tokens.size();
202 }
203
204 // =============================================================================
205 // -----------------------------------------------------------------------------
206 bool LDConfigParser::tokenCompare (int inPos, const char* sOther)
207 {
208 QString tok;
209
210 if (!getToken (tok, inPos))
211 return false;
212
213 return (tok == sOther);
214 }

mercurial