diff -r 40e2940605a3 -r 4bec0525ef1b src/linetypes/object.h --- a/src/linetypes/object.h Wed Mar 18 17:11:23 2020 +0200 +++ b/src/linetypes/object.h Thu Mar 19 21:06:06 2020 +0200 @@ -30,12 +30,12 @@ { Color, // Color of the object Text, // Text contained in a comment - Point1, // First vertex in a polygon or edge line - Point2, // Second vertex in a polygon or edge line - Point3, // Third vertex in a polygon - Point4, // Fourth vertex in a quadrilateral - ControlPoint1, // First control point in a conditional edge line - ControlPoint2, // Second control point in a conditional edge line + Point0, // First vertex in a polygon or edge line + Point1, // Second vertex in a polygon or edge line + Point2, // Third vertex in a polygon + Point3, // Fourth vertex in a quadrilateral + ControlPoint0, // First control point in a conditional edge line + ControlPoint1, // Second control point in a conditional edge line Transformation, // 4x4 transformation matrix of a subfile reference ReferenceName, // Subfile reference name IsInverted, // Whether or not the object has been inverted with BFC INVERTNEXT @@ -57,30 +57,49 @@ template using PropertyType_t = typename PropertyType::type; + + struct PropertyKeyValue + { + Property key; + QVariant value; + }; + + constexpr Property pointProperty(int n) + { + Q_ASSERT(n >= 0 and n < 4); + return static_cast(static_cast(Property::Point0) + n); + } } LDFORGE_DEFINE_PROPERTY_TYPE(Color, int) LDFORGE_DEFINE_PROPERTY_TYPE(Text, QString) +LDFORGE_DEFINE_PROPERTY_TYPE(Point0, glm::vec3) LDFORGE_DEFINE_PROPERTY_TYPE(Point1, glm::vec3) LDFORGE_DEFINE_PROPERTY_TYPE(Point2, glm::vec3) LDFORGE_DEFINE_PROPERTY_TYPE(Point3, glm::vec3) -LDFORGE_DEFINE_PROPERTY_TYPE(Point4, glm::vec3) +LDFORGE_DEFINE_PROPERTY_TYPE(ControlPoint0, glm::vec3) LDFORGE_DEFINE_PROPERTY_TYPE(ControlPoint1, glm::vec3) -LDFORGE_DEFINE_PROPERTY_TYPE(ControlPoint2, glm::vec3) LDFORGE_DEFINE_PROPERTY_TYPE(Transformation, glm::mat4) LDFORGE_DEFINE_PROPERTY_TYPE(ReferenceName, QString) LDFORGE_DEFINE_PROPERTY_TYPE(IsInverted, bool) LDFORGE_DEFINE_PROPERTY_TYPE(ErrorMessage, QString) +#define LDRAW_OBJECT_HANDLE_SET_PROPERTY(PROPERTY, HANDLER) \ + {this->handle(result, pair, \ + [&](const ldraw::PropertyType_t& value) HANDLER);} + class ldraw::Object { public: enum class SetPropertyResult { Success = 0, - PropertyNotHandled, - InvalidValue + PropertyNotHandled }; + friend bool handled(SetPropertyResult result) + { + return result == SetPropertyResult::Success; + } class BadPointIndex : public std::exception { }; @@ -88,10 +107,11 @@ Object(const Object&) = delete; virtual ~Object(); const id_t id; - //virtual void toString(QTextStream &out) = 0; virtual bool hasColor() const; virtual QVariant getProperty(Property id) const; - virtual SetPropertyResult setProperty(Property id, const QVariant& value); + template + SetPropertyResult setProperty(const PropertyType_t& value); + SetPropertyResult setProperty(const PropertyKeyValue& pair); virtual QString textRepresentation() const = 0; virtual QBrush textRepresentationForeground() const; virtual QBrush textRepresentationBackground() const; @@ -100,16 +120,39 @@ virtual void invert() {} virtual int numPoints() const { return 0; } virtual const glm::vec3& getPoint(int index) const; +protected: + template + void handle(SetPropertyResult* result, const PropertyKeyValue& pair, Function function); + virtual void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair); }; +template +ldraw::Object::SetPropertyResult ldraw::Object::setProperty(const PropertyType_t& value) +{ + SetPropertyResult result = SetPropertyResult::PropertyNotHandled; + this->setProperty(&result, PropertyKeyValue{property, QVariant::fromValue(value)}); + return result; +} + +template +void ldraw::Object::handle(SetPropertyResult* result, const PropertyKeyValue& pair, Function function) +{ + if (pair.key == property) + { + function(pair.value.value>()); + *result = SetPropertyResult::Success; + } +} + class ldraw::ColoredObject : public Object { public: ColoredObject(const Color colorIndex = ldraw::mainColor); bool hasColor() const override final; QVariant getProperty(Property id) const override; - SetPropertyResult setProperty(Property id, const QVariant& value) override; Color colorIndex = ldraw::mainColor; +protected: + void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) override; }; /**