Thu, 04 Jan 2018 20:23:21 +0200
reduce main.cpp
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> |
998 | 20 | #include <QMessageBox> |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | #include "main.h" |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | #include "colors.h" |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | #include "ldDocument.h" |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | #include "miscallenous.h" |
962
a4b463a7ee82
Rename MainWindow files
Teemu Piippo <crimsondusk64@gmail.com>
parents:
952
diff
changeset
|
25 | #include "mainwindow.h" |
998 | 26 | #include "documentmanager.h" |
1012 | 27 | #include "ldpaths.h" |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | |
998 | 29 | static ColorData* colorData = nullptr; |
946 | 30 | |
998 | 31 | void initColors() |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | { |
1217 | 33 | print("Initializing color information.\n"); |
998 | 34 | static ColorData colors; |
35 | colorData = &colors; | |
946 | 36 | } |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | |
946 | 38 | bool LDColor::isValid() const |
39 | { | |
998 | 40 | if (isLDConfigColor() and data().name.isEmpty()) |
946 | 41 | return false; // Unknown LDConfig color |
42 | ||
43 | 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
|
44 | } |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
45 | |
946 | 46 | 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
|
47 | { |
1217 | 48 | return colorData->contains(index()); |
998 | 49 | } |
50 | ||
51 | const ColorData::Entry& LDColor::data() const | |
52 | { | |
1217 | 53 | 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
|
54 | } |
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
|
55 | |
946 | 56 | 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
|
57 | { |
946 | 58 | if (isDirect()) |
1217 | 59 | return "0x" + QString::number(index(), 16).toUpper(); |
946 | 60 | else if (isLDConfigColor()) |
998 | 61 | return data().name; |
946 | 62 | else if (index() == -1) |
63 | return "null color"; | |
64 | else | |
65 | return ""; | |
66 | } | |
67 | ||
68 | QString LDColor::hexcode() const | |
69 | { | |
70 | return faceColor().name(); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
71 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
72 | |
946 | 73 | QColor LDColor::faceColor() const |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | { |
946 | 75 | if (isDirect()) |
76 | { | |
77 | QColor color; | |
1217 | 78 | color.setRed((index() & 0x0FF0000) >> 16); |
79 | color.setGreen((index() & 0x000FF00) >> 8); | |
80 | color.setBlue(index() & 0x00000FF); | |
946 | 81 | |
82 | if (index() >= 0x3000000) | |
1217 | 83 | color.setAlpha(128); |
946 | 84 | |
85 | return color; | |
86 | } | |
87 | else if (isLDConfigColor()) | |
88 | { | |
998 | 89 | return data().faceColor; |
946 | 90 | } |
91 | else | |
92 | { | |
93 | return Qt::black; | |
94 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
95 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | |
946 | 97 | QColor LDColor::edgeColor() const |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
98 | { |
946 | 99 | if (isDirect()) |
1217 | 100 | return ::luma(faceColor()) < 48 ? Qt::white : Qt::black; |
946 | 101 | else if (isLDConfigColor()) |
998 | 102 | return data().edgeColor; |
946 | 103 | else |
104 | return Qt::black; | |
105 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | |
946 | 107 | int LDColor::luma() const |
108 | { | |
1217 | 109 | return ::luma(faceColor()); |
946 | 110 | } |
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
|
111 | |
946 | 112 | int LDColor::edgeLuma() const |
113 | { | |
1217 | 114 | return ::luma(edgeColor()); |
946 | 115 | } |
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
|
116 | |
946 | 117 | qint32 LDColor::index() const |
118 | { | |
119 | return m_index; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
120 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
121 | |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
122 | QString LDColor::indexString() const |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
123 | { |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
124 | if (isDirect()) |
1217 | 125 | return "0x" + QString::number(index(), 16).toUpper(); |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
126 | |
1217 | 127 | return QString::number(index()); |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
128 | } |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
129 | |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
130 | bool LDColor::isDirect() const |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
131 | { |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
132 | return index() >= 0x02000000; |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
133 | } |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
134 | |
1217 | 135 | int luma(const QColor& col) |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
136 | { |
1217 | 137 | 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
|
138 | } |
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
|
139 | |
998 | 140 | 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
|
141 | { |
998 | 142 | if (colorData == nullptr) |
143 | colorData = this; | |
144 | ||
145 | // Initialize main and edge colors, they're special like that. | |
146 | m_data[MainColor].faceColor = | |
147 | m_data[MainColor].hexcode = "#AAAAAA"; | |
148 | m_data[MainColor].edgeColor = Qt::black; | |
149 | m_data[MainColor].name = "Main color"; | |
150 | m_data[EdgeColor].faceColor = | |
151 | m_data[EdgeColor].edgeColor = | |
152 | m_data[EdgeColor].hexcode = "#000000"; | |
153 | m_data[EdgeColor].name = "Edge color"; | |
154 | loadFromLdconfig(); | |
155 | } | |
156 | ||
157 | ColorData::~ColorData() | |
158 | { | |
159 | if (colorData == this) | |
160 | colorData = nullptr; | |
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
|
161 | } |
946 | 162 | |
1217 | 163 | bool ColorData::contains(int code) const |
946 | 164 | { |
998 | 165 | return code >= 0 and code < EntryCount; |
166 | } | |
167 | ||
1217 | 168 | const ColorData::Entry& ColorData::get(int code) const |
998 | 169 | { |
1217 | 170 | if (not contains(code)) |
171 | throw std::runtime_error("Attempted to get non-existant color information"); | |
946 | 172 | |
998 | 173 | return m_data[code]; |
174 | } | |
175 | ||
176 | void ColorData::loadFromLdconfig() | |
177 | { | |
1012 | 178 | QString path = LDPaths::ldConfigPath(); |
1217 | 179 | QFile fp(path); |
998 | 180 | |
1217 | 181 | if (not fp.open(QIODevice::ReadOnly)) |
946 | 182 | { |
1217 | 183 | QMessageBox::critical(nullptr, "Error", "Unable to open LDConfig.ldr for parsing: " + fp.errorString()); |
946 | 184 | return; |
185 | } | |
186 | ||
998 | 187 | // TODO: maybe LDConfig can be loaded as a Document? Or would that be overkill? |
188 | while (not fp.atEnd()) | |
946 | 189 | { |
1217 | 190 | QString line = QString::fromUtf8(fp.readLine()); |
946 | 191 | |
192 | if (line.isEmpty() or line[0] != '0') | |
193 | continue; // empty or illogical | |
194 | ||
1217 | 195 | line.remove('\r'); |
196 | line.remove('\n'); | |
946 | 197 | |
198 | // Parse the line | |
1217 | 199 | LDConfigParser parser(line, ' '); |
998 | 200 | QString name; |
201 | QString facename; | |
202 | QString edgename; | |
203 | QString codestring; | |
946 | 204 | |
205 | // Check 0 !COLOUR, parse the name | |
1217 | 206 | if (not parser.compareToken(0, "0") or not parser.compareToken(1, "!COLOUR") or not parser.getToken(name, 2)) |
946 | 207 | continue; |
208 | ||
209 | // Replace underscores in the name with spaces for readability | |
1217 | 210 | name.replace("_", " "); |
946 | 211 | |
1217 | 212 | if (not parser.parseTag("CODE", codestring)) |
946 | 213 | continue; |
214 | ||
215 | bool ok; | |
1217 | 216 | int code = codestring.toShort(&ok); |
946 | 217 | |
1217 | 218 | if (not ok or not contains(code)) |
946 | 219 | continue; |
220 | ||
1217 | 221 | if (not parser.parseTag("VALUE", facename) or not parser.parseTag("EDGE", edgename)) |
946 | 222 | continue; |
223 | ||
224 | // Ensure that our colors are correct | |
1217 | 225 | QColor faceColor(facename); |
226 | QColor edgeColor(edgename); | |
946 | 227 | |
228 | if (not faceColor.isValid() or not edgeColor.isValid()) | |
229 | continue; | |
230 | ||
998 | 231 | Entry& entry = m_data[code]; |
946 | 232 | entry.name = name; |
233 | entry.faceColor = faceColor; | |
234 | entry.edgeColor = edgeColor; | |
235 | entry.hexcode = facename; | |
998 | 236 | |
1217 | 237 | if (parser.parseTag("ALPHA", codestring)) |
238 | entry.faceColor.setAlpha(qBound(0, codestring.toInt(), 255)); | |
946 | 239 | } |
240 | } | |
241 | ||
242 | // ============================================================================= | |
243 | // | |
1217 | 244 | LDConfigParser::LDConfigParser(QString inText, char sep) |
946 | 245 | { |
1217 | 246 | m_tokens = inText.split(sep, QString::SkipEmptyParts); |
946 | 247 | m_pos = -1; |
248 | } | |
249 | ||
250 | // ============================================================================= | |
251 | // | |
1217 | 252 | bool LDConfigParser::getToken(QString& val, const int pos) |
946 | 253 | { |
254 | if (pos >= m_tokens.size()) | |
255 | return false; | |
256 | ||
257 | val = m_tokens[pos]; | |
258 | return true; | |
259 | } | |
260 | ||
261 | // ============================================================================= | |
262 | // | |
1217 | 263 | bool LDConfigParser::findToken(int& result, char const* needle, int args) |
946 | 264 | { |
1217 | 265 | for (int i = 0; i <(m_tokens.size() - args); ++i) |
946 | 266 | { |
267 | if (m_tokens[i] == needle) | |
268 | { | |
269 | result = i; | |
270 | return true; | |
271 | } | |
272 | } | |
273 | ||
274 | return false; | |
275 | } | |
276 | ||
277 | // ============================================================================= | |
278 | // | |
1217 | 279 | bool LDConfigParser::compareToken(int inPos, QString text) |
946 | 280 | { |
281 | QString tok; | |
282 | ||
1217 | 283 | if (not getToken(tok, inPos)) |
946 | 284 | return false; |
285 | ||
286 | return (tok == text); | |
287 | } | |
288 | ||
289 | // ============================================================================= | |
290 | // | |
291 | // Helper function for parseLDConfig | |
292 | // | |
1217 | 293 | bool LDConfigParser::parseTag(char const* tag, QString& val) |
946 | 294 | { |
295 | int pos; | |
296 | ||
297 | // Try find the token and get its position | |
1217 | 298 | if (not findToken(pos, tag, 1)) |
946 | 299 | return false; |
300 | ||
301 | // Get the token after it and store it into val | |
1217 | 302 | return getToken(val, pos + 1); |
946 | 303 | } |