QUndoStack

Inheritance diagram of QUndoStack

Synopsis

Functions

Slots

Signals

Detailed Description

The PySide.QtGui.QUndoStack class is a stack of PySide.QtGui.QUndoCommand objects.

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

An undo stack maintains a stack of commands that have been applied to a document.

New commands are pushed on the stack using PySide.QtGui.QUndoStack.push() . Commands can be undone and redone using PySide.QtGui.QUndoStack.undo() and PySide.QtGui.QUndoStack.redo() , or by triggering the actions returned by PySide.QtGui.QUndoStack.createUndoAction() and PySide.QtGui.QUndoStack.createRedoAction() .

PySide.QtGui.QUndoStack keeps track of the current command. This is the command which will be executed by the next call to PySide.QtGui.QUndoStack.redo() . The index of this command is returned by PySide.QtGui.QUndoStack.index() . The state of the edited object can be rolled forward or back using PySide.QtGui.QUndoStack.setIndex() . If the top-most command on the stack has already been redone, PySide.QtGui.QUndoStack.index() is equal to PySide.QtGui.QUndoStack.count() .

PySide.QtGui.QUndoStack provides support for undo and redo actions, command compression, command macros, and supports the concept of a clean state .

Undo and Redo Actions

PySide.QtGui.QUndoStack provides convenient undo and redo PySide.QtGui.QAction objects, which can be inserted into a menu or a toolbar. When commands are undone or redone, PySide.QtGui.QUndoStack updates the text properties of these actions to reflect what change they will trigger. The actions are also disabled when no command is available for undo or redo. These actions are returned by QUndoStack.createUndoAction() and QUndoStack.createRedoAction() .

Command Compression and Macros

Command compression is useful when several commands can be compressed into a single command that can be undone and redone in a single operation. For example, when a user types a character in a text editor, a new command is created. This command inserts the character into the document at the cursor position. However, it is more convenient for the user to be able to undo or redo typing of whole words, sentences, or paragraphs. Command compression allows these single-character commands to be merged into a single command which inserts or deletes sections of text. For more information, see QUndoCommand.mergeWith() and PySide.QtGui.QUndoStack.push() .

A command macro is a sequence of commands, all of which are undone and redone in one go. Command macros are created by giving a command a list of child commands. Undoing or redoing the parent command will cause the child commands to be undone or redone. Command macros may be created explicitly by specifying a parent in the PySide.QtGui.QUndoCommand constructor, or by using the convenience functions PySide.QtGui.QUndoStack.beginMacro() and PySide.QtGui.QUndoStack.endMacro() .

Although command compression and macros appear to have the same effect to the user, they often have different uses in an application. Commands that perform small changes to a document may be usefully compressed if there is no need to individually record them, and if only larger changes are relevant to the user. However, for commands that need to be recorded individually, or those that cannot be compressed, it is useful to use macros to provide a more convenient user experience while maintaining a record of each command.

Clean State

PySide.QtGui.QUndoStack supports the concept of a clean state. When the document is saved to disk, the stack can be marked as clean using PySide.QtGui.QUndoStack.setClean() . Whenever the stack returns to this state through undoing and redoing commands, it emits the signal PySide.QtGui.QUndoStack.cleanChanged() . This signal is also emitted when the stack leaves the clean state. This signal is usually used to enable and disable the save actions in the application, and to update the document’s title to reflect that it contains unsaved changes.

class PySide.QtGui.QUndoStack([parent=None])
Parameters:parentPySide.QtCore.QObject

Constructs an empty undo stack with the parent parent . The stack will initially be in the clean state. If parent is a PySide.QtGui.QUndoGroup object, the stack is automatically added to the group.

PySide.QtGui.QUndoStack.beginMacro(text)
Parameters:text – unicode

Begins composition of a macro command with the given text description.

An empty command described by the specified text is pushed on the stack. Any subsequent commands pushed on the stack will be appended to the empty command’s children until PySide.QtGui.QUndoStack.endMacro() is called.

Calls to PySide.QtGui.QUndoStack.beginMacro() and PySide.QtGui.QUndoStack.endMacro() may be nested, but every call to PySide.QtGui.QUndoStack.beginMacro() must have a matching call to PySide.QtGui.QUndoStack.endMacro() .

While a macro is composed, the stack is disabled. This means that:

The stack becomes enabled and appropriate signals are emitted when PySide.QtGui.QUndoStack.endMacro() is called for the outermost macro.

stack.beginMacro("insert red text")
stack.push(InsertText(document, idx, text))
stack.push(SetColor(document, idx, text.length(), Qt.red))
stack.endMacro() # indexChanged() is emitted

