# HG changeset patch # User Teemu Piippo # Date 1656266049 -10800 # Node ID dc33f8a707c4a4e8dddf9bd6df9de1cc84f068a2 # Parent 6a875faebde2908b1bf53774591396fd7dc21119 Add action to make a model unofficial (modifies the !LDRAW_ORG line) diff -r 6a875faebde2 -r dc33f8a707c4 src/document.h --- a/src/document.h Sun Jun 26 20:27:04 2022 +0300 +++ b/src/document.h Sun Jun 26 20:54:09 2022 +0300 @@ -33,18 +33,6 @@ Q_DECLARE_METATYPE(EditingMode) -struct AppendToModel -{ - ModelElement newElement; -}; - -struct DeleteFromModel -{ - std::size_t position; -}; - -using ModelAction = std::variant; - Q_DECLARE_METATYPE(ModelAction) class EditTools final : public QObject, public RenderLayer diff -r 6a875faebde2 -r dc33f8a707c4 src/documentmanager.cpp --- a/src/documentmanager.cpp Sun Jun 26 20:27:04 2022 +0300 +++ b/src/documentmanager.cpp Sun Jun 26 20:54:09 2022 +0300 @@ -195,7 +195,7 @@ QSaveFile file{info->path}; file.setDirectWriteFallback(true); if (file.open(QSaveFile::WriteOnly)) { - ::save(info->model.get(), &file); + ::save(*info->model.get(), &file); const bool commitSucceeded = file.commit(); if (not commitSucceeded) { errors << QObject::tr("Could not save: %1").arg(file.errorString()); diff -r 6a875faebde2 -r dc33f8a707c4 src/ldrawalgorithm.cpp --- a/src/ldrawalgorithm.cpp Sun Jun 26 20:27:04 2022 +0300 +++ b/src/ldrawalgorithm.cpp Sun Jun 26 20:54:09 2022 +0300 @@ -17,18 +17,12 @@ return result; } -/** - * @brief Modifies the !LDRAW_ORG line so that it becomes unofficial - */ -/* -void ldraw::makeUnofficial(ModelEditor& editor) +std::vector ldraw::makeUnofficial(const Model* model) { - if (editor.model().size() >= 4) - { - const ldraw::Object* ldrawOrgLine = editor.model()[3]; - if (isA(ldrawOrgLine)) - { - const QString& body = ldrawOrgLine->getProperty(); + std::vector actions; + if (model->size() >= 4) { + if (const Comment* comment = std::get_if(&(*model)[3])) { + const QString& body = comment->text; if (body.startsWith("!LDRAW_ORG ") and not body.startsWith("!LDRAW_ORG Unofficial_")) { // Add Unofficial_ to part type @@ -40,12 +34,15 @@ tokens.removeAt(3); tokens.removeAt(2); } - editor.setObjectProperty(ldrawOrgLine->id, tokens.join(" ")); + actions.push_back(ModifyModel{ + .position = 3, + .newElement = Comment{.text = tokens.join(" ")} + }); } } } + return actions; } -*/ ModelElement inverted(const ModelElement& element) { diff -r 6a875faebde2 -r dc33f8a707c4 src/ldrawalgorithm.h --- a/src/ldrawalgorithm.h Sun Jun 26 20:27:04 2022 +0300 +++ b/src/ldrawalgorithm.h Sun Jun 26 20:54:09 2022 +0300 @@ -15,9 +15,7 @@ const Quadrilateral& q, ldraw::Diagonal diagonal); - /* - void makeUnofficial(ModelEditor &editor); - */ + std::vector makeUnofficial(const Model *model); constexpr float circleAngle(unsigned int divisions, unsigned int i) { diff -r 6a875faebde2 -r dc33f8a707c4 src/main.cpp --- a/src/main.cpp Sun Jun 26 20:27:04 2022 +0300 +++ b/src/main.cpp Sun Jun 26 20:54:09 2022 +0300 @@ -18,6 +18,7 @@ #include "ui/circletooloptionswidget.h" #include "messagelog.h" #include "ui/objecteditor.h" +#include "ldrawalgorithm.h" static const QDir LOCALE_DIR {":/locale"}; @@ -416,7 +417,10 @@ model->append(action.newElement); }, [model](const DeleteFromModel& action){ - model->remove(action.position); + model->remove(action.position); + }, + [model](const ModifyModel& action){ + model->assignAt(action.position, action.newElement); }, }, action); }; @@ -694,6 +698,17 @@ data->tools->setCircleToolOptions(options); } }); + QObject::connect( + ui.actionMakeUnofficial, + &QAction::triggered, + [&]{ + if (ModelData* data = currentModelData(&ui, &documents)) { + Model* const model = data->model; + for (const ModelAction& action : ldraw::makeUnofficial(model)) { + executeAction(model, action); + } + } + }); mainWindow.setWindowTitle(title()); mainWindow.tabifyDockWidget(ui.messageLogDock, ui.toolOptionsDock); mainWindow.restoreGeometry(setting()); diff -r 6a875faebde2 -r dc33f8a707c4 src/mainwindow.ui --- a/src/mainwindow.ui Sun Jun 26 20:27:04 2022 +0300 +++ b/src/mainwindow.ui Sun Jun 26 20:54:09 2022 +0300 @@ -42,7 +42,7 @@ 0 0 729 - 40 + 28 @@ -88,6 +88,8 @@ + + @@ -470,6 +472,11 @@ Wireframe + + + Make unofficial + + diff -r 6a875faebde2 -r dc33f8a707c4 src/model.h --- a/src/model.h Sun Jun 26 20:27:04 2022 +0300 +++ b/src/model.h Sun Jun 26 20:54:09 2022 +0300 @@ -214,7 +214,7 @@ std::map positions; ModelId runningId = {1}; public: - Model(QObject* parent); + explicit Model(QObject* parent); virtual ~Model(); ModelId append(const ModelElement& value); const ModelElement& at(std::size_t position) const; @@ -276,3 +276,24 @@ { return Colored{{.p1 = p1, .p2 = p2, .p3 = p3, .p4 = p4}, MAIN_COLOR}; } + +struct AppendToModel +{ + ModelElement newElement; +}; + +struct DeleteFromModel +{ + std::size_t position; +}; + +struct ModifyModel +{ + std::size_t position; + ModelElement newElement; +}; + +using ModelAction = std::variant< + AppendToModel, + DeleteFromModel, + ModifyModel>;