PDA

View Full Version : Ideas about how to implement Undo and Redo actions?



pir
21st July 2006, 08:07
Hi!

I am looking for ideas how to implement undo/redo functionality in my program. Maybe there are some tricks...

My program:
Is a 3d graphic program where the user can look at 3d objects in a scene and transform them, cut them and do other operations like rotate around it with the camera. Cutting an object means drawing a polygon on the object and delete parts of it and by that creating a new shape ( like in PhotoShop ).

So i suppose I need some kind of structure that represents some kind of history of actions on the 3d scene as a linked list of nodes containg the actions... but it would be quite slow for operations that need a lot of calculation.

thanks for reading
pir

jpn
21st July 2006, 08:29
A common way to implement undo/redo functionality is to encapsulate all undoable/redoable actions into commands and store them into a stack.

The command interface could look at simplest like this:


class Command
{
public:
Command();
virtual ~Command();

virtual undo() = 0;
virtual redo() = 0;
};

Subclass and implement the undo() and redo() methods appropriately.

Search Google on "command design pattern", and take a look at Qt 4.2's Undo Framework (http://doc.trolltech.com/4.2/qundo.html).

fullmetalcoder
21st July 2006, 08:32
I think Qt 4.2 offers the framework you're looking for : Undo framework (http://doc.trolltech.com/4.2/qundo.html#undo-framework)

AFAIUnderstand you'll need to subclass QUndoCommand, to create an undo stack, to connect a few actions and everything will work... :)
The hardest part will certainly be implementing the undo() / redo() methods of QUndoCommand... Good luck!

pir
21st July 2006, 09:27
Thanks! Found a lot on Command pattern... but it made me think. It's often the case that you want to look for design patterns fitting your problem but do not know what it might be called or where to look. Is there any good websites that list the most common design patterns in a way making it easy to search?

Another design pattern I would like to find is how to handle transformations on components in a 3d world... at the moment I'm merging the matrices, but it should be better to make each component have some kind of transformation history having different levels of transformations. This would make operations like group/ungroup, chaning pivot points a lot easier.

thanks for reading
pir