CopperSpice API
1.9.2
|
The QUndoCommand class is the base class of all commands stored on a QUndoStack. More...
Public Methods | |
QUndoCommand (const QString &text, QUndoCommand *parent=nullptr) | |
QUndoCommand (QUndoCommand *parent=nullptr) | |
virtual | ~QUndoCommand () |
QString | actionText () const |
const QUndoCommand * | child (int index) const |
int | childCount () const |
virtual int | id () const |
virtual bool | mergeWith (const QUndoCommand *other) |
virtual void | redo () |
void | setText (const QString &text) |
QString | text () const |
virtual void | undo () |
Friends | |
class | QUndoStack |
The QUndoCommand class is the base class of all commands stored on a QUndoStack. For an overview of the CopperSpice refer to the Undo System documentation.
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.
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.
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 methods 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 does not 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().
Another way to create macros is to use the methods QUndoStack::beginMacro() and QUndoStack::endMacro().
|
explicit |
Constructs a QUndoCommand object with the given parent. If parent is not a nullptr this command is appended to the parent's child list. The parent command then owns this command and will delete it in the destructor.
|
explicit |
Constructs a QUndoCommand object with the given parent and text. If parent is not a nullptr this command is appended to parent's child list. The parent command then owns this command and will delete it in the destructor.
|
virtual |
Destroys the QUndoCommand object and all child commands.
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.
const QUndoCommand * QUndoCommand::child | ( | int | index | ) | const |
Returns the child command at index.
int QUndoCommand::childCount | ( | ) | const |
Returns the number of child commands in this command.
|
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 does not support compression. If the command supports compression this method 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.
|
virtual |
Attempts to merge this QUndoCommand with other. Returns true on success, otherwise returns false.
If this method returns true, calling redo() must have the same effect as redoing both this QUndoCommand and the passed 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.
|
virtual |
Applies a change to the document. This method must be implemented in the derived class. Calling QUndoStack::push(), QUndoStack::undo() or QUndoStack::redo() from this method leads to undefined behavior.
The default implementation calls redo() on all child commands.
void QUndoCommand::setText | ( | const QString & | text | ) |
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.
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.
|
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 behavior.
The default implementation calls undo() on all child commands in reverse order.