Qt Jambi Home

com.trolltech.qt.gui
Class QUndoStack

java.lang.Object
  extended by com.trolltech.qt.QSignalEmitter
      extended by com.trolltech.qt.QtJambiObject
          extended by com.trolltech.qt.core.QObject
              extended by com.trolltech.qt.gui.QUndoStack
All Implemented Interfaces:
QtJambiInterface

public class QUndoStack
extends QObject

The QUndoStack class is a stack of 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 push. Commands can be undone and redone using undo and redo, or by triggering the actions returned by createUndoAction and createRedoAction.

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

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

Undo and Redo Actions

QUndoStack provides convenient undo and redo QAction objects, which can be inserted into a menu or a toolbar. When commands are undone or redone, 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 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 QUndoCommand constructor, or by using the convenience functions beginMacro and 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

QUndoStack supports the concept of a clean state. When the document is saved to disk, the stack can be marked as clean using setClean. Whenever the stack returns to this state through undoing and redoing commands, it emits the signal 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.

See Also:
QUndoCommand, QUndoView

Nested Class Summary
 
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I>
 
Field Summary
 QSignalEmitter.Signal1<java.lang.Boolean> canRedoChanged
          This signal is emitted whenever the value of canRedo changes.
 QSignalEmitter.Signal1<java.lang.Boolean> canUndoChanged
          This signal is emitted whenever the value of canUndo changes.
 QSignalEmitter.Signal1<java.lang.Boolean> cleanChanged
          This signal is emitted whenever the stack enters or leaves the clean state.
 QSignalEmitter.Signal1<java.lang.Integer> indexChanged
          This signal is emitted whenever a command modifies the state of the document.
 QSignalEmitter.Signal1<java.lang.String> redoTextChanged
          This signal is emitted whenever the value of redoText changes.
 QSignalEmitter.Signal1<java.lang.String> undoTextChanged
          This signal is emitted whenever the value of undoText changes.
 
Constructor Summary
QUndoStack()
          Equivalent to QUndoStack(0).
QUndoStack(QObject parent)
          Constructs an empty undo stack with the parent parent.
 
Method Summary
 void beginMacro(java.lang.String text)
          Begins composition of a macro command with the given text description.
 boolean canRedo()
          Returns true if there is a command available for redo; otherwise returns false.
 boolean canUndo()
          Returns true if there is a command available for undo; otherwise returns false.
 int cleanIndex()
          Returns the clean index.
 void clear()
          Clears the command stack by deleting all commands on it, and returns the stack to the clean state.
 int count()
          Returns the number of commands on the stack.
 QAction createRedoAction(QObject parent)
          Equivalent to createRedoAction(parent, QString()).
 QAction createRedoAction(QObject parent, java.lang.String prefix)
          Creates an redo QAction object with the given parent.
 QAction createUndoAction(QObject parent)
          Equivalent to createUndoAction(parent, QString()).
 QAction createUndoAction(QObject parent, java.lang.String prefix)
          Creates an undo QAction object with the given parent.
 void endMacro()
          Ends composition of a macro command.
static QUndoStack fromNativePointer(QNativePointer nativePointer)
          This function returns the QUndoStack instance pointed to by nativePointer
 int index()
          Returns the index of the current command.
 boolean isActive()
          Returns the active status of this stack..
 boolean isClean()
          If the stack is in the clean state, returns true; otherwise returns false.
 void push(QUndoCommand cmd)
          Pushes cmd on the stack or merges it with the most recently executed command.
 void redo()
          Redoes the current command by calling QUndoCommand::redo().
 java.lang.String redoText()
          Returns the text of the command which will be redone in the next call to redo.
 void setActive()
          Equivalent to setActive(true).
 void setActive(boolean active)
          Sets the active status of this stack. to active.
 void setClean()
          Marks the stack as clean and emits cleanChanged if the stack was not already clean.
 void setIndex(int idx)
          Repeatedly calls undo or redo until the the current command index reaches idx.
 void setUndoLimit(int limit)
          Sets the maximum number of commands on this stack. to limit.
 java.lang.String text(int idx)
          Returns the text of the command at index idx.
 void undo()
          Undoes the command below the current command by calling QUndoCommand::undo().
 int undoLimit()
          Returns the maximum number of commands on this stack..
 java.lang.String undoText()
          Returns the text of the command which will be undone in the next call to undo.
 
