src/history.cpp

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

mercurial