This code is equivalent to:

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)
PySide.QtGui.QUndoStack.canRedo()
Return type:PySide.QtCore.bool

Returns true if there is a command available for redo; otherwise returns false.

This function returns false if the stack is empty or if the top command on the stack has already been redone.

Synonymous with PySide.QtGui.QUndoStack.index() == PySide.QtGui.QUndoStack.count() .

PySide.QtGui.QUndoStack.canRedoChanged(canRedo)
Parameters:canRedoPySide.QtCore.bool
PySide.QtGui.QUndoStack.canUndo()
Return type:PySide.QtCore.bool

Returns true if there is a command available for undo; otherwise returns false.

This function returns false if the stack is empty, or if the bottom command on the stack has already been undone.

Synonymous with PySide.QtGui.QUndoStack.index() == 0.

PySide.QtGui.QUndoStack.canUndoChanged(canUndo)
Parameters:canUndoPySide.QtCore.bool
PySide.QtGui.QUndoStack.cleanChanged(clean)
Parameters:cleanPySide.QtCore.bool
PySide.QtGui.QUndoStack.cleanIndex()
Return type:PySide.QtCore.int

Returns the clean index. This is the index at which PySide.QtGui.QUndoStack.setClean() was called.

A stack may not have a clean index. This happens if a document is saved, some commands are undone, then a new command is pushed. Since PySide.QtGui.QUndoStack.push() deletes all the undone commands before pushing the new command, the stack can’t return to the clean state again. In this case, this function returns -1.

PySide.QtGui.QUndoStack.clear()

Clears the command stack by deleting all commands on it, and returns the stack to the clean state.

Commands are not undone or redone; the state of the edited object remains unchanged.

This function is usually used when the contents of the document are abandoned.

See also

PySide.QtGui.QUndoStack.QUndoStack()

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

Returns a const pointer to the command at index .

This function returns a const pointer, because modifying a command, once it has been pushed onto the stack and executed, almost always causes corruption of the state of the document, if the command is later undone or redone.

PySide.QtGui.QUndoStack.count()
Return type:PySide.QtCore.int

Returns the number of commands on the stack. Macro commands are counted as one command.

PySide.QtGui.QUndoStack.createRedoAction(parent[, prefix=""])
Parameters:
Return type:

PySide.QtGui.QAction

Creates an redo PySide.QtGui.QAction object with the given parent .

Triggering this action will cause a call to PySide.QtGui.QUndoStack.redo() . The text of this action is the text of the command which will be redone in the next call to PySide.QtGui.QUndoStack.redo() , prefixed by the specified prefix . If there is no command available for redo, this action will be disabled.

If prefix is empty, the default prefix “Redo” is used.

PySide.QtGui.QUndoStack.createUndoAction(parent[, prefix=""])
Parameters:
Return type:

PySide.QtGui.QAction

Creates an undo PySide.QtGui.QAction object with the given parent .

Triggering this action will cause a call to PySide.QtGui.QUndoStack.undo() . The text of this action is the text of the command which will be undone in the next call to PySide.QtGui.QUndoStack.undo() , prefixed by the specified prefix . If there is no command available for undo, this action will be disabled.

If prefix is empty, the default prefix “Undo” is used.

PySide.QtGui.QUndoStack.endMacro()

Ends composition of a macro command.

If this is the outermost macro in a set nested macros, this function emits PySide.QtGui.QUndoStack.indexChanged() once for the entire macro command.

PySide.QtGui.QUndoStack.index()
Return type:PySide.QtCore.int

Returns the index of the current command. This is the command that will be executed on the next call to PySide.QtGui.QUndoStack.redo() . It is not always the top-most command on the stack, since a number of commands may have been undone.

PySide.QtGui.QUndoStack.indexChanged(idx)
Parameters:idxPySide.QtCore.int
PySide.QtGui.QUndoStack.isActive()
Return type:PySide.QtCore.bool

This property holds the active status of this stack..

An application often has multiple undo stacks, one for each opened document. The active stack is the one associated with the currently active document. If the stack belongs to a PySide.QtGui.QUndoGroup , calls to QUndoGroup.undo() or QUndoGroup.redo() will be forwarded to this stack when it is active. If the PySide.QtGui.QUndoGroup is watched by a PySide.QtGui.QUndoView , the view will display the contents of this stack when it is active. If the stack does not belong to a PySide.QtGui.QUndoGroup , making it active has no effect.

It is the programmer’s responsibility to specify which stack is active by calling PySide.QtGui.QUndoStack.setActive() , usually when the associated document window receives focus.

PySide.QtGui.QUndoStack.isClean()
Return type:PySide.QtCore.bool

If the stack is in the clean state, returns true; otherwise returns false.