Methods inherited from class com.trolltech.qt.core.QObject
blockSignals, childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, property, removeEventFilter, setObjectName, setParent, setProperty, signalsBlocked, startTimer, thread, timerEvent
 
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr
 
Methods inherited from class com.trolltech.qt.QSignalEmitter
disconnect, disconnect, signalSender
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
 

Field Detail

canRedoChanged

public final QSignalEmitter.Signal1<java.lang.Boolean> canRedoChanged

This signal is emitted whenever the value of canRedo changes. It is used to enable or disable the redo action returned by createRedoAction. canRedo specifies the new value.

Compatible Slot Signatures:
void mySlot(boolean canRedo)
void mySlot()


canUndoChanged

public final QSignalEmitter.Signal1<java.lang.Boolean> canUndoChanged

This signal is emitted whenever the value of canUndo changes. It is used to enable or disable the undo action returned by createUndoAction. canUndo specifies the new value.

Compatible Slot Signatures:
void mySlot(boolean canUndo)
void mySlot()


cleanChanged

public final QSignalEmitter.Signal1<java.lang.Boolean> cleanChanged

This signal is emitted whenever the stack enters or leaves the clean state. If clean is true, the stack is in a clean state; otherwise this signal indicates that it has left the clean state.

Compatible Slot Signatures:
void mySlot(boolean clean)
void mySlot()
See Also:
isClean, setClean


indexChanged

public final QSignalEmitter.Signal1<java.lang.Integer> indexChanged

This signal is emitted whenever a command modifies the state of the document. This happens when a command is undone or redone. When a macro command is undone or redone, or setIndex is called, this signal is emitted only once.

idx specifies the index of the current command, ie. the command which will be executed on the next call to redo.

Compatible Slot Signatures:
void mySlot(int idx)
void mySlot()
See Also:
index, setIndex


redoTextChanged

public final QSignalEmitter.Signal1<java.lang.String> redoTextChanged

This signal is emitted whenever the value of redoText changes. It is used to update the text property of the redo action returned by createRedoAction. redoText specifies the new text.

Compatible Slot Signatures:
void mySlot(java.lang.String redoText)
void mySlot()


undoTextChanged

public final QSignalEmitter.Signal1<java.lang.String> undoTextChanged

This signal is emitted whenever the value of undoText changes. It is used to update the text property of the undo action returned by createUndoAction. undoText specifies the new text.

Compatible Slot Signatures:
void mySlot(java.lang.String undoText)
void mySlot()

Constructor Detail

QUndoStack

public QUndoStack()

Equivalent to QUndoStack(0).


QUndoStack

public QUndoStack(QObject parent)

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

See Also:
push
Method Detail

beginMacro

public final void beginMacro(java.lang.String text)

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 endMacro is called.

Calls to beginMacro and endMacro may be nested, but every call to beginMacro must have a matching call to endMacro.

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

The stack becomes enabled and appropriate signals are emitted when endMacro is called for the outermost macro.

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

This code is equivalent to:

    QUndoCommand *insertRed = new QUndoCommand(); // an empty command
    insertRed->setText("insert red text");

    new InsertText(document, idx, text, insertRed); // becomes child of insertRed
    new SetColor(document, idx, text.length(), Qt::red, insertRed);

    stack.push(insertRed);

See Also:
endMacro

canRedo

public final boolean canRedo()

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 index == count.

See Also:
index, canUndo

canUndo

public final boolean canUndo()

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 index == 0.

See Also:
index, canRedo

cleanIndex

public final int cleanIndex()

Returns the clean index. This is the index at which 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 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.

See Also:
isClean, setClean

