# HG changeset patch # User Teemu Piippo # Date 1632259531 -10800 # Node ID d384df40c8e7fb377634e62e7e05ab69f7e598ff # Parent f77d2230e87c731ba3324d38bf70d49e6bdeb1c6# Parent e39326ee48dcc14806fd40983c05826fbf07ec45 Merge commit diff -r f77d2230e87c -r d384df40c8e7 CMakeLists.txt --- 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 diff -r f77d2230e87c -r d384df40c8e7 src/edithistory.cpp --- /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(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 f77d2230e87c -r d384df40c8e7 src/edithistory.h --- /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>; + EditHistory(); +}; \ No newline at end of file diff -r f77d2230e87c -r d384df40c8e7 src/model.cpp --- 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(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 f77d2230e87c -r d384df40c8e7 src/model.h --- 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& 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 f77d2230e87c -r d384df40c8e7 src/modeleditcontext.cpp --- 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); } }