|
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.gui.QUndoCommand
public class QUndoCommand
The QUndoCommand class is the base class of all commands stored on a QUndoStack.
For an overview of Qt's Undo Framework, see the 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().
Nested Class Summary |
---|
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter |
---|
QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I> |
Constructor Summary | |
---|---|
QUndoCommand()
Equivalent to QUndoCommand(0). |
|
QUndoCommand(QUndoCommand parent)
Constructs a QUndoCommand object with parent parent. |
|
QUndoCommand(java.lang.String text)
Equivalent to QUndoCommand(text, 0). |
|
QUndoCommand(java.lang.String text,
QUndoCommand parent)
Constructs a QUndoCommand object with the given parent and text. |
Method Summary | |
---|---|
static QUndoCommand |
fromNativePointer(QNativePointer nativePointer)
This function returns the QUndoCommand instance pointed to by nativePointer |
int |
id()
Returns the ID of this command. |
boolean |
mergeWith(QUndoCommand other)
Attempts to merge this command with other. |
void |
redo()
Applies a change to the document. |
void |
setText(java.lang.String text)
Sets the command's text to be the text specified. |
java.lang.String |
text()
Returns a short text string describing what this command does; for example, "insert text". |
void |
undo()
Reverts a change to the document. |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Constructor Detail |
---|
public QUndoCommand()
Equivalent to QUndoCommand(0).
public QUndoCommand(QUndoCommand parent)
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.
public QUndoCommand(java.lang.String text)
Equivalent to QUndoCommand(text, 0).
public QUndoCommand(java.lang.String text, QUndoCommand parent)
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.
Method Detail |
---|
public final void setText(java.lang.String 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.
public final java.lang.String text()
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.
public int id()
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.
public boolean mergeWith(QUndoCommand other)
Attempts to merge this command with other. 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 other. Similarly, calling this command's undo must have the same effect as undoing other 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;
}
public void redo()
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.
public void undo()
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.
public static QUndoCommand fromNativePointer(QNativePointer nativePointer)
nativePointer
- the QNativePointer of which object should be returned.
|
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |