Mercurial > hg > svgui
comparison widgets/CommandHistory.cpp @ 1266:a34a2a25907c
Untabify
author | Chris Cannam |
---|---|
date | Thu, 01 Mar 2018 18:02:22 +0000 |
parents | c91878670297 |
children | c8a6fd3f9dff |
comparison
equal
deleted
inserted
replaced
1265:6e724c81f18f | 1266:a34a2a25907c |
---|---|
68 connect(m_undoMenuAction, SIGNAL(triggered()), this, SLOT(undo())); | 68 connect(m_undoMenuAction, SIGNAL(triggered()), this, SLOT(undo())); |
69 | 69 |
70 m_undoMenu = new QMenu(tr("&Undo")); | 70 m_undoMenu = new QMenu(tr("&Undo")); |
71 m_undoMenuAction->setMenu(m_undoMenu); | 71 m_undoMenuAction->setMenu(m_undoMenu); |
72 connect(m_undoMenu, SIGNAL(triggered(QAction *)), | 72 connect(m_undoMenu, SIGNAL(triggered(QAction *)), |
73 this, SLOT(undoActivated(QAction*))); | 73 this, SLOT(undoActivated(QAction*))); |
74 | 74 |
75 m_redoAction = new QAction(redoIcon, tr("Re&do"), this); | 75 m_redoAction = new QAction(redoIcon, tr("Re&do"), this); |
76 m_redoAction->setShortcut(tr("Ctrl+Shift+Z")); | 76 m_redoAction->setShortcut(tr("Ctrl+Shift+Z")); |
77 m_redoAction->setStatusTip(tr("Redo the last operation that was undone")); | 77 m_redoAction->setStatusTip(tr("Redo the last operation that was undone")); |
78 connect(m_redoAction, SIGNAL(triggered()), this, SLOT(redo())); | 78 connect(m_redoAction, SIGNAL(triggered()), this, SLOT(redo())); |
81 connect(m_redoMenuAction, SIGNAL(triggered()), this, SLOT(redo())); | 81 connect(m_redoMenuAction, SIGNAL(triggered()), this, SLOT(redo())); |
82 | 82 |
83 m_redoMenu = new QMenu(tr("Re&do")); | 83 m_redoMenu = new QMenu(tr("Re&do")); |
84 m_redoMenuAction->setMenu(m_redoMenu); | 84 m_redoMenuAction->setMenu(m_redoMenu); |
85 connect(m_redoMenu, SIGNAL(triggered(QAction *)), | 85 connect(m_redoMenu, SIGNAL(triggered(QAction *)), |
86 this, SLOT(redoActivated(QAction*))); | 86 this, SLOT(redoActivated(QAction*))); |
87 } | 87 } |
88 | 88 |
89 CommandHistory::~CommandHistory() | 89 CommandHistory::~CommandHistory() |
90 { | 90 { |
91 m_savedAt = -1; | 91 m_savedAt = -1; |
134 CommandHistory::addCommand(Command *command) | 134 CommandHistory::addCommand(Command *command) |
135 { | 135 { |
136 if (!command) return; | 136 if (!command) return; |
137 | 137 |
138 if (m_currentCompound) { | 138 if (m_currentCompound) { |
139 addToCompound(command, m_executeCompound); | 139 addToCompound(command, m_executeCompound); |
140 return; | 140 return; |
141 } | 141 } |
142 | 142 |
143 addCommand(command, true); | 143 addCommand(command, true); |
144 } | 144 } |
145 | 145 |
151 #ifdef DEBUG_COMMAND_HISTORY | 151 #ifdef DEBUG_COMMAND_HISTORY |
152 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; | 152 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; |
153 #endif | 153 #endif |
154 | 154 |
155 if (m_currentCompound) { | 155 if (m_currentCompound) { |
156 addToCompound(command, execute); | 156 addToCompound(command, execute); |
157 return; | 157 return; |
158 } | 158 } |
159 | 159 |
160 if (bundle) { | 160 if (bundle) { |
161 addToBundle(command, execute); | 161 addToBundle(command, execute); |
162 return; | 162 return; |
163 } else if (m_currentBundle) { | 163 } else if (m_currentBundle) { |
164 closeBundle(); | 164 closeBundle(); |
165 } | 165 } |
166 | 166 |
167 #ifdef DEBUG_COMMAND_HISTORY | 167 #ifdef DEBUG_COMMAND_HISTORY |
168 if (!m_redoStack.empty()) { | 168 if (!m_redoStack.empty()) { |
169 cerr << "CommandHistory::clearing redo stack" << endl; | 169 cerr << "CommandHistory::clearing redo stack" << endl; |
178 | 178 |
179 m_undoStack.push(command); | 179 m_undoStack.push(command); |
180 clipCommands(); | 180 clipCommands(); |
181 | 181 |
182 if (execute) { | 182 if (execute) { |
183 command->execute(); | 183 command->execute(); |
184 } | 184 } |
185 | 185 |
186 // Emit even if we aren't executing the command, because | 186 // Emit even if we aren't executing the command, because |
187 // someone must have executed it for this to make any sense | 187 // someone must have executed it for this to make any sense |
188 emit commandExecuted(); | 188 emit commandExecuted(); |
194 | 194 |
195 void | 195 void |
196 CommandHistory::addToBundle(Command *command, bool execute) | 196 CommandHistory::addToBundle(Command *command, bool execute) |
197 { | 197 { |
198 if (m_currentBundle) { | 198 if (m_currentBundle) { |
199 if (!command || (command->getName() != m_currentBundleName)) { | 199 if (!command || (command->getName() != m_currentBundleName)) { |
200 #ifdef DEBUG_COMMAND_HISTORY | 200 #ifdef DEBUG_COMMAND_HISTORY |
201 cerr << "CommandHistory::addToBundle: " << command->getName() | 201 cerr << "CommandHistory::addToBundle: " << command->getName() |
202 << ": closing current bundle" << endl; | 202 << ": closing current bundle" << endl; |
203 #endif | 203 #endif |
204 closeBundle(); | 204 closeBundle(); |
205 } | 205 } |
206 } | 206 } |
207 | 207 |
208 if (!command) return; | 208 if (!command) return; |
209 | 209 |
210 if (!m_currentBundle) { | 210 if (!m_currentBundle) { |
212 #ifdef DEBUG_COMMAND_HISTORY | 212 #ifdef DEBUG_COMMAND_HISTORY |
213 cerr << "CommandHistory::addToBundle: " << command->getName() | 213 cerr << "CommandHistory::addToBundle: " << command->getName() |
214 << ": creating new bundle" << endl; | 214 << ": creating new bundle" << endl; |
215 #endif | 215 #endif |
216 | 216 |
217 // need to addCommand before setting m_currentBundle, as addCommand | 217 // need to addCommand before setting m_currentBundle, as addCommand |
218 // with bundle false will reset m_currentBundle to 0 | 218 // with bundle false will reset m_currentBundle to 0 |
219 MacroCommand *mc = new BundleCommand(command->getName()); | 219 MacroCommand *mc = new BundleCommand(command->getName()); |
220 m_bundling = true; | 220 m_bundling = true; |
221 addCommand(mc, false); | 221 addCommand(mc, false); |
222 m_bundling = false; | 222 m_bundling = false; |
223 m_currentBundle = mc; | 223 m_currentBundle = mc; |
224 m_currentBundleName = command->getName(); | 224 m_currentBundleName = command->getName(); |
225 } | 225 } |
226 | 226 |
227 #ifdef DEBUG_COMMAND_HISTORY | 227 #ifdef DEBUG_COMMAND_HISTORY |
228 cerr << "CommandHistory::addToBundle: " << command->getName() | 228 cerr << "CommandHistory::addToBundle: " << command->getName() |
229 << ": adding to bundle" << endl; | 229 << ": adding to bundle" << endl; |
270 | 270 |
271 void | 271 void |
272 CommandHistory::addToCompound(Command *command, bool execute) | 272 CommandHistory::addToCompound(Command *command, bool execute) |
273 { | 273 { |
274 if (!m_currentCompound) { | 274 if (!m_currentCompound) { |
275 cerr << "CommandHistory::addToCompound: ERROR: no compound operation in progress!" << endl; | 275 cerr << "CommandHistory::addToCompound: ERROR: no compound operation in progress!" << endl; |
276 return; | 276 return; |
277 } | 277 } |
278 | 278 |
279 #ifdef DEBUG_COMMAND_HISTORY | 279 #ifdef DEBUG_COMMAND_HISTORY |
280 cerr << "CommandHistory::addToCompound[" << m_currentCompound->getName() << "]: " << command->getName() << " (exec: " << execute << ")" << endl; | 280 cerr << "CommandHistory::addToCompound[" << m_currentCompound->getName() << "]: " << command->getName() << " (exec: " << execute << ")" << endl; |
286 | 286 |
287 void | 287 void |
288 CommandHistory::startCompoundOperation(QString name, bool execute) | 288 CommandHistory::startCompoundOperation(QString name, bool execute) |
289 { | 289 { |
290 if (m_currentCompound) { | 290 if (m_currentCompound) { |
291 cerr << "CommandHistory::startCompoundOperation: ERROR: compound operation already in progress!" << endl; | 291 cerr << "CommandHistory::startCompoundOperation: ERROR: compound operation already in progress!" << endl; |
292 cerr << "(name is " << m_currentCompound->getName() << ")" << endl; | 292 cerr << "(name is " << m_currentCompound->getName() << ")" << endl; |
293 return; | 293 return; |
294 } | 294 } |
295 | 295 |
296 #ifdef DEBUG_COMMAND_HISTORY | 296 #ifdef DEBUG_COMMAND_HISTORY |
297 cerr << "CommandHistory::startCompoundOperation: " << name << " (exec: " << execute << ")" << endl; | 297 cerr << "CommandHistory::startCompoundOperation: " << name << " (exec: " << execute << ")" << endl; |
305 | 305 |
306 void | 306 void |
307 CommandHistory::endCompoundOperation() | 307 CommandHistory::endCompoundOperation() |
308 { | 308 { |
309 if (!m_currentCompound) { | 309 if (!m_currentCompound) { |
310 cerr << "CommandHistory::endCompoundOperation: ERROR: no compound operation in progress!" << endl; | 310 cerr << "CommandHistory::endCompoundOperation: ERROR: no compound operation in progress!" << endl; |
311 return; | 311 return; |
312 } | 312 } |
313 | 313 |
314 #ifdef DEBUG_COMMAND_HISTORY | 314 #ifdef DEBUG_COMMAND_HISTORY |
315 cerr << "CommandHistory::endCompoundOperation: " << m_currentCompound->getName() << endl; | 315 cerr << "CommandHistory::endCompoundOperation: " << m_currentCompound->getName() << endl; |
431 | 431 |
432 void | 432 void |
433 CommandHistory::clipCommands() | 433 CommandHistory::clipCommands() |
434 { | 434 { |
435 if (int(m_undoStack.size()) > m_undoLimit) { | 435 if (int(m_undoStack.size()) > m_undoLimit) { |
436 m_savedAt -= (int(m_undoStack.size()) - m_undoLimit); | 436 m_savedAt -= (int(m_undoStack.size()) - m_undoLimit); |
437 } | 437 } |
438 | 438 |
439 clipStack(m_undoStack, m_undoLimit); | 439 clipStack(m_undoStack, m_undoLimit); |
440 clipStack(m_redoStack, m_redoLimit); | 440 clipStack(m_redoStack, m_redoLimit); |
441 } | 441 } |
445 { | 445 { |
446 int i; | 446 int i; |
447 | 447 |
448 if ((int)stack.size() > limit) { | 448 if ((int)stack.size() > limit) { |
449 | 449 |
450 CommandStack tempStack; | 450 CommandStack tempStack; |
451 | 451 |
452 for (i = 0; i < limit; ++i) { | 452 for (i = 0; i < limit; ++i) { |
453 #ifdef DEBUG_COMMAND_HISTORY | 453 #ifdef DEBUG_COMMAND_HISTORY |
454 Command *command = stack.top(); | 454 Command *command = stack.top(); |
455 cerr << "CommandHistory::clipStack: Saving recent command: " << command->getName() << " at " << command << endl; | 455 cerr << "CommandHistory::clipStack: Saving recent command: " << command->getName() << " at " << command << endl; |
456 #endif | 456 #endif |
457 tempStack.push(stack.top()); | 457 tempStack.push(stack.top()); |
458 stack.pop(); | 458 stack.pop(); |
459 } | 459 } |
460 | 460 |
461 clearStack(stack); | 461 clearStack(stack); |
462 | 462 |
463 for (i = 0; i < m_undoLimit; ++i) { | 463 for (i = 0; i < m_undoLimit; ++i) { |
464 stack.push(tempStack.top()); | 464 stack.push(tempStack.top()); |
465 tempStack.pop(); | 465 tempStack.pop(); |
466 } | 466 } |
467 } | 467 } |
468 } | 468 } |
469 | 469 |
470 void | 470 void |
471 CommandHistory::clearStack(CommandStack &stack) | 471 CommandHistory::clearStack(CommandStack &stack) |
472 { | 472 { |
473 while (!stack.empty()) { | 473 while (!stack.empty()) { |
474 Command *command = stack.top(); | 474 Command *command = stack.top(); |
475 // Not safe to call getName() on a command about to be deleted | 475 // Not safe to call getName() on a command about to be deleted |
476 #ifdef DEBUG_COMMAND_HISTORY | 476 #ifdef DEBUG_COMMAND_HISTORY |
477 cerr << "CommandHistory::clearStack: About to delete command " << command << endl; | 477 cerr << "CommandHistory::clearStack: About to delete command " << command << endl; |
478 #endif | 478 #endif |
479 delete command; | 479 delete command; |
480 stack.pop(); | 480 stack.pop(); |
481 } | 481 } |
482 } | 482 } |
483 | 483 |
484 void | 484 void |
485 CommandHistory::undoActivated(QAction *action) | 485 CommandHistory::undoActivated(QAction *action) |
486 { | 486 { |
487 int pos = m_actionCounts[action]; | 487 int pos = m_actionCounts[action]; |
488 for (int i = 0; i <= pos; ++i) { | 488 for (int i = 0; i <= pos; ++i) { |
489 undo(); | 489 undo(); |
490 } | 490 } |
491 } | 491 } |
492 | 492 |
493 void | 493 void |
494 CommandHistory::redoActivated(QAction *action) | 494 CommandHistory::redoActivated(QAction *action) |
495 { | 495 { |
496 int pos = m_actionCounts[action]; | 496 int pos = m_actionCounts[action]; |
497 for (int i = 0; i <= pos; ++i) { | 497 for (int i = 0; i <= pos; ++i) { |
498 redo(); | 498 redo(); |
499 } | 499 } |
500 } | 500 } |
501 | 501 |
502 void | 502 void |
503 CommandHistory::updateActions() | 503 CommandHistory::updateActions() |
504 { | 504 { |
505 m_actionCounts.clear(); | 505 m_actionCounts.clear(); |
506 | 506 |
507 for (int undo = 0; undo <= 1; ++undo) { | 507 for (int undo = 0; undo <= 1; ++undo) { |
508 | 508 |
509 QAction *action(undo ? m_undoAction : m_redoAction); | 509 QAction *action(undo ? m_undoAction : m_redoAction); |
510 QAction *menuAction(undo ? m_undoMenuAction : m_redoMenuAction); | 510 QAction *menuAction(undo ? m_undoMenuAction : m_redoMenuAction); |
511 QMenu *menu(undo ? m_undoMenu : m_redoMenu); | 511 QMenu *menu(undo ? m_undoMenu : m_redoMenu); |
512 CommandStack &stack(undo ? m_undoStack : m_redoStack); | 512 CommandStack &stack(undo ? m_undoStack : m_redoStack); |
513 | 513 |
514 if (stack.empty()) { | 514 if (stack.empty()) { |
515 | 515 |
516 QString text(undo ? tr("Nothing to undo") : tr("Nothing to redo")); | 516 QString text(undo ? tr("Nothing to undo") : tr("Nothing to redo")); |
517 | 517 |
518 action->setEnabled(false); | 518 action->setEnabled(false); |
519 action->setText(text); | 519 action->setText(text); |
520 | 520 |
521 menuAction->setEnabled(false); | 521 menuAction->setEnabled(false); |
522 menuAction->setText(text); | 522 menuAction->setText(text); |
523 | 523 |
524 } else { | 524 } else { |
525 | 525 |
526 action->setEnabled(true); | 526 action->setEnabled(true); |
527 menuAction->setEnabled(true); | 527 menuAction->setEnabled(true); |
528 | 528 |
529 QString commandName = stack.top()->getName(); | 529 QString commandName = stack.top()->getName(); |
530 commandName.replace(QRegExp("&"), ""); | 530 commandName.replace(QRegExp("&"), ""); |
531 | 531 |
532 QString text = (undo ? tr("&Undo %1") : tr("Re&do %1")) | 532 QString text = (undo ? tr("&Undo %1") : tr("Re&do %1")) |
533 .arg(commandName); | 533 .arg(commandName); |
534 | 534 |
535 action->setText(text); | 535 action->setText(text); |
536 menuAction->setText(text); | 536 menuAction->setText(text); |
537 } | 537 } |
538 | 538 |
539 menu->clear(); | 539 menu->clear(); |
540 | 540 |
541 CommandStack tempStack; | 541 CommandStack tempStack; |
542 int j = 0; | 542 int j = 0; |
543 | 543 |
544 while (j < m_menuLimit && !stack.empty()) { | 544 while (j < m_menuLimit && !stack.empty()) { |
545 | 545 |
546 Command *command = stack.top(); | 546 Command *command = stack.top(); |
547 tempStack.push(command); | 547 tempStack.push(command); |
548 stack.pop(); | 548 stack.pop(); |
549 | 549 |
550 QString commandName = command->getName(); | 550 QString commandName = command->getName(); |
551 commandName.replace(QRegExp("&"), ""); | 551 commandName.replace(QRegExp("&"), ""); |
552 | 552 |
553 QString text; | 553 QString text; |
554 if (undo) text = tr("&Undo %1").arg(commandName); | 554 if (undo) text = tr("&Undo %1").arg(commandName); |
555 else text = tr("Re&do %1").arg(commandName); | 555 else text = tr("Re&do %1").arg(commandName); |
556 | 556 |
557 QAction *action = menu->addAction(text); | 557 QAction *action = menu->addAction(text); |
558 m_actionCounts[action] = j++; | 558 m_actionCounts[action] = j++; |
559 } | 559 } |
560 | 560 |
561 while (!tempStack.empty()) { | 561 while (!tempStack.empty()) { |
562 stack.push(tempStack.top()); | 562 stack.push(tempStack.top()); |
563 tempStack.pop(); | 563 tempStack.pop(); |
564 } | 564 } |
565 } | 565 } |
566 } | 566 } |
567 | 567 |