src/edithistory.h

changeset 200
ca23936b455b
parent 199
6988973515d2
child 201
5d201ee4a9c3
equal deleted inserted replaced
199:6988973515d2 200:ca23936b455b
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 - 2020 Teemu Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #pragma once
20 #include "main.h"
21 #include "model.h"
22
23 class ModelEditor;
24
25 class AbstractHistoryEntry
26 {
27 public:
28 virtual void undo(ModelEditor& editContext) = 0;
29 virtual void redo(ModelEditor& editContext) = 0;
30 };
31
32 class InsertHistoryEntry : public AbstractHistoryEntry
33 {
34 public:
35 InsertHistoryEntry(int position, const QByteArray& state) :
36 position{position},
37 state{state} {}
38 void undo(ModelEditor& editContext) override;
39 void redo(ModelEditor& editContext) override;
40 protected:
41 int position;
42 QByteArray state;
43 };
44
45 class DeleteHistoryEntry : public InsertHistoryEntry
46 {
47 public:
48 void undo(ModelEditor& editContext) override;
49 void redo(ModelEditor& editContext) override;
50 };
51
52 class EditHistoryEntry : public AbstractHistoryEntry
53 {
54 public:
55 EditHistoryEntry(int position, const QByteArray& stateBefore, const QByteArray& stateAfter) :
56 position{position},
57 stateBefore{stateBefore},
58 stateAfter{stateAfter} {}
59 void undo(ModelEditor& editContext) override;
60 void redo(ModelEditor& editContext) override;
61 private:
62 int position;
63 QByteArray stateBefore;
64 QByteArray stateAfter;
65 };
66
67 class EditHistory
68 {
69 public:
70 using Changeset = std::vector<std::unique_ptr<AbstractHistoryEntry>>;
71 EditHistory();
72 /**
73 * @brief Adds a new entry into the edit history. Creates a new changeset if there is not one already open.
74 * If behind in history, deletes all history entries in front.
75 */
76 template<typename T, typename... Rs>
77 void add(Rs&&... args)
78 {
79 if (not this->changesetOpen)
80 {
81 while (this->position < this->changesets.size())
82 {
83 this->changesets.erase(this->changesets.end() - 1);
84 }
85 if (this->changesets.empty())
86 {
87 this->changesets.emplace_back();
88 }
89 this->changesetOpen = true;
90 }
91 this->changesets.back().emplace_back(args...);
92 }
93 void commit()
94 {
95 this->changesetOpen = false;
96 this->position += 1;
97 }
98 private:
99 std::vector<Changeset> changesets;
100 std::size_t position = 0;
101 bool changesetOpen = false;
102 };

mercurial