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 #include "EditHistory.h" |
|
20 #include "LDObject.h" |
|
21 #include "Document.h" |
|
22 #include "Misc.h" |
|
23 #include "MainWindow.h" |
|
24 #include "GLRenderer.h" |
|
25 |
|
26 // ============================================================================= |
|
27 // |
|
28 History::History() : |
|
29 m_position (-1) {} |
|
30 |
|
31 // ============================================================================= |
|
32 // |
|
33 void History::undo() |
|
34 { |
|
35 if (m_changesets.isEmpty() || position() == -1) |
|
36 return; |
|
37 |
|
38 // Don't take the changes done here as actual edits to the document |
|
39 setIgnoring (true); |
|
40 |
|
41 const Changeset& set = getChangeset (position()); |
|
42 |
|
43 // Iterate the list in reverse and undo all actions |
|
44 for (int i = set.size() - 1; i >= 0; --i) |
|
45 { |
|
46 AbstractHistoryEntry* change = set[i]; |
|
47 change->undo(); |
|
48 } |
|
49 |
|
50 m_position--; |
|
51 g_win->refresh(); |
|
52 g_win->updateActions(); |
|
53 dprint ("Position is now %1", position()); |
|
54 setIgnoring (false); |
|
55 } |
|
56 |
|
57 // ============================================================================= |
|
58 // |
|
59 void History::redo() |
|
60 { |
|
61 if (position() == m_changesets.size()) |
|
62 return; |
|
63 |
|
64 setIgnoring (true); |
|
65 const Changeset& set = getChangeset (position() + 1); |
|
66 |
|
67 // Redo things - in the order as they were done in the first place |
|
68 for (const AbstractHistoryEntry* change : set) |
|
69 change->redo(); |
|
70 |
|
71 setPosition (position() + 1); |
|
72 g_win->refresh(); |
|
73 g_win->updateActions(); |
|
74 dprint ("Position is now %1", position()); |
|
75 setIgnoring (false); |
|
76 } |
|
77 |
|
78 // ============================================================================= |
|
79 // |
|
80 void History::clear() |
|
81 { |
|
82 for (Changeset set : m_changesets) |
|
83 for (AbstractHistoryEntry* change : set) |
|
84 delete change; |
|
85 |
|
86 m_changesets.clear(); |
|
87 dprint ("History: cleared"); |
|
88 } |
|
89 |
|
90 // ============================================================================= |
|
91 // |
|
92 void History::addStep() |
|
93 { |
|
94 if (m_currentChangeset.isEmpty()) |
|
95 return; |
|
96 |
|
97 while (position() < getSize() - 1) |
|
98 { |
|
99 Changeset last = m_changesets.last(); |
|
100 |
|
101 for (AbstractHistoryEntry* entry : last) |
|
102 delete entry; |
|
103 |
|
104 m_changesets.removeLast(); |
|
105 } |
|
106 |
|
107 dprint ("History: step added (%1 changes)", m_currentChangeset.size()); |
|
108 m_changesets << m_currentChangeset; |
|
109 m_currentChangeset.clear(); |
|
110 setPosition (position() + 1); |
|
111 g_win->updateActions(); |
|
112 } |
|
113 |
|
114 // ============================================================================= |
|
115 // |
|
116 void History::add (AbstractHistoryEntry* entry) |
|
117 { |
|
118 if (isIgnoring()) |
|
119 { |
|
120 delete entry; |
|
121 return; |
|
122 } |
|
123 |
|
124 entry->setParent (this); |
|
125 m_currentChangeset << entry; |
|
126 dprint ("History: added entry of type %1", entry->getTypeName()); |
|
127 } |
|
128 |
|
129 // ============================================================================= |
|
130 // |
|
131 void AddHistory::undo() const |
|
132 { |
|
133 LDObject* obj = parent()->document()->getObject (index()); |
|
134 obj->destroy(); |
|
135 } |
|
136 |
|
137 // ============================================================================= |
|
138 // |
|
139 void AddHistory::redo() const |
|
140 { |
|
141 LDObject* obj = parseLine (code()); |
|
142 parent()->document()->insertObj (index(), obj); |
|
143 g_win->R()->compileObject (obj); |
|
144 } |
|
145 |
|
146 // ============================================================================= |
|
147 // |
|
148 DelHistory::DelHistory (int idx, LDObject* obj) : |
|
149 m_index (idx), |
|
150 m_code (obj->asText()) {} |
|
151 |
|
152 // ============================================================================= |
|
153 // heh |
|
154 // |
|
155 void DelHistory::undo() const |
|
156 { |
|
157 LDObject* obj = parseLine (code()); |
|
158 parent()->document()->insertObj (index(), obj); |
|
159 g_win->R()->compileObject (obj); |
|
160 } |
|
161 |
|
162 // ============================================================================= |
|
163 // |
|
164 void DelHistory::redo() const |
|
165 { |
|
166 LDObject* obj = parent()->document()->getObject (index()); |
|
167 obj->destroy(); |
|
168 } |
|
169 |
|
170 // ============================================================================= |
|
171 // |
|
172 void EditHistory::undo() const |
|
173 { |
|
174 LDObject* obj = getCurrentDocument()->getObject (index()); |
|
175 LDObject* newobj = parseLine (oldCode()); |
|
176 obj->replace (newobj); |
|
177 g_win->R()->compileObject (newobj); |
|
178 } |
|
179 |
|
180 // ============================================================================= |
|
181 // |
|
182 void EditHistory::redo() const |
|
183 { |
|
184 LDObject* obj = getCurrentDocument()->getObject (index()); |
|
185 LDObject* newobj = parseLine (newCode()); |
|
186 obj->replace (newobj); |
|
187 g_win->R()->compileObject (newobj); |
|
188 } |
|
189 |
|
190 // ============================================================================= |
|
191 // |
|
192 void SwapHistory::undo() const |
|
193 { |
|
194 LDObject::fromID (a)->swap (LDObject::fromID (b)); |
|
195 } |
|
196 |
|
197 // ============================================================================= |
|
198 // |
|
199 void SwapHistory::redo() const |
|
200 { |
|
201 undo(); |
|
202 } |
|