PySide.QtGui.QUndoStack.push(cmd)
Parameters:cmdPySide.QtGui.QUndoCommand

Pushes cmd on the stack or merges it with the most recently executed command. In either case, executes cmd by calling its PySide.QtGui.QUndoStack.redo() function.

If cmd ‘s id is not -1, and if the id is the same as that of the most recently executed command, PySide.QtGui.QUndoStack will attempt to merge the two commands by calling QUndoCommand.mergeWith() on the most recently executed command. If QUndoCommand.mergeWith() returns true, cmd is deleted.

In all other cases cmd is simply pushed on the stack.

If commands were undone before cmd was pushed, the current command and all commands above it are deleted. Hence cmd always ends up being the top-most on the stack.

Once a command is pushed, the stack takes ownership of it. There are no getters to return the command, since modifying it after it has been executed will almost always lead to corruption of the document’s state.

PySide.QtGui.QUndoStack.redo()

Redoes the current command by calling QUndoCommand.redo() . Increments the current command index.

If the stack is empty, or if the top command on the stack has already been redone, this function does nothing.

PySide.QtGui.QUndoStack.redoText()
Return type:unicode

Returns the text of the command which will be redone in the next call to PySide.QtGui.QUndoStack.redo() .

PySide.QtGui.QUndoStack.redoTextChanged(redoText)
Parameters:redoText – unicode
PySide.QtGui.QUndoStack.setActive([active=true])
Parameters:activePySide.QtCore.bool

This property holds the active status of this stack..

An application often has multiple undo stacks, one for each opened document. The active stack is the one associated with the currently active document. If the stack belongs to a PySide.QtGui.QUndoGroup , calls to QUndoGroup.undo() or QUndoGroup.redo() will be forwarded to this stack when it is active. If the PySide.QtGui.QUndoGroup is watched by a PySide.QtGui.QUndoView , the view will display the contents of this stack when it is active. If the stack does not belong to a PySide.QtGui.QUndoGroup , making it active has no effect.

It is the programmer’s responsibility to specify which stack is active by calling PySide.QtGui.QUndoStack.setActive() , usually when the associated document window receives focus.

PySide.QtGui.QUndoStack.setClean()

Marks the stack as clean and emits PySide.QtGui.QUndoStack.cleanChanged() if the stack was not already clean.

Whenever the stack returns to this state through the use of undo/redo commands, it emits the signal PySide.QtGui.QUndoStack.cleanChanged() . This signal is also emitted when the stack leaves the clean state.

PySide.QtGui.QUndoStack.setIndex(idx)
Parameters:idxPySide.QtCore.int

Repeatedly calls PySide.QtGui.QUndoStack.undo() or PySide.QtGui.QUndoStack.redo() until the current command index reaches idx . This function can be used to roll the state of the document forwards of backwards. PySide.QtGui.QUndoStack.indexChanged() is emitted only once.

PySide.QtGui.QUndoStack.setUndoLimit(limit)
Parameters:limitPySide.QtCore.int

This property holds the maximum number of commands on this stack..

When the number of commands on a stack exceedes the stack’s PySide.QtGui.QUndoStack.undoLimit() , commands are deleted from the bottom of the stack. Macro commands (commands with child commands) are treated as one command. The default value is 0, which means that there is no limit.

This property may only be set when the undo stack is empty, since setting it on a non-empty stack might delete the command at the current index. Calling PySide.QtGui.QUndoStack.setUndoLimit() on a non-empty stack prints a warning and does nothing.

PySide.QtGui.QUndoStack.text(idx)
Parameters:idxPySide.QtCore.int
Return type:unicode

Returns the text of the command at index idx .

PySide.QtGui.QUndoStack.undo()

Undoes the command below the current command by calling QUndoCommand.undo() . Decrements the current command index.

If the stack is empty, or if the bottom command on the stack has already been undone, this function does nothing.

PySide.QtGui.QUndoStack.undoLimit()
Return type:PySide.QtCore.int

This property holds the maximum number of commands on this stack..

When the number of commands on a stack exceedes the stack’s PySide.QtGui.QUndoStack.undoLimit() , commands are deleted from the bottom of the stack. Macro commands (commands with child commands) are treated as one command. The default value is 0, which means that there is no limit.

This property may only be set when the undo stack is empty, since setting it on a non-empty stack might delete the command at the current index. Calling PySide.QtGui.QUndoStack.setUndoLimit() on a non-empty stack prints a warning and does nothing.

PySide.QtGui.QUndoStack.undoText()
Return type:unicode

Returns the text of the command which will be undone in the next call to PySide.QtGui.QUndoStack.undo() .

PySide.QtGui.QUndoStack.undoTextChanged(undoText)
Parameters:undoText – unicode