src/main.h

Wed, 08 Jun 2022 20:41:21 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 08 Jun 2022 20:41:21 +0300
changeset 205
1a4342d80de7
parent 200
ca23936b455b
permissions
-rw-r--r--

Refactor colors.cpp/.h

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
18
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
2
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
20 #include <QString>
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
21 #include <QVector>
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
22 #include <QSet>
96
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
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
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents: 2
diff changeset
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
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
28 #include "geometry.h"
63
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
29 #include "functional.h"
2
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
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
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
41
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
42 inline QString operator""_q(const char* string, const unsigned long int length)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
43 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
44 Q_UNUSED(length)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
45 return QString{string};
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
46 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
47
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
48 inline QPointF pointToPointF(const QPoint& point)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
49 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
50 return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())};
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
51 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
52
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
53 inline QPoint pointFToPoint(const QPointF& point)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
54 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
55 return {static_cast<int>(std::round(point.x())), static_cast<int>(std::round(point.y()))};
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
56 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
57
113
c0d064521ee0 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 107
diff changeset
58 inline QSizeF sizeToSizeF(const QSize& size)
c0d064521ee0 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 107
diff changeset
59 {
c0d064521ee0 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 107
diff changeset
60 return {static_cast<qreal>(size.width()), static_cast<qreal>(size.height())};
c0d064521ee0 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 107
diff changeset
61 }
c0d064521ee0 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 107
diff changeset
62
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
63 /**
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
64 * \brief Hints to the specified vector that a certain amount of new elements are going to be added.
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
65 * \param vector vector to consider
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
66 * \param amount amount of new elements to expect
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
67 */
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
68 template<typename T>
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
69 void reserveMore(std::vector<T>& vector, std::size_t amount)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
70 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
71 vector.reserve(vector.size() + amount);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
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
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
97
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
98 template<typename K, typename V>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
99 struct KeyValuePair
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
100 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
101 K key;
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
102 V value;
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
103 };
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
104
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
105 template<typename K, typename V, typename IteratorType>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
106 struct MapItemsIterator : IteratorType
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
107 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
108 template<typename... Ts>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
109 MapItemsIterator(Ts&&... args) : IteratorType{args...} {}
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
110 auto operator*() const
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
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
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
113 }
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
114 };
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
115
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
116 template<typename K, typename V, typename MapType, typename IteratorType>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
117 struct MapItems
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
118 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
119 MapType& map;
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
120 IteratorType begin()
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
121 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
122 return IteratorType(this->map.begin());
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
123 }
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
124
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
125 IteratorType end()
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
126 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
127 return IteratorType(this->map.end());
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
128 }
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
129 };
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
130
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
131 /*
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
132 * Python's dict.items for QMap: use in a for loop to iterate a map to
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
133 * get both keys and values. Iteration yields KeyValuePairs.
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
134 */
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
135 template<typename K, typename V>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
136 auto items(const QMap<K, V>& map)
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
137 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
138 return MapItems<
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
139 const K&,
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
140 const V&,
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
141 const QMap<K, V>,
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
142 MapItemsIterator<K, const V, typename QMap<K, V>::const_iterator>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
143 >{map};
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
144 }
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
145
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
146 template<typename K, typename V>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
147 auto items(QMap<K, V>& map)
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
148 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
149 return MapItems<
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
150 const K&,
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
151 V&,
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
152 QMap<K, V>,
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
153 MapItemsIterator<K, const V, typename QMap<K, V>::iterator>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
154 >{map};
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
155 }
132
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
156
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
157 template<typename T, typename IdentifierType>
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
158 struct TypeValue
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
159 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
160 T value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
161 bool operator==(TypeValue<T, IdentifierType> other) const
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
162 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
163 return value == other.value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
164 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
165 bool operator!=(TypeValue<T, IdentifierType> other) const
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
166 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
167 return value != other.value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
168 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
169 bool operator<(TypeValue<T, IdentifierType> other) const
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
170 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
171 return value < other.value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
172 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
173 bool operator>(TypeValue<T, IdentifierType> other) const
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
174 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
175 return value > other.value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
176 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
177 bool operator<=(TypeValue<T, IdentifierType> other) const
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
178 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
179 return value <= other.value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
180 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
181 bool operator>=(TypeValue<T, IdentifierType> other) const
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
182 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
183 return value >= other.value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
184 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
185 };
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
186
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
187 template<typename T, typename R>
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
188 int qHash(TypeValue<T, R> value)
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
189 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
190 return qHash(value.value);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
191 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
208 inline QDataStream& operator<<(QDataStream& stream, const glm::vec3& vec)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
209 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
210 return stream << vec.x << vec.y << vec.z;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
211 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
212
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
213 inline QDataStream& operator>>(QDataStream& stream, glm::vec3& vec)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
214 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
215 return stream >> vec.x >> vec.y >> vec.z;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
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
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
248
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
249 template<typename T>
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
250 std::optional<T> pointerToOptional(const T* p)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
251 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
252 std::optional<T> result;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
253 if (p != nullptr) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
254 result = *p;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
255 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
256 return result;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
257 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
258
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
259 template<typename T, typename R>
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
260 void removeFromMap(std::map<T, R>& map, T&& key)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
261 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
262 const auto it = map.find(key);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
263 if (it != map.end()) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
264 map.erase(it);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
265 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
266 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
267
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
268 template<typename T>
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
269 using opt = std::optional<T>;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
270
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
271 // some magic code from https://en.cppreference.com/w/cpp/utility/variant/visit
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
272 // for use with std::visit
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
273 template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
274 template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

mercurial