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