Example: Using the IlvManagerCommand Class to Undo/Repeat

This subsection shows the implementation of the IlvTranslateObjectCommand class, subclass of IlvManagerCommand

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 ::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;
}