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