![]() |
Qt 6.x
The Qt SDK
|
#include <qundostack.h>
Public Member Functions | |
QUndoCommand (QUndoCommand *parent=nullptr) | |
Constructs a QUndoCommand object with parent parent. | |
QUndoCommand (const QString &text, QUndoCommand *parent=nullptr) | |
Constructs a QUndoCommand object with the given parent and text. | |
virtual | ~QUndoCommand () |
Destroys the QUndoCommand object and all child commands. | |
virtual void | undo () |
Reverts a change to the document. | |
virtual void | redo () |
Applies a change to the document. | |
QString | text () const |
Returns a short text string describing what this command does; for example, "insert text". | |
QString | actionText () const |
void | setText (const QString &text) |
Sets the command's text to be the text specified. | |
bool | isObsolete () const |
void | setObsolete (bool obsolete) |
virtual int | id () const |
Returns the ID of this command. | |
virtual bool | mergeWith (const QUndoCommand *other) |
Attempts to merge this command with command. | |
int | childCount () const |
const QUndoCommand * | child (int index) const |
Friends | |
class | QUndoStack |
Definition at line 19 of file qundostack.h.
|
explicit |
Constructs a QUndoCommand object with parent parent.
If parent is not \nullptr, this command is appended to parent's child list. The parent command then owns this command and will delete it in its destructor.
Definition at line 92 of file qundostack.cpp.
References parent.
|
explicit |
Constructs a QUndoCommand object with the given parent and text.
\class QUndoCommand \brief The QUndoCommand class is the base class of all commands stored on a QUndoStack. \since 4.2 \inmodule QtGui For an overview of Qt's Undo Framework, see the \l{Overview of Qt's Undo Framework}{overview document}. A QUndoCommand represents a single editing action on a document; for example, inserting or deleting a block of text in a text editor. QUndoCommand can apply a change to the document with redo() and undo the change with undo(). The implementations for these functions must be provided in a derived class. \snippet code/src_gui_util_qundostack.cpp 0 A QUndoCommand has an associated 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(). QUndoCommand objects are owned by the stack they were pushed on. QUndoStack deletes a command if it has been undone and a new command is pushed. For example:
In effect, when a command is pushed, it becomes the top-most command on the stack. To support command compression, QUndoCommand has an id() and the virtual function mergeWith(). These functions are used by QUndoStack::push(). To support command macros, a 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 undo() and redo(). Instead, it uses the base implementations of these functions, which simply call undo() or redo() on all its children. The parent should, however, have a meaningful text(). \snippet code/src_gui_util_qundostack.cpp 2 Another way to create macros is to use the convenience functions QUndoStack::beginMacro() and QUndoStack::endMacro(). \sa QUndoStack
If parent is not \nullptr, this command is appended to parent's child list. The parent command then owns this command and will delete it in its destructor.
Definition at line 76 of file qundostack.cpp.
References setText(), and text().
|
virtual |
Destroys the QUndoCommand object and all child commands.
Definition at line 105 of file qundostack.cpp.
References QUndoCommandPrivate::child_list, and qDeleteAll().
QString QUndoCommand::actionText | ( | ) | const |
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.
Definition at line 247 of file qundostack.cpp.
References QUndoCommandPrivate::actionText.
const QUndoCommand * QUndoCommand::child | ( | int | index | ) | const |
Returns the child command at index.
Definition at line 300 of file qundostack.cpp.
References QList< T >::at(), QUndoCommandPrivate::child_list, and QList< T >::size().
int QUndoCommand::childCount | ( | ) | const |
Returns the number of child commands in this command.
Definition at line 287 of file qundostack.cpp.
References QUndoCommandPrivate::child_list, and QList< T >::size().
|
virtual |
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.
Definition at line 156 of file qundostack.cpp.
bool QUndoCommand::isObsolete | ( | ) | const |
Returns whether the command is obsolete.
The boolean is used for the automatic removal of commands that are not necessary in the stack anymore. The isObsolete function is checked in the functions QUndoStack::push(), QUndoStack::undo(), QUndoStack::redo(), and QUndoStack::setIndex().
Definition at line 123 of file qundostack.cpp.
References QUndoCommandPrivate::obsolete.
|
virtual |
Attempts to merge this command with command.
Returns true
on success; otherwise returns false
.
If this function returns true
, calling this command's redo() must have the same effect as redoing both this command and command. Similarly, calling this command's undo() must have the same effect as undoing command and this command.
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
.
Definition at line 180 of file qundostack.cpp.
References Q_UNUSED.
|
virtual |
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 redo() on all child commands.
Reimplemented in AppendText.
Definition at line 197 of file qundostack.cpp.
References QList< T >::at(), QUndoCommandPrivate::child_list, i, redo(), and QList< T >::size().
Referenced by redo().
void QUndoCommand::setObsolete | ( | bool | obsolete | ) |
Sets whether the command is obsolete to obsolete.
Definition at line 136 of file qundostack.cpp.
References QUndoCommandPrivate::obsolete.
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.
If you need to have two different strings for text() and actionText(), separate them with "\\n" and pass into this function. Even if you do not use this feature for English strings during development, you can still let translators use two different strings in order to match specific languages' needs. The described feature and the function actionText() are available since Qt 4.8.
Definition at line 267 of file qundostack.cpp.
References QUndoCommandPrivate::actionText, QString::indexOf(), QString::left(), QString::mid(), text(), and QUndoCommandPrivate::text.
Referenced by AppendText::AppendText(), and QUndoCommand().
QString QUndoCommand::text | ( | ) | const |
Returns a short text string describing what this command does; for example, "insert text".
The text is used for names of items in QUndoView.
Definition at line 230 of file qundostack.cpp.
References QUndoCommandPrivate::text.
Referenced by QUndoCommand(), and setText().
|
virtual |
Reverts a change to the document.
After undo() is called, the state of the document should be the same as before 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 undo() on all child commands in reverse order.
Reimplemented in AppendText.
Definition at line 215 of file qundostack.cpp.
References QList< T >::at(), QUndoCommandPrivate::child_list, i, QList< T >::size(), and undo().
Referenced by undo().
|
friend |
Definition at line 46 of file qundostack.h.