# HG changeset patch # User Teemu Piippo # Date 1632243486 -10800 # Node ID e39326ee48dcc14806fd40983c05826fbf07ec45 # Parent e2080ac440398213aa7c6d708c3b4ab873b30dbc Begin work on edit history diff -r e2080ac44039 -r e39326ee48dc CMakeLists.txt --- 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 diff -r e2080ac44039 -r e39326ee48dc src/edithistory.cpp --- /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(this)->redo(editContext); +} + +void DeleteHistoryEntry::redo(Model::EditContext &editContext) +{ + static_cast(this)->undo(editContext); +} + +void EditHistoryEntry::undo(Model::EditContext &editContext) +{ + +} + +void EditHistoryEntry::redo(Model::EditContext &editContext) +{ + +} diff -r e2080ac44039 -r e39326ee48dc src/edithistory.h --- /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>; + EditHistory(); +}; \ No newline at end of file diff -r e2080ac44039 -r e39326ee48dc src/model.cpp --- 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(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(this->body.size()); diff -r e2080ac44039 -r e39326ee48dc src/model.h --- 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& 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 objectsById; mutable std::vector cachedPolygons; mutable bool needRecache = true; + int editCounter = 0; }; /** diff -r e2080ac44039 -r e39326ee48dc src/modeleditcontext.cpp --- 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); } }