在python中进行撤消 [英] making undo in python

查看:220
本文介绍了在python中进行撤消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先......抱歉,如果我的英语不好。它是我的第三语言


我正在使用绘制软件绘制图像并再次保存它们(用于评论建议)
我使用pile和wxpython。
但我仍然遇到一些功能问题..

什么是做出撤销选项的理想方法?

另一个问题..当用户缩放图片时(通过放大图框)
线条不会缩放。我是如何实现这一目标的。

first of all .. sorry if my english was bad. its my 3rd language
im working on a paint software that draw over images and save them again ( for commenting propose )
i use pile and wxpython. but im still having problems with some features ..
what is the ideal way to make the undo option ?
another question .. when the user scale the picture ( by enlarging the drawing frame) the lines doesn't scale . how do i make that happen .

我通过在用户完成一行并将其分配给新硬盘时将临时图像保存到硬盘中来解决所有这些问题画面(旧画面上有一条线)到画面。
undo和redo将通过在这些图像之间切换来完成...
所以当用户缩放图像时,线条也会缩放。但这很糟糕,因为它需要大量的硬盘空间(当你绘制1000行时)并且它很慢,因为它每次用户画线时都会分配一个新图像

i got rid of all those problems by saving temporary images to the hard disk whenever the user finishes a line and assign that new picture ( the old one with a line on it) to the frame . undo and redo will is done by switching between those images ... so when user scale image the line will scale too . but that is bad since it takes a lot of HDD space ( when you draw 1000 lines ) and its slow because it assigns a new image every time a user draw a line

希望我的想法很清楚

有没有人有更好的解决方案?

does anyone have a better solution ?

推荐答案

规范策略是使用 命令模式 。您将代表可以作为Command对象执行的操作,并将每个对象放在堆栈上。然后,应用程序的状态由初始状态加上堆栈具有的所有内容定义。因此,撤消操作然后只是弹出顶部堆栈项目并将剩余项目重新应用到初始状态。

The canonical strategy is to use the Command pattern. You'll represent the things you can do as Command objects, and each object is placed on a stack. The state of the application is then defined by an initial state plus everything that the stack has. Thus, the "undo" operation is then just popping the top stack item and reapplying the remaining items to the initial state.

实际上,有时继续应用这些项目是昂贵的操作到初始状态以生成当前状态。例如,对于一系列复杂的图像调整,可能会出现这种情况,就像在Photoshop中找到的那样。在这种情况下,在内存中保持交替状态堆栈序列是很常见的:

In practice, sometimes it's expensive to keep applying those operations to the initial state to generate the current state. For example, this might be true with something like a complex series of image adjustments, like you might find in Photoshop. In cases like that, it's common to keep an alternating state-stack series in memory:

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

每个 stack_i 保存命令,直到它超过某些预先设定的复杂度(例如,命令超过计算成本 X )或序数(例如,堆栈持有 X 或更多命令)限制。此时,创建一个新的中间状态对象 state_i + 1 来封装状态,以及一个新的空堆栈 stack_i + 1 以保存新命令。

Each stack_i holds commands until it exceeds some pre-set complexity (e.g., the commands exceed computational cost X) or ordinal (e.g. the stack holds X or more commands) limit. At that point, a new intermediate state object state_i+1 is created to encapsulate the state, and a new, empty stack stack_i+1 is created to hold new commands.

这样,您只需将一小段命令应用于上次快照状态即可获得当前状态。这是以记忆整个状态为代价的,这对于非常大的应用程序可能是不可行的,但是您可以选择仅对一组状态进行快照以进行优化。

In this way, you only have to apply a small sequence of commands to the last snapshotted state to get the current state. This comes at the expense of memorizing entire states, which may not be feasible for very large applications, but you can choose to snapshot only a set of the state to optimize.

这篇关于在python中进行撤消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