Tue, 28 Sep 2021 23:07:23 +0300
Use QSaveFile to save the file more safely
89 | 1 | #pragma once |
2 | #include "object.h" | |
3 | #include "widgets/vec3editor.h" | |
4 | #include "model.h" | |
5 | #include "modeleditcontext.h" | |
6 | ||
7 | namespace ldraw | |
8 | { | |
9 | template<int N, typename> | |
10 | class PolygonObject; | |
11 | } | |
12 | ||
13 | template<int N, typename = std::enable_if_t<(N > 0 and N <= 4)>> | |
14 | class ldraw::PolygonObject : public ColoredObject | |
15 | { | |
16 | public: | |
17 | using BaseClass = ColoredObject; | |
18 | PolygonObject(const std::array<glm::vec3, N>& points, const Color color) : | |
19 | ColoredObject{color}, | |
20 | points{points} {} | |
21 | int numPoints() const override | |
22 | { | |
23 | return N; | |
24 | } | |
25 | const glm::vec3& getPoint(int index) const override | |
26 | { | |
27 | Q_ASSERT(index >= 0 and index < N); | |
28 | return this->points[index]; | |
29 | } | |
30 | QVariant getProperty(const Property id) const override | |
31 | { | |
32 | switch (id) | |
33 | { | |
34 | case Property::Point0: | |
35 | return QVariant::fromValue(points[0]); | |
36 | case Property::Point1: | |
37 | return QVariant::fromValue(points[1]); | |
38 | case Property::Point2: | |
39 | if (N >= 3) | |
40 | { | |
41 | return QVariant::fromValue(points[2]); | |
42 | } | |
43 | break; | |
44 | case Property::Point3: | |
45 | if (N >= 4) | |
46 | { | |
47 | return QVariant::fromValue(points[3]); | |
48 | } | |
49 | break; | |
50 | default: | |
51 | break; | |
52 | } | |
53 | return BaseClass::getProperty(id); | |
54 | } | |
93 | 55 | void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) override |
89 | 56 | { |
57 | LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point0, {points[0] = value;}) | |
58 | LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point1, {points[1] = value;}) | |
59 | if constexpr (N >= 3) | |
60 | { | |
61 | LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point2, {points[2] = value;}) | |
62 | } | |
63 | if constexpr (N >= 4) | |
64 | { | |
65 | LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point3, {points[3] = value;}) | |
66 | } | |
67 | ColoredObject::setProperty(result, pair); | |
68 | } | |
132
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
69 | QDataStream &serialize(QDataStream& stream) const override |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
70 | { |
134
f77d2230e87c
Add remaining serialize methods
Teemu Piippo <teemu@hecknology.net>
parents:
132
diff
changeset
|
71 | ColoredObject::serialize(stream); |
132
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
72 | for (const glm::vec3& point : this->points) |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
73 | { |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
74 | stream << point; |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
75 | } |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
76 | return stream; |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
77 | } |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
78 | QDataStream& deserialize(QDataStream& stream) override |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
79 | { |
134
f77d2230e87c
Add remaining serialize methods
Teemu Piippo <teemu@hecknology.net>
parents:
132
diff
changeset
|
80 | ColoredObject::deserialize(stream); |
132
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
81 | for (glm::vec3& point : this->points) |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
82 | { |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
83 | stream >> point; |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
84 | } |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
85 | return stream; |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
93
diff
changeset
|
86 | } |
89 | 87 | std::array<glm::vec3, N> points; |
88 | }; |