Cancel in python

first of all .. sorry if my english was bad. its my third language
im working on a drawing software that draws images and saves them again (for commenting on sentences). I use heap and wxpython. but they still have problems with some features.
What is the ideal way to make a cancellation option?
another question .. when the user scales the image (increasing the frame of the drawing) the lines do not scale. How can i do this.

I got rid of all these problems by saving temporary images to the hard drive whenever the user ends the line and assigns a new image (the old one with a line on it) to this frame. cancel and redo will be done by switching between these images ... therefore, when the user scale image is scaled, the line will also scale. but this is bad because it takes up a lot of space on the hard drive (when you draw 1000 lines) and it is slow because it assigns a new image every time the user draws a line

hope my idea is clear.

Does anyone have a better solution?

+4
source share
2 answers

The canonical strategy is to use the Team Template . You will represent what you can do as Command objects, and each object is pushed onto the stack. The state of the application is then determined by the initial state plus everything that the stack has. Thus, the undo operation then simply pops the top element of the stack and reapplies the remaining elements to the original state.

In practice, sometimes it is worth continuing to apply these operations to the initial state to generate the current state. For example, this might be true with something like a complex series of image settings, as you can find in Photoshop. In such cases, an alternating series of states is usually stored in memory:

+---------+ | state_0 | +---------+ +---------+ | next -------> | stack_0 | +---------+ +---------+ | data | +---------+ | next -------> | state_1 | +---------+ +---------+ | next ----- ... +---------+ 

Each stack_i contains instructions until it exceeds some predefined complexity (for example, instructions exceed the computational cost of X ) or a sequence number (for example, an X restriction or more instructions is kept on the stack). At this point, a new intermediate state object state_i+1 to encapsulate the state and a new empty stack stack_i+1 to store new commands.

Thus, you need to apply a small sequence of commands to the last state of the snapshot to get the current state. This happens by storing entire states, which may not be possible for very large applications, but you can choose a snapshot just to optimize the state.

+12
source

Also, keep in mind that Python functions are first class objects that can make the implementation of the Command pattern very smooth:

 actions = [] def do_action1(arg1, arg2): # .. do the action .. # remember we did the action: actions.append(do_action1, (arg1, arg2)) def do_action2(arg1, arg2): # .. do the action .. actions.append(do_action2, (arg1, arg2)) def replay_actions(): for fn, args in actions: fn(*args) 
+7
source

All Articles