Sat, 23 Jun 2018 13:57:04 +0300
added icons for circular primitives and enhanced some existing ones
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 |
1326 | 3 | * Copyright (C) 2013 - 2018 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 | |
1308
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
19 | #include <QDir> |
998 | 20 | #include <QMessageBox> |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | #include "colors.h" |
1310 | 22 | #include "main.h" |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | |
1114
ffd49a28f49e
Moved some global constants into appropriate namespaces.
Teemu Piippo <teemu@hecknology.net>
parents:
1086
diff
changeset
|
24 | ColorData* LDColor::colorData = nullptr; |
1397
5d5c11af0268
cleaned up LDColor constructors
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
25 | const LDColor LDColor::nullColor {-1}; |
946 | 26 | |
1044 | 27 | /* |
28 | * Initializes the color information module. | |
29 | */ | |
1114
ffd49a28f49e
Moved some global constants into appropriate namespaces.
Teemu Piippo <teemu@hecknology.net>
parents:
1086
diff
changeset
|
30 | void LDColor::initColors() |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | { |
998 | 32 | static ColorData colors; |
1308
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
33 | colors.loadFromLdconfig(); |
1114
ffd49a28f49e
Moved some global constants into appropriate namespaces.
Teemu Piippo <teemu@hecknology.net>
parents:
1086
diff
changeset
|
34 | LDColor::colorData = &colors; |
946 | 35 | } |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | |
1044 | 37 | /* |
1153 | 38 | * Default-constructs an LDColor to 0(black). |
1044 | 39 | */ |
40 | LDColor::LDColor() : | |
1153 | 41 | m_index {0} {} |
1044 | 42 | |
43 | /* | |
44 | * Constructs an LDColor by index. | |
45 | */ | |
1153 | 46 | LDColor::LDColor(qint32 index) : |
47 | m_index {index} {} | |
1044 | 48 | |
49 | /* | |
50 | * Returns whether or not the color is valid. | |
51 | */ | |
946 | 52 | bool LDColor::isValid() const |
53 | { | |
998 | 54 | if (isLDConfigColor() and data().name.isEmpty()) |
946 | 55 | return false; // Unknown LDConfig color |
1044 | 56 | else |
57 | 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
|
58 | } |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | |
1044 | 60 | /* |
61 | * Returns whether or not this color is defined in LDConfig.ldr. | |
62 | * This is false for e.g. direct colors. | |
63 | */ | |
946 | 64 | 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
|
65 | { |
1044 | 66 | return colorData->contains(index()); |
998 | 67 | } |
68 | ||
1044 | 69 | /* |
70 | * Returns the ColorData entry for this color. | |
71 | */ | |
998 | 72 | const ColorData::Entry& LDColor::data() const |
73 | { | |
1044 | 74 | 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
|
75 | } |
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
|
76 | |
1044 | 77 | /* |
78 | * Returns the name of this color. | |
79 | */ | |
946 | 80 | 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
|
81 | { |
946 | 82 | if (isDirect()) |
1153 | 83 | return "0x" + QString::number(index(), 16).toUpper(); |
946 | 84 | else if (isLDConfigColor()) |
998 | 85 | return data().name; |
946 | 86 | else if (index() == -1) |
87 | return "null color"; | |
88 | else | |
1044 | 89 | return "unknown"; |
946 | 90 | } |
91 | ||
1044 | 92 | /* |
93 | * Returns the hexadecimal code of this color. | |
94 | */ | |
946 | 95 | QString LDColor::hexcode() const |
96 | { | |
97 | return faceColor().name(); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
98 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
99 | |
1044 | 100 | /* |
101 | * Returns the color used for surfaces. | |
102 | */ | |
946 | 103 | QColor LDColor::faceColor() const |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
104 | { |
946 | 105 | if (isDirect()) |
106 | { | |
1044 | 107 | // Direct color -- compute from the index. |
946 | 108 | QColor color; |
1044 | 109 | color.setRed((index() & 0x0FF0000) >> 16); |
110 | color.setGreen((index() & 0x000FF00) >> 8); | |
111 | color.setBlue(index() & 0x00000FF); | |
946 | 112 | |
113 | if (index() >= 0x3000000) | |
1044 | 114 | color.setAlpha(128); |
946 | 115 | |
116 | return color; | |
117 | } | |
118 | else if (isLDConfigColor()) | |
119 | { | |
998 | 120 | return data().faceColor; |
946 | 121 | } |
122 | else | |
123 | { | |
124 | return Qt::black; | |
125 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
126 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
127 | |
1044 | 128 | /* |
129 | * Returns the color used for edge lines. | |
130 | */ | |
946 | 131 | QColor LDColor::edgeColor() const |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
132 | { |
946 | 133 | if (isDirect()) |
1044 | 134 | return luma(faceColor()) < 48 ? Qt::white : Qt::black; |
946 | 135 | else if (isLDConfigColor()) |
998 | 136 | return data().edgeColor; |
946 | 137 | else |
138 | return Qt::black; | |
139 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
140 | |
1044 | 141 | /* |
142 | * Returns the index number of this color. | |
143 | */ | |
946 | 144 | qint32 LDColor::index() const |
145 | { | |
146 | return m_index; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
147 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
148 | |
1044 | 149 | /* |
150 | * Returns a string containing the preferred representation of the index. | |
151 | */ | |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
152 | QString LDColor::indexString() const |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
153 | { |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
154 | if (isDirect()) |
1044 | 155 | { |
156 | // Use hexadecimal notation for direct colors. | |
157 | return "0x" + QString::number(index(), 16).toUpper(); | |
158 | } | |
159 | else | |
160 | { | |
161 | return QString::number(index()); | |
162 | } | |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
163 | } |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
164 | |
1044 | 165 | /* |
166 | * Returns whether or not this color is a direct color. | |
167 | * Direct colors are picked by RGB value and are not defined in LDConfig.ldr. | |
168 | */ | |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
169 | bool LDColor::isDirect() const |
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 | return index() >= 0x02000000; |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
172 | } |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
173 | |
1044 | 174 | /* |
1174
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
175 | * Returns the LDraw color index for a direct color. |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
176 | */ |
1397
5d5c11af0268
cleaned up LDColor constructors
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
177 | LDColor LDColor::directColor(QColor color, bool transparent) |
1174
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
178 | { |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
179 | qint32 index = transparent ? 0x03000000 : 0x02000000; |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
180 | index |= color.red() << 16; |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
181 | index |= color.green() << 8; |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
182 | index |= color.blue(); |
1397
5d5c11af0268
cleaned up LDColor constructors
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
183 | return LDColor {index}; |
1174
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
184 | } |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
185 | |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
186 | /* |
1044 | 187 | * LDColors are hashed by their index. |
188 | */ | |
1031
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
189 | 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
|
190 | { |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
191 | return color.index(); |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
192 | } |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
193 | |
1044 | 194 | /* |
195 | * Calculates the luma-value for the given color. | |
196 | * c.f. https://en.wikipedia.org/wiki/Luma_(video) | |
197 | */ | |
1153 | 198 | int luma(const QColor& color) |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
199 | { |
1153 | 200 | 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
|
201 | } |
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
|
202 | |
1044 | 203 | /* |
204 | * Constructs the color data array. | |
205 | */ | |
998 | 206 | 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
|
207 | { |
998 | 208 | // Initialize main and edge colors, they're special like that. |
1397
5d5c11af0268
cleaned up LDColor constructors
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
209 | m_data[MainColor.index()].faceColor = "#AAAAAA"; |
5d5c11af0268
cleaned up LDColor constructors
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
210 | m_data[MainColor.index()].edgeColor = Qt::black; |
5d5c11af0268
cleaned up LDColor constructors
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
211 | m_data[MainColor.index()].name = "Main color"; |
5d5c11af0268
cleaned up LDColor constructors
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
212 | m_data[EdgeColor.index()].faceColor = Qt::black; |
5d5c11af0268
cleaned up LDColor constructors
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
213 | m_data[EdgeColor.index()].edgeColor = Qt::black; |
5d5c11af0268
cleaned up LDColor constructors
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
214 | m_data[EdgeColor.index()].name = "Edge color"; |
998 | 215 | } |
216 | ||
1044 | 217 | /* |
218 | * ColorData :: contains | |
219 | * | |
220 | * Returns whether or not the given color index is present in the array. | |
221 | */ | |
222 | bool ColorData::contains(int code) const | |
946 | 223 | { |
1065
c8ecddbd99e9
Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents:
1063
diff
changeset
|
224 | return code >= 0 and code < countof(m_data); |
998 | 225 | } |
226 | ||
1044 | 227 | /* |
228 | * Returns an entry in the color array. | |
229 | */ | |
230 | const ColorData::Entry& ColorData::get(int code) const | |
998 | 231 | { |
1044 | 232 | if (not contains(code)) |
233 | throw std::runtime_error {"Attempted to get non-existant color information"}; | |
946 | 234 | |
998 | 235 | return m_data[code]; |
236 | } | |
237 | ||
1044 | 238 | /* |
239 | * Loads color information from LDConfig.ldr. | |
240 | */ | |
998 | 241 | void ColorData::loadFromLdconfig() |
242 | { | |
1308
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
243 | *this = {}; |
998 | 244 | |
1323
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1310
diff
changeset
|
245 | for (const Library& library : config::libraries()) |
946 | 246 | { |
1308
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
247 | QDir dir {library.path}; |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
248 | |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
249 | if (dir.exists("LDConfig.ldr")) |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
250 | { |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
251 | QFile file {dir.filePath("LDConfig.ldr")}; |
946 | 252 | |
1308
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
253 | if (file.open(QIODevice::ReadOnly)) |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
254 | { |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
255 | this->loadFromFile(file); |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
256 | } |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
257 | else |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
258 | { |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
259 | QMessageBox::critical( |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
260 | nullptr, |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
261 | QObject::tr("Error"), |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
262 | format( |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
263 | QObject::tr("Unable to open LDConfig.ldr for parsing: %1"), |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
264 | file.errorString() |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
265 | ) |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
266 | ); |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
267 | } |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
268 | } |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
269 | } |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
270 | } |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
271 | |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
272 | void ColorData::loadFromFile(QIODevice& device) |
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
273 | { |
998 | 274 | // TODO: maybe LDConfig can be loaded as a Document? Or would that be overkill? |
1308
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
275 | while (not device.atEnd()) |
946 | 276 | { |
1308
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1174
diff
changeset
|
277 | QString line = QString::fromUtf8(device.readLine()); |
946 | 278 | |
279 | if (line.isEmpty() or line[0] != '0') | |
280 | continue; // empty or illogical | |
281 | ||
1044 | 282 | line.remove('\r'); |
283 | line.remove('\n'); | |
946 | 284 | |
285 | // Parse the line | |
1044 | 286 | LDConfigParser parser = {line}; |
998 | 287 | QString name; |
288 | QString facename; | |
289 | QString edgename; | |
290 | QString codestring; | |
946 | 291 | |
292 | // Check 0 !COLOUR, parse the name | |
1044 | 293 | if (not parser.compareToken(0, "0") or not parser.compareToken(1, "!COLOUR") or not parser.getToken(name, 2)) |
946 | 294 | continue; |
295 | ||
296 | // Replace underscores in the name with spaces for readability | |
1044 | 297 | name.replace("_", " "); |
946 | 298 | |
1153 | 299 | if (not parser.parseTag("CODE", codestring)) |
946 | 300 | continue; |
301 | ||
302 | bool ok; | |
1044 | 303 | int code = codestring.toShort(&ok); |
946 | 304 | |
1044 | 305 | if (not ok or not contains(code)) |
946 | 306 | continue; |
307 | ||
1044 | 308 | if (not parser.parseTag("VALUE", facename) or not parser.parseTag("EDGE", edgename)) |
946 | 309 | continue; |
310 | ||
311 | // Ensure that our colors are correct | |
1044 | 312 | QColor faceColor = {facename}; |
313 | QColor edgeColor = {edgename}; | |
946 | 314 | |
315 | if (not faceColor.isValid() or not edgeColor.isValid()) | |
316 | continue; | |
317 | ||
1044 | 318 | // Fill in the entry now. |
998 | 319 | Entry& entry = m_data[code]; |
946 | 320 | entry.name = name; |
321 | entry.faceColor = faceColor; | |
322 | entry.edgeColor = edgeColor; | |
998 | 323 | |
1044 | 324 | // If the alpha tag is present, fill in that too. |
325 | if (parser.parseTag("ALPHA", codestring)) | |
326 | entry.faceColor.setAlpha(qBound(0, codestring.toInt(), 255)); | |
946 | 327 | } |
328 | } | |
329 | ||
1044 | 330 | /* |
331 | * Constructs the LDConfig.ldr parser. | |
332 | */ | |
333 | LDConfigParser::LDConfigParser(QString inputText) | |
946 | 334 | { |
1153 | 335 | m_tokens = inputText.split(' ', QString::SkipEmptyParts); |
946 | 336 | } |
337 | ||
1044 | 338 | /* |
339 | * Returns whether or not there is a token at the given position. | |
340 | * If there is, fills in the value parameter with it. | |
341 | */ | |
342 | bool LDConfigParser::getToken(QString& tokenText, int position) | |
946 | 343 | { |
1065
c8ecddbd99e9
Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents:
1063
diff
changeset
|
344 | if (position >= countof(m_tokens)) |
1044 | 345 | { |
946 | 346 | return false; |
1044 | 347 | } |
348 | else | |
349 | { | |
350 | tokenText = m_tokens[position]; | |
351 | return true; | |
352 | } | |
946 | 353 | } |
354 | ||
1044 | 355 | /* |
356 | * Attempts to find the provided token in the parsed LDConfig.ldr line. | |
357 | * If found, fills in the first parameter with the position of the token. | |
358 | * | |
359 | * The args parameter specifies how many arguments (i.e. following tokens) the token needs to have. | |
360 | */ | |
361 | bool LDConfigParser::findToken(int& tokenPosition, QString needle, int args) | |
946 | 362 | { |
1065
c8ecddbd99e9
Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents:
1063
diff
changeset
|
363 | for (int i = 0; i < (countof(m_tokens) - args); ++i) |
946 | 364 | { |
365 | if (m_tokens[i] == needle) | |
366 | { | |
1044 | 367 | tokenPosition = i; |
946 | 368 | return true; |
369 | } | |
370 | } | |
371 | ||
372 | return false; | |
373 | } | |
374 | ||
1044 | 375 | /* |
376 | * Returns whether or not the token at the given position has the given text value. | |
377 | */ | |
1153 | 378 | bool LDConfigParser::compareToken(int position, QString text) |
946 | 379 | { |
1044 | 380 | QString token; |
1153 | 381 | return getToken(token, position) and (token == text); |
946 | 382 | } |
383 | ||
1044 | 384 | /* |
385 | * Finds an attribute in the line, and fills in its value. | |
386 | * For instance, if the line contains "ALPHA 128", this function can find the "128" for "ALPHA". | |
387 | * Returns whether or not the attribute was found. | |
388 | */ | |
1153 | 389 | bool LDConfigParser::parseTag(QString key, QString& value) |
946 | 390 | { |
1044 | 391 | int position; |
946 | 392 | |
393 | // Try find the token and get its position | |
1153 | 394 | if (not findToken(position, key, 1)) |
1044 | 395 | { |
946 | 396 | return false; |
1044 | 397 | } |
398 | else | |
399 | { | |
400 | // Get the token after it and store it in. | |
1153 | 401 | return getToken(value, position + 1); |
1044 | 402 | } |
403 | } |