Qt 4.8
|
The QUndoStack class is a stack of QUndoCommand objects. More...
#include <qundostack.h>
Public Slots | |
void | redo () |
Redoes the current command by calling QUndoCommand::redo(). More... | |
void | setActive (bool active=true) |
void | setClean () |
Marks the stack as clean and emits cleanChanged() if the stack was not already clean. More... | |
void | setIndex (int idx) |
Repeatedly calls undo() or redo() until the current command index reaches idx. More... | |
void | undo () |
Undoes the command below the current command by calling QUndoCommand::undo(). More... | |
Public Slots inherited from QObject | |
void | deleteLater () |
Schedules this object for deletion. More... | |
Signals | |
void | canRedoChanged (bool canRedo) |
This signal is emitted whenever the value of canRedo() changes. More... | |
void | canUndoChanged (bool canUndo) |
This signal is emitted whenever the value of canUndo() changes. More... | |
void | cleanChanged (bool clean) |
This signal is emitted whenever the stack enters or leaves the clean state. More... | |
void | indexChanged (int idx) |
This signal is emitted whenever a command modifies the state of the document. More... | |
void | redoTextChanged (const QString &redoText) |
This signal is emitted whenever the value of redoText() changes. More... | |
void | undoTextChanged (const QString &undoText) |
This signal is emitted whenever the value of undoText() changes. More... | |
Signals inherited from QObject | |
void | destroyed (QObject *=0) |
This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More... | |
Public Functions | |
void | beginMacro (const QString &text) |
Begins composition of a macro command with the given text description. More... | |
bool | canRedo () const |
Returns true if there is a command available for redo; otherwise returns false. More... | |
bool | canUndo () const |
Returns true if there is a command available for undo; otherwise returns false. More... | |
int | cleanIndex () const |
Returns the clean index. More... | |
void | clear () |
Clears the command stack by deleting all commands on it, and returns the stack to the clean state. More... | |
const QUndoCommand * | command (int index) const |
Returns a const pointer to the command at index. More... | |
int | count () const |
Returns the number of commands on the stack. More... | |
QAction * | createRedoAction (QObject *parent, const QString &prefix=QString()) const |
Creates an redo QAction object with the given parent. More... | |
QAction * | createUndoAction (QObject *parent, const QString &prefix=QString()) const |
Creates an undo QAction object with the given parent. More... | |
void | endMacro () |
Ends composition of a macro command. More... | |
int | index () const |
Returns the index of the current command. More... | |
bool | isActive () const |
bool | isClean () const |
If the stack is in the clean state, returns true; otherwise returns false. More... | |
void | push (QUndoCommand *cmd) |
Pushes cmd on the stack or merges it with the most recently executed command. More... | |
QUndoStack (QObject *parent=0) | |
Constructs an empty undo stack with the parent parent. More... | |
QString | redoText () const |
Returns the text of the command which will be redone in the next call to redo(). More... | |
void | setUndoLimit (int limit) |
QString | text (int idx) const |
Returns the text of the command at index idx. More... | |
int | undoLimit () const |
QString | undoText () const |
Returns the text of the command which will be undone in the next call to undo(). More... | |
~QUndoStack () | |
Destroys the undo stack, deleting any commands that are on it. More... | |
Public Functions inherited from QObject | |
bool | blockSignals (bool b) |
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More... | |
const QObjectList & | children () const |
Returns a list of child objects. More... | |
bool | connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const |
bool | disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0) |
bool | disconnect (const QObject *receiver, const char *member=0) |
void | dumpObjectInfo () |
Dumps information about signal connections, etc. More... | |
void | dumpObjectTree () |
Dumps a tree of children to the debug output. More... | |
QList< QByteArray > | dynamicPropertyNames () const |
Returns the names of all properties that were dynamically added to the object using setProperty(). More... | |
virtual bool | event (QEvent *) |
This virtual function receives events to an object and should return true if the event e was recognized and processed. More... | |
virtual bool | eventFilter (QObject *, QEvent *) |
Filters events if this object has been installed as an event filter for the watched object. More... | |
template<typename T > | |
T | findChild (const QString &aName=QString()) const |
Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More... | |
template<typename T > | |
QList< T > | findChildren (const QString &aName=QString()) const |
Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More... | |
template<typename T > | |
QList< T > | findChildren (const QRegExp &re) const |
bool | inherits (const char *classname) const |
Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More... | |
void | installEventFilter (QObject *) |
Installs an event filter filterObj on this object. More... | |
bool | isWidgetType () const |
Returns true if the object is a widget; otherwise returns false. More... | |
void | killTimer (int id) |
Kills the timer with timer identifier, id. More... | |
virtual const QMetaObject * | metaObject () const |
Returns a pointer to the meta-object of this object. More... | |
void | moveToThread (QThread *thread) |
Changes the thread affinity for this object and its children. More... | |
QString | objectName () const |
QObject * | parent () const |
Returns a pointer to the parent object. More... | |
QVariant | property (const char *name) const |
Returns the value of the object's name property. More... | |
Q_INVOKABLE | QObject (QObject *parent=0) |
Constructs an object with parent object parent. More... | |
void | removeEventFilter (QObject *) |
Removes an event filter object obj from this object. More... | |
void | setObjectName (const QString &name) |
void | setParent (QObject *) |
Makes the object a child of parent. More... | |
bool | setProperty (const char *name, const QVariant &value) |
Sets the value of the object's name property to value. More... | |
void | setUserData (uint id, QObjectUserData *data) |
bool | signalsBlocked () const |
Returns true if signals are blocked; otherwise returns false. More... | |
int | startTimer (int interval) |
Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More... | |
QThread * | thread () const |
Returns the thread in which the object lives. More... | |
QObjectUserData * | userData (uint id) const |
virtual | ~QObject () |
Destroys the object, deleting all its child objects. More... | |
Properties | |
bool | active |
the active status of this stack. More... | |
int | undoLimit |
the maximum number of commands on this stack. More... | |
Friends | |
class | QUndoGroup |
Additional Inherited Members | |
Static Public Functions inherited from QObject | |
static bool | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection) |
Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More... | |
static bool | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member) |
Disconnects signal in object sender from method in object receiver. More... | |
static bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member) |
static uint | registerUserData () |
static QString | tr (const char *sourceText, const char *comment=0, int n=-1) |
static QString | trUtf8 (const char *sourceText, const char *comment=0, int n=-1) |
Static Public Variables inherited from QObject | |
static const QMetaObject | staticMetaObject |
This variable stores the meta-object for the class. More... | |
Protected Functions inherited from QObject | |
virtual void | childEvent (QChildEvent *) |
This event handler can be reimplemented in a subclass to receive child events. More... | |
virtual void | connectNotify (const char *signal) |
This virtual function is called when something has been connected to signal in this object. More... | |
virtual void | customEvent (QEvent *) |
This event handler can be reimplemented in a subclass to receive custom events. More... | |
virtual void | disconnectNotify (const char *signal) |
This virtual function is called when something has been disconnected from signal in this object. More... | |
QObject (QObjectPrivate &dd, QObject *parent=0) | |
int | receivers (const char *signal) const |
Returns the number of receivers connected to the signal. More... | |
QObject * | sender () const |
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More... | |
int | senderSignalIndex () const |
virtual void | timerEvent (QTimerEvent *) |
This event handler can be reimplemented in a subclass to receive timer events for the object. More... | |
Protected Variables inherited from QObject | |
QScopedPointer< QObjectData > | d_ptr |
Static Protected Variables inherited from QObject | |
static const QMetaObject | staticQtMetaObject |
Related Functions inherited from QObject | |
T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QRegExp ®Exp)() |
T * | qobject_cast (QObject *object) |
QObjectList | |
void * | qt_find_obj_child (QObject *parent, const char *type, const QString &name) |
Returns a pointer to the object named name that inherits type and with a given parent. More... | |
The QUndoStack class is a stack of QUndoCommand objects.
For an overview of Qt's Undo Framework, see the Overview of Qt's Undo Framework{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.
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 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.
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.
Definition at line 91 of file qundostack.h.
|
explicit |
Constructs an empty undo stack with the parent parent.
The stack will initially be in the clean state. If parent is a QUndoGroup object, the stack is automatically added to the group.
Definition at line 513 of file qundostack.cpp.
QUndoStack::~QUndoStack | ( | ) |
Destroys the undo stack, deleting any commands that are on it.
If the stack is in a QUndoGroup, the stack is automatically removed from the group.
Definition at line 529 of file qundostack.cpp.
void QUndoStack::beginMacro | ( | const QString & | 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.
This code is equivalent to:
Definition at line 972 of file qundostack.cpp.
bool QUndoStack::canRedo | ( | ) | const |
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().
Definition at line 838 of file qundostack.cpp.
Referenced by createRedoAction(), and push().
|
signal |
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.
Referenced by beginMacro(), clear(), createRedoAction(), and push().
bool QUndoStack::canUndo | ( | ) | const |
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.
Definition at line 819 of file qundostack.cpp.
Referenced by createUndoAction(), and push().
|
signal |
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.
Referenced by beginMacro(), clear(), createUndoAction(), and push().
|
signal |
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.
Referenced by clear().
int QUndoStack::cleanIndex | ( | ) | const |
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.
Definition at line 694 of file qundostack.cpp.
Referenced by QUndoModel::data().
void QUndoStack::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.
Definition at line 552 of file qundostack.cpp.
Referenced by ~QUndoStack().
const QUndoCommand * QUndoStack::command | ( | int | index | ) | const |
Returns a const pointer to the command at index.
This function returns a const pointer, because modifying a command, once it has been pushed onto the stack and executed, almost always causes corruption of the state of the document, if the command is later undone or redone.
Definition at line 1037 of file qundostack.cpp.
int QUndoStack::count | ( | ) | const |
Returns the number of commands on the stack.
Macro commands are counted as one command.
Definition at line 758 of file qundostack.cpp.
Referenced by QUndoModel::data(), QUndoModel::index(), and QUndoModel::rowCount().
QAction * QUndoStack::createRedoAction | ( | QObject * | parent, |
const QString & | prefix = QString() |
||
) | const |
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 template "Redo %1" is used instead of prefix. Before Qt 4.8, the prefix "Redo" was used by default.
Definition at line 924 of file qundostack.cpp.
QAction * QUndoStack::createUndoAction | ( | QObject * | parent, |
const QString & | prefix = QString() |
||
) | const |
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 template "Undo %1" is used instead of prefix. Before Qt 4.8, the prefix "Undo" was used by default.
Definition at line 894 of file qundostack.cpp.
void QUndoStack::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.
Definition at line 1006 of file qundostack.cpp.
int QUndoStack::index | ( | ) | const |
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.
Definition at line 772 of file qundostack.cpp.
Referenced by QUndoModel::selectedIndex().
|
signal |
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().
bool QUndoStack::isActive | ( | ) | const |
Definition at line 1137 of file qundostack.cpp.
bool QUndoStack::isClean | ( | ) | const |
If the stack is in the clean state, returns true; otherwise returns false.
Definition at line 675 of file qundostack.cpp.
Referenced by clear().
void QUndoStack::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.
Definition at line 601 of file qundostack.cpp.
|
slot |
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.
Definition at line 736 of file qundostack.cpp.
Referenced by createRedoAction().
QString QUndoStack::redoText | ( | ) | const |
Returns the text of the command which will be redone in the next call to redo().
Definition at line 868 of file qundostack.cpp.
Referenced by createRedoAction(), and push().
|
signal |
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.
Referenced by beginMacro(), clear(), createRedoAction(), and push().
|
slot |
Definition at line 1121 of file qundostack.cpp.
|
slot |
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.
Definition at line 658 of file qundostack.cpp.
|
slot |
Repeatedly calls undo() or redo() until 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.
Definition at line 786 of file qundostack.cpp.
Referenced by QUndoModel::setStackCurrentIndex().
void QUndoStack::setUndoLimit | ( | int | limit | ) |
Definition at line 1079 of file qundostack.cpp.
QString QUndoStack::text | ( | int | idx | ) | const |
Returns the text of the command at index idx.
Definition at line 1052 of file qundostack.cpp.
Referenced by QUndoModel::data().
|
slot |
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.
Definition at line 710 of file qundostack.cpp.
Referenced by createUndoAction().
int QUndoStack::undoLimit | ( | ) | const |
Referenced by setUndoLimit().
QString QUndoStack::undoText | ( | ) | const |
Returns the text of the command which will be undone in the next call to undo().
Definition at line 852 of file qundostack.cpp.
Referenced by createUndoAction(), and push().
|
signal |
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.
Referenced by beginMacro(), clear(), createUndoAction(), and push().
|
friend |
Definition at line 150 of file qundostack.h.
|
private |
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.
Definition at line 95 of file qundostack.h.
|
private |
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.
Definition at line 96 of file qundostack.h.