Add action to make a model unofficial (modifies the !LDRAW_ORG line)

Sun, 26 Jun 2022 20:54:09 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 26 Jun 2022 20:54:09 +0300
changeset 262
dc33f8a707c4
parent 261
6a875faebde2
child 263
59b6027b9843

Add action to make a model unofficial (modifies the !LDRAW_ORG line)

src/document.h file | annotate | diff | comparison | revisions
src/documentmanager.cpp file | annotate | diff | comparison | revisions
src/ldrawalgorithm.cpp file | annotate | diff | comparison | revisions
src/ldrawalgorithm.h file | annotate | diff | comparison | revisions
src/main.cpp file | annotate | diff | comparison | revisions
src/mainwindow.ui file | annotate | diff | comparison | revisions
src/model.h file | annotate | diff | comparison | revisions
--- 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<AppendToModel, DeleteFromModel>;
-
 Q_DECLARE_METATYPE(ModelAction)
 
 class EditTools final : public QObject, public RenderLayer
--- 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());
--- 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<ModelAction> ldraw::makeUnofficial(const Model* model)
 {
-	if (editor.model().size() >= 4)
-	{
-		const ldraw::Object* ldrawOrgLine = editor.model()[3];
-		if (isA<ldraw::MetaCommand>(ldrawOrgLine))
-		{
-			const QString& body = ldrawOrgLine->getProperty<ldraw::Property::Text>();
+	std::vector<ModelAction> actions;
+	if (model->size() >= 4) {
+		if (const Comment* comment = std::get_if<Comment>(&(*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<ldraw::Property::Text>(ldrawOrgLine->id, tokens.join(" "));
+				actions.push_back(ModifyModel{
+					.position = 3,
+					.newElement = Comment{.text = tokens.join(" ")}
+				});
 			}
 		}
 	}
+	return actions;
 }
-*/
 
 ModelElement inverted(const ModelElement& element)
 {
--- 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<ModelAction> makeUnofficial(const Model *model);
 
 	constexpr float circleAngle(unsigned int divisions, unsigned int i)
 	{
--- 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<Setting::MainWindowGeometry>());
--- 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 @@
      <x>0</x>
      <y>0</y>
      <width>729</width>
-     <height>40</height>
+     <height>28</height>
     </rect>
    </property>
    <widget class="QMenu" name="menuFile">
@@ -88,6 +88,8 @@
     <addaction name="actionGridFine"/>
     <addaction name="actionGridMedium"/>
     <addaction name="actionGridCoarse"/>
+    <addaction name="separator"/>
+    <addaction name="actionMakeUnofficial"/>
    </widget>
    <widget class="QMenu" name="menuHelp">
     <property name="title">
@@ -470,6 +472,11 @@
     <string>Wireframe</string>
    </property>
   </action>
+  <action name="actionMakeUnofficial">
+   <property name="text">
+    <string>Make unofficial</string>
+   </property>
+  </action>
  </widget>
  <resources>
   <include location="../ldforge.qrc"/>
--- 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<ModelId, std::size_t> 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<Quadrilateral>{{.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>;

mercurial