Qt 4.8
Public Slots | Signals | Public Functions | Properties | Friends | List of all members
QUndoStack Class Reference

The QUndoStack class is a stack of QUndoCommand objects. More...

#include <qundostack.h>

Inheritance diagram for QUndoStack:
QObject

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 QUndoCommandcommand (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...
 
QActioncreateRedoAction (QObject *parent, const QString &prefix=QString()) const
 Creates an redo QAction object with the given parent. More...
 
QActioncreateUndoAction (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 QObjectListchildren () 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< QByteArraydynamicPropertyNames () 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 >
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 QMetaObjectmetaObject () 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
 
QObjectparent () 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...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (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...
 
QObjectsender () 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< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QUndoStack class is a stack of QUndoCommand objects.

Since
4.2

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.

Undo and Redo Actions

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 and Macros

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.

Clean State

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.

See also
QUndoCommand, QUndoView

Definition at line 91 of file qundostack.h.

Constructors and Destructors

◆ QUndoStack()

QUndoStack::QUndoStack ( QObject parent = 0)
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.

See also
push()

Definition at line 513 of file qundostack.cpp.

514  : QObject(*(new QUndoStackPrivate), parent)
515 {
516 #ifndef QT_NO_UNDOGROUP
517  if (QUndoGroup *group = qobject_cast<QUndoGroup*>(parent))
518  group->addStack(this);
519 #endif
520 }
The QUndoGroup class is a group of QUndoStack objects.
Definition: qundogroup.h:60
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753

◆ ~QUndoStack()

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.

See also
QUndoStack()

Definition at line 529 of file qundostack.cpp.

530 {
531 #ifndef QT_NO_UNDOGROUP
532  Q_D(QUndoStack);
533  if (d->group != 0)
534  d->group->removeStack(this);
535 #endif
536  clear();
537 }
double d
Definition: qnumeric_p.h:62
void clear()
Clears the command stack by deleting all commands on it, and returns the stack to the clean state...
Definition: qundostack.cpp:552
#define Q_D(Class)
Definition: qglobal.h:2482
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

Functions

◆ beginMacro()

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.

stack.beginMacro("insert red text");
stack.push(new InsertText(document, idx, text));
stack.push(new SetColor(document, idx, text.length(), Qt::red));
stack.endMacro(); // indexChanged() is emitted

This code is equivalent to:

QUndoCommand *insertRed = new QUndoCommand(); // an empty command
insertRed->setText("insert red text");
new InsertText(document, idx, text, insertRed); // becomes child of insertRed
new SetColor(document, idx, text.length(), Qt::red, insertRed);
stack.push(insertRed);
See also
endMacro()

Definition at line 972 of file qundostack.cpp.

973 {
974  Q_D(QUndoStack);
975  QUndoCommand *cmd = new QUndoCommand();
976  cmd->setText(text);
977 
978  if (d->macro_stack.isEmpty()) {
979  while (d->index < d->command_list.size())
980  delete d->command_list.takeLast();
981  if (d->clean_index > d->index)
982  d->clean_index = -1; // we've deleted the clean state
983  d->command_list.append(cmd);
984  } else {
985  d->macro_stack.last()->d->child_list.append(cmd);
986  }
987  d->macro_stack.append(cmd);
988 
989  if (d->macro_stack.count() == 1) {
990  emit canUndoChanged(false);
992  emit canRedoChanged(false);
994  }
995 }
double d
Definition: qnumeric_p.h:62
void setText(const QString &text)
Sets the command&#39;s text to be the text specified.
Definition: qundostack.cpp:281
void redoTextChanged(const QString &redoText)
This signal is emitted whenever the value of redoText() changes.
void canUndoChanged(bool canUndo)
This signal is emitted whenever the value of canUndo() changes.
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
#define emit
Definition: qobjectdefs.h:76
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91
void undoTextChanged(const QString &undoText)
This signal is emitted whenever the value of undoText() changes.
void canRedoChanged(bool canRedo)
This signal is emitted whenever the value of canRedo() changes.
The QUndoCommand class is the base class of all commands stored on a QUndoStack.
Definition: qundostack.h:60

◆ canRedo()

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().

See also
index() canUndo()

Definition at line 838 of file qundostack.cpp.

Referenced by createRedoAction(), and push().

839 {
840  Q_D(const QUndoStack);
841  if (!d->macro_stack.isEmpty())
842  return false;
843  return d->index < d->command_list.size();
844 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ canRedoChanged

void QUndoStack::canRedoChanged ( bool  canRedo)
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().

◆ canUndo()

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.

See also
index() canRedo()

Definition at line 819 of file qundostack.cpp.

Referenced by createUndoAction(), and push().

820 {
821  Q_D(const QUndoStack);
822  if (!d->macro_stack.isEmpty())
823  return false;
824  return d->index > 0;
825 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ canUndoChanged

void QUndoStack::canUndoChanged ( bool  canUndo)
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().

◆ cleanChanged

void QUndoStack::cleanChanged ( bool  clean)
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.

See also
isClean() setClean()

Referenced by clear().

◆ cleanIndex()

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.

See also
isClean() setClean()

Definition at line 694 of file qundostack.cpp.

Referenced by QUndoModel::data().

695 {
696  Q_D(const QUndoStack);
697  return d->clean_index;
698 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ clear()

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.

See also
QUndoStack()

Definition at line 552 of file qundostack.cpp.

Referenced by ~QUndoStack().

553 {
554  Q_D(QUndoStack);
555 
556  if (d->command_list.isEmpty())
557  return;
558 
559  bool was_clean = isClean();
560 
561  d->macro_stack.clear();
562  qDeleteAll(d->command_list);
563  d->command_list.clear();
564 
565  d->index = 0;
566  d->clean_index = 0;
567 
568  emit indexChanged(0);
569  emit canUndoChanged(false);
571  emit canRedoChanged(false);
573 
574  if (!was_clean)
575  emit cleanChanged(true);
576 }
double d
Definition: qnumeric_p.h:62
void indexChanged(int idx)
This signal is emitted whenever a command modifies the state of the document.
void redoTextChanged(const QString &redoText)
This signal is emitted whenever the value of redoText() changes.
void canUndoChanged(bool canUndo)
This signal is emitted whenever the value of canUndo() changes.
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
#define emit
Definition: qobjectdefs.h:76
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91
void undoTextChanged(const QString &undoText)
This signal is emitted whenever the value of undoText() changes.
void canRedoChanged(bool canRedo)
This signal is emitted whenever the value of canRedo() changes.
bool isClean() const
If the stack is in the clean state, returns true; otherwise returns false.
Definition: qundostack.cpp:675
void cleanChanged(bool clean)
This signal is emitted whenever the stack enters or leaves the clean state.
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319

◆ command()

const QUndoCommand * QUndoStack::command ( int  index) const

Returns a const pointer to the command at index.

Since
4.4

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.

See also
QUndoCommand::child()

Definition at line 1037 of file qundostack.cpp.

1038 {
1039  Q_D(const QUndoStack);
1040 
1041  if (index < 0 || index >= d->command_list.count())
1042  return 0;
1043  return d->command_list.at(index);
1044 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91
int index() const
Returns the index of the current command.
Definition: qundostack.cpp:772

◆ count()

int QUndoStack::count ( ) const

Returns the number of commands on the stack.

Macro commands are counted as one command.

See also
index() setIndex() command()

Definition at line 758 of file qundostack.cpp.

Referenced by QUndoModel::data(), QUndoModel::index(), and QUndoModel::rowCount().

759 {
760  Q_D(const QUndoStack);
761  return d->command_list.size();
762 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ createRedoAction()

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.

See also
createUndoAction(), canRedo(), QUndoCommand::text()

Definition at line 924 of file qundostack.cpp.

925 {
926  QUndoAction *result = new QUndoAction(prefix, parent);
927  if (prefix.isEmpty())
928  result->setTextFormat(tr("Redo %1"), tr("Redo", "Default text for redo action"));
929 
930  result->setEnabled(canRedo());
931  result->setPrefixedText(redoText());
932  connect(this, SIGNAL(canRedoChanged(bool)),
933  result, SLOT(setEnabled(bool)));
935  result, SLOT(setPrefixedText(QString)));
936  connect(result, SIGNAL(triggered()), this, SLOT(redo()));
937  return result;
938 }
#define SLOT(a)
Definition: qobjectdefs.h:226
void redoTextChanged(const QString &redoText)
This signal is emitted whenever the value of redoText() changes.
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
The QString class provides a Unicode character string.
Definition: qstring.h:83
void setEnabled(bool)
Definition: qaction.cpp:1192
QString redoText() const
Returns the text of the command which will be redone in the next call to redo().
Definition: qundostack.cpp:868
void setTextFormat(const QString &textFormat, const QString &defaultText)
Definition: qundostack.cpp:435
#define SIGNAL(a)
Definition: qobjectdefs.h:227
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 rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
void setPrefixedText(const QString &text)
Definition: qundostack.cpp:419
void canRedoChanged(bool canRedo)
This signal is emitted whenever the value of canRedo() changes.
bool canRedo() const
Returns true if there is a command available for redo; otherwise returns false.
Definition: qundostack.cpp:838
void redo()
Redoes the current command by calling QUndoCommand::redo().
Definition: qundostack.cpp:736

◆ createUndoAction()

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.

See also
createRedoAction(), canUndo(), QUndoCommand::text()

Definition at line 894 of file qundostack.cpp.

895 {
896  QUndoAction *result = new QUndoAction(prefix, parent);
897  if (prefix.isEmpty())
898  result->setTextFormat(tr("Undo %1"), tr("Undo", "Default text for undo action"));
899 
900  result->setEnabled(canUndo());
901  result->setPrefixedText(undoText());
902  connect(this, SIGNAL(canUndoChanged(bool)),
903  result, SLOT(setEnabled(bool)));
905  result, SLOT(setPrefixedText(QString)));
906  connect(result, SIGNAL(triggered()), this, SLOT(undo()));
907  return result;
908 }
#define SLOT(a)
Definition: qobjectdefs.h:226
void undo()
Undoes the command below the current command by calling QUndoCommand::undo().
Definition: qundostack.cpp:710
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
void canUndoChanged(bool canUndo)
This signal is emitted whenever the value of canUndo() changes.
The QString class provides a Unicode character string.
Definition: qstring.h:83
void setEnabled(bool)
Definition: qaction.cpp:1192
void setTextFormat(const QString &textFormat, const QString &defaultText)
Definition: qundostack.cpp:435
#define SIGNAL(a)
Definition: qobjectdefs.h:227
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 rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QString undoText() const
Returns the text of the command which will be undone in the next call to undo().
Definition: qundostack.cpp:852
void undoTextChanged(const QString &undoText)
This signal is emitted whenever the value of undoText() changes.
void setPrefixedText(const QString &text)
Definition: qundostack.cpp:419
bool canUndo() const
Returns true if there is a command available for undo; otherwise returns false.
Definition: qundostack.cpp:819

◆ endMacro()

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.

See also
beginMacro()

Definition at line 1006 of file qundostack.cpp.

1007 {
1008  Q_D(QUndoStack);
1009  if (d->macro_stack.isEmpty()) {
1010  qWarning("QUndoStack::endMacro(): no matching beginMacro()");
1011  return;
1012  }
1013 
1014  d->macro_stack.removeLast();
1015 
1016  if (d->macro_stack.isEmpty()) {
1017  d->checkUndoLimit();
1018  d->setIndex(d->index + 1, false);
1019  }
1020 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT void qWarning(const char *,...)
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ index()

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.

See also
undo() redo() count()

Definition at line 772 of file qundostack.cpp.

Referenced by QUndoModel::selectedIndex().

773 {
774  Q_D(const QUndoStack);
775  return d->index;
776 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ indexChanged

void QUndoStack::indexChanged ( int  idx)
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().

See also
index() setIndex()

Referenced by clear(), and push().

◆ isActive()

bool QUndoStack::isActive ( ) const

Definition at line 1137 of file qundostack.cpp.

1138 {
1139 #ifdef QT_NO_UNDOGROUP
1140  return true;
1141 #else
1142  Q_D(const QUndoStack);
1143  return d->group == 0 || d->group->activeStack() == this;
1144 #endif
1145 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ isClean()

bool QUndoStack::isClean ( ) const

If the stack is in the clean state, returns true; otherwise returns false.

See also
setClean() cleanIndex()

Definition at line 675 of file qundostack.cpp.

Referenced by clear().

676 {
677  Q_D(const QUndoStack);
678  if (!d->macro_stack.isEmpty())
679  return false;
680  return d->clean_index == d->index;
681 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ push()

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.

See also
QUndoCommand::id() QUndoCommand::mergeWith()

Definition at line 601 of file qundostack.cpp.

602 {
603  Q_D(QUndoStack);
604  cmd->redo();
605 
606  bool macro = !d->macro_stack.isEmpty();
607 
608  QUndoCommand *cur = 0;
609  if (macro) {
610  QUndoCommand *macro_cmd = d->macro_stack.last();
611  if (!macro_cmd->d->child_list.isEmpty())
612  cur = macro_cmd->d->child_list.last();
613  } else {
614  if (d->index > 0)
615  cur = d->command_list.at(d->index - 1);
616  while (d->index < d->command_list.size())
617  delete d->command_list.takeLast();
618  if (d->clean_index > d->index)
619  d->clean_index = -1; // we've deleted the clean state
620  }
621 
622  bool try_merge = cur != 0
623  && cur->id() != -1
624  && cur->id() == cmd->id()
625  && (macro || d->index != d->clean_index);
626 
627  if (try_merge && cur->mergeWith(cmd)) {
628  delete cmd;
629  if (!macro) {
630  emit indexChanged(d->index);
635  }
636  } else {
637  if (macro) {
638  d->macro_stack.last()->d->child_list.append(cmd);
639  } else {
640  d->command_list.append(cmd);
641  d->checkUndoLimit();
642  d->setIndex(d->index + 1, false);
643  }
644  }
645 }
double d
Definition: qnumeric_p.h:62
void indexChanged(int idx)
This signal is emitted whenever a command modifies the state of the document.
virtual bool mergeWith(const QUndoCommand *other)
Attempts to merge this command with command.
Definition: qundostack.cpp:191
QList< QUndoCommand * > child_list
Definition: qundostack_p.h:71
virtual void redo()
Applies a change to the document.
Definition: qundostack.cpp:208
void redoTextChanged(const QString &redoText)
This signal is emitted whenever the value of redoText() changes.
void canUndoChanged(bool canUndo)
This signal is emitted whenever the value of canUndo() changes.
#define Q_D(Class)
Definition: qglobal.h:2482
QString redoText() const
Returns the text of the command which will be redone in the next call to redo().
Definition: qundostack.cpp:868
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
#define emit
Definition: qobjectdefs.h:76
QString undoText() const
Returns the text of the command which will be undone in the next call to undo().
Definition: qundostack.cpp:852
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91
void undoTextChanged(const QString &undoText)
This signal is emitted whenever the value of undoText() changes.
QUndoCommandPrivate * d
Definition: qundostack.h:62
T & last()
Returns a reference to the last item in the list.
Definition: qlist.h:284
void canRedoChanged(bool canRedo)
This signal is emitted whenever the value of canRedo() changes.
bool canUndo() const
Returns true if there is a command available for undo; otherwise returns false.
Definition: qundostack.cpp:819
bool canRedo() const
Returns true if there is a command available for redo; otherwise returns false.
Definition: qundostack.cpp:838
The QUndoCommand class is the base class of all commands stored on a QUndoStack.
Definition: qundostack.h:60
virtual int id() const
Returns the ID of this command.
Definition: qundostack.cpp:167

◆ redo

void QUndoStack::redo ( )
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.

See also
undo() index()

Definition at line 736 of file qundostack.cpp.

Referenced by createRedoAction().

737 {
738  Q_D(QUndoStack);
739  if (d->index == d->command_list.size())
740  return;
741 
742  if (!d->macro_stack.isEmpty()) {
743  qWarning("QUndoStack::redo(): cannot redo in the middle of a macro");
744  return;
745  }
746 
747  d->command_list.at(d->index)->redo();
748  d->setIndex(d->index + 1, false);
749 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT void qWarning(const char *,...)
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ redoText()

QString QUndoStack::redoText ( ) const

Returns the text of the command which will be redone in the next call to redo().

See also
QUndoCommand::actionText() undoText()

Definition at line 868 of file qundostack.cpp.

Referenced by createRedoAction(), and push().

869 {
870  Q_D(const QUndoStack);
871  if (!d->macro_stack.isEmpty())
872  return QString();
873  if (d->index < d->command_list.size())
874  return d->command_list.at(d->index)->actionText();
875  return QString();
876 }
double d
Definition: qnumeric_p.h:62
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ redoTextChanged

void QUndoStack::redoTextChanged ( const QString redoText)
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().

◆ setActive

void QUndoStack::setActive ( bool  active = true)
slot

Definition at line 1121 of file qundostack.cpp.

1122 {
1123 #ifdef QT_NO_UNDOGROUP
1124  Q_UNUSED(active);
1125 #else
1126  Q_D(QUndoStack);
1127 
1128  if (d->group != 0) {
1129  if (active)
1130  d->group->setActiveStack(this);
1131  else if (d->group->activeStack() == this)
1132  d->group->setActiveStack(0);
1133  }
1134 #endif
1135 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91
bool active
the active status of this stack.
Definition: qundostack.h:95
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ setClean

void QUndoStack::setClean ( )
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.

See also
isClean(), cleanIndex()

Definition at line 658 of file qundostack.cpp.

659 {
660  Q_D(QUndoStack);
661  if (!d->macro_stack.isEmpty()) {
662  qWarning("QUndoStack::setClean(): cannot set clean in the middle of a macro");
663  return;
664  }
665 
666  d->setIndex(d->index, true);
667 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT void qWarning(const char *,...)
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ setIndex

void QUndoStack::setIndex ( int  idx)
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.

See also
index() count() undo() redo()

Definition at line 786 of file qundostack.cpp.

Referenced by QUndoModel::setStackCurrentIndex().

787 {
788  Q_D(QUndoStack);
789  if (!d->macro_stack.isEmpty()) {
790  qWarning("QUndoStack::setIndex(): cannot set index in the middle of a macro");
791  return;
792  }
793 
794  if (idx < 0)
795  idx = 0;
796  else if (idx > d->command_list.size())
797  idx = d->command_list.size();
798 
799  int i = d->index;
800  while (i < idx)
801  d->command_list.at(i++)->redo();
802  while (i > idx)
803  d->command_list.at(--i)->undo();
804 
805  d->setIndex(idx, false);
806 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT void qWarning(const char *,...)
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ setUndoLimit()

void QUndoStack::setUndoLimit ( int  limit)

Definition at line 1079 of file qundostack.cpp.

1080 {
1081  Q_D(QUndoStack);
1082 
1083  if (!d->command_list.isEmpty()) {
1084  qWarning("QUndoStack::setUndoLimit(): an undo limit can only be set when the stack is empty");
1085  return;
1086  }
1087 
1088  if (limit == d->undo_limit)
1089  return;
1090  d->undo_limit = limit;
1091  d->checkUndoLimit();
1092 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT void qWarning(const char *,...)
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ text()

QString QUndoStack::text ( int  idx) const

Returns the text of the command at index idx.

See also
beginMacro()

Definition at line 1052 of file qundostack.cpp.

Referenced by QUndoModel::data().

1053 {
1054  Q_D(const QUndoStack);
1055 
1056  if (idx < 0 || idx >= d->command_list.size())
1057  return QString();
1058  return d->command_list.at(idx)->text();
1059 }
double d
Definition: qnumeric_p.h:62
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ undo

void QUndoStack::undo ( )
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.

See also
redo() index()

Definition at line 710 of file qundostack.cpp.

Referenced by createUndoAction().

711 {
712  Q_D(QUndoStack);
713  if (d->index == 0)
714  return;
715 
716  if (!d->macro_stack.isEmpty()) {
717  qWarning("QUndoStack::undo(): cannot undo in the middle of a macro");
718  return;
719  }
720 
721  int idx = d->index - 1;
722  d->command_list.at(idx)->undo();
723  d->setIndex(idx, false);
724 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT void qWarning(const char *,...)
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ undoLimit()

int QUndoStack::undoLimit ( ) const

Referenced by setUndoLimit().

◆ undoText()

QString QUndoStack::undoText ( ) const

Returns the text of the command which will be undone in the next call to undo().

See also
QUndoCommand::actionText() redoText()

Definition at line 852 of file qundostack.cpp.

Referenced by createUndoAction(), and push().

853 {
854  Q_D(const QUndoStack);
855  if (!d->macro_stack.isEmpty())
856  return QString();
857  if (d->index > 0)
858  return d->command_list.at(d->index - 1)->actionText();
859  return QString();
860 }
double d
Definition: qnumeric_p.h:62
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
The QUndoStack class is a stack of QUndoCommand objects.
Definition: qundostack.h:91

◆ undoTextChanged

void QUndoStack::undoTextChanged ( const QString undoText)
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().

Friends and Related Functions

◆ QUndoGroup

friend class QUndoGroup
friend

Definition at line 150 of file qundostack.h.

Properties

◆ active

QUndoStack::active
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.

See also
QUndoGroup

Definition at line 95 of file qundostack.h.

◆ undoLimit

int QUndoStack::undoLimit
private

the maximum number of commands on this stack.

Since
4.3

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.


The documentation for this class was generated from the following files: