src/history.cpp

changeset 493
16766ac1bbd9
parent 455
c5d14d112034
child 497
c51941e590b6
equal deleted inserted replaced
492:e964085e6913 493:16766ac1bbd9
31 m_pos (-1), 31 m_pos (-1),
32 m_opened (false) {} 32 m_opened (false) {}
33 33
34 // ============================================================================= 34 // =============================================================================
35 // ----------------------------------------------------------------------------- 35 // -----------------------------------------------------------------------------
36 void History::undo() { 36 void History::undo()
37 if (m_changesets.size() == 0 || pos() == -1) 37 { if (m_changesets.size() == 0 || pos() == -1)
38 return; 38 return;
39 39
40 const list& set = changeset (pos()); 40 const list& set = changeset (pos());
41 g_fullRefresh = false; 41 g_fullRefresh = false;
42 42
43 // Iterate the list in reverse and undo all actions 43 // Iterate the list in reverse and undo all actions
44 for (const AbstractHistoryEntry * change : c_rev<AbstractHistoryEntry*> (set)) 44 for (const AbstractHistoryEntry* change : c_rev<AbstractHistoryEntry*> (set))
45 change->undo(); 45 change->undo();
46 46
47 setPos (pos() - 1); 47 setPos (pos() - 1);
48 48
49 if (!g_fullRefresh) 49 if (!g_fullRefresh)
50 g_win->refresh(); 50 g_win->refresh();
51 else 51 else
52 g_win->fullRefresh(); 52 g_win->fullRefresh();
53 53
54 updateActions(); 54 updateActions();
55 } 55 }
56 56
57 // ============================================================================= 57 // =============================================================================
58 // ----------------------------------------------------------------------------- 58 // -----------------------------------------------------------------------------
59 void History::redo() { 59 void History::redo()
60 if (pos() == (long) m_changesets.size()) 60 { if (pos() == (long) m_changesets.size())
61 return; 61 return;
62 62
63 const list& set = changeset (pos() + 1); 63 const list& set = changeset (pos() + 1);
64 g_fullRefresh = false; 64 g_fullRefresh = false;
65 65
66 // Redo things - in the order as they were done in the first place 66 // Redo things - in the order as they were done in the first place
67 for (const AbstractHistoryEntry * change : set) 67 for (const AbstractHistoryEntry* change : set)
68 change->redo(); 68 change->redo();
69 69
70 setPos (pos() + 1); 70 setPos (pos() + 1);
71 71
72 if (!g_fullRefresh) 72 if (!g_fullRefresh)
73 g_win->refresh(); 73 g_win->refresh();
74 else 74 else
75 g_win->fullRefresh(); 75 g_win->fullRefresh();
76 76
77 updateActions(); 77 updateActions();
78 } 78 }
79 79
80 // ============================================================================= 80 // =============================================================================
81 // ----------------------------------------------------------------------------- 81 // -----------------------------------------------------------------------------
82 void History::clear() { 82 void History::clear()
83 for (List<AbstractHistoryEntry*> set : m_changesets) 83 { for (List<AbstractHistoryEntry*> set : m_changesets)
84 for (AbstractHistoryEntry * change : set) 84 for (AbstractHistoryEntry* change : set)
85 delete change; 85 delete change;
86 86
87 m_changesets.clear(); 87 m_changesets.clear();
88 } 88 }
89 89
90 // ============================================================================= 90 // =============================================================================
91 // ----------------------------------------------------------------------------- 91 // -----------------------------------------------------------------------------
92 void History::updateActions() const { 92 void History::updateActions() const
93 ACTION (Undo)->setEnabled (pos() != -1); 93 { ACTION (Undo)->setEnabled (pos() != -1);
94 ACTION (Redo)->setEnabled (pos() < (long) m_changesets.size() - 1); 94 ACTION (Redo)->setEnabled (pos() < (long) m_changesets.size() - 1);
95 } 95 }
96 96
97 // ============================================================================= 97 // =============================================================================
98 // ----------------------------------------------------------------------------- 98 // -----------------------------------------------------------------------------
99 void History::open() { 99 void History::open()
100 if (opened()) 100 { if (opened())
101 return; 101 return;
102 102
103 setOpened (true); 103 setOpened (true);
104 } 104 }
105 105
106 // ============================================================================= 106 // =============================================================================
107 // ----------------------------------------------------------------------------- 107 // -----------------------------------------------------------------------------
108 void History::close() { 108 void History::close()
109 if (!opened()) 109 { if (!opened())
110 return; 110 return;
111 111
112 setOpened (false); 112 setOpened (false);
113 113
114 if (m_currentArchive.size() == 0) 114 if (m_currentArchive.size() == 0)
115 return; 115 return;
116 116
117 while (pos() < size() - 1) 117 while (pos() < size() - 1)
118 m_changesets.erase (size() - 1); 118 m_changesets.erase (size() - 1);
119 119
120 m_changesets << m_currentArchive; 120 m_changesets << m_currentArchive;
121 m_currentArchive.clear(); 121 m_currentArchive.clear();
122 setPos (pos() + 1); 122 setPos (pos() + 1);
123 updateActions(); 123 updateActions();
124 } 124 }
125 125
126 // ============================================================================= 126 // =============================================================================
127 // ----------------------------------------------------------------------------- 127 // -----------------------------------------------------------------------------
128 void History::add (AbstractHistoryEntry* entry) { 128 void History::add (AbstractHistoryEntry* entry)
129 if (!opened()) { 129 { if (!opened())
130 delete entry; 130 { delete entry;
131 return; 131 return;
132 } 132 }
133 133
134 entry->setParent (this); 134 entry->setParent (this);
135 m_currentArchive << entry; 135 m_currentArchive << entry;
136 } 136 }
137 137
138 // ============================================================================= 138 // =============================================================================
139 // ----------------------------------------------------------------------------- 139 // -----------------------------------------------------------------------------
140 void AddHistory::undo() const { 140 void AddHistory::undo() const
141 LDFile* f = parent()->file(); 141 { LDFile* f = parent()->file();
142 LDObject* obj = f->object (index()); 142 LDObject* obj = f->object (index());
143 f->forgetObject (obj); 143 f->forgetObject (obj);
144 delete obj; 144 delete obj;
145 145
146 g_fullRefresh = true; 146 g_fullRefresh = true;
147 } 147 }
148 148
149 // ============================================================================= 149 // =============================================================================
150 // ----------------------------------------------------------------------------- 150 // -----------------------------------------------------------------------------
151 void AddHistory::redo() const { 151 void AddHistory::redo() const
152 LDFile* f = parent()->file(); 152 { LDFile* f = parent()->file();
153 LDObject* obj = parseLine (code()); 153 LDObject* obj = parseLine (code());
154 f->insertObj (index(), obj); 154 f->insertObj (index(), obj);
155 g_win->R()->compileObject (obj); 155 g_win->R()->compileObject (obj);
156 } 156 }
157 157
158 AddHistory::~AddHistory() {} 158 AddHistory::~AddHistory() {}
159 159
160 // ============================================================================= 160 // =============================================================================
161 // heh 161 // heh
162 // ----------------------------------------------------------------------------- 162 // -----------------------------------------------------------------------------
163 void DelHistory::undo() const { 163 void DelHistory::undo() const
164 LDFile* f = parent()->file(); 164 { LDFile* f = parent()->file();
165 LDObject* obj = parseLine (code()); 165 LDObject* obj = parseLine (code());
166 f->insertObj (index(), obj); 166 f->insertObj (index(), obj);
167 g_win->R()->compileObject (obj); 167 g_win->R()->compileObject (obj);
168 } 168 }
169 169
170 // ============================================================================= 170 // =============================================================================
171 // ----------------------------------------------------------------------------- 171 // -----------------------------------------------------------------------------
172 void DelHistory::redo() const { 172 void DelHistory::redo() const
173 LDFile* f = parent()->file(); 173 { LDFile* f = parent()->file();
174 LDObject* obj = f->object (index()); 174 LDObject* obj = f->object (index());
175 f->forgetObject (obj); 175 f->forgetObject (obj);
176 delete obj; 176 delete obj;
177 177
178 g_fullRefresh = true; 178 g_fullRefresh = true;
179 } 179 }
180 180
181 DelHistory::~DelHistory() {} 181 DelHistory::~DelHistory() {}
182 182
183 // ============================================================================= 183 // =============================================================================
184 // ----------------------------------------------------------------------------- 184 // -----------------------------------------------------------------------------
185 void EditHistory::undo() const { 185 void EditHistory::undo() const
186 LDObject* obj = LDFile::current()->object (index()); 186 { LDObject* obj = LDFile::current()->object (index());
187 LDObject* newobj = parseLine (oldCode()); 187 LDObject* newobj = parseLine (oldCode());
188 obj->replace (newobj); 188 obj->replace (newobj);
189 g_win->R()->compileObject (newobj); 189 g_win->R()->compileObject (newobj);
190 } 190 }
191 191
192 // ============================================================================= 192 // =============================================================================
193 // ----------------------------------------------------------------------------- 193 // -----------------------------------------------------------------------------
194 void EditHistory::redo() const { 194 void EditHistory::redo() const
195 LDObject* obj = LDFile::current()->object (index()); 195 { LDObject* obj = LDFile::current()->object (index());
196 LDObject* newobj = parseLine (newCode()); 196 LDObject* newobj = parseLine (newCode());
197 obj->replace (newobj); 197 obj->replace (newobj);
198 g_win->R()->compileObject (newobj); 198 g_win->R()->compileObject (newobj);
199 } 199 }
200 200
201 EditHistory::~EditHistory() {} 201 EditHistory::~EditHistory() {}
202 202
203 // ============================================================================= 203 // =============================================================================
204 // ----------------------------------------------------------------------------- 204 // -----------------------------------------------------------------------------
205 void SwapHistory::undo() const { 205 void SwapHistory::undo() const
206 LDObject::fromID (a)->swap (LDObject::fromID (b)); 206 { LDObject::fromID (a)->swap (LDObject::fromID (b));
207 } 207 }
208 208
209 void SwapHistory::redo() const { 209 void SwapHistory::redo() const
210 undo(); // :v 210 { undo(); // :v
211 } 211 }
212 212
213 SwapHistory::~SwapHistory() {} 213 SwapHistory::~SwapHistory() {}

mercurial