src/colors.cpp

Tue, 15 Nov 2016 17:44:54 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 15 Nov 2016 17:44:54 +0200
changeset 1050
848ad1516e4d
parent 1044
b5106ca8705e
child 1062
4119185b56ca
permissions
-rw-r--r--

Removed the 'once' macro since it's not used

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

mercurial