Wed, 08 Jun 2022 20:41:21 +0300
Refactor colors.cpp/.h
24 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
0 | 19 | #pragma once |
2 | 20 | #include <QString> |
21 | #include <QVector> | |
22 | #include <QSet> | |
96 | 23 | #include <QDebug> |
188
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
148
diff
changeset
|
24 | #include <compare> |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
25 | #include <memory> |
3 | 26 | #include "basics.h" |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
27 | #include "utility.h" |
55 | 28 | #include "geometry.h" |
63
f7dd937667a5
omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
29 | #include "functional.h" |
2 | 30 | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
31 | namespace settingGroups |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
32 | { |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
33 | // List of setting groups |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
34 | constexpr char mainwindow[] = "mainwindow"; |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
35 | } |
21 | 36 | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
37 | constexpr std::size_t operator""_z(const unsigned long long int x) |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
38 | { |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
39 | return static_cast<std::size_t>(x); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
40 | } |
26 | 41 | |
42 | inline QString operator""_q(const char* string, const unsigned long int length) | |
43 | { | |
44 | Q_UNUSED(length) | |
45 | return QString{string}; | |
46 | } | |
47 | ||
48 | inline QPointF pointToPointF(const QPoint& point) | |
49 | { | |
50 | return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())}; | |
51 | } | |
52 | ||
53 | inline QPoint pointFToPoint(const QPointF& point) | |
54 | { | |
55 | return {static_cast<int>(std::round(point.x())), static_cast<int>(std::round(point.y()))}; | |
56 | } | |
57 | ||
113 | 58 | inline QSizeF sizeToSizeF(const QSize& size) |
59 | { | |
60 | return {static_cast<qreal>(size.width()), static_cast<qreal>(size.height())}; | |
61 | } | |
62 | ||
26 | 63 | /** |
64 | * \brief Hints to the specified vector that a certain amount of new elements are going to be added. | |
65 | * \param vector vector to consider | |
66 | * \param amount amount of new elements to expect | |
67 | */ | |
68 | template<typename T> | |
69 | void reserveMore(std::vector<T>& vector, std::size_t amount) | |
70 | { | |
71 | vector.reserve(vector.size() + amount); | |
72 | } | |
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
73 | |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
74 | inline QString vectorToString(const glm::vec2& vec) |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
75 | { |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
76 | return "(%1, %2)"_q |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
77 | .arg(toDouble(vec.x)) |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
78 | .arg(toDouble(vec.y)); |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
79 | } |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
80 | |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
81 | inline QString vectorToString(const glm::vec3& vec) |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
82 | { |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
83 | return "(%1, %2, %3)"_q |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
84 | .arg(toDouble(vec.x)) |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
85 | .arg(toDouble(vec.y)) |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
86 | .arg(toDouble(vec.z)); |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
87 | } |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
88 | |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
89 | inline QString vectorToString(const glm::vec4& vec) |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
90 | { |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
91 | return "(%1, %2, %3, %4)"_q |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
92 | .arg(toDouble(vec.x)) |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
93 | .arg(toDouble(vec.y)) |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
94 | .arg(toDouble(vec.z)) |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
95 | .arg(toDouble(vec.w)); |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
96 | } |
97 | 97 | |
98 | template<typename K, typename V> | |
99 | struct KeyValuePair | |
100 | { | |
101 | K key; | |
102 | V value; | |
103 | }; | |
104 | ||
105 | template<typename K, typename V, typename IteratorType> | |
106 | struct MapItemsIterator : IteratorType | |
107 | { | |
108 | template<typename... Ts> | |
109 | MapItemsIterator(Ts&&... args) : IteratorType{args...} {} | |
110 | auto operator*() const | |
111 | { | |
101
910890292639
added references to items()
Teemu Piippo <teemu@hecknology.net>
parents:
97
diff
changeset
|
112 | return KeyValuePair<const K&, V&>{this->key(), this->value()}; |
97 | 113 | } |
114 | }; | |
115 | ||
116 | template<typename K, typename V, typename MapType, typename IteratorType> | |
117 | struct MapItems | |
118 | { | |
119 | MapType& map; | |
120 | IteratorType begin() | |
121 | { | |
122 | return IteratorType(this->map.begin()); | |
123 | } | |
124 | ||
125 | IteratorType end() | |
126 | { | |
127 | return IteratorType(this->map.end()); | |
128 | } | |
129 | }; | |
130 | ||
131 | /* | |
132 | * Python's dict.items for QMap: use in a for loop to iterate a map to | |
133 | * get both keys and values. Iteration yields KeyValuePairs. | |
134 | */ | |
135 | template<typename K, typename V> | |
136 | auto items(const QMap<K, V>& map) | |
137 | { | |
138 | return MapItems< | |
139 | const K&, | |
140 | const V&, | |
141 | const QMap<K, V>, | |
142 | MapItemsIterator<K, const V, typename QMap<K, V>::const_iterator> | |
143 | >{map}; | |
144 | } | |
145 | ||
146 | template<typename K, typename V> | |
147 | auto items(QMap<K, V>& map) | |
148 | { | |
149 | return MapItems< | |
150 | const K&, | |
151 | V&, | |
152 | QMap<K, V>, | |
153 | MapItemsIterator<K, const V, typename QMap<K, V>::iterator> | |
154 | >{map}; | |
155 | } | |
132
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
156 | |
148 | 157 | template<typename T, typename IdentifierType> |
158 | struct TypeValue | |
159 | { | |
160 | T value; | |
161 | bool operator==(TypeValue<T, IdentifierType> other) const | |
162 | { | |
163 | return value == other.value; | |
164 | } | |
165 | bool operator!=(TypeValue<T, IdentifierType> other) const | |
166 | { | |
167 | return value != other.value; | |
168 | } | |
169 | bool operator<(TypeValue<T, IdentifierType> other) const | |
170 | { | |
171 | return value < other.value; | |
172 | } | |
173 | bool operator>(TypeValue<T, IdentifierType> other) const | |
174 | { | |
175 | return value > other.value; | |
176 | } | |
177 | bool operator<=(TypeValue<T, IdentifierType> other) const | |
178 | { | |
179 | return value <= other.value; | |
180 | } | |
181 | bool operator>=(TypeValue<T, IdentifierType> other) const | |
182 | { | |
183 | return value >= other.value; | |
184 | } | |
185 | }; | |
186 | ||
187 | template<typename T, typename R> | |
188 | int qHash(TypeValue<T, R> value) | |
189 | { | |
190 | return qHash(value.value); | |
191 | } | |
192 | ||
132
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
193 | /** |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
194 | * Iterates a @c glm::mat |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
195 | */ |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
196 | template<int X, int Y, typename T, glm::qualifier Q, typename Fn> |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
197 | void iter_matrix(const glm::mat<X, Y, T, Q>& matrix, Fn&& fn) |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
198 | { |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
199 | for (int i = 0; i < X; ++i) |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
200 | { |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
201 | for (int j = 0; j < Y; ++j) |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
202 | { |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
203 | fn(i, j, matrix[i][j]); |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
204 | } |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
205 | } |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
206 | } |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
207 | |
200 | 208 | inline QDataStream& operator<<(QDataStream& stream, const glm::vec3& vec) |
209 | { | |
210 | return stream << vec.x << vec.y << vec.z; | |
211 | } | |
212 | ||
213 | inline QDataStream& operator>>(QDataStream& stream, glm::vec3& vec) | |
214 | { | |
215 | return stream >> vec.x >> vec.y >> vec.z; | |
216 | } | |
132
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
217 | |
134
f77d2230e87c
Add remaining serialize methods
Teemu Piippo <teemu@hecknology.net>
parents:
132
diff
changeset
|
218 | template<int X, int Y, typename T, glm::qualifier Q> |
132
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
219 | QDataStream& operator<<(QDataStream& stream, const glm::mat<X, Y, T, Q>& mat) |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
220 | { |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
221 | iter_matrix(mat, [&stream](int, int, float x) |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
222 | { |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
223 | stream << x; |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
224 | }); |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
225 | return stream; |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
226 | } |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
227 | |
134
f77d2230e87c
Add remaining serialize methods
Teemu Piippo <teemu@hecknology.net>
parents:
132
diff
changeset
|
228 | template<int X, int Y, typename T, glm::qualifier Q> |
132
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
229 | QDataStream& operator>>(QDataStream& stream, glm::mat<X, Y, T, Q>& mat) |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
230 | { |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
231 | iter_matrix(mat, [&stream](int, int, float x) |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
232 | { |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
233 | stream >> x; |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
234 | }); |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
235 | return stream; |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
236 | } |
191
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
237 | |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
238 | template<std::size_t N, typename T> |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
239 | std::array<T, N> vectorToArray(const std::vector<T>& x) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
240 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
241 | std::array<T, N> result; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
242 | for (std::size_t i = 0; i < x.size() and i < N; i += 1) |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
243 | { |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
244 | result[i] = x[i]; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
245 | } |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
246 | return result; |
d355d4c52d51
made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents:
188
diff
changeset
|
247 | } |
200 | 248 | |
249 | template<typename T> | |
250 | std::optional<T> pointerToOptional(const T* p) | |
251 | { | |
252 | std::optional<T> result; | |
253 | if (p != nullptr) { | |
254 | result = *p; | |
255 | } | |
256 | return result; | |
257 | } | |
258 | ||
259 | template<typename T, typename R> | |
260 | void removeFromMap(std::map<T, R>& map, T&& key) | |
261 | { | |
262 | const auto it = map.find(key); | |
263 | if (it != map.end()) { | |
264 | map.erase(it); | |
265 | } | |
266 | } | |
267 | ||
205 | 268 | template<typename T> |
269 | using opt = std::optional<T>; | |
270 | ||
200 | 271 | // some magic code from https://en.cppreference.com/w/cpp/utility/variant/visit |
272 | // for use with std::visit | |
273 | template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; | |
274 | template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; |