Merge commit

Wed, 22 Sep 2021 00:25:31 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 22 Sep 2021 00:25:31 +0300
changeset 135
d384df40c8e7
parent 134
f77d2230e87c (current diff)
parent 133
e39326ee48dc (diff)
child 136
e8444e0d7f1a

Merge commit

--- a/CMakeLists.txt	Wed Sep 22 00:25:13 2021 +0300
+++ b/CMakeLists.txt	Wed Sep 22 00:25:31 2021 +0300
@@ -32,6 +32,7 @@
 	src/colors.cpp
 	src/document.cpp
 	src/documentmanager.cpp
+	src/edithistory.cpp
 	src/geometry.cpp
 	src/libraries.cpp
 	src/invert.cpp
@@ -81,6 +82,7 @@
 	src/colors.h
 	src/document.h
 	src/documentmanager.h
+	src/edithistory.h
 	src/functional.h
 	src/geometry.h
 	src/header.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/edithistory.cpp	Wed Sep 22 00:25:31 2021 +0300
@@ -0,0 +1,36 @@
+#include "edithistory.h"
+
+EditHistory::EditHistory()
+{
+	
+}
+
+void InsertHistoryEntry::undo(Model::EditContext &editContext)
+{
+	editContext.remove(this->position);
+}
+
+void InsertHistoryEntry::redo(Model::EditContext &editContext)
+{
+	
+}
+
+void DeleteHistoryEntry::undo(Model::EditContext &editContext)
+{
+	static_cast<InsertHistoryEntry*>(this)->redo(editContext);
+}
+
+void DeleteHistoryEntry::redo(Model::EditContext &editContext)
+{
+	static_cast<InsertHistoryEntry*>(this)->undo(editContext);
+}
+
+void EditHistoryEntry::undo(Model::EditContext &editContext)
+{
+	
+}
+
+void EditHistoryEntry::redo(Model::EditContext &editContext)
+{
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/edithistory.h	Wed Sep 22 00:25:31 2021 +0300
@@ -0,0 +1,53 @@
+#pragma once
+#include "main.h"
+#include "model.h"
+#include "modeleditcontext.h"
+
+class AbstractHistoryEntry
+{
+public:
+	virtual void undo(Model::EditContext& editContext) = 0;
+	virtual void redo(Model::EditContext& editContext) = 0;
+};
+
+class InsertHistoryEntry : public AbstractHistoryEntry
+{
+public:
+	InsertHistoryEntry(int position, const QString& code) :
+		position{position},
+		code{code} {}
+	void undo(Model::EditContext& editContext) override;
+	void redo(Model::EditContext& editContext) override;
+protected:
+	int position;
+	QString code;
+};
+
+class DeleteHistoryEntry : public InsertHistoryEntry
+{
+public:
+	void undo(Model::EditContext& editContext) override;
+	void redo(Model::EditContext& editContext) override;
+};
+
+class EditHistoryEntry : public AbstractHistoryEntry
+{
+public:
+	EditHistoryEntry(int position, const QString& codeBefore, const QString& codeAfter) :
+		position{position},
+		codeBefore{codeBefore},
+		codeAfter{codeAfter} {}
+	void undo(Model::EditContext& editContext) override;
+	void redo(Model::EditContext& editContext) override;
+private:
+	int position;
+	QString codeBefore;
+	QString codeAfter;
+};
+
+class EditHistory
+{
+public:
+	using Changeset = QVector<std::unique_ptr<AbstractHistoryEntry>>;
+	EditHistory();
+};
\ No newline at end of file
--- a/src/model.cpp	Wed Sep 22 00:25:13 2021 +0300
+++ b/src/model.cpp	Wed Sep 22 00:25:31 2021 +0300
@@ -32,8 +32,26 @@
 	return static_cast<int>(this->body.size());
 }
 
+/**
+ * @brief Looks up the object ID at the specified index. If out of bounds, returns NULL_ID.
+ * @param index Index of object to look up
+ * @return object ID
+ */
+ldraw::id_t Model::at(int index) const
+{
+	if (index >= 0 and index < this->size())
+	{
+		return this->body[index]->id;
+	}
+	else
+	{
+		return ldraw::NULL_ID;
+	}
+}
+
 Model::EditContext Model::edit()
 {
+	this->editCounter += 1;
 	return {*this};
 }
 
@@ -118,6 +136,17 @@
 	object->getPolygons(polygons_out, context);
 }
 
+void Model::editFinished()
+{
+	this->editCounter -= 1;
+}
+
+void Model::objectModified(ldraw::id_t id)
+{
+	const QModelIndex index = this->lookup(id);
+	Q_EMIT this->dataChanged(index, index);
+}
+
 void Model::append(ModelObjectPointer&& object)
 {
 	const int position = static_cast<int>(this->body.size());
--- a/src/model.h	Wed Sep 22 00:25:13 2021 +0300
+++ b/src/model.h	Wed Sep 22 00:25:31 2021 +0300
@@ -37,6 +37,7 @@
 	Model(QObject* parent = nullptr);
 	Model(const Model&) = delete;
 	int size() const;
+	ldraw::id_t at(int index) const;
 	EditContext edit();
 	int rowCount(const QModelIndex&) const override;
 	QVariant data(const QModelIndex& index, int role) const override;
@@ -88,6 +89,8 @@
 		const int index,
 		std::vector<gl::Polygon>& polygons_out,
 		ldraw::GetPolygonsContext* context) const;
+	void editFinished();
+	void objectModified(ldraw::id_t id);
 	bool modified = false;
 	QString path;
 	LDHeader header;
@@ -95,6 +98,7 @@
 	std::map<ldraw::id_t, ldraw::Object*> objectsById;
 	mutable std::vector<gl::Polygon> cachedPolygons;
 	mutable bool needRecache = true;
+	int editCounter = 0;
 };
 
 /**
--- a/src/modeleditcontext.cpp	Wed Sep 22 00:25:13 2021 +0300
+++ b/src/modeleditcontext.cpp	Wed Sep 22 00:25:31 2021 +0300
@@ -29,8 +29,7 @@
 {
 	for (ldraw::id_t id : this->modifiedObjects)
 	{
-		const QModelIndex index = this->model().lookup(id);
-		Q_EMIT this->model().dataChanged(index, index);
+		this->model().objectModified(id);
 	}
 }
 

mercurial