Begin work on edit history

Tue, 21 Sep 2021 19:58:06 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 21 Sep 2021 19:58:06 +0300
changeset 133
e39326ee48dc
parent 131
e2080ac44039
child 135
d384df40c8e7

Begin work on edit history

CMakeLists.txt file | annotate | diff | comparison | revisions
src/edithistory.cpp file | annotate | diff | comparison | revisions
src/edithistory.h file | annotate | diff | comparison | revisions
src/model.cpp file | annotate | diff | comparison | revisions
src/model.h file | annotate | diff | comparison | revisions
src/modeleditcontext.cpp file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Fri Sep 17 22:43:22 2021 +0300
+++ b/CMakeLists.txt	Tue Sep 21 19:58:06 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	Tue Sep 21 19:58:06 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	Tue Sep 21 19:58:06 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	Fri Sep 17 22:43:22 2021 +0300
+++ b/src/model.cpp	Tue Sep 21 19:58:06 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	Fri Sep 17 22:43:22 2021 +0300
+++ b/src/model.h	Tue Sep 21 19:58:06 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	Fri Sep 17 22:43:22 2021 +0300
+++ b/src/modeleditcontext.cpp	Tue Sep 21 19:58:06 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