/python/pyside_undo.py
Python | 59 lines | 39 code | 18 blank | 2 comment | 1 complexity | e5632885eb8e19b1a0b77194158e94d7 MD5 | raw file
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from PySide.QtGui import QUndoCommand, QUndoStack
- class MyDocument(object):
- def __init__(self):
- self.document = []
- def __repr__(self):
- return repr(self.document)
- def chop(self):
- self.document = self.document[:-1]
- def append(self, item):
- self.document.append(item)
- class MyCommand(QUndoCommand):
- def __init__(self, doc, *args, **kwargs):
- super(type(self), self).__init__(*args, **kwargs)
- self.document = doc
- def undo(self):
- self.document.chop()
- print("undo: {0}, undo-text: {1}".format(self.document, self.text()))
- def redo(self):
- self.document.append(self.text())
- print("redo: {0}, redo-text: {1}".format(self.document, self.text()))
- if __name__ == '__main__':
- stack1 = QUndoStack()
- document1 = MyDocument()
- c = MyCommand(document1)
- c.setText("comamnd1")
- stack1.push(c)
- print(stack1.count())
- c = MyCommand(document1)
- c.setText("comamnd2")
- stack1.push(c)
- print(stack1.count())
- stack1.undo()
- stack1.undo()
- stack1.redo()
- print(stack1.count())
- c = MyCommand(document1)
- c.setText("comamnd3")
- stack1.push(c) # command2 gets deleted
- print(stack1.count())