The CommandHistory class stores a list of executed commands and maintains Undo and Redo actions synchronised with those commands. More...

#include <CommandHistory.h>

Inheritance diagram for CommandHistory:
Inheritance graph
Collaboration diagram for CommandHistory:
Collaboration graph

Public Slots

virtual void documentSaved ()
 Checkpoint function that should be called when the document is saved. More...
 
void addExecutedCommand (Command *)
 Add a command to the history that has already been executed, without executing it again. More...
 
void addCommandAndExecute (Command *)
 Add a command to the history and also execute it. More...
 
void undo ()
 
void redo ()
 

Signals

void commandExecuted ()
 Emitted whenever a command has just been executed or unexecuted, whether by addCommand, undo, or redo. More...
 
void commandExecuted (Command *)
 Emitted whenever a command has just been executed, whether by addCommand or redo. More...
 
void commandUnexecuted (Command *)
 Emitted whenever a command has just been unexecuted, whether by addCommand or undo. More...
 
void documentRestored ()
 Emitted when the undo/redo stack has reached the same state at which the documentSaved slot was last called. More...
 
void activity (QString)
 Emitted when some activity happened (for activity logging). More...
 

Public Member Functions

virtual ~CommandHistory ()
 
void clear ()
 
void registerMenu (QMenu *menu)
 
void registerToolbar (QToolBar *toolbar)
 
void addCommand (Command *command)
 Add a command to the command history. More...
 
void addCommand (Command *command, bool execute, bool bundle=false)
 Add a command to the command history. More...
 
int getUndoLimit () const
 Return the maximum number of items in the undo history. More...
 
void setUndoLimit (int limit)
 Set the maximum number of items in the undo history. More...
 
int getRedoLimit () const
 Return the maximum number of items in the redo history. More...
 
void setRedoLimit (int limit)
 Set the maximum number of items in the redo history. More...
 
int getMenuLimit () const
 Return the maximum number of items visible in undo and redo menus. More...
 
void setMenuLimit (int limit)
 Set the maximum number of items in the menus. More...
 
int getBundleTimeout () const
 Return the time after which a bundle will be closed if nothing is added. More...
 
void setBundleTimeout (int msec)
 Set the time after which a bundle will be closed if nothing is added. More...
 
void startCompoundOperation (QString name, bool execute)
 Start recording commands to batch up into a single compound command. More...
 
void endCompoundOperation ()
 Finish recording commands and store the compound command. More...
 

Static Public Member Functions

static CommandHistorygetInstance ()
 

Protected Types

typedef std::stack< Command * > CommandStack
 

Protected Slots

void undoActivated (QAction *)
 
void redoActivated (QAction *)
 
void bundleTimerTimeout ()
 

Protected Member Functions

 CommandHistory ()
 
void addToCompound (Command *command, bool execute)
 
void addToBundle (Command *command, bool execute)
 
void closeBundle ()
 
void updateActions ()
 
void clipCommands ()
 
void clipStack (CommandStack &stack, int limit)
 
void clearStack (CommandStack &stack)
 

Protected Attributes

QAction * m_undoAction
 
QAction * m_redoAction
 
QAction * m_undoMenuAction
 
QAction * m_redoMenuAction
 
QMenu * m_undoMenu
 
QMenu * m_redoMenu
 
std::map< QAction *, int > m_actionCounts
 
CommandStack m_undoStack
 
CommandStack m_redoStack
 
int m_undoLimit
 
int m_redoLimit
 
int m_menuLimit
 
int m_savedAt
 
MacroCommand * m_currentCompound
 
bool m_executeCompound
 
MacroCommand * m_currentBundle
 
bool m_bundling
 
QString m_currentBundleName
 
QTimer * m_bundleTimer
 
int m_bundleTimeout
 

Static Protected Attributes

static CommandHistorym_instance = nullptr
 

Detailed Description

The CommandHistory class stores a list of executed commands and maintains Undo and Redo actions synchronised with those commands.

CommandHistory allows you to associate more than one Undo and Redo menu or toolbar with the same command history, and it keeps them all up-to-date at once. This makes it effective in systems where multiple views may be editing the same data.

Definition at line 52 of file CommandHistory.h.

Member Typedef Documentation

typedef std::stack<Command *> CommandHistory::CommandStack
protected

Definition at line 202 of file CommandHistory.h.

Constructor & Destructor Documentation

CommandHistory::~CommandHistory ( )
virtual

Definition at line 90 of file CommandHistory.cpp.

References clearStack(), m_redoMenu, m_redoStack, m_savedAt, m_undoMenu, and m_undoStack.

CommandHistory::CommandHistory ( )
protected

Member Function Documentation

void CommandHistory::clear ( )
void CommandHistory::registerMenu ( QMenu *  menu)

Definition at line 121 of file CommandHistory.cpp.

References m_redoAction, and m_undoAction.

void CommandHistory::registerToolbar ( QToolBar *  toolbar)

Definition at line 128 of file CommandHistory.cpp.

