diff -r 14e51640c189 -r 7abaf1d64719 src/linetypes/object.h --- a/src/linetypes/object.h Mon May 11 12:18:04 2020 +0300 +++ b/src/linetypes/object.h Mon May 11 12:18:59 2020 +0300 @@ -5,17 +5,17 @@ #include "main.h" #include "colors.h" #include "gl/common.h" +#include "linetypes/propertygenerics.h" + +class Model; namespace ldraw { struct GetPolygonsContext; - enum class Property; class Object; class ColoredObject; class Empty; class UnhandledProperty; - template - class PolygonObject; } class DocumentManager; @@ -25,67 +25,6 @@ ::DocumentManager* documents; }; -/** - * @brief Different properties that can be queried with getProperty - */ -enum class ldraw::Property -{ - Color, // Color of the object - Text, // Text contained in a comment - 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 - Transformation, // 4x4 transformation matrix of a subfile reference - ReferenceName, // Subfile reference name - IsInverted, // Whether or not the object has been inverted with BFC INVERTNEXT - ErrorMessage // For error lines, why parsing failed -}; - -// Mapping of properties to types -#define LDFORGE_DEFINE_PROPERTY_TYPE(PROPERTY, TYPE) \ - namespace ldraw { \ - template<> struct PropertyType { using type = TYPE; }; \ - } - -namespace ldraw -{ - template - struct PropertyType - { - }; - - 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(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: @@ -108,7 +47,7 @@ virtual bool hasColor() const; virtual QVariant getProperty(Property id) const; template - SetPropertyResult setProperty(const PropertyType_t& value); + SetPropertyResult setProperty(const PropertyType& value); SetPropertyResult setProperty(const PropertyKeyValue& pair); virtual QString textRepresentation() const = 0; virtual QBrush textRepresentationForeground() const; @@ -125,7 +64,7 @@ }; template -ldraw::Object::SetPropertyResult ldraw::Object::setProperty(const PropertyType_t& value) +ldraw::Object::SetPropertyResult ldraw::Object::setProperty(const ldraw::PropertyType& value) { SetPropertyResult result = SetPropertyResult::PropertyNotHandled; this->setProperty(&result, PropertyKeyValue{property, QVariant::fromValue(value)}); @@ -137,7 +76,7 @@ { if (pair.key == property) { - function(pair.value.value>()); + function(pair.value.value>()); *result = SetPropertyResult::Success; } } @@ -153,64 +92,6 @@ void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) override; }; -template -class ldraw::PolygonObject : public ColoredObject -{ -public: - PolygonObject(const std::array& points, const Color color) : - ColoredObject{color}, - points{points} {} - int numPoints() const override - { - return N; - } - const glm::vec3& getPoint(int index) const override - { - Q_ASSERT(index >= 0 and index < N); - return this->points[index]; - } - QVariant getProperty(const Property id) const override - { - switch (id) - { - case Property::Point0: - return QVariant::fromValue(points[0]); - case Property::Point1: - return QVariant::fromValue(points[1]); - case Property::Point2: - if (N >= 3) - { - return QVariant::fromValue(points[2]); - } - break; - case Property::Point3: - if (N >= 4) - { - return QVariant::fromValue(points[3]); - } - break; - default: - break; - } - return ColoredObject::getProperty(id); - } - void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) - { - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point0, {points[0] = value;}) - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point1, {points[1] = value;}) - if constexpr (N >= 3) - { - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point2, {points[2] = value;}) - } - if constexpr (N >= 4) - { - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point3, {points[2] = value;}) - } - ColoredObject::setProperty(result, pair); - } - std::array 0 and N <= 4), glm::vec3>, N> points; -}; - /** * @brief Represents an empty line. */