# HG changeset patch # User Teemu Piippo # Date 1589188739 -10800 # Node ID 7abaf1d64719e30ef707e891d259481ce7b4386a # Parent 14e51640c1895758bd68950e7a1ec40e327cf41c object editing diff -r 14e51640c189 -r 7abaf1d64719 CMakeLists.txt --- a/CMakeLists.txt Mon May 11 12:18:04 2020 +0300 +++ b/CMakeLists.txt Mon May 11 12:18:59 2020 +0300 @@ -94,6 +94,8 @@ src/linetypes/errorline.h src/linetypes/metacommand.h src/linetypes/object.h + src/linetypes/polygonobject.h + src/linetypes/propertygenerics.h src/linetypes/quadrilateral.h src/linetypes/subfilereference.h src/linetypes/triangle.h diff -r 14e51640c189 -r 7abaf1d64719 src/colors.h --- a/src/colors.h Mon May 11 12:18:04 2020 +0300 +++ b/src/colors.h Mon May 11 12:18:59 2020 +0300 @@ -28,9 +28,11 @@ struct ldraw::Color { - qint32 index; + qint32 index = 0; }; +Q_DECLARE_METATYPE(ldraw::Color) + class ldraw::ColorTable { public: diff -r 14e51640c189 -r 7abaf1d64719 src/linetypes/conditionaledge.h --- a/src/linetypes/conditionaledge.h Mon May 11 12:18:04 2020 +0300 +++ b/src/linetypes/conditionaledge.h Mon May 11 12:18:59 2020 +0300 @@ -1,5 +1,5 @@ #pragma once -#include "edge.h" +#include "polygonobject.h" namespace ldraw { diff -r 14e51640c189 -r 7abaf1d64719 src/linetypes/edge.h --- a/src/linetypes/edge.h Mon May 11 12:18:04 2020 +0300 +++ b/src/linetypes/edge.h Mon May 11 12:18:59 2020 +0300 @@ -1,5 +1,5 @@ #pragma once -#include "object.h" +#include "polygonobject.h" namespace ldraw { diff -r 14e51640c189 -r 7abaf1d64719 src/linetypes/object.cpp --- a/src/linetypes/object.cpp Mon May 11 12:18:04 2020 +0300 +++ b/src/linetypes/object.cpp Mon May 11 12:18:59 2020 +0300 @@ -1,6 +1,8 @@ #include #include #include "object.h" +#include "widgets/vec3editor.h" +#include "modeleditcontext.h" static std::int32_t getIdForNewObject() { @@ -87,7 +89,7 @@ switch (id) { case Property::Color: - return colorIndex.index; + return QVariant::fromValue(colorIndex); default: return Object::getProperty(id); } @@ -95,7 +97,7 @@ void ldraw::ColoredObject::setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) { - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Color, {colorIndex.index = value;}); + LDRAW_OBJECT_HANDLE_SET_PROPERTY(Color, {colorIndex = value;}); Object::setProperty(result, pair); } 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. */ diff -r 14e51640c189 -r 7abaf1d64719 src/linetypes/polygonobject.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/linetypes/polygonobject.h Mon May 11 12:18:59 2020 +0300 @@ -0,0 +1,70 @@ +#pragma once +#include "object.h" +#include "widgets/vec3editor.h" +#include "model.h" +#include "modeleditcontext.h" + +namespace ldraw +{ + template + class PolygonObject; +} + +template 0 and N <= 4)>> +class ldraw::PolygonObject : public ColoredObject +{ +public: + using BaseClass = ColoredObject; + 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 BaseClass::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[3] = value;}) + } + ColoredObject::setProperty(result, pair); + } + std::array points; +}; diff -r 14e51640c189 -r 7abaf1d64719 src/linetypes/propertygenerics.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/linetypes/propertygenerics.h Mon May 11 12:18:59 2020 +0300 @@ -0,0 +1,153 @@ +#pragma once +#include "main.h" +#include "colors.h" + +class Vec3Editor; +class MatrixEditor; + +namespace ldraw +{ + enum class Property; + struct PropertyKeyValue; + template + struct PropertyTraits + { + static constexpr bool defined = false; + }; +} + +/** + * Different properties + */ +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 +}; + +Q_DECLARE_METATYPE(ldraw::Property) + +// Mapping of properties to types +#define LDFORGE_DEFINE_PROPERTY_TYPE(PROPERTY, TYPE) \ + namespace ldraw \ + { \ + template<> struct PropertyTraits \ + { \ + using type = TYPE; \ + static constexpr std::array name{#PROPERTY}; \ + static constexpr bool defined = true; \ + }; \ + } + +LDFORGE_DEFINE_PROPERTY_TYPE(Color, ldraw::Color) +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& value) HANDLER);} + +// Generics +namespace ldraw +{ + template + using PropertyType = typename PropertyTraits::type; + + template + inline const char* PROPERTY_NAME = PropertyTraits::name; + + 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); + } + + struct PropertyTrait + { + ldraw::Property property; + std::array name; + int type; + }; + + namespace detail + { + template + constexpr int propertyCountHelper() + { + if constexpr (ldraw::PropertyTraits(N)>::defined) + { + return propertyCountHelper(); + } + else + { + return N; + } + } + + template + constexpr PropertyTrait getPropertyTrait() + { + constexpr auto property = static_cast(k); + using trait = ldraw::PropertyTraits; + return PropertyTrait{ + property, + trait::name, + qMetaTypeId() + }; + } + + template + auto getPropertyTraits(std::integer_sequence) + { + return std::array{getPropertyTrait()...}; + } + } + + constexpr int NUM_PROPERTIES = detail::propertyCountHelper<0>(); + inline const auto& traits() + { + static std::array result = + detail::getPropertyTraits(std::make_integer_sequence()); + return result; + } + + inline const auto& traits(ldraw::Property property) + { + return traits()[static_cast(property)]; + } + + template + constexpr auto makeIndexArray(std::index_sequence) + { + return std::array{static_cast(Ints)...}; + } + + constexpr auto ALL_PROPERTIES = makeIndexArray(std::make_index_sequence{}); + + template + bool testPropertyType(ldraw::Property property) + { + return qMetaTypeId() == ldraw::traits(property).type; + } +} diff -r 14e51640c189 -r 7abaf1d64719 src/linetypes/quadrilateral.h --- a/src/linetypes/quadrilateral.h Mon May 11 12:18:04 2020 +0300 +++ b/src/linetypes/quadrilateral.h Mon May 11 12:18:59 2020 +0300 @@ -1,5 +1,5 @@ #pragma once -#include "object.h" +#include "polygonobject.h" namespace ldraw { diff -r 14e51640c189 -r 7abaf1d64719 src/linetypes/triangle.h --- a/src/linetypes/triangle.h Mon May 11 12:18:04 2020 +0300 +++ b/src/linetypes/triangle.h Mon May 11 12:18:59 2020 +0300 @@ -1,5 +1,5 @@ #pragma once -#include "object.h" +#include "polygonobject.h" namespace ldraw { diff -r 14e51640c189 -r 7abaf1d64719 src/main.cpp --- a/src/main.cpp Mon May 11 12:18:04 2020 +0300 +++ b/src/main.cpp Mon May 11 12:18:59 2020 +0300 @@ -20,6 +20,8 @@ #include #include "mainwindow.h" #include "version.h" +#include +#include int main(int argc, char *argv[]) { @@ -30,6 +32,9 @@ ::qRegisterMetaTypeStreamOperators("Library"); ::qRegisterMetaTypeStreamOperators("Libraries"); QApplication app{argc, argv}; + /* + QMessageBox::information(nullptr, "", QMetaType::typeName( qMetaTypeId() )); + */ MainWindow mainwindow; mainwindow.show(); return app.exec(); diff -r 14e51640c189 -r 7abaf1d64719 src/modeleditcontext.cpp --- a/src/modeleditcontext.cpp Mon May 11 12:18:04 2020 +0300 +++ b/src/modeleditcontext.cpp Mon May 11 12:18:59 2020 +0300 @@ -17,6 +17,8 @@ */ #include "modeleditcontext.h" +#include "linetypes/triangle.h" +#include "linetypes/quadrilateral.h" Model::EditContext::EditContext(Model& model) : storedModel{model} @@ -45,6 +47,25 @@ this->model().remove(position); } +auto Model::EditContext::setObjectProperty( + const ldraw::id_t id, + const ldraw::Property property, + const QVariant& value) + -> ldraw::Object::SetPropertyResult +{ + ldraw::Object* const object = this->model().objectAt(id); + if (object != nullptr) + { + const ldraw::Object::SetPropertyResult result = object->setProperty(ldraw::PropertyKeyValue{property, value}); + modifiedObjects.insert(id); + return result; + } + else + { + return ldraw::Object::SetPropertyResult::PropertyNotHandled; + } +} + void Model::EditContext::setObjectPoint(ldraw::id_t id, int pointId, const glm::vec3& value) { ldraw::Object* object = this->model().objectAt(id); diff -r 14e51640c189 -r 7abaf1d64719 src/modeleditcontext.h --- a/src/modeleditcontext.h Mon May 11 12:18:04 2020 +0300 +++ b/src/modeleditcontext.h Mon May 11 12:18:59 2020 +0300 @@ -18,9 +18,6 @@ #pragma once #include "model.h" -#include "linetypes/object.h" -#include "linetypes/quadrilateral.h" -#include "linetypes/triangle.h" class Model::EditContext { @@ -33,15 +30,9 @@ ldraw::Id insert(int position, Args&&... args); void remove(int position); template - void setObjectProperty(ldraw::id_t id, const ldraw::PropertyType_t& value) - { - ldraw::Object* object = this->model().objectAt(id); - if (object != nullptr) - { - object->setProperty(value); - modifiedObjects.insert(id); - } - } + void setObjectProperty(ldraw::id_t id, const ldraw::PropertyType& value); + auto setObjectProperty(ldraw::id_t id, ldraw::Property property, const QVariant& value) + -> ldraw::Object::SetPropertyResult; void setObjectPoint(ldraw::id_t id, int pointId, const glm::vec3& value); void invertObject(ldraw::id_t id); Model& model(); @@ -52,6 +43,17 @@ Model& storedModel; }; +template +void Model::EditContext::setObjectProperty(const ldraw::id_t id, const ldraw::PropertyType& value) +{ + ldraw::Object* object = this->model().objectAt(id); + if (object != nullptr) + { + object->setProperty(value); + modifiedObjects.insert(id); + } +} + template ldraw::Id Model::EditContext::append(Args&&... args) { diff -r 14e51640c189 -r 7abaf1d64719 src/ui/polygonobjecteditor.cpp --- a/src/ui/polygonobjecteditor.cpp Mon May 11 12:18:04 2020 +0300 +++ b/src/ui/polygonobjecteditor.cpp Mon May 11 12:18:59 2020 +0300 @@ -1,27 +1,21 @@ -#include +#include #include #include "model.h" #include "modeleditcontext.h" #include "widgets/vec3editor.h" #include "ui/polygonobjecteditor.h" -constexpr char COLUMN_PROPERTY[] = "_ldforge_column"; +static constexpr char INDEX_NAME[] = "_ldforge_index"; +static constexpr char PROPERTY_NAME[] = "_ldforge_property"; +static constexpr char OBJECT_ID_NAME[] = "_ldforge_id"; +static constexpr char LABEL_NAME[] = "_ldforge_label"; PolygonObjectEditor::PolygonObjectEditor(Model* model, ldraw::id_t id, QWidget* parent) : QWidget{parent}, model{model}, storedObjectId{ldraw::NULL_ID.value} { - this->setLayout(new QVBoxLayout{this}); - for (int i = 0; i < countof(this->vec3Editors); i += 1) - { - std::optional& editorPointer = this->vec3Editors[i]; - editorPointer.emplace(glm::vec3{}, this); - editorPointer->setProperty(COLUMN_PROPERTY, QVariant::fromValue(i)); - this->layout()->addWidget(&*editorPointer); - connect(&*editorPointer, &Vec3Editor::valueChanged, this, &PolygonObjectEditor::vectorChanged); - } - this->layout()->addWidget(new QSplitter{Qt::Vertical, this}); + this->splitter.emplace(Qt::Vertical, this); this->setObjectId(id); } @@ -38,37 +32,46 @@ void PolygonObjectEditor::setObjectId(ldraw::id_t id) { this->storedObjectId = id; - this->updateNumRows(); + this->buildWidgets(); } -void PolygonObjectEditor::vectorChanged(const glm::vec3& value) +void PolygonObjectEditor::buildWidgets() { - bool ok; - const int num = sender()->property(COLUMN_PROPERTY).toInt(&ok); - if (ok and num >= 0 and num < countof(this->vec3Editors)) + this->widgets.clear(); + delete this->layout(); + QFormLayout* layout = new QFormLayout{this}; + this->setLayout(layout); + for (int n : {0, 1, 2, 3}) + { + this->setupPointWidget(n); + } + for (std::unique_ptr& widget : this->widgets) { - Model::EditContext editor = this->model->edit(); - editor.setObjectPoint(this->objectId(), num, value); + const QString label = widget->property(LABEL_NAME).toString(); + layout->addRow(label, widget.get()); + } + layout->addRow("", &*this->splitter); +} + +void PolygonObjectEditor::setupPointWidget(int n) +{ + const ldraw::Object* const object = this->model->get(this->objectId()); + const ldraw::Property property = ldraw::pointProperty(n); + const QVariant value = object->getProperty(property); + if (value.isValid()) + { + std::unique_ptr editor = std::make_unique(value.value(), this); + QObject::connect(editor.get(), &Vec3Editor::valueChanged, this, &PolygonObjectEditor::pointChanged); + editor->setProperty(INDEX_NAME, QVariant::fromValue(n)); + editor->setProperty(LABEL_NAME, &ldraw::traits(property).name[0]); + this->widgets.push_back(std::move(editor)); } } -void PolygonObjectEditor::updateNumRows() +void PolygonObjectEditor::pointChanged(const glm::vec3& value) { - const ldraw::Object* object = model->get(this->storedObjectId); - const int numPoints = object != nullptr ? object->numPoints() : 0; - for (int i = 0; i < countof(this->vec3Editors); i += 1) - { - Vec3Editor& editor = *this->vec3Editors[i]; - QSignalBlocker blocker{&editor}; - if (i < numPoints) - { - editor.setVisible(true); - editor.setValue(object->getPoint(i)); - } - else - { - editor.setVisible(false); - editor.setValue(glm::vec3{}); - } - } + Model::EditContext editcontext = this->model->edit(); + const int n = this->sender()->property(INDEX_NAME).toInt(); + const ldraw::Property property = ldraw::pointProperty(n); + editcontext.setObjectProperty(this->objectId(), property, QVariant::fromValue(value)); } diff -r 14e51640c189 -r 7abaf1d64719 src/ui/polygonobjecteditor.h --- a/src/ui/polygonobjecteditor.h Mon May 11 12:18:04 2020 +0300 +++ b/src/ui/polygonobjecteditor.h Mon May 11 12:18:59 2020 +0300 @@ -13,9 +13,11 @@ ldraw::id_t objectId() const; void setObjectId(ldraw::id_t id); private: - Q_SLOT void vectorChanged(const glm::vec3& value); - void updateNumRows(); + void buildWidgets(); + void setupPointWidget(int n); + Q_SLOT void pointChanged(const glm::vec3& value); Model* model; ldraw::id_t storedObjectId; - std::optional vec3Editors[4]; + std::vector> widgets; + std::optional splitter; };