2D Graphics > Managers > Advanced Manager Features > Undoing and Redoing Actions
 
Undoing and Redoing Actions
This section describes how to implement the undo/redo process with the IlvManagerCommand class.
To remember every action that your program user may apply to objects (and the objects as well), the manager creates specific instances of the IlvManagerCommand class, depending on what kind of action was required. The manager can then manipulate a stack of these commands. A request for IlvManager::unDo pops an item off the stack, and applies the inverse operation that created the popped item.
The IlvManager::repeat operation duplicates the topmost item of the command stack and executes the operation again.
This section is divided as follows:
*Command Class
*Managing Undo
*Example: Using the IlvManagerCommand Class to Undo/Repeat
*Managing Modifications
Command Class
Each ready-to-use command in Rogue Wave Views was implemented with the IlvManagerCommand class. To carry out undo/repeat operations, the subtypes of this class merely store the arguments of commands. The actual command to be remembered is known by the type of the IlvManagerCommand objects.
If you create a new operation for the manager and you want to undo and repeat it, you have to create a specific subtype of the IlvManagerCommand class. A complete example of this subtyping is described in Example: Using the IlvManagerCommand Class to Undo/Repeat.
Managing Undo
The following IlvManager member functions handle undo operations:
*IlvManager::addCommand
*IlvManager::isUndoEnabled
*IlvManager::setUndoEnabled
*IlvManager::forgetUndo
*IlvManager::repeat
*IlvManager::unDo
Each action applied to manager objects is inserted in a special queue maintained by each IlvManager instance. The undo/repeat process is based on this queue management.
Example: Using the IlvManagerCommand Class to Undo/Repeat
This subsection shows the implementation of the IlvTranslateObjectCommand class, subclass ofIlvManagerCommand
IlvTranslateObjectCommand::IlvTranslateObjectCommand(IlvManager* manager,
IlvGraphic* object,
const IlvPoint& dp)
: IlvManagerCommand(manager),
_dx(dp.x()),
_dy(dp.y()),
_object(object)
{}
 
The constructor of this class stores the parameters of the translation:
 
operation:IlvTranslateObjectCommand::IlvTranslateObjectCommand(IlvActionHistory* h,
IlvGraphic* object,
const IlvPoint& dp)
: IlvCommand(h,undoable,IlFalse,IlTrue,IlFalse),
_dx(dp.x()),
_dy(dp.y()),
_object(object)
{}
 
IlvTranslateObjectCommand::IlvTranslateObjectCommand(const IlvTranslateObjectCommand& cmd):IlvCommand(cmd) {
_dx = cmd._dx;
_dy = cmd._dy;
_object = cmd._object;
}
executeIt Member Function
The IlvTranslateObjectCommand::executeIt (called to execute the command) member function is implemented as follows:
void
IlvTranslateObjectCommand::executeIt()
{
IlvManager* mgr = (getContext())? getContext()->getManager() : 0;
if (mgr)
mgr->translateObject(_object, _dx, _dy, IlTrue);
}
The operation to be performed is the translation of the object by _dx and _dy.
unDoIt Member Function
The IlvTranslateObjectCommand::unDoIt member function is as follows:
void
IlvTranslateObjectCommand::undoIt()
{
IlvManager* mgr = (getContext())? getContext()->getManager() : 0;
if (mgr)
mgr->translateObject(_object, -_dx, -_dy, IlTrue);
}
The inverse translation is applied and the regions are redrawn.
Note: You must define a copy constructor as follows:
IlvTranslateObjectCommand::IlvTranslateObjectCommand(const IlvTranslateObjectCommand& cmd):IlvCommand(cmd) {
_dx = cmd._dx;
_dy = cmd._dy;
_object = cmd._object;
}
Managing Modifications
The following IlvManager member functions let you manage the state of objects (modified or not) handled by the manager:
*IlvManager::isModified
*IlvManager::setModified
*IlvManager::contentsChanged
Example: Setting the State of a Manager to Unmodified
manager->setModified(IlFalse);
There are also two global functions:
*IlvGetContentsChangedUpdate
*IlvSetContentsChangedUpdate
Example: Disallowing View Hook Calls in contentsChanged
The following code disallows the calls to the IlvManager::contentsChanged member functions of the existing view hooks associated with the manager view:
IlvSetContentsChangedUpdate(IlTrue);

Version 6.2.1
Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.