src/linetypes/object.h

changeset 86
4bec0525ef1b
parent 81
62373840e33a
child 87
93ec4d630346
equal deleted inserted replaced
85:40e2940605a3 86:4bec0525ef1b
28 */ 28 */
29 enum class ldraw::Property 29 enum class ldraw::Property
30 { 30 {
31 Color, // Color of the object 31 Color, // Color of the object
32 Text, // Text contained in a comment 32 Text, // Text contained in a comment
33 Point1, // First vertex in a polygon or edge line 33 Point0, // First vertex in a polygon or edge line
34 Point2, // Second vertex in a polygon or edge line 34 Point1, // Second vertex in a polygon or edge line
35 Point3, // Third vertex in a polygon 35 Point2, // Third vertex in a polygon
36 Point4, // Fourth vertex in a quadrilateral 36 Point3, // Fourth vertex in a quadrilateral
37 ControlPoint1, // First control point in a conditional edge line 37 ControlPoint0, // First control point in a conditional edge line
38 ControlPoint2, // Second control point in a conditional edge line 38 ControlPoint1, // Second control point in a conditional edge line
39 Transformation, // 4x4 transformation matrix of a subfile reference 39 Transformation, // 4x4 transformation matrix of a subfile reference
40 ReferenceName, // Subfile reference name 40 ReferenceName, // Subfile reference name
41 IsInverted, // Whether or not the object has been inverted with BFC INVERTNEXT 41 IsInverted, // Whether or not the object has been inverted with BFC INVERTNEXT
42 ErrorMessage // For error lines, why parsing failed 42 ErrorMessage // For error lines, why parsing failed
43 }; 43 };
55 { 55 {
56 }; 56 };
57 57
58 template<ldraw::Property property> 58 template<ldraw::Property property>
59 using PropertyType_t = typename PropertyType<property>::type; 59 using PropertyType_t = typename PropertyType<property>::type;
60
61 struct PropertyKeyValue
62 {
63 Property key;
64 QVariant value;
65 };
66
67 constexpr Property pointProperty(int n)
68 {
69 Q_ASSERT(n >= 0 and n < 4);
70 return static_cast<Property>(static_cast<int>(Property::Point0) + n);
71 }
60 } 72 }
61 73
62 LDFORGE_DEFINE_PROPERTY_TYPE(Color, int) 74 LDFORGE_DEFINE_PROPERTY_TYPE(Color, int)
63 LDFORGE_DEFINE_PROPERTY_TYPE(Text, QString) 75 LDFORGE_DEFINE_PROPERTY_TYPE(Text, QString)
76 LDFORGE_DEFINE_PROPERTY_TYPE(Point0, glm::vec3)
64 LDFORGE_DEFINE_PROPERTY_TYPE(Point1, glm::vec3) 77 LDFORGE_DEFINE_PROPERTY_TYPE(Point1, glm::vec3)
65 LDFORGE_DEFINE_PROPERTY_TYPE(Point2, glm::vec3) 78 LDFORGE_DEFINE_PROPERTY_TYPE(Point2, glm::vec3)
66 LDFORGE_DEFINE_PROPERTY_TYPE(Point3, glm::vec3) 79 LDFORGE_DEFINE_PROPERTY_TYPE(Point3, glm::vec3)
67 LDFORGE_DEFINE_PROPERTY_TYPE(Point4, glm::vec3) 80 LDFORGE_DEFINE_PROPERTY_TYPE(ControlPoint0, glm::vec3)
68 LDFORGE_DEFINE_PROPERTY_TYPE(ControlPoint1, glm::vec3) 81 LDFORGE_DEFINE_PROPERTY_TYPE(ControlPoint1, glm::vec3)
69 LDFORGE_DEFINE_PROPERTY_TYPE(ControlPoint2, glm::vec3)
70 LDFORGE_DEFINE_PROPERTY_TYPE(Transformation, glm::mat4) 82 LDFORGE_DEFINE_PROPERTY_TYPE(Transformation, glm::mat4)
71 LDFORGE_DEFINE_PROPERTY_TYPE(ReferenceName, QString) 83 LDFORGE_DEFINE_PROPERTY_TYPE(ReferenceName, QString)
72 LDFORGE_DEFINE_PROPERTY_TYPE(IsInverted, bool) 84 LDFORGE_DEFINE_PROPERTY_TYPE(IsInverted, bool)
73 LDFORGE_DEFINE_PROPERTY_TYPE(ErrorMessage, QString) 85 LDFORGE_DEFINE_PROPERTY_TYPE(ErrorMessage, QString)
86
87 #define LDRAW_OBJECT_HANDLE_SET_PROPERTY(PROPERTY, HANDLER) \
88 {this->handle<ldraw::Property::PROPERTY>(result, pair, \
89 [&](const ldraw::PropertyType_t<ldraw::Property::PROPERTY>& value) HANDLER);}
74 90
75 class ldraw::Object 91 class ldraw::Object
76 { 92 {
77 public: 93 public:
78 enum class SetPropertyResult 94 enum class SetPropertyResult
79 { 95 {
80 Success = 0, 96 Success = 0,
81 PropertyNotHandled, 97 PropertyNotHandled
82 InvalidValue
83 }; 98 };
99 friend bool handled(SetPropertyResult result)
100 {
101 return result == SetPropertyResult::Success;
102 }
84 class BadPointIndex : public std::exception 103 class BadPointIndex : public std::exception
85 { 104 {
86 }; 105 };
87 Object(); 106 Object();
88 Object(const Object&) = delete; 107 Object(const Object&) = delete;
89 virtual ~Object(); 108 virtual ~Object();
90 const id_t id; 109 const id_t id;
91 //virtual void toString(QTextStream &out) = 0;
92 virtual bool hasColor() const; 110 virtual bool hasColor() const;
93 virtual QVariant getProperty(Property id) const; 111 virtual QVariant getProperty(Property id) const;
94 virtual SetPropertyResult setProperty(Property id, const QVariant& value); 112 template<ldraw::Property property>
113 SetPropertyResult setProperty(const PropertyType_t<property>& value);
114 SetPropertyResult setProperty(const PropertyKeyValue& pair);
95 virtual QString textRepresentation() const = 0; 115 virtual QString textRepresentation() const = 0;
96 virtual QBrush textRepresentationForeground() const; 116 virtual QBrush textRepresentationForeground() const;
97 virtual QBrush textRepresentationBackground() const; 117 virtual QBrush textRepresentationBackground() const;
98 virtual QFont textRepresentationFont() const; 118 virtual QFont textRepresentationFont() const;
99 virtual void getPolygons(std::vector<gl::Polygon>& polygons, GetPolygonsContext* context) const; 119 virtual void getPolygons(std::vector<gl::Polygon>& polygons, GetPolygonsContext* context) const;
100 virtual void invert() {} 120 virtual void invert() {}
101 virtual int numPoints() const { return 0; } 121 virtual int numPoints() const { return 0; }
102 virtual const glm::vec3& getPoint(int index) const; 122 virtual const glm::vec3& getPoint(int index) const;
123 protected:
124 template<Property property, typename Function>
125 void handle(SetPropertyResult* result, const PropertyKeyValue& pair, Function function);
126 virtual void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair);
103 }; 127 };
128
129 template<ldraw::Property property>
130 ldraw::Object::SetPropertyResult ldraw::Object::setProperty(const PropertyType_t<property>& value)
131 {
132 SetPropertyResult result = SetPropertyResult::PropertyNotHandled;
133 this->setProperty(&result, PropertyKeyValue{property, QVariant::fromValue(value)});
134 return result;
135 }
136
137 template<ldraw::Property property, typename Function>
138 void ldraw::Object::handle(SetPropertyResult* result, const PropertyKeyValue& pair, Function function)
139 {
140 if (pair.key == property)
141 {
142 function(pair.value.value<ldraw::PropertyType_t<property>>());
143 *result = SetPropertyResult::Success;
144 }
145 }
104 146
105 class ldraw::ColoredObject : public Object 147 class ldraw::ColoredObject : public Object
106 { 148 {
107 public: 149 public:
108 ColoredObject(const Color colorIndex = ldraw::mainColor); 150 ColoredObject(const Color colorIndex = ldraw::mainColor);
109 bool hasColor() const override final; 151 bool hasColor() const override final;
110 QVariant getProperty(Property id) const override; 152 QVariant getProperty(Property id) const override;
111 SetPropertyResult setProperty(Property id, const QVariant& value) override;
112 Color colorIndex = ldraw::mainColor; 153 Color colorIndex = ldraw::mainColor;
154 protected:
155 void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) override;
113 }; 156 };
114 157
115 /** 158 /**
116 * @brief Represents an empty line. 159 * @brief Represents an empty line.
117 */ 160 */

mercurial