src/edithistory.h

Tue, 02 Nov 2021 15:43:57 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 02 Nov 2021 15:43:57 +0200
changeset 148
e1ced2523cad
parent 136
e8444e0d7f1a
child 149
008989bc7d6e
child 152
03f8e6d42e13
permissions
-rw-r--r--

reworking

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
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 class AbstractHistoryEntry
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 {
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 public:
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 virtual void undo(Model::EditContext& editContext) = 0;
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 virtual void redo(Model::EditContext& editContext) = 0;
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 };
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 class InsertHistoryEntry : public AbstractHistoryEntry
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 public:
136
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
33 InsertHistoryEntry(int position, const QByteArray& state) :
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 position{position},
136
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
35 state{state} {}
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 void undo(Model::EditContext& editContext) override;
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 void redo(Model::EditContext& editContext) override;
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 protected:
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 int position;
136
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
40 QByteArray state;
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 };
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 class DeleteHistoryEntry : public InsertHistoryEntry
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 public:
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 void undo(Model::EditContext& editContext) override;
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 void redo(Model::EditContext& editContext) override;
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 };
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 class EditHistoryEntry : public AbstractHistoryEntry
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 public:
136
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
53 EditHistoryEntry(int position, const QByteArray& stateBefore, const QByteArray& stateAfter) :
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 position{position},
136
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
55 stateBefore{stateBefore},
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
56 stateAfter{stateAfter} {}
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 void undo(Model::EditContext& editContext) override;
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 void redo(Model::EditContext& editContext) override;
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 private:
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 int position;
136
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
61 QByteArray stateBefore;
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
62 QByteArray stateAfter;
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 };
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 class EditHistory
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 public:
136
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
68 using Changeset = std::vector<std::unique_ptr<AbstractHistoryEntry>>;
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 EditHistory();
136
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
70 /**
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
71 * @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
72 * If behind in history, deletes all history entries in front.
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
73 */
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
74 template<typename T, typename... Rs>
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
75 void add(Rs&&... args)
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
76 {
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
77 if (not this->changesetOpen)
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 while (this->position < this->changesets.size())
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 this->changesets.erase(this->changesets.end() - 1);
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 if (this->changesets.empty())
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 this->changesets.emplace_back();
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->changesetOpen = true;
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->changesets.back().emplace_back(args...);
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 void commit()
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 this->changesetOpen = false;
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
94 this->position += 1;
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
95 }
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
96 private:
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
97 std::vector<Changeset> changesets;
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
98 std::size_t position = 0;
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
99 bool changesetOpen = false;
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 };

mercurial