References m_redoMenuAction, and m_undoMenuAction.

Referenced by ModelDataTableDialog::ModelDataTableDialog().

void CommandHistory::addCommand ( Command *  command,
bool  execute,
bool  bundle = false 
)

Add a command to the command history.

If execute is true, the command will be executed before being added. Otherwise it will be assumed to have been already executed – a command should not be added to the history unless its work has actually been done somehow!

If a compound operation is in use (see startCompoundOperation below), the execute value passed to this method will override the execute status of the compound operation. In this way it's possible to have a compound operation mixing both to-execute and pre-executed commands.

If bundle is true, the command will be a candidate for bundling with any adjacent bundleable commands that have the same name, into a single compound command. This is useful for small commands that may be executed repeatedly altering the same data (e.g. type text, set a parameter) whose number and extent is not known in advance. The bundle parameter will be ignored if a compound operation is already in use.

Definition at line 148 of file CommandHistory.cpp.

References activity(), addToBundle(), addToCompound(), clearStack(), clipCommands(), closeBundle(), commandExecuted(), m_bundling, m_currentBundle, m_currentCompound, m_redoStack, m_savedAt, m_undoStack, and updateActions().

int CommandHistory::getUndoLimit ( ) const
inline

Return the maximum number of items in the undo history.

Definition at line 101 of file CommandHistory.h.

References m_undoLimit, and setUndoLimit().

void CommandHistory::setUndoLimit ( int  limit)

Set the maximum number of items in the undo history.

Definition at line 396 of file CommandHistory.cpp.

References clipCommands(), and m_undoLimit.

Referenced by getUndoLimit().

int CommandHistory::getRedoLimit ( ) const
inline

Return the maximum number of items in the redo history.

Definition at line 107 of file CommandHistory.h.

References m_redoLimit, and setRedoLimit().

void CommandHistory::setRedoLimit ( int  limit)

Set the maximum number of items in the redo history.

Definition at line 405 of file CommandHistory.cpp.

References clipCommands(), and m_redoLimit.

Referenced by getRedoLimit().

int CommandHistory::getMenuLimit ( ) const
inline

Return the maximum number of items visible in undo and redo menus.

Definition at line 113 of file CommandHistory.h.

References m_menuLimit, and setMenuLimit().

void CommandHistory::setMenuLimit ( int  limit)

Set the maximum number of items in the menus.

Definition at line 414 of file CommandHistory.cpp.

References m_menuLimit, and updateActions().

Referenced by getMenuLimit().

int CommandHistory::getBundleTimeout ( ) const
inline
void CommandHistory::setBundleTimeout ( int  msec)

Set the time after which a bundle will be closed if nothing is added.

Definition at line 421 of file CommandHistory.cpp.

References m_bundleTimeout.

Referenced by getBundleTimeout().

void CommandHistory::startCompoundOperation ( QString  name,
bool  execute 
)

Start recording commands to batch up into a single compound command.

Definition at line 289 of file CommandHistory.cpp.

References closeBundle(), m_currentCompound, and m_executeCompound.

Referenced by Pane::editSelectionEnd(), and getBundleTimeout().

void CommandHistory::endCompoundOperation ( )

Finish recording commands and store the compound command.

Definition at line 308 of file CommandHistory.cpp.

References addCommand(), and m_currentCompound.

Referenced by Pane::editSelectionEnd(), and getBundleTimeout().

void CommandHistory::documentSaved ( )
virtualslot

Checkpoint function that should be called when the document is saved.

If the undo/redo stack later returns to the point at which the document was saved, the documentRestored signal will be emitted.

Definition at line 427 of file CommandHistory.cpp.

References closeBundle(), m_savedAt, and m_undoStack.

Referenced by getBundleTimeout().

void CommandHistory::addExecutedCommand ( Command *  command)
slot

Add a command to the history that has already been executed, without executing it again.

Equivalent to addCommand(command, false).

Definition at line 332 of file CommandHistory.cpp.

References addCommand().

Referenced by getBundleTimeout().

void CommandHistory::addCommandAndExecute ( Command *  command)
slot

Add a command to the history and also execute it.

Equivalent to addCommand(command, true).

Definition at line 338 of file CommandHistory.cpp.

References addCommand().

Referenced by getBundleTimeout().

void CommandHistory::redo ( )
slot
void CommandHistory::undoActivated ( QAction *  action)
protectedslot

Definition at line 488 of file CommandHistory.cpp.

References m_actionCounts, and undo().

Referenced by CommandHistory(), and getBundleTimeout().

void CommandHistory::redoActivated ( QAction *  action)
protectedslot

Definition at line 497 of file CommandHistory.cpp.

References m_actionCounts, and redo().

Referenced by CommandHistory(), and getBundleTimeout().

void CommandHistory::bundleTimerTimeout ( )
protectedslot

Definition at line 263 of file CommandHistory.cpp.

References closeBundle(), and m_currentBundle.

Referenced by addToBundle(), and getBundleTimeout().

