Home

QtUndoStack Class Reference

The QtUndoStack class is a stack of QtCommand objects. More...

#include <QtUndoStack>

Inherits QObject and QList<QtCommand *> (private).

Public Functions

Public Slots

Signals

Additional Inherited Members


Detailed Description

The QtUndoStack class is a stack of QtCommand objects.

For an overview of the Qt Undo/Redo framework, see the overview.

New commands are added with push(). When a command is pushed on to the stack, QtUndoStack takes ownership of the command and applies it by calling QtCommand::redo(). Undo and redo are invoked with undo() and redo(). These functions may be called directly, or through the QtUndoManager::undoAction() and QtUndoManager::redoAction() QAction objects. In the latter case, QtUndoManager will choose a stack based on which object has the focus, and call the relevant undo() or redo() function.

    FaceEdit::FaceEdit(QWidget *parent, const char *name)
        : QWidget(parent, name, Qt::WDestructiveClose)
    {
        ...
        m_undo_stack = new QtUndoStack(this);
        ...
    }

    void FaceEdit::setImage(const QString &image_name)
    {
        Face *face = focusedFace();
        ...
        m_undo_stack->push(new CmdChangeImage(face, image_name));
        ...
    }

QtUndoStack supports command compression. This is useful when several commands can be compressed into a single command, which can be undone and redone in one go. For example, in a text editor, when the user types in a character, a new action is created, which inserts that character into the document at the cursor position. However, it is more convenient for the user to be able to undo or redo typing in whole words, sentences, and paragraphs. Command compression allows these single-character commands to be merged into a single command which inserts or deletes chunks of text. See push() for more information on command compression.

QtUndoStack supports command macros. A command macro is a sequence of commands which are undone and redone in one go. The sequence starts with a QtCommand object whose type() is MacroBegin. The description() of this command is taken to describe the effect of the entire macro. The sequence ends with a QtCommand object whose type() is MacroEnd. See undo() and redo() for more information on command macros.

    void FaceEdit::clearFaces()
    {
        ...
        m_undo_stack->push(new QtCommand(QtCommand::MacroBegin, "Clear faces"));
        for (; *child_it != 0; ++child_it) {
            Face *face = (Face*) *child_it;
            m_undo_stack->push(new CmdChangeImage(face, "none"));
            m_undo_stack->push(new CmdChangeColor(face, "green"));
        }
        m_undo_stack->push(new QtCommand(QtCommand::MacroEnd));
        ...
    }

A certain state of the edited object may be marked as "clean", using setClean(). This function is usually called whenever the edited object is saved. QtUndoStack emits the cleanChanged() signal whenever the edited object enters or leaves the clean state.

See also QtCommand and QtUndoManager.


Member Function Documentation

QtUndoStack::QtUndoStack ( QObject * parent = 0 )

Constructs a QtUndoStack object. parent is passed to the QObject constructor. Additionally, parent is registered as the target for the newly created stack. When parent has the focus the QtUndoManager will use this undo stack for its undo() and redo() actions.

bool QtUndoStack::canRedo () const

Returns true if a command is available for redo; otherwise returns false. Redo is not possible if the stack is empty or if the top command on the stack has already been redone.

See also redo() and canUndo().

void QtUndoStack::canRedoChanged ( bool enabled )   [signal]

This signal is emitted whenever the state reported by canRedo() changes. enabled is the new state.

This function is useful if you want to trigger redo with a custom widget, rather than the QAction returned by createRedoAction().

See also canRedo(), redoDescriptionChanged(), and canUndoChanged().

bool QtUndoStack::canUndo () const

Returns true if a command is available for undo; otherwise returns false. Undo is not possible if the stack is empty or if the bottom command on the stack has already been undone.

See also undo() and canRedo().

void QtUndoStack::canUndoChanged ( bool enabled )   [signal]

This signal is emitted whenever the state reported by canUndo() changes. enabled is the new state.

This function is useful if you want to trigger undo with a custom widget, rather than the QAction returned by createUndoAction().

See also canUndo(), undoDescriptionChanged(), and canRedoChanged().

void QtUndoStack::cleanChanged ( bool clean )   [signal]

This signal is emitted whenever the edited object enters or leaves the clean state. If clean is true, the edited object is currently clean; otherwise it is currently not clean.

See also isClean() and setClean().

void QtUndoStack::clear ()   [slot]

Clears all the commands on this undo stack.

See also push().

void QtUndoStack::commandExecuted ()   [signal]

This signal is emitted whenever a QtCommand on the stack is undone or redone, and when the stack has been cleared by clear(). When macro commands are undone or redone, this signal is emitted only once, even though the macro may contain more than one command.

See also redo(), undo(), and clear().

QAction * QtUndoStack::createRedoAction ( QObject * parent ) const

Creates a QAction object connected to the QtUndoStack's redo() slot. The parent becomes the owner and parent of the QAction. This is significant, since any accelerators that are assigned to the QAction will only work within the parent.

