src/linetypes/object.h

changeset 200
ca23936b455b
parent 199
6988973515d2
child 201
5d201ee4a9c3
equal deleted inserted replaced
199:6988973515d2 200:ca23936b455b
1 #pragma once
2 #include <QPointF>
3 #include <QString>
4 #include <QStringView>
5 #include "main.h"
6 #include "colors.h"
7 #include "gl/common.h"
8 #include "linetypes/propertygenerics.h"
9
10 class Model;
11
12 namespace ldraw
13 {
14 struct GetPolygonsContext;
15 class Object;
16 class ColoredObject;
17 class Empty;
18 class UnhandledProperty;
19 }
20
21 class DocumentManager;
22
23 struct ldraw::GetPolygonsContext
24 {
25 ::ModelId modelId;
26 ::DocumentManager* documents;
27 };
28
29 class ldraw::Object
30 {
31 public:
32 enum class SetPropertyResult
33 {
34 Success = 0,
35 PropertyNotHandled
36 };
37 /**
38 * @brief Enumerates different object types
39 */
40 enum class Type
41 {
42 Empty,
43 Comment,
44 MetaCommand,
45 ErrorLine,
46 SubfileReference,
47 EdgeLine,
48 ConditionalEdge,
49 Triangle,
50 Quadrilateral,
51 CircularPrimitive,
52 };
53 friend bool handled(SetPropertyResult result)
54 {
55 return result == SetPropertyResult::Success;
56 }
57 class BadPointIndex : public std::exception
58 {
59 };
60 Object();
61 Object(const Object&) = delete;
62 virtual ~Object();
63 const id_t id;
64 virtual bool hasColor() const;
65 virtual QVariant getProperty(Property id) const;
66 template<ldraw::Property property>
67 PropertyType<property> getProperty() const;
68 template<ldraw::Property property>
69 SetPropertyResult setProperty(const PropertyType<property>& value);
70 SetPropertyResult setProperty(const PropertyKeyValue& pair);
71 virtual QString textRepresentation() const = 0;
72 virtual QBrush textRepresentationForeground() const;
73 virtual QBrush textRepresentationBackground() const;
74 virtual QFont textRepresentationFont() const;
75 virtual void getPolygons(std::vector<gl::Polygon>& polygons, GetPolygonsContext* context) const;
76 virtual void invert(GetPolygonsContext*) {}
77 virtual int numPoints() const { return 0; }
78 virtual const glm::vec3& getPoint(int index) const;
79 virtual QDataStream& serialize(QDataStream& stream) const;
80 virtual QDataStream& deserialize(QDataStream& stream);
81 virtual Type typeIdentifier() const = 0;
82 virtual QString toLDrawCode() const = 0;
83 virtual QString iconName() const;
84 virtual QString typeName() const = 0;
85
86 protected:
87 template<Property property, typename Function>
88 void handle(SetPropertyResult* result, const PropertyKeyValue& pair, Function function);
89 virtual void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair);
90 };
91
92 /**
93 * @brief Tests whether the object is exactly of the specified type
94 * @tparam R Type of LDraw line type object to test for
95 * @param object Object to test
96 * @returns whether the type of the object specified by @c id is the same type as R. Returns false if it is a subclass.
97 */
98 template<typename R>
99 bool isA(const ldraw::Object* object)
100 {
101 const std::type_info& a = typeid(*object);
102 const std::type_info& b = typeid(R);
103 return a == b;
104 }
105
106 template<ldraw::Property property>
107 ldraw::Object::SetPropertyResult ldraw::Object::setProperty(const ldraw::PropertyType<property>& value)
108 {
109 SetPropertyResult result = SetPropertyResult::PropertyNotHandled;
110 this->setProperty(&result, PropertyKeyValue{property, QVariant::fromValue(value)});
111 return result;
112 }
113
114 template<ldraw::Property property, typename Function>
115 void ldraw::Object::handle(SetPropertyResult* result, const PropertyKeyValue& pair, Function function)
116 {
117 if (pair.key == property)
118 {
119 function(pair.value.value<ldraw::PropertyType<property>>());
120 *result = SetPropertyResult::Success;
121 }
122 }
123
124 template<ldraw::Property property>
125 ldraw::PropertyType<property> ldraw::Object::getProperty() const
126 {
127 return this->getProperty(property).value<ldraw::PropertyType<property>>();
128 }
129
130 class ldraw::ColoredObject : public Object
131 {
132 public:
133 ColoredObject(const Color colorIndex = ldraw::MAIN_COLOR);
134 bool hasColor() const override final;
135 QVariant getProperty(Property id) const override;
136 QDataStream &serialize(QDataStream& stream) const override;
137 QDataStream& deserialize(QDataStream& stream) override;
138 Color colorIndex = ldraw::MAIN_COLOR;
139 protected:
140 void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) override;
141 };
142
143 /**
144 * @brief Represents an empty line.
145 */
146 class ldraw::Empty : public Object
147 {
148 QString textRepresentation() const override;
149 Type typeIdentifier() const override;
150 QString toLDrawCode() const override;
151 QString typeName() const override;
152 };

mercurial