void CommandHistory::commandExecuted ( )
signal

Emitted whenever a command has just been executed or unexecuted, whether by addCommand, undo, or redo.

Referenced by addCommand(), addToBundle(), getBundleTimeout(), redo(), and undo().

void CommandHistory::commandExecuted ( Command *  )
signal

Emitted whenever a command has just been executed, whether by addCommand or redo.

void CommandHistory::commandUnexecuted ( Command *  )
signal

Emitted whenever a command has just been unexecuted, whether by addCommand or undo.

Referenced by getBundleTimeout(), and undo().

void CommandHistory::documentRestored ( )
signal

Emitted when the undo/redo stack has reached the same state at which the documentSaved slot was last called.

Referenced by getBundleTimeout(), redo(), and undo().

void CommandHistory::activity ( QString  )
signal

Emitted when some activity happened (for activity logging).

Referenced by addCommand(), closeBundle(), getBundleTimeout(), redo(), and undo().

void CommandHistory::addToCompound ( Command *  command,
bool  execute 
)
protected

Definition at line 273 of file CommandHistory.cpp.

References m_currentCompound.

Referenced by addCommand().

void CommandHistory::addToBundle ( Command *  command,
bool  execute 
)
protected
void CommandHistory::closeBundle ( )
protected
void CommandHistory::updateActions ( )
protected
void CommandHistory::clipCommands ( )
protected
void CommandHistory::clipStack ( CommandStack stack,
int  limit 
)
protected

Definition at line 445 of file CommandHistory.cpp.

References clearStack(), and m_undoLimit.

Referenced by clipCommands().

void CommandHistory::clearStack ( CommandStack stack)
protected

Definition at line 472 of file CommandHistory.cpp.

Referenced by addCommand(), clear(), clipStack(), and ~CommandHistory().

Member Data Documentation

CommandHistory * CommandHistory::m_instance = nullptr
staticprotected

Definition at line 191 of file CommandHistory.h.

Referenced by getInstance().

QAction* CommandHistory::m_undoAction
protected

Definition at line 193 of file CommandHistory.h.

Referenced by CommandHistory(), registerMenu(), and updateActions().

QAction* CommandHistory::m_redoAction
protected

Definition at line 194 of file CommandHistory.h.

Referenced by CommandHistory(), registerMenu(), and updateActions().

QAction* CommandHistory::m_undoMenuAction
protected

Definition at line 195 of file CommandHistory.h.

Referenced by CommandHistory(), registerToolbar(), and updateActions().

QAction* CommandHistory::m_redoMenuAction
protected

Definition at line 196 of file CommandHistory.h.

Referenced by CommandHistory(), registerToolbar(), and updateActions().

QMenu* CommandHistory::m_undoMenu
protected

Definition at line 197 of file CommandHistory.h.

Referenced by CommandHistory(), updateActions(), and ~CommandHistory().

QMenu* CommandHistory::m_redoMenu
protected

Definition at line 198 of file CommandHistory.h.

Referenced by CommandHistory(), updateActions(), and ~CommandHistory().

std::map<QAction *, int> CommandHistory::m_actionCounts
protected

Definition at line 200 of file CommandHistory.h.

Referenced by redoActivated(), undoActivated(), and updateActions().

CommandStack CommandHistory::m_undoStack
protected
CommandStack CommandHistory::m_redoStack
protected
int CommandHistory::m_undoLimit
protected

Definition at line 206 of file CommandHistory.h.

Referenced by clipCommands(), clipStack(), getUndoLimit(), and setUndoLimit().

int CommandHistory::m_redoLimit
protected

Definition at line 207 of file CommandHistory.h.

Referenced by clipCommands(), getRedoLimit(), and setRedoLimit().

int CommandHistory::m_menuLimit
protected

Definition at line 208 of file CommandHistory.h.

Referenced by getMenuLimit(), setMenuLimit(), and updateActions().

int CommandHistory::m_savedAt
protected
MacroCommand* CommandHistory::m_currentCompound
protected
bool CommandHistory::m_executeCompound
protected

Definition at line 212 of file CommandHistory.h.

Referenced by addCommand(), and startCompoundOperation().

MacroCommand* CommandHistory::m_currentBundle
protected

Definition at line 215 of file CommandHistory.h.

Referenced by addCommand(), addToBundle(), bundleTimerTimeout(), and closeBundle().

bool CommandHistory::m_bundling
protected

Definition at line 216 of file CommandHistory.h.

Referenced by addCommand(), and addToBundle().

QString CommandHistory::m_currentBundleName
protected

Definition at line 217 of file CommandHistory.h.

Referenced by addToBundle(), and closeBundle().

QTimer* CommandHistory::m_bundleTimer
protected

Definition at line 218 of file CommandHistory.h.

Referenced by addToBundle().

int CommandHistory::m_bundleTimeout
protected

Definition at line 219 of file CommandHistory.h.

Referenced by addToBundle(), getBundleTimeout(), and setBundleTimeout().


The documentation for this class was generated from the following files: