Qt 4.8
Public Functions | Properties | Friends | List of all members
QUndoCommand Class Reference

The QUndoCommand class is the base class of all commands stored on a QUndoStack. More...

#include <qundostack.h>

Public Functions

QString actionText () const
 Returns a short text string describing what this command does; for example, "insert text". More...
 
const QUndoCommandchild (int index) const
 Returns the child command at index. More...
 
int childCount () const
 Returns the number of child commands in this command. More...
 
virtual int id () const
 Returns the ID of this command. More...
 
virtual bool mergeWith (const QUndoCommand *other)
 Attempts to merge this command with command. More...
 
 QUndoCommand (QUndoCommand *parent=0)
 Constructs a QUndoCommand object with parent parent. More...
 
 QUndoCommand (const QString &text, QUndoCommand *parent=0)
 Constructs a QUndoCommand object with the given parent and text. More...
 
virtual void redo ()
 Applies a change to the document. More...
 
void setText (const QString &text)
 Sets the command's text to be the text specified. More...
 
QString text () const
 Returns a short text string describing what this command does; for example, "insert text". More...
 
virtual void undo ()
 Reverts a change to the document. More...
 
virtual ~QUndoCommand ()
 Destroys the QUndoCommand object and all child commands. More...
 

Properties

QUndoCommandPrivated
 

Friends

class QUndoStack
 

Detailed Description

The QUndoCommand class is the base class of all commands stored on a QUndoStack.

Since
4.2

For an overview of Qt's Undo Framework, see the 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.

class AppendText : public QUndoCommand
{
public:
AppendText(QString *doc, const QString &text)
: m_document(doc), m_text(text) { setText("append text"); }
virtual void undo()
{ m_document->chop(m_text.length()); }
virtual void redo()
{ m_document->append(m_text); }
private:
QString *m_document;
QString m_text;
};

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:

MyCommand *command1 = new MyCommand();
stack->push(command1);
MyCommand *command2 = new MyCommand();
stack->push(command2);
stack->undo();
MyCommand *command3 = new MyCommand();
stack->push(command3); // command2 gets deleted

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

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);

Another way to create macros is to use the convenience functions QUndoStack::beginMacro() and QUndoStack::endMacro().

See also
QUndoStack

Definition at line 60 of file qundostack.h.

Constructors and Destructors

◆ QUndoCommand() [1/2]

QUndoCommand::QUndoCommand ( QUndoCommand parent = 0)
explicit

Constructs a QUndoCommand object with parent parent.

If parent is not 0, this command is appended to parent's child list. The parent command then owns this command and will delete it in its destructor.

See also
~QUndoCommand()

Definition at line 133 of file qundostack.cpp.

134 {
135  d = new QUndoCommandPrivate;
136  if (parent != 0)
137  parent->d->child_list.append(this);
138 }
QList< QUndoCommand * > child_list
Definition: qundostack_p.h:71
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QUndoCommandPrivate * d
Definition: qundostack.h:62

◆ QUndoCommand() [2/2]

QUndoCommand::QUndoCommand ( const QString text,
QUndoCommand parent = 0 
)
explicit

Constructs a QUndoCommand object with the given parent and text.

If parent is not 0, this command is appended to parent's child list. The parent command then owns this command and will delete it in its destructor.

See also
~QUndoCommand()

Definition at line 115 of file qundostack.cpp.

116 {
117  d = new QUndoCommandPrivate;
118  if (parent != 0)
119  parent->d->child_list.append(this);
120  setText(text);
121 }
void setText(const QString &text)
Sets the command&#39;s text to be the text specified.
Definition: qundostack.cpp:281
QList< QUndoCommand * > child_list
Definition: qundostack_p.h:71
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QUndoCommandPrivate * d
Definition: qundostack.h:62

◆ ~QUndoCommand()

QUndoCommand::~QUndoCommand ( )
virtual

Destroys the QUndoCommand object and all child commands.

See also
QUndoCommand()

Definition at line 146 of file qundostack.cpp.

147 {
149  delete d;
150 }
QList< QUndoCommand * > child_list
Definition: qundostack_p.h:71
QUndoCommandPrivate * d
Definition: qundostack.h:62
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319

Functions

◆ actionText()

QString QUndoCommand::actionText ( ) const

Returns a short text string describing what this command does; for example, "insert text".

Since
4.8

The text is used when the text properties of the stack's undo and redo actions are updated.

See also
text(), setText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction()

Definition at line 261 of file qundostack.cpp.

262 {
263  return d->actionText;
264 }
QUndoCommandPrivate * d
Definition: qundostack.h:62

◆ child()

const QUndoCommand * QUndoCommand::child ( int  index) const

Returns the child command at index.

Since
4.4
See also
childCount(), QUndoStack::command()

Definition at line 320 of file qundostack.cpp.

321 {
322  if (index < 0 || index >= d->child_list.count())
323  return 0;
324  return d->child_list.at(index);
325 }
QList< QUndoCommand * > child_list
Definition: qundostack_p.h:71
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QUndoCommandPrivate * d
Definition: qundostack.h:62
quint16 index

◆ childCount()

int QUndoCommand::childCount ( ) const

Returns the number of child commands in this command.

Since
4.4
See also
child()

Definition at line 304 of file qundostack.cpp.

305 {
306  return d->child_list.count();
307 }
QList< QUndoCommand * > child_list
Definition: qundostack_p.h:71
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QUndoCommandPrivate * d
Definition: qundostack.h:62

◆ id()

int QUndoCommand::id ( ) const
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.

See also
mergeWith(), QUndoStack::push()

Definition at line 167 of file qundostack.cpp.

Referenced by QUndoStack::push().

168 {
169  return -1;
170 }

◆ mergeWith()

bool QUndoCommand::mergeWith ( const QUndoCommand command)
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.

bool AppendText::mergeWith(const QUndoCommand *other)
{
if (other->id() != id()) // make sure other is also an AppendText command
return false;
m_text += static_cast<const AppendText*>(other)->m_text;
return true;
}
See also
id() QUndoStack::push()

Definition at line 191 of file qundostack.cpp.

Referenced by QUndoStack::push().

192 {
193  Q_UNUSED(command);
194  return false;
195 }
#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

◆ redo()

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

See also
undo()

Definition at line 208 of file qundostack.cpp.

Referenced by QUndoStack::push(), and redo().

209 {
210  for (int i = 0; i < d->child_list.size(); ++i)
211  d->child_list.at(i)->redo();
212 }
QList< QUndoCommand * > child_list
Definition: qundostack_p.h:71
virtual void redo()
Applies a change to the document.
Definition: qundostack.cpp:208
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QUndoCommandPrivate * d
Definition: qundostack.h:62
int size() const
Returns the number of items in the list.
Definition: qlist.h:137

◆ setText()

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. The described feature and the function actionText() are available since Qt 4.8.

See also
text() actionText() QUndoStack::createUndoAction() QUndoStack::createRedoAction()

Definition at line 281 of file qundostack.cpp.

Referenced by QUndoStack::beginMacro(), and QUndoCommand().

282 {
283  int cdpos = text.indexOf(QLatin1Char('\n'));
284  if (cdpos > 0) {
285  d->text = text.left(cdpos);
286  d->actionText = text.mid(cdpos + 1);
287  } else {
288  d->text = text;
289  d->actionText = text;
290  }
291 }
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
QUndoCommandPrivate * d
Definition: qundostack.h:62
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QString text() const
Returns a short text string describing what this command does; for example, "insert text"...
Definition: qundostack.cpp:241

◆ text()

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.

See also
actionText(), setText(), QUndoStack::createUndoAction(), QUndoStack::createRedoAction()

Definition at line 241 of file qundostack.cpp.

Referenced by setText().

242 {
243  return d->text;
244 }
QUndoCommandPrivate * d
Definition: qundostack.h:62

◆ undo()

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

See also
redo()

Definition at line 226 of file qundostack.cpp.

Referenced by undo().

227 {
228  for (int i = d->child_list.size() - 1; i >= 0; --i)
229  d->child_list.at(i)->undo();
230 }
QList< QUndoCommand * > child_list
Definition: qundostack_p.h:71
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
virtual void undo()
Reverts a change to the document.
Definition: qundostack.cpp:226
QUndoCommandPrivate * d
Definition: qundostack.h:62
int size() const
Returns the number of items in the list.
Definition: qlist.h:137

Friends and Related Functions

◆ QUndoStack

friend class QUndoStack
friend

Definition at line 84 of file qundostack.h.

Properties

◆ d

QUndoCommandPrivate* QUndoCommand::d
private

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