best practice to implement undo/redo

Zunbeltz wrote:

I am planning to implement undo/redo capabilites in my application. What
ways are in wxpython to do this?

There isn't anything specific in wxPython. Your actual implementation
will depend on your underlying data structure and format.

The simplest brute force method is to save a copy of the data with
each step taken and switch to it on undo/redo.

A more thorough approach is to store a transaction log. You store
the data in its current state, and then have a list of function
calls and parameters to undo/redo.

For example, say the user types in "Hello", and then deletes item 3.
You would store the results of that, and a transaction log of:

   data.Add, 3, "whatever it was"
   data.Remove, "Hello"

Note the transactions are the opposite of what the user did, but applying
them to the current data will get you back to where you were.

You can get more sophisticated by storing a description of each change
and if done really well even allow applying them in a different order.
You can also save the undo/redo log to disk.

Roger

Zunbeltz wrote:

   * The undo/redo stack shuld be global.

You can use publish/subscribe instead. Any place that makes changes
can publish them, and there would be one subscriber that keeps the
log. (Normally the subscribers outnumber the publishers, but this
works just as well. It also allows a seamless way of doing testing/
debugging by adding in another subscriber without touching any other
code).

   * The callback funtions should be methods of the objects? (how can i
store the method and the objects? this mehtods are best coded as
classmethods instead of instance methods?)

I would aim for a goal of being able to save the undo/redo log to disk.
Users love being able to continue with undo/redo information from
the previous session. You didn't state what your application does
or what data it operates on, so you'll have to judge if that is
appropriate.

If you can identify each piece of information with a unique id, then
having a text log will work well. An example would be if you were
implementing an addressbook. You could ensure that every entry,
and every piece of information for an entry (eg name, phonenumber,
address) got a unique id. The log would then specify the id of the
item the action happened on.

Doing it for a word processor app is actually quite hard since
you can't really assign unique unchanging ids.

Is there any on-line tutorial or material about this topic (in general or
in python)?

No. The details are very specific to how you store your data, what
that data is, and what your app does.

p.s. I'm planing to read "Design Patterns" and learn more on the topic.
Would it be helpfull for the design of this things=

The design patterns books are highly recommended independent of this topic.
They show you years of best practise for expressing particular solutions,
make you aware of issues you may not have thought of, and help give you
a vocabulary that lets you communicate with other programmers about how
you are solving a problem. (eg if someone says they will be using a
singleton, a factory and the observer pattern to solve a problem then
you will know precisely what they mean).

Roger