src/editHistory.h

Fri, 22 Jun 2018 12:13:01 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 22 Jun 2018 12:13:01 +0300
changeset 1419
f7c53002a990
parent 1326
69a90bd2dba2
child 1440
265b2e95a8e8
permissions
-rw-r--r--

replaced uses of QList with QVector

655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
1326
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1273
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #pragma once
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include "main.h"
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
21 #include "serializer.h"
1147
a26568aa3cce Renamed ldObject.cpp → linetypes/modelobject.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
22 #include "linetypes/modelobject.h"
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 class AbstractHistoryEntry;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
26 class EditHistory : public QObject
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 {
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
28 Q_OBJECT
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29
794
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
30 public:
1419
f7c53002a990 replaced uses of QList with QVector
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
31 using Changeset = QVector<AbstractHistoryEntry*>;
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
32
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
33 EditHistory (LDDocument* document);
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
34 ~EditHistory();
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
35
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
36 template<typename T, typename... Args>
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
37 void add(Args&&... args)
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
38 {
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
39 if (not isIgnoring())
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
40 m_currentChangeset << new T {args..., this};
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
41 }
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
42
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
43 void addStep();
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
44 const Changeset& changesetAt (int pos) const;
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
45 void clear();
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
46 LDDocument* document() const;
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
47 bool isIgnoring() const;
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
48 int position();
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
49 void redo();
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
50 void setIgnoring (bool value);
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
51 int size() const;
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
52 void undo();
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
53
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
54 signals:
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
55 void undone();
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
56 void redone();
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
57 void stepAdded();
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
58
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
59 private:
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
60 LDDocument* m_document;
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
61 Changeset m_currentChangeset;
1419
f7c53002a990 replaced uses of QList with QVector
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
62 QVector<Changeset> m_changesets;
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
63 bool m_isIgnoring;
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
64 int m_position;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 };
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
67 class AbstractHistoryEntry
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 {
794
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
69 public:
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
70 AbstractHistoryEntry(EditHistory* parent);
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
71 virtual ~AbstractHistoryEntry();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
73 EditHistory* parent() const;
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
74 virtual void redo() = 0;
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
75 virtual void undo() = 0;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
77 private:
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
78 EditHistory* const m_parent;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 };
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
81 class AddHistoryEntry : public AbstractHistoryEntry
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 {
794
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
83 public:
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
84 AddHistoryEntry (const QModelIndex& index, EditHistory* parent);
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
85 void undo() override;
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
86 void redo() override;
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
87
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
88 private:
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
89 int m_row;
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
90 Serializer::Archive m_code;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 };
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
93 class DelHistoryEntry : public AddHistoryEntry
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 {
794
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
95 public:
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
96 using AddHistoryEntry::AddHistoryEntry;
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
97 void undo() override;
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
98 void redo() override;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 };
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
101 class EditHistoryEntry : public AbstractHistoryEntry
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 {
794
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
103 public:
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
104 EditHistoryEntry(
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
105 const QModelIndex& index,
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
106 const Serializer::Archive& oldCode,
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
107 const Serializer::Archive& newCode,
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
108 EditHistory* parent
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
109 );
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
110 void undo() override;
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
111 void redo() override;
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
112
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
113 private:
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
114 int row;
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
115 Serializer::Archive oldState;
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
116 Serializer::Archive newState;
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
117 };
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118
1273
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents: 1272
diff changeset
119 class MoveHistoryEntry : public AbstractHistoryEntry
983
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
120 {
05ba93066194 Refactor edit history
Teemu Piippo <crimsondusk64@gmail.com>
parents: 968
diff changeset
121 public:
1273
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents: 1272
diff changeset
122 MoveHistoryEntry(int top, int bottom, int destination, EditHistory* parent);
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
123 void undo() override;
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1147
diff changeset
124 void redo() override;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125
794
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
126 private:
1273
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents: 1272
diff changeset
127 int top;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents: 1272
diff changeset
128 int bottom;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents: 1272
diff changeset
129 int destination;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 };

mercurial