kspread
Commands
To implement undo and redo functionality, every possible action by the user for editing and manipulating the document is encapsulated in a command (based on QUndoCommand).
There is one command class (which will be instantiated) for every unique action. You need to reimplement the redo() and undo() methods of QUndoCommand.
Each command is created from the user interface. The commands working on cell ranges, i.e. derivatives of AbstractRegionCommand, have to check wether the operation is allowed before execution. Cells may be protected or locked as elements of matrices. AbstractRegionCommand::execute takes care of that. If the operation is not allowed, the command will neither get executed nor added to the undo stack. All other commands can be added directly to the undo stack by using Doc::addCommand, which executes the operation immediately.
This is an example of typical use of a command NOT processing cell ranges:
QUndoCommand* command = new RenameSheetCommand( sheet, name ); doc->addCommand( command );
This is an example of typical use of a command, which works on a cell range:
CommentCommand* command = new CommentCommand(); command->setSheet( sheet ); command->setName( i18n( "Add Comment" ) ); command->setComment( comment ); command->add( cellRegion ); command->execute();
Then whenever the user triggers an "undo", the corresponding undo() method of the command is called by the undo action, thereby reverting the previously executed command. Similar thing happens for the "redo".
All command classes reside in the subdirectory commands/.
- See also:
- Doc::addCommand
