src/editHistory.cc

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

mercurial