src/edithistory.h

Tue, 15 Mar 2022 18:56:02 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 15 Mar 2022 18:56:02 +0200
changeset 184
35bbfad4d0f4
parent 152
03f8e6d42e13
permissions
-rw-r--r--

Add missing svg icon

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

mercurial