Home

QtCommand Class Reference

The QtCommand class is the base class of all commands stored on a QtUndoStack. More...

#include <QtCommand>

Inherits QObject.

Public Types

Public Functions

Protected Functions

Additional Inherited Members


Detailed Description

The QtCommand class is the base class of all commands stored on a QtUndoStack.

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

A QtCommand represents a single editing action which an application can make, for example, inserting or deleting a block of text in a text editor. QtCommand knows how to apply a change to an object and how to undo the change.

A change is applied by calling redo(). A change is undone by calling undo(). The default implementations of these virtual functions in QtCommand do nothing, they must be overriden in a derived class.

    CmdChangeImage::CmdChangeImage(Face *face, const QString &image)
    {
        m_face = face;
        m_old_image = face->image();
        m_new_image = image;
        setCanMerge(false);
        setDescription(tr("change %1 to %2").arg(m_old_image).arg(m_new_image));
    }

    void CmdChangeImage::redo()
    {
        m_face->setImage(m_new_image);
    }

    void CmdChangeImage::undo()
    {
        m_face->setImage(m_old_image);
    }

A QtCommand object has a type(), which is used to differentiate between ordinary commands and macro delimiters. An ordinary command is of type() Command. A macro delimiter's type() is either MacroBegin or MacroEnd. Sequences of commands between macro delimiters are undone and redone in one go, rather than one at a time.

Each QtCommand has a merge flag, returned by canMerge(). QtUndoStack::push() will attempt to merge a new command with the current command in 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.

A QtCommand object has a description(), which describes its effect on the edited object. This description is used to give human-readable information to the widgets which trigger undo or redo in an application, such as the QAction objects returned by QtUndoManager::createUndoAction() and QtUndoManager::createRedoAction().

Warning: All classes derived from QtCommand must contain the Q_OBJECT macro in their declaration.

See also QtUndoStack and QtUndoManager.


Member Type Documentation

enum QtCommand::Type

This enumerated type is used by QtCommand to differentiate between ordinary commands and macro delimiters.

ConstantValueDescription
QtCommand::Command0The command is a normal command which applies a change to an object.
QtCommand::MacroBegin1The command marks the start of a macro sequence.
QtCommand::MacroEnd2The command marks the end of a macro sequence.


Member Function Documentation

QtCommand::QtCommand ( Type type, const QString & description = QString(), bool canMerge = false )

Constructs a QtCommand object of the given type, with a description and the merge flag set to canMerge. This is the preferred constructor for macro delimiters.

See also QtCommand::Type, description(), and canMerge().

QtCommand::QtCommand ( const QString & description = QString(), bool canMerge = true )

Constructs a QtCommand object with description and the merge flag set to canMerge. The command type is set to Command. This is the preferred constructor for commands which are not macro delimiters.

See also description() and canMerge().

bool QtCommand::canMerge () const

Returns the command's merge flag. QtUndoStack::push() 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.

See also setCanMerge() and mergeMeWith().

QString QtCommand::description () const

Returns a string which describes the effect of this command on the edited object.

Typical examples include "typing", "delete block", "change font", etc.

This description is used to assign human-readable information to the widgets which trigger undo or redo in an application, such as the QAction objects returned by QtUndoManager::createUndoAction() and QtUndoManager::createRedoAction().

See also setDescription(), QtUndoStack::undoDescription(), QtUndoStack::redoDescription(), QtUndoManager::undoDescription(), and QtUndoManager::redoDescription().

bool QtCommand::isCommand () const

Returns true if the command is an ordinary editing command; otherwise returns false.

See also QtCommand::Type and type().

bool QtCommand::isMacroBegin () const

Returns true if the command is a macro start delimiter; otherwise returns false.

See also QtCommand::Type and type().

bool QtCommand::isMacroEnd () const

Returns true if the command is a macro end delimiter; otherwise returns false.

See also QtCommand::Type and type().

bool QtCommand::mergeMeWith ( QtCommand * other )   [virtual protected]

Attempts to merge the other command into this command. Returns true if it succeeds; otherwise returns false. If this function returns false, QtUndoStack::push() will push the other command on top of the stack. The default implementation does nothing and returns false. This function must be reimplemented in each derived command class which sets its merge flag to true.

    CmdChangeColor::CmdChangeColor(Face *face, const QString &color)
    {
        ...
        setCanMerge(true);
        ...
    }

    bool CmdChangeColor::mergeMeWith(QtCommand *c)
    {
        CmdChangeColor *other = (CmdChangeColor*) c;

        if (m_face != other->m_face)
            return false;

        m_new_color = other->m_new_color;
        setDescription(tr("change %1 to %2").arg(m_old_color).arg(m_new_color));
        return true;
    }

See also canMerge() and setCanMerge().

void QtCommand::redo ()   [virtual]

This virtual function must be reimplemented by subclasses to apply changes.

See also undo().

void QtCommand::setCanMerge ( bool b )

Sets the merge flag for this command to b.

See also canMerge() and mergeMeWith().

void QtCommand::setDescription ( const QString & s )

Sets the description of this command to s.

See also description().

Type QtCommand::type () const

Returns the type of this command.

void QtCommand::undo ()   [virtual]

This virtual function must be reimplemented by subclasses to undo the changes applied by redo(). Calling redo() and then undo() should leave the edited object unmodified.


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