clear

public final void 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:
QUndoStack

count

public final int count()

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

See Also:
index, setIndex

createRedoAction

public final QAction createRedoAction(QObject parent)

Equivalent to createRedoAction(parent, QString()).


createRedoAction

public final QAction createRedoAction(QObject parent,
                                      java.lang.String prefix)

Creates an redo QAction object with the given parent.

Triggering this action will cause a call to redo. The text of this action is the text of the command which will be redone in the next call to 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.

See Also:
createUndoAction, canRedo, QUndoCommand::text

createUndoAction

public final QAction createUndoAction(QObject parent)

Equivalent to createUndoAction(parent, QString()).


createUndoAction

public final QAction createUndoAction(QObject parent,
                                      java.lang.String prefix)

Creates an undo QAction object with the given parent.

Triggering this action will cause a call to undo. The text of this action is the text of the command which will be undone in the next call to 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.

See Also:
createRedoAction, canUndo, QUndoCommand::text

endMacro

public final void endMacro()

Ends composition of a macro command.

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

See Also:
beginMacro

index

public final int index()

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

See Also:
setIndex, undo, redo, count

isActive

public final boolean isActive()

Returns 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 QUndoGroup, calls to QUndoGroup::undo() or QUndoGroup::redo() will be forwarded to this stack when it is active. If the QUndoGroup is watched by a QUndoView, the view will display the contents of this stack when it is active. If the stack does not belong to a QUndoGroup, making it active has no effect.

It is the programmer's responsibility to specify which stack is active by calling setActive, usually when the associated document window receives focus.

See Also:
QUndoGroup

isClean

public final boolean isClean()

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

See Also:
setClean, cleanIndex

push

public final void push(QUndoCommand cmd)

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

If cmd's id is not -1, and if the id is the same as that of the most recently executed command, 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.

See Also:
QUndoCommand::id, QUndoCommand::mergeWith

redo

public final void 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.

See Also:
undo, index

redoText

public final java.lang.String redoText()

Returns the text of the command which will be redone in the next call to redo.

See Also:
QUndoCommand::text, undoText

setActive

public final void setActive()

Equivalent to setActive(true).


setActive

public final void setActive(boolean active)

Sets the active status of this stack. to active.

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 QUndoGroup, calls to QUndoGroup::undo() or QUndoGroup::redo() will be forwarded to this stack when it is active. If the QUndoGroup is watched by a QUndoView, the view will display the contents of this stack when it is active. If the stack does not belong to a QUndoGroup, making it active has no effect.

It is the programmer's responsibility to specify which stack is active by calling setActive, usually when the associated document window receives focus.

See Also:
isActive, QUndoGroup

setClean

public final void setClean()

Marks the stack as clean and emits 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 cleanChanged. This signal is also emitted when the stack leaves the clean state.

See Also:
isClean, cleanIndex

setIndex

public final void setIndex(int idx)

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

See Also:
index, count, undo, redo

setUndoLimit

public final void setUndoLimit(int limit)

Sets the maximum number of commands on this stack. to limit.

When the number of commands on a stack exceedes the stack's 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 setUndoLimit on a non-empty stack prints a warning and does nothing.

See Also:
undoLimit

text

public final java.lang.String text(int idx)

Returns the text of the command at index idx.

See Also:
beginMacro

undo

public final void 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.

See Also:
redo, index

undoLimit

public final int undoLimit()

Returns the maximum number of commands on this stack..

When the number of commands on a stack exceedes the stack's 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 setUndoLimit on a non-empty stack prints a warning and does nothing.

See Also:
setUndoLimit

undoText

public final java.lang.String undoText()

Returns the text of the command which will be undone in the next call to undo.

See Also:
QUndoCommand::text, redoText

fromNativePointer

public static QUndoStack fromNativePointer(QNativePointer nativePointer)
This function returns the QUndoStack instance pointed to by nativePointer

Parameters:
nativePointer - the QNativePointer of which object should be returned.

Qt Jambi Home