27 #include "base/Command.h" 50 m_executeCompound(false),
56 m_undoAction =
new QAction(QIcon(
":/icons/undo.png"), tr(
"&Undo"),
this);
58 m_undoAction->setStatusTip(tr(
"Undo the last editing operation"));
61 m_undoMenuAction =
new QAction(QIcon(
":/icons/undo.png"), tr(
"&Undo"),
this);
66 connect(
m_undoMenu, SIGNAL(triggered(QAction *)),
69 m_redoAction =
new QAction(QIcon(
":/icons/redo.png"), tr(
"Re&do"),
this);
71 m_redoAction->setStatusTip(tr(
"Redo the last operation that was undone"));
74 m_redoMenuAction =
new QAction(QIcon(
":/icons/redo.png"), tr(
"Re&do"),
this);
79 connect(
m_redoMenu, SIGNAL(triggered(QAction *)),
103 #ifdef DEBUG_COMMAND_HISTORY 104 cerr <<
"CommandHistory::clear()" << endl;
130 if (!command)
return;
143 if (!command)
return;
145 #ifdef DEBUG_COMMAND_HISTORY 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;
161 #ifdef DEBUG_COMMAND_HISTORY 163 cerr <<
"CommandHistory::clearing redo stack" << endl;
194 #ifdef DEBUG_COMMAND_HISTORY 195 cerr <<
"CommandHistory::addToBundle: " << command->getName()
196 <<
": closing current bundle" << endl;
202 if (!command)
return;
206 #ifdef DEBUG_COMMAND_HISTORY 207 cerr <<
"CommandHistory::addToBundle: " << command->getName()
208 <<
": creating new bundle" << endl;
213 MacroCommand *mc =
new BundleCommand(command->getName());
221 #ifdef DEBUG_COMMAND_HISTORY 222 cerr <<
"CommandHistory::addToBundle: " << command->getName()
223 <<
": adding to bundle" << endl;
226 if (execute) command->execute();
246 #ifdef DEBUG_COMMAND_HISTORY 247 cerr <<
"CommandHistory::closeBundle" << endl;
258 #ifdef DEBUG_COMMAND_HISTORY 259 cerr <<
"CommandHistory::bundleTimerTimeout: bundle is " <<
m_currentBundle << endl;
269 cerr <<
"CommandHistory::addToCompound: ERROR: no compound operation in progress!" << endl;
273 #ifdef DEBUG_COMMAND_HISTORY 274 cerr <<
"CommandHistory::addToCompound[" <<
m_currentCompound->getName() <<
"]: " << command->getName() <<
" (exec: " << execute <<
")" << endl;
277 if (execute) command->execute();
285 cerr <<
"CommandHistory::startCompoundOperation: ERROR: compound operation already in progress!" << endl;
290 #ifdef DEBUG_COMMAND_HISTORY 291 cerr <<
"CommandHistory::startCompoundOperation: " << name <<
" (exec: " << execute <<
")" << endl;
304 cerr <<
"CommandHistory::endCompoundOperation: ERROR: no compound operation in progress!" << endl;
308 #ifdef DEBUG_COMMAND_HISTORY 309 cerr <<
"CommandHistory::endCompoundOperation: " <<
m_currentCompound->getName() << endl;
315 if (toAdd->haveCommands()) {
341 #ifdef DEBUG_COMMAND_HISTORY 342 cerr <<
"CommandHistory::undo()" << endl;
348 command->unexecute();
351 emit
activity(tr(
"Undo %1").arg(command->getName()));
367 #ifdef DEBUG_COMMAND_HISTORY 368 cerr <<
"CommandHistory::redo()" << endl;
377 emit
activity(tr(
"Redo %1").arg(command->getName()));
442 if ((
int)stack.size() > limit) {
446 for (i = 0; i < limit; ++i) {
447 #ifdef DEBUG_COMMAND_HISTORY 448 Command *command = stack.top();
449 cerr <<
"CommandHistory::clipStack: Saving recent command: " << command->getName() <<
" at " << command << endl;
451 tempStack.push(stack.top());
458 stack.push(tempStack.top());
467 while (!stack.empty()) {
468 Command *command = stack.top();
470 #ifdef DEBUG_COMMAND_HISTORY 471 cerr <<
"CommandHistory::clearStack: About to delete command " << command << endl;
482 for (
int i = 0; i <= pos; ++i) {
491 for (
int i = 0; i <= pos; ++i) {
510 QString text(
undo ? tr(
"Nothing to undo") : tr(
"Nothing to redo"));
512 action->setEnabled(
false);
513 action->setText(text);
515 menuAction->setEnabled(
false);
516 menuAction->setText(text);
520 action->setEnabled(
true);
521 menuAction->setEnabled(
true);
523 QString commandName = stack.top()->getName();
524 commandName.replace(QRegExp(
"&"),
"");
526 QString text = (
undo ? tr(
"&Undo %1") : tr(
"Re&do %1"))
529 action->setText(text);
530 menuAction->setText(text);
540 Command *command = stack.top();
541 tempStack.push(command);
544 QString commandName = command->getName();
545 commandName.replace(QRegExp(
"&"),
"");
548 if (
undo) text = tr(
"&Undo %1").arg(commandName);
549 else text = tr(
"Re&do %1").arg(commandName);
551 QAction *action = menu->addAction(text);
555 while (!tempStack.empty()) {
556 stack.push(tempStack.top());
void addToCompound(Command *command, bool execute)
void setBundleTimeout(int msec)
Set the time after which a bundle will be closed if nothing is added.
void bundleTimerTimeout()
void addExecutedCommand(Command *)
Add a command to the history that has already been executed, without executing it again.
void addCommandAndExecute(Command *)
Add a command to the history and also execute it.
void startCompoundOperation(QString name, bool execute)
Start recording commands to batch up into a single compound command.
void endCompoundOperation()
Finish recording commands and store the compound command.
std::stack< Command * > CommandStack
void addCommand(Command *command)
Add a command to the command history.
void setUndoLimit(int limit)
Set the maximum number of items in the undo history.
void activity(QString)
Emitted when some activity happened (for activity logging).
void setMenuLimit(int limit)
Set the maximum number of items in the menus.
QAction * m_redoMenuAction
QAction * m_undoMenuAction
void documentRestored()
Emitted when the undo/redo stack has reached the same state at which the documentSaved slot was last ...
void registerToolbar(QToolBar *toolbar)
void commandUnexecuted(Command *)
Emitted whenever a command has just been unexecuted, whether by addCommand or undo.
void redoActivated(QAction *)
std::map< QAction *, int > m_actionCounts
void undoActivated(QAction *)
The CommandHistory class stores a list of executed commands and maintains Undo and Redo actions synch...
static CommandHistory * getInstance()
void registerMenu(QMenu *menu)
static CommandHistory * m_instance
void setRedoLimit(int limit)
Set the maximum number of items in the redo history.
void commandExecuted()
Emitted whenever a command has just been executed or unexecuted, whether by addCommand,...
virtual ~CommandHistory()
void clipStack(CommandStack &stack, int limit)
void addToBundle(Command *command, bool execute)
MacroCommand * m_currentBundle
virtual void documentSaved()
Checkpoint function that should be called when the document is saved.
MacroCommand * m_currentCompound
QString m_currentBundleName
void clearStack(CommandStack &stack)