Skip to content

Instantly share code, notes, and snippets.

@ikeikeikeike
Created March 31, 2012 08:33
Show Gist options
  • Save ikeikeikeike/2260879 to your computer and use it in GitHub Desktop.
Save ikeikeikeike/2260879 to your computer and use it in GitHub Desktop.
PySide UndoFramework
#!/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())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment