# HG changeset patch # User Teemu Piippo # Date 1646386819 -7200 # Node ID 2f79053c2e9a03e10c93cc9526227bf4005db9b6 # Parent 03f8e6d42e13a499eb850d3a4157b5a6bce7a228 Renamed modeleditcontext.cpp -> modeleditor.cpp diff -r 03f8e6d42e13 -r 2f79053c2e9a CMakeLists.txt --- a/CMakeLists.txt Fri Mar 04 11:37:50 2022 +0200 +++ b/CMakeLists.txt Fri Mar 04 11:40:19 2022 +0200 @@ -26,7 +26,7 @@ source_group("3.2 Widgets" REGULAR_EXPRESSION "src/(ui|widgets)/.+\\.(cpp|h|ui)") source_group("3.1 Settings editor" REGULAR_EXPRESSION "src/settingseditor/.+\\.(cpp|h|ui)") source_group("3 User interface" REGULAR_EXPRESSION "src/(mainwindow|document|documentmanager|uiutilities)\\.(cpp|h|ui)") -source_group("2 Model handling" REGULAR_EXPRESSION "src/(model|modeleditcontext|libraries|colors|parser|vertexmap|edithistory|polygoncache)\\.(cpp|h|ui)") +source_group("2 Model handling" REGULAR_EXPRESSION "src/(model|modeleditor|libraries|colors|parser|vertexmap|edithistory|polygoncache)\\.(cpp|h|ui)") source_group("6 Editing tools" REGULAR_EXPRESSION "src/tools/.+\\.(cpp|h|ui)") set (LDFORGE_SOURCES @@ -42,7 +42,7 @@ src/main.cpp src/mainwindow.cpp src/model.cpp - src/modeleditcontext.cpp + src/modeleditor.cpp src/parser.cpp src/polygoncache.cpp src/uiutilities.cpp @@ -97,7 +97,7 @@ src/mainwindow.h src/maths.h src/model.h - src/modeleditcontext.h + src/modeleditor.h src/parser.h src/polygoncache.h src/ring.h diff -r 03f8e6d42e13 -r 2f79053c2e9a src/document.cpp --- a/src/document.cpp Fri Mar 04 11:37:50 2022 +0200 +++ b/src/document.cpp Fri Mar 04 11:40:19 2022 +0200 @@ -21,7 +21,7 @@ #include "document.h" #include "ui_document.h" #include "model.h" -#include "modeleditcontext.h" +#include "modeleditor.h" #include "tools/basetool.h" #include "tools/drawtool.h" #include "tools/selecttool.h" diff -r 03f8e6d42e13 -r 2f79053c2e9a src/documentmanager.cpp --- a/src/documentmanager.cpp Fri Mar 04 11:37:50 2022 +0200 +++ b/src/documentmanager.cpp Fri Mar 04 11:40:19 2022 +0200 @@ -21,7 +21,7 @@ #include #include #include "documentmanager.h" -#include "modeleditcontext.h" +#include "modeleditor.h" #include "linetypes/comment.h" #include "linetypes/subfilereference.h" #include "parser.h" diff -r 03f8e6d42e13 -r 2f79053c2e9a src/edithistory.cpp --- a/src/edithistory.cpp Fri Mar 04 11:37:50 2022 +0200 +++ b/src/edithistory.cpp Fri Mar 04 11:40:19 2022 +0200 @@ -17,7 +17,7 @@ */ #include "edithistory.h" -#include "modeleditcontext.h" +#include "modeleditor.h" EditHistory::EditHistory() { diff -r 03f8e6d42e13 -r 2f79053c2e9a src/linetypes/object.cpp --- a/src/linetypes/object.cpp Fri Mar 04 11:37:50 2022 +0200 +++ b/src/linetypes/object.cpp Fri Mar 04 11:40:19 2022 +0200 @@ -2,7 +2,7 @@ #include #include "object.h" #include "widgets/vec3editor.h" -#include "modeleditcontext.h" +#include "modeleditor.h" static std::int32_t getIdForNewObject() { diff -r 03f8e6d42e13 -r 2f79053c2e9a src/linetypes/polygonobject.h --- a/src/linetypes/polygonobject.h Fri Mar 04 11:37:50 2022 +0200 +++ b/src/linetypes/polygonobject.h Fri Mar 04 11:40:19 2022 +0200 @@ -2,7 +2,7 @@ #include "object.h" #include "widgets/vec3editor.h" #include "model.h" -#include "modeleditcontext.h" +#include "modeleditor.h" namespace ldraw { diff -r 03f8e6d42e13 -r 2f79053c2e9a src/model.cpp --- a/src/model.cpp Fri Mar 04 11:37:50 2022 +0200 +++ b/src/model.cpp Fri Mar 04 11:40:19 2022 +0200 @@ -22,7 +22,7 @@ #include #include #include "model.h" -#include "modeleditcontext.h" +#include "modeleditor.h" #include "documentmanager.h" /** diff -r 03f8e6d42e13 -r 2f79053c2e9a src/modeleditcontext.cpp --- a/src/modeleditcontext.cpp Fri Mar 04 11:37:50 2022 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +0,0 @@ -/* - * LDForge: LDraw parts authoring CAD - * Copyright (C) 2013 - 2020 Teemu Piippo - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "modeleditcontext.h" -#include "linetypes/triangle.h" -#include "linetypes/quadrilateral.h" - -ModelEditor::ModelEditor(Model& model) : - storedModel{model} -{ -} - -ModelEditor::~ModelEditor() -{ - for (ldraw::id_t id : this->modifiedObjects) - { - const QModelIndex index = this->model().find(id); - if (index.isValid()) - { - Q_EMIT this->objectModified(index.row()); - } - } -} - -ldraw::id_t ModelEditor::append(std::unique_ptr&& object) -{ - this->storedModel.append(std::move(object)); - Q_EMIT this->objectAdded(this->model().size() - 1); - return object->id; -} - -void ModelEditor::remove(int position) -{ - this->storedModel.remove(position); -} - -auto ModelEditor::setObjectProperty( - const ldraw::id_t id, - const ldraw::Property property, - const QVariant& value) - -> ldraw::Object::SetPropertyResult -{ - ldraw::Object* const object = this->storedModel.findObjectById(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 ModelEditor::setObjectPoint(ldraw::id_t id, int pointId, const glm::vec3& value) -{ - ldraw::Object* object = this->storedModel.findObjectById(id); - if (object != nullptr) - { - object->setProperty(ldraw::PropertyKeyValue{ldraw::pointProperty(pointId), QVariant::fromValue(value)}); - modifiedObjects.insert(id); - } -} - -const Model &ModelEditor::model() -{ - return this->storedModel; -} - diff -r 03f8e6d42e13 -r 2f79053c2e9a src/modeleditcontext.h --- a/src/modeleditcontext.h Fri Mar 04 11:37:50 2022 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,98 +0,0 @@ -/* - * LDForge: LDraw parts authoring CAD - * Copyright (C) 2013 - 2020 Teemu Piippo - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once -#include "model.h" - -/** - * @brief Provides an interface for editing a model such that signals are emitted for each edit done. - * User edits to models should always be done through this class. - */ -class ModelEditor : public QObject -{ - Q_OBJECT -public: - ModelEditor(Model& model); - ~ModelEditor(); - template - ldraw::Id append(Args&&... args); - ldraw::id_t append(std::unique_ptr&& object); - template - ldraw::Id insert(int position, Args&&... args); - void remove(int position); - template - 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); - template - bool modifyObject(ldraw::Id id, Fn&& function); - template - bool modifyObjectAt(int position, Fn&& function); - const Model& model(); -Q_SIGNALS: - void objectAdded(int position); - void objectModified(int position); - void objectRemoved(ldraw::id_t id); -private: - QSet modifiedObjects; - Model& storedModel; -}; - -template -void ModelEditor::setObjectProperty(const ldraw::id_t id, const ldraw::PropertyType& value) -{ - this->modifyObject(id, [&](ldraw::Object* object){ - object->setProperty(value); - }); -} - -template -ldraw::Id ModelEditor::append(Args&&... args) -{ - return this->storedModel.append(args...); -} - -template -ldraw::Id ModelEditor::insert(int position, Args&&... args) -{ - return this->storedModel.insert(position, args...); -} - -template -bool ModelEditor::modifyObject(ldraw::Id id, Fn&& function) -{ - QModelIndex index = this->model().find(id); - return this->modifyObjectAt(index.row(), function); -} - -template -bool ModelEditor::modifyObjectAt(int position, Fn&& function) -{ - if (position >= 0 and position < this->model().size()) - { - T* object = dynamic_cast(this->storedModel[position]); - if (object != nullptr) - { - Q_EMIT this->objectModified(position); - function(object); - return true; - } - } - return false; -} \ No newline at end of file diff -r 03f8e6d42e13 -r 2f79053c2e9a src/modeleditor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/modeleditor.cpp Fri Mar 04 11:40:19 2022 +0200 @@ -0,0 +1,85 @@ +/* + * LDForge: LDraw parts authoring CAD + * Copyright (C) 2013 - 2020 Teemu Piippo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "modeleditor.h" +#include "linetypes/triangle.h" +#include "linetypes/quadrilateral.h" + +ModelEditor::ModelEditor(Model& model) : + storedModel{model} +{ +} + +ModelEditor::~ModelEditor() +{ + for (ldraw::id_t id : this->modifiedObjects) + { + const QModelIndex index = this->model().find(id); + if (index.isValid()) + { + Q_EMIT this->objectModified(index.row()); + } + } +} + +ldraw::id_t ModelEditor::append(std::unique_ptr&& object) +{ + this->storedModel.append(std::move(object)); + Q_EMIT this->objectAdded(this->model().size() - 1); + return object->id; +} + +void ModelEditor::remove(int position) +{ + this->storedModel.remove(position); +} + +auto ModelEditor::setObjectProperty( + const ldraw::id_t id, + const ldraw::Property property, + const QVariant& value) + -> ldraw::Object::SetPropertyResult +{ + ldraw::Object* const object = this->storedModel.findObjectById(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 ModelEditor::setObjectPoint(ldraw::id_t id, int pointId, const glm::vec3& value) +{ + ldraw::Object* object = this->storedModel.findObjectById(id); + if (object != nullptr) + { + object->setProperty(ldraw::PropertyKeyValue{ldraw::pointProperty(pointId), QVariant::fromValue(value)}); + modifiedObjects.insert(id); + } +} + +const Model &ModelEditor::model() +{ + return this->storedModel; +} + diff -r 03f8e6d42e13 -r 2f79053c2e9a src/modeleditor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/modeleditor.h Fri Mar 04 11:40:19 2022 +0200 @@ -0,0 +1,98 @@ +/* + * LDForge: LDraw parts authoring CAD + * Copyright (C) 2013 - 2020 Teemu Piippo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include "model.h" + +/** + * @brief Provides an interface for editing a model such that signals are emitted for each edit done. + * User edits to models should always be done through this class. + */ +class ModelEditor : public QObject +{ + Q_OBJECT +public: + ModelEditor(Model& model); + ~ModelEditor(); + template + ldraw::Id append(Args&&... args); + ldraw::id_t append(std::unique_ptr&& object); + template + ldraw::Id insert(int position, Args&&... args); + void remove(int position); + template + 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); + template + bool modifyObject(ldraw::Id id, Fn&& function); + template + bool modifyObjectAt(int position, Fn&& function); + const Model& model(); +Q_SIGNALS: + void objectAdded(int position); + void objectModified(int position); + void objectRemoved(ldraw::id_t id); +private: + QSet modifiedObjects; + Model& storedModel; +}; + +template +void ModelEditor::setObjectProperty(const ldraw::id_t id, const ldraw::PropertyType& value) +{ + this->modifyObject(id, [&](ldraw::Object* object){ + object->setProperty(value); + }); +} + +template +ldraw::Id ModelEditor::append(Args&&... args) +{ + return this->storedModel.append(args...); +} + +template +ldraw::Id ModelEditor::insert(int position, Args&&... args) +{ + return this->storedModel.insert(position, args...); +} + +template +bool ModelEditor::modifyObject(ldraw::Id id, Fn&& function) +{ + QModelIndex index = this->model().find(id); + return this->modifyObjectAt(index.row(), function); +} + +template +bool ModelEditor::modifyObjectAt(int position, Fn&& function) +{ + if (position >= 0 and position < this->model().size()) + { + T* object = dynamic_cast(this->storedModel[position]); + if (object != nullptr) + { + Q_EMIT this->objectModified(position); + function(object); + return true; + } + } + return false; +} \ No newline at end of file diff -r 03f8e6d42e13 -r 2f79053c2e9a src/parser.h --- a/src/parser.h Fri Mar 04 11:37:50 2022 +0200 +++ b/src/parser.h Fri Mar 04 11:40:19 2022 +0200 @@ -20,7 +20,7 @@ #include "main.h" #include "linetypes/object.h" #include "model.h" -#include "modeleditcontext.h" +#include "modeleditor.h" #include "header.h" class Parser : public QObject diff -r 03f8e6d42e13 -r 2f79053c2e9a src/tools/drawtool.cpp --- a/src/tools/drawtool.cpp Fri Mar 04 11:37:50 2022 +0200 +++ b/src/tools/drawtool.cpp Fri Mar 04 11:40:19 2022 +0200 @@ -4,7 +4,7 @@ #include "linetypes/triangle.h" #include "linetypes/quadrilateral.h" #include "drawtool.h" -#include "modeleditcontext.h" +#include "modeleditor.h" static const QBrush pointBrush = {Qt::white}; static const QPen polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine}; diff -r 03f8e6d42e13 -r 2f79053c2e9a src/tools/transformtool.cpp --- a/src/tools/transformtool.cpp Fri Mar 04 11:37:50 2022 +0200 +++ b/src/tools/transformtool.cpp Fri Mar 04 11:40:19 2022 +0200 @@ -1,6 +1,6 @@ #include #include "model.h" -#include "modeleditcontext.h" +#include "modeleditor.h" #include "linetypes/object.h" #include "transformtool.h" #include "document.h" diff -r 03f8e6d42e13 -r 2f79053c2e9a src/ui/polygonobjecteditor.cpp --- a/src/ui/polygonobjecteditor.cpp Fri Mar 04 11:37:50 2022 +0200 +++ b/src/ui/polygonobjecteditor.cpp Fri Mar 04 11:40:19 2022 +0200 @@ -1,7 +1,7 @@ #include #include #include "document.h" -#include "modeleditcontext.h" +#include "modeleditor.h" #include "widgets/vec3editor.h" #include "ui/polygonobjecteditor.h"