QUndoCommand

Inheritance diagram of QUndoCommand

Synopsis

Functions

Virtual functions

Detailed Description

The PySide.QtGui.QUndoCommand class is the base class of all commands stored on a PySide.QtGui.QUndoStack .

For an overview of Qt’s Undo Framework, see the overview document .

A PySide.QtGui.QUndoCommand represents a single editing action on a document; for example, inserting or deleting a block of text in a text editor. PySide.QtGui.QUndoCommand can apply a change to the document with PySide.QtGui.QUndoCommand.redo() and undo the change with PySide.QtGui.QUndoCommand.undo() . The implementations for these functions must be provided in a derived class.

class AppendText(QUndoCommand):
    self.m_document = ''
    self.m_text = ''

    def AppendText(self, doc, text):
        self.m_document = doc
        self.m_text = text
        self.setText("append text")

    def undo(self):
        self.m_document.chop(self.m_text.length())

    def redo(self):
        self.m_document->append(self.m_text)

A PySide.QtGui.QUndoCommand has an associated PySide.QtGui.QUndoCommand.text() . This is a short string describing what the command does. It is used to update the text properties of the stack’s undo and redo actions; see QUndoStack.createUndoAction() and QUndoStack.createRedoAction() .

PySide.QtGui.QUndoCommand objects are owned by the stack they were pushed on. PySide.QtGui.QUndoStack deletes a command if it has been undone and a new command is pushed. For example:

command1 = MyCommand()
stack.push(command1)
command2 = MyCommand()
stack.push(command2)

stack.undo()

command3 = MyCommand()
stack.push(command3) # command2 gets deleted

In effect, when a command is pushed, it becomes the top-most command on the stack.

To support command compression, PySide.QtGui.QUndoCommand has an PySide.QtGui.QUndoCommand.id() and the virtual function PySide.QtGui.QUndoCommand.mergeWith() . These functions are used by QUndoStack.push() .

To support command macros, a PySide.QtGui.QUndoCommand object can have any number of child commands. Undoing or redoing the parent command will cause the child commands to be undone or redone. A command can be assigned to a parent explicitly in the constructor. In this case, the command will be owned by the parent.

The parent in this case is usually an empty command, in that it doesn’t provide its own implementation of PySide.QtGui.QUndoCommand.undo() and PySide.QtGui.QUndoCommand.redo() . Instead, it uses the base implementations of these functions, which simply call PySide.QtGui.QUndoCommand.undo() or PySide.QtGui.QUndoCommand.redo() on all its children. The parent should, however, have a meaningful PySide.QtGui.QUndoCommand.text() .

insertRed = QUndoCommand() # an empty command
insertRed.setText("insert red text")

InsertText(document, idx, text, insertRed) # becomes child of insertRed
SetColor(document, idx, text.length(), Qt.red, insertRed)

stack.push(insertRed)

Another way to create macros is to use the convenience functions QUndoStack.beginMacro() and QUndoStack.endMacro() .

class PySide.QtGui.QUndoCommand([parent=None])
class PySide.QtGui.QUndoCommand(text[, parent=None])
Parameters:

Constructs a PySide.QtGui.QUndoCommand object with parent parent .

If parent is not 0, this command is appended to parent’s child list. The parent command then owns this command and will delete it in its destructor.

See also

~QUndoCommand()

Constructs a PySide.QtGui.QUndoCommand object with the given parent and text .

If parent is not 0, this command is appended to parent’s child list. The parent command then owns this command and will delete it in its destructor.

See also

~QUndoCommand()

PySide.QtGui.QUndoCommand.child(index)
Parameters:indexPySide.QtCore.int
Return type:PySide.QtGui.QUndoCommand

Returns the child command at index .

PySide.QtGui.QUndoCommand.childCount()
Return type:PySide.QtCore.int

Returns the number of child commands in this command.

PySide.QtGui.QUndoCommand.id()
Return type:PySide.QtCore.int

Returns the ID of this command.

A command ID is used in command compression. It must be an integer unique to this command’s class, or -1 if the command doesn’t support compression.

If the command supports compression this function must be overridden in the derived class to return the correct ID. The base implementation returns -1.

QUndoStack.push() will only try to merge two commands if they have the same ID, and the ID is not -1.

PySide.QtGui.QUndoCommand.mergeWith(other)
Parameters:otherPySide.QtGui.QUndoCommand
Return type:PySide.QtCore.bool

Attempts to merge this command with command . Returns true on success; otherwise returns false.

If this function returns true, calling this command’s PySide.QtGui.QUndoCommand.redo() must have the same effect as redoing both this command and command . Similarly, calling this command’s PySide.QtGui.QUndoCommand.undo() must have the same effect as undoing command and this command.

PySide.QtGui.QUndoStack will only try to merge two commands if they have the same id, and the id is not -1.

The default implementation returns false.

class AppendText(QUndoCommand):
    ...
    def mergeWith(self, other):
        if other.id() != self.id(): # make sure other is also an AppendText command
            return False
        m_text += other.m_text
        return True
PySide.QtGui.QUndoCommand.redo()

Applies a change to the document. This function must be implemented in the derived class. Calling QUndoStack.push() , QUndoStack.undo() or QUndoStack.redo() from this function leads to undefined beahavior.

The default implementation calls PySide.QtGui.QUndoCommand.redo() on all child commands.

PySide.QtGui.QUndoCommand.setText(text)
Parameters:text – unicode

Sets the command’s text to be the text specified.

The specified text should be a short user-readable string describing what this command does.

PySide.QtGui.QUndoCommand.text()
Return type:unicode

Returns a short text string describing what this command does; for example, “insert text”.

The text is used when the text properties of the stack’s undo and redo actions are updated.

PySide.QtGui.QUndoCommand.undo()

Reverts a change to the document. After PySide.QtGui.QUndoCommand.undo() is called, the state of the document should be the same as before PySide.QtGui.QUndoCommand.redo() was called. This function must be implemented in the derived class. Calling QUndoStack.push() , QUndoStack.undo() or QUndoStack.redo() from this function leads to undefined beahavior.

The default implementation calls PySide.QtGui.QUndoCommand.undo() on all child commands in reverse order.