Unlike QtUndoManager::createRedoAction(), the returned QAction is connected directly to this stack's redo() slot. This is useful if you want each of your target windows to have it's own redo button.

The returned QAction will keep its text property in sync with redoDescription() and disable itself whenever no commands are available for redo.

If the application's default QMimeSourceFactory contains a pixmap called "redo" or "redo.png", this pixmap is assigned to the QAction.

See also redo(), redoDescription(), and createUndoAction().

QAction * QtUndoStack::createUndoAction ( QObject * parent ) const

Creates a QAction object connected to the QtUndoStack's undo() slot. The parent becomes the owner and parent of the QAction. This is significant, since any accelerators that are assigned to the QAction will only work within the parent.

Unlike QtUndoManager::createUndoAction(), the returned QAction is connected directly to this stack's undo() slot. This is useful if you want each of your target windows to have it's own undo button.

The returned QAction will keep its text property in sync with undoDescription() and disable itself whenever no commands are available for undo.

If the application's default QMimeSourceFactory contains a pixmap called "undo" or "undo.png", this pixmap is assigned to the QAction.

See also undo(), undoDescription(), and createRedoAction().

bool QtUndoStack::isClean () const

Returns true if the edited object is in a clean state; otherwise returns false. The edited object is in a clean state if setClean() was previously called and the state of the edited object at the time of the call was the same as it is now.

More precisely, the edited object is in a clean state if the current QtUndoStack command is the same one as it was at the time of the last call to setClean().

See also setClean() and cleanChanged().

void QtUndoStack::push ( QtCommand * command )

Pushes the command onto this stack or merges it with the current command, and calls the command's QtCommand::redo() function to apply it.

If the current command is not topmost on the stack, all commands above it are deleted.

This function will attempt to merge a new command with the command on top of the stack only if they are both instances of the same class (ascertained using QObject::className()) and if the new command's merge flag is true.

If there is no current command on this stack, or command and the current command are of different types, or command's merge flag is false, or QtCommand::mergeMeWith() returns false, push() will push command onto the stack.

See also undo(), redo(), and clear().

void QtUndoStack::redo ( int count = 1 )   [slot]

If the current command's type() is Command, moves the current position one command up in the stack and calls the new current command's redo().

If the current command's type() is MacroBegin, traverses the stack upwards calling each command's redo(), until a command of type MacroEnd is found. The current position remains at this command.

This process is repeated count times. count defaults to 1.

See also push(), undo(), and canRedo().

QString QtUndoStack::redoDescription () const

Returns the description() for the command preceding the current command on the stack, or an empty string if the current command is at the top.

See also QtUndoManager::redoDescription(), QtCommand::description(), and undoDescription().

void QtUndoStack::redoDescriptionChanged ( const QString & newDescription )   [signal]

This signal is emitted whenever the redo description for this QtUndoStack changes. newDescription is the new redo description. It is useful when you want to trigger undo using a custom widget, rather than using the QAction returned by createRedoAction().

See also redoDescription(), canRedoChanged(), and undoDescriptionChanged().

QStringList QtUndoStack::redoList () const

Returns a list of descriptions of all the commands preceding the current command in this stack.

See also undoList().

void QtUndoStack::setClean ()   [slot]

Marks the state of the edited object as clean. This function is usually called whenever the edited object is saved. The cleanChanged() signal is emited whenever the edited object enters or leaves the clean state.

See also isClean() and cleanChanged().

void QtUndoStack::setCurrent ()

Sets this stack as the current stack. The undo and redo actions provided by QtUndoManager, as well as the QtUndoListBox will update themselves to reflect the state of this stack. This function is usually called by a document window when it receives input focus.

See also QtUndoManager::setCurrentStack() and QtUndoManager::currentStack().

void QtUndoStack::undo ( int count = 1 )   [slot]

If the current command's type() is Command, calls the current command's undo() function and moves the current position one command down the stack.

If the current command's type() is MacroEnd, traverses the stack downwards calling each command's undo(), until a command of type MacroBegin is found. The current position is then set to one command below the macro begin marker.

This process is repeated count times. count defaults to 1.

See also push(), redo(), and canUndo().

QString QtUndoStack::undoDescription () const

Returns the description() of the current command on the stack, or an empty string if there is no current command.

See also QtUndoManager::undoDescription(), QtCommand::description(), and redoDescription().

void QtUndoStack::undoDescriptionChanged ( const QString & newDescription )   [signal]

This signal is emitted whenever the undo description for this QtUndoStack changes. newDescription is the new undo description. It is useful when you want to trigger undo using a custom widget, rather than using the QAction returned by createUndoAction().

See also undoDescription(), canUndoChanged(), and redoDescriptionChanged().

QStringList QtUndoStack::undoList () const

Returns a list of descriptions of all the commands up to the current command in this stack.

See also redoList().


Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt Solutions