|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013, 2014 Santeri 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 #ifndef LDFORGE_HISTORY_H |
|
20 #define LDFORGE_HISTORY_H |
|
21 |
|
22 #include "Main.h" |
|
23 #include "LDObject.h" |
|
24 |
|
25 #define IMPLEMENT_HISTORY_TYPE(N) \ |
|
26 virtual ~N##History() {} \ |
|
27 virtual void undo() const override; \ |
|
28 virtual void redo() const override; \ |
|
29 \ |
|
30 virtual History::EHistoryType getType() const override \ |
|
31 { \ |
|
32 return History::E##N##History; \ |
|
33 } \ |
|
34 \ |
|
35 virtual QString getTypeName() const \ |
|
36 { \ |
|
37 return #N; \ |
|
38 } |
|
39 |
|
40 class AbstractHistoryEntry; |
|
41 |
|
42 // ============================================================================= |
|
43 class History |
|
44 { |
|
45 PROPERTY (private, int, Position, NUM_OPS, STOCK_WRITE) |
|
46 PROPERTY (public, LDDocument*, Document, NO_OPS, STOCK_WRITE) |
|
47 PROPERTY (public, bool, Ignoring, BOOL_OPS, STOCK_WRITE) |
|
48 |
|
49 public: |
|
50 typedef QList<AbstractHistoryEntry*> Changeset; |
|
51 |
|
52 enum EHistoryType |
|
53 { |
|
54 EDelHistory, |
|
55 EEditHistory, |
|
56 EAddHistory, |
|
57 EMoveHistory, |
|
58 ESwapHistory, |
|
59 }; |
|
60 |
|
61 History(); |
|
62 void undo(); |
|
63 void redo(); |
|
64 void clear(); |
|
65 |
|
66 void addStep(); |
|
67 void add (AbstractHistoryEntry* entry); |
|
68 |
|
69 inline long getSize() const |
|
70 { |
|
71 return m_changesets.size(); |
|
72 } |
|
73 |
|
74 inline History& operator<< (AbstractHistoryEntry* entry) |
|
75 { |
|
76 add (entry); |
|
77 return *this; |
|
78 } |
|
79 |
|
80 inline const Changeset& getChangeset (long pos) const |
|
81 { |
|
82 return m_changesets[pos]; |
|
83 } |
|
84 |
|
85 private: |
|
86 Changeset m_currentChangeset; |
|
87 QList<Changeset> m_changesets; |
|
88 }; |
|
89 |
|
90 // ============================================================================= |
|
91 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
92 // ============================================================================= |
|
93 class AbstractHistoryEntry |
|
94 { |
|
95 PROPERTY (public, History*, Parent, NO_OPS, STOCK_WRITE) |
|
96 |
|
97 public: |
|
98 virtual ~AbstractHistoryEntry() {} |
|
99 virtual void undo() const = 0; |
|
100 virtual void redo() const = 0; |
|
101 virtual History::EHistoryType getType() const = 0; |
|
102 virtual QString getTypeName() const = 0; |
|
103 }; |
|
104 |
|
105 // ============================================================================= |
|
106 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
107 // ============================================================================= |
|
108 class DelHistory : public AbstractHistoryEntry |
|
109 { |
|
110 PROPERTY (private, int, Index, NO_OPS, STOCK_WRITE) |
|
111 PROPERTY (private, QString, Code, NO_OPS, STOCK_WRITE) |
|
112 |
|
113 public: |
|
114 IMPLEMENT_HISTORY_TYPE (Del) |
|
115 DelHistory (int idx, LDObject* obj); |
|
116 }; |
|
117 |
|
118 // ============================================================================= |
|
119 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
120 // ============================================================================= |
|
121 class EditHistory : public AbstractHistoryEntry |
|
122 { |
|
123 PROPERTY (private, int, Index, NO_OPS, STOCK_WRITE) |
|
124 PROPERTY (private, QString, OldCode, NO_OPS, STOCK_WRITE) |
|
125 PROPERTY (private, QString, NewCode, NO_OPS, STOCK_WRITE) |
|
126 |
|
127 public: |
|
128 IMPLEMENT_HISTORY_TYPE (Edit) |
|
129 |
|
130 EditHistory (int idx, QString oldCode, QString newCode) : |
|
131 m_Index (idx), |
|
132 m_OldCode (oldCode), |
|
133 m_NewCode (newCode) {} |
|
134 }; |
|
135 |
|
136 // ============================================================================= |
|
137 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
138 // ============================================================================= |
|
139 class AddHistory : public AbstractHistoryEntry |
|
140 { |
|
141 PROPERTY (private, int, Index, NO_OPS, STOCK_WRITE) |
|
142 PROPERTY (private, QString, Code, NO_OPS, STOCK_WRITE) |
|
143 |
|
144 public: |
|
145 IMPLEMENT_HISTORY_TYPE (Add) |
|
146 |
|
147 AddHistory (int idx, LDObject* obj) : |
|
148 m_Index (idx), |
|
149 m_Code (obj->raw()) {} |
|
150 }; |
|
151 |
|
152 // ============================================================================= |
|
153 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
154 // ============================================================================= |
|
155 class MoveHistory : public AbstractHistoryEntry |
|
156 { |
|
157 public: |
|
158 IMPLEMENT_HISTORY_TYPE (Move) |
|
159 |
|
160 QList<int> indices; |
|
161 Vertex dest; |
|
162 |
|
163 MoveHistory (QList<int> indices, Vertex dest) : |
|
164 indices (indices), |
|
165 dest (dest) {} |
|
166 }; |
|
167 |
|
168 // ============================================================================= |
|
169 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
170 // ============================================================================= |
|
171 class SwapHistory : public AbstractHistoryEntry |
|
172 { |
|
173 public: |
|
174 IMPLEMENT_HISTORY_TYPE (Swap) |
|
175 |
|
176 SwapHistory (int a, int b) : |
|
177 a (a), |
|
178 b (b) {} |
|
179 |
|
180 private: |
|
181 int a, b; |
|
182 }; |
|
183 |
|
184 #endif // LDFORGE_HISTORY_H |