comparison widgets/CommandHistory.cpp @ 752:f428bd852580 tonioni

Debug output
author Chris Cannam
date Tue, 01 Apr 2014 16:20:55 +0100
parents 692a8b9804fe
children 102ffad481e6
comparison
equal deleted inserted replaced
751:34c1203d21b5 752:f428bd852580
35 35
36 #include <iostream> 36 #include <iostream>
37 37
38 #include <typeinfo> 38 #include <typeinfo>
39 39
40 //#define DEBUG_COMMAND_HISTORY 1 40 #define DEBUG_COMMAND_HISTORY 1
41 41
42 CommandHistory *CommandHistory::m_instance = 0; 42 CommandHistory *CommandHistory::m_instance = 0;
43 43
44 CommandHistory::CommandHistory() : 44 CommandHistory::CommandHistory() :
45 m_undoLimit(50), 45 m_undoLimit(50),
99 99
100 void 100 void
101 CommandHistory::clear() 101 CommandHistory::clear()
102 { 102 {
103 #ifdef DEBUG_COMMAND_HISTORY 103 #ifdef DEBUG_COMMAND_HISTORY
104 SVDEBUG << "CommandHistory::clear()" << endl; 104 cerr << "CommandHistory::clear()" << endl;
105 #endif 105 #endif
106 closeBundle(); 106 closeBundle();
107 m_savedAt = -1; 107 m_savedAt = -1;
108 clearStack(m_undoStack); 108 clearStack(m_undoStack);
109 clearStack(m_redoStack); 109 clearStack(m_redoStack);
141 CommandHistory::addCommand(Command *command, bool execute, bool bundle) 141 CommandHistory::addCommand(Command *command, bool execute, bool bundle)
142 { 142 {
143 if (!command) return; 143 if (!command) return;
144 144
145 #ifdef DEBUG_COMMAND_HISTORY 145 #ifdef DEBUG_COMMAND_HISTORY
146 SVDEBUG << "CommandHistory::addCommand: " << command->getName() << " of type " << typeid(*command).name() << " at " << command << ": execute = " << execute << ", bundle = " << bundle << " (m_currentCompound = " << m_currentCompound << ", m_currentBundle = " << m_currentBundle << ")" << endl; 146 cerr << "CommandHistory::addCommand: " << command->getName() << " of type " << typeid(*command).name() << " at " << command << ": execute = " << execute << ", bundle = " << bundle << " (m_currentCompound = " << m_currentCompound << ", m_currentBundle = " << m_currentBundle << ")" << endl;
147 #endif 147 #endif
148 148
149 if (m_currentCompound) { 149 if (m_currentCompound) {
150 addToCompound(command, execute); 150 addToCompound(command, execute);
151 return; 151 return;
158 closeBundle(); 158 closeBundle();
159 } 159 }
160 160
161 #ifdef DEBUG_COMMAND_HISTORY 161 #ifdef DEBUG_COMMAND_HISTORY
162 if (!m_redoStack.empty()) { 162 if (!m_redoStack.empty()) {
163 SVDEBUG << "CommandHistory::clearing redo stack" << endl; 163 cerr << "CommandHistory::clearing redo stack" << endl;
164 } 164 }
165 #endif 165 #endif
166 166
167 // We can't redo after adding a command 167 // We can't redo after adding a command
168 clearStack(m_redoStack); 168 clearStack(m_redoStack);
190 CommandHistory::addToBundle(Command *command, bool execute) 190 CommandHistory::addToBundle(Command *command, bool execute)
191 { 191 {
192 if (m_currentBundle) { 192 if (m_currentBundle) {
193 if (!command || (command->getName() != m_currentBundleName)) { 193 if (!command || (command->getName() != m_currentBundleName)) {
194 #ifdef DEBUG_COMMAND_HISTORY 194 #ifdef DEBUG_COMMAND_HISTORY
195 SVDEBUG << "CommandHistory::addToBundle: " 195 cerr << "CommandHistory::addToBundle: " << command->getName()
196 << command->getName() << ": closing current bundle" << endl; 196 << ": closing current bundle" << endl;
197 #endif 197 #endif
198 closeBundle(); 198 closeBundle();
199 } 199 }
200 } 200 }
201 201
202 if (!command) return; 202 if (!command) return;
203 203
204 if (!m_currentBundle) { 204 if (!m_currentBundle) {
205 205
206 #ifdef DEBUG_COMMAND_HISTORY 206 #ifdef DEBUG_COMMAND_HISTORY
207 SVDEBUG << "CommandHistory::addToBundle: " 207 cerr << "CommandHistory::addToBundle: " << command->getName()
208 << command->getName() << ": creating new bundle" << endl; 208 << ": creating new bundle" << endl;
209 #endif 209 #endif
210 210
211 // need to addCommand before setting m_currentBundle, as addCommand 211 // need to addCommand before setting m_currentBundle, as addCommand
212 // with bundle false will reset m_currentBundle to 0 212 // with bundle false will reset m_currentBundle to 0
213 MacroCommand *mc = new BundleCommand(command->getName()); 213 MacroCommand *mc = new BundleCommand(command->getName());
217 m_currentBundle = mc; 217 m_currentBundle = mc;
218 m_currentBundleName = command->getName(); 218 m_currentBundleName = command->getName();
219 } 219 }
220 220
221 #ifdef DEBUG_COMMAND_HISTORY 221 #ifdef DEBUG_COMMAND_HISTORY
222 SVDEBUG << "CommandHistory::addToBundle: " 222 cerr << "CommandHistory::addToBundle: " << command->getName()
223 << command->getName() << ": adding to bundle" << endl; 223 << ": adding to bundle" << endl;
224 #endif 224 #endif
225 225
226 if (execute) command->execute(); 226 if (execute) command->execute();
227 m_currentBundle->addCommand(command); 227 m_currentBundle->addCommand(command);
228 228
240 } 240 }
241 241
242 void 242 void
243 CommandHistory::closeBundle() 243 CommandHistory::closeBundle()
244 { 244 {
245 #ifdef DEBUG_COMMAND_HISTORY 245 if (m_currentBundle) {
246 SVDEBUG << "CommandHistory::closeBundle" << endl; 246 #ifdef DEBUG_COMMAND_HISTORY
247 #endif 247 cerr << "CommandHistory::closeBundle" << endl;
248 248 #endif
249 if (m_currentBundle) emit activity(m_currentBundle->getName()); 249 emit activity(m_currentBundle->getName());
250 }
250 m_currentBundle = 0; 251 m_currentBundle = 0;
251 m_currentBundleName = ""; 252 m_currentBundleName = "";
252 } 253 }
253 254
254 void 255 void
255 CommandHistory::bundleTimerTimeout() 256 CommandHistory::bundleTimerTimeout()
256 { 257 {
257 #ifdef DEBUG_COMMAND_HISTORY 258 #ifdef DEBUG_COMMAND_HISTORY
258 SVDEBUG << "CommandHistory::bundleTimerTimeout: bundle is " << m_currentBundle << endl; 259 cerr << "CommandHistory::bundleTimerTimeout: bundle is " << m_currentBundle << endl;
259 #endif 260 #endif
260 261
261 closeBundle(); 262 closeBundle();
262 } 263 }
263 264
264 void 265 void
265 CommandHistory::addToCompound(Command *command, bool execute) 266 CommandHistory::addToCompound(Command *command, bool execute)
266 { 267 {
267 #ifdef DEBUG_COMMAND_HISTORY
268 SVDEBUG << "CommandHistory::addToCompound: " << command->getName() << endl;
269 #endif
270 if (!m_currentCompound) { 268 if (!m_currentCompound) {
271 SVDEBUG << "CommandHistory::addToCompound: ERROR: no compound operation in progress!" << endl; 269 cerr << "CommandHistory::addToCompound: ERROR: no compound operation in progress!" << endl;
272 return; 270 return;
273 } 271 }
272
273 #ifdef DEBUG_COMMAND_HISTORY
274 cerr << "CommandHistory::addToCompound[" << m_currentCompound->getName() << "]: " << command->getName() << " (exec: " << execute << ")" << endl;
275 #endif
274 276
275 if (execute) command->execute(); 277 if (execute) command->execute();
276 m_currentCompound->addCommand(command); 278 m_currentCompound->addCommand(command);
277 } 279 }
278 280
279 void 281 void
280 CommandHistory::startCompoundOperation(QString name, bool execute) 282 CommandHistory::startCompoundOperation(QString name, bool execute)
281 { 283 {
282 if (m_currentCompound) { 284 if (m_currentCompound) {
283 SVDEBUG << "CommandHistory::startCompoundOperation: ERROR: compound operation already in progress!" << endl; 285 cerr << "CommandHistory::startCompoundOperation: ERROR: compound operation already in progress!" << endl;
284 cerr << "(name is " << m_currentCompound->getName() << ")" << endl; 286 cerr << "(name is " << m_currentCompound->getName() << ")" << endl;
285 return; 287 return;
286 } 288 }
287 289
290 #ifdef DEBUG_COMMAND_HISTORY
291 cerr << "CommandHistory::startCompoundOperation: " << name << " (exec: " << execute << ")" << endl;
292 #endif
293
288 closeBundle(); 294 closeBundle();
289 295
290 m_currentCompound = new MacroCommand(name); 296 m_currentCompound = new MacroCommand(name);
291 m_executeCompound = execute; 297 m_executeCompound = execute;
292 } 298 }
293 299
294 void 300 void
295 CommandHistory::endCompoundOperation() 301 CommandHistory::endCompoundOperation()
296 { 302 {
297 if (!m_currentCompound) { 303 if (!m_currentCompound) {
298 SVDEBUG << "CommandHistory::endCompoundOperation: ERROR: no compound operation in progress!" << endl; 304 cerr << "CommandHistory::endCompoundOperation: ERROR: no compound operation in progress!" << endl;
299 return; 305 return;
300 } 306 }
307
308 #ifdef DEBUG_COMMAND_HISTORY
309 cerr << "CommandHistory::endCompoundOperation: " << m_currentCompound->getName() << endl;
310 #endif
301 311
302 MacroCommand *toAdd = m_currentCompound; 312 MacroCommand *toAdd = m_currentCompound;
303 m_currentCompound = 0; 313 m_currentCompound = 0;
304 314
305 if (toAdd->haveCommands()) { 315 if (toAdd->haveCommands()) {
327 CommandHistory::undo() 337 CommandHistory::undo()
328 { 338 {
329 if (m_undoStack.empty()) return; 339 if (m_undoStack.empty()) return;
330 340
331 #ifdef DEBUG_COMMAND_HISTORY 341 #ifdef DEBUG_COMMAND_HISTORY
332 SVDEBUG << "CommandHistory::undo()" << endl; 342 cerr << "CommandHistory::undo()" << endl;
333 #endif 343 #endif
334 344
335 closeBundle(); 345 closeBundle();
336 346
337 Command *command = m_undoStack.top(); 347 Command *command = m_undoStack.top();
353 CommandHistory::redo() 363 CommandHistory::redo()
354 { 364 {
355 if (m_redoStack.empty()) return; 365 if (m_redoStack.empty()) return;
356 366
357 #ifdef DEBUG_COMMAND_HISTORY 367 #ifdef DEBUG_COMMAND_HISTORY
358 SVDEBUG << "CommandHistory::redo()" << endl; 368 cerr << "CommandHistory::redo()" << endl;
359 #endif 369 #endif
360 370
361 closeBundle(); 371 closeBundle();
362 372
363 Command *command = m_redoStack.top(); 373 Command *command = m_redoStack.top();
434 CommandStack tempStack; 444 CommandStack tempStack;
435 445
436 for (i = 0; i < limit; ++i) { 446 for (i = 0; i < limit; ++i) {
437 #ifdef DEBUG_COMMAND_HISTORY 447 #ifdef DEBUG_COMMAND_HISTORY
438 Command *command = stack.top(); 448 Command *command = stack.top();
439 SVDEBUG << "CommandHistory::clipStack: Saving recent command: " << command->getName() << " at " << command << endl; 449 cerr << "CommandHistory::clipStack: Saving recent command: " << command->getName() << " at " << command << endl;
440 #endif 450 #endif
441 tempStack.push(stack.top()); 451 tempStack.push(stack.top());
442 stack.pop(); 452 stack.pop();
443 } 453 }
444 454
456 { 466 {
457 while (!stack.empty()) { 467 while (!stack.empty()) {
458 Command *command = stack.top(); 468 Command *command = stack.top();
459 // Not safe to call getName() on a command about to be deleted 469 // Not safe to call getName() on a command about to be deleted
460 #ifdef DEBUG_COMMAND_HISTORY 470 #ifdef DEBUG_COMMAND_HISTORY
461 SVDEBUG << "CommandHistory::clearStack: About to delete command " << command << endl; 471 cerr << "CommandHistory::clearStack: About to delete command " << command << endl;
462 #endif 472 #endif
463 delete command; 473 delete command;
464 stack.pop(); 474 stack.pop();
465 } 475 }
466 } 476 }