42 #include <QtCore/qdebug.h> 47 #ifndef QT_NO_UNDOCOMMAND 327 #endif // QT_NO_UNDOCOMMAND 329 #ifndef QT_NO_UNDOSTACK 441 #endif // QT_NO_ACTION 455 bool was_clean =
index == clean_index;
460 emit q->canUndoChanged(q->canUndo());
461 emit q->undoTextChanged(q->undoText());
462 emit q->canRedoChanged(q->canRedo());
463 emit q->redoTextChanged(q->redoText());
469 bool is_clean =
index == clean_index;
470 if (is_clean != was_clean)
471 emit q->cleanChanged(is_clean);
486 if (undo_limit <= 0 || !macro_stack.isEmpty() || undo_limit >= command_list.count())
489 int del_count = command_list.count() - undo_limit;
491 for (
int i = 0; i < del_count; ++i)
492 delete command_list.takeFirst();
495 if (clean_index != -1) {
496 if (clean_index < del_count)
499 clean_index -= del_count;
516 #ifndef QT_NO_UNDOGROUP 517 if (
QUndoGroup *group = qobject_cast<QUndoGroup*>(parent))
518 group->addStack(
this);
531 #ifndef QT_NO_UNDOGROUP 534 d->group->removeStack(
this);
556 if (
d->command_list.isEmpty())
561 d->macro_stack.clear();
563 d->command_list.clear();
606 bool macro = !
d->macro_stack.isEmpty();
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)
622 bool try_merge = cur != 0
624 && cur->
id() == cmd->
id()
625 && (macro ||
d->index !=
d->clean_index);
638 d->macro_stack.last()->d->child_list.append(cmd);
640 d->command_list.append(cmd);
642 d->setIndex(
d->index + 1,
false);
661 if (!
d->macro_stack.isEmpty()) {
662 qWarning(
"QUndoStack::setClean(): cannot set clean in the middle of a macro");
666 d->setIndex(
d->index,
true);
678 if (!
d->macro_stack.isEmpty())
680 return d->clean_index ==
d->index;
697 return d->clean_index;
716 if (!
d->macro_stack.isEmpty()) {
717 qWarning(
"QUndoStack::undo(): cannot undo in the middle of a macro");
721 int idx =
d->index - 1;
722 d->command_list.at(idx)->undo();
723 d->setIndex(idx,
false);
739 if (
d->index ==
d->command_list.size())
742 if (!
d->macro_stack.isEmpty()) {
743 qWarning(
"QUndoStack::redo(): cannot redo in the middle of a macro");
747 d->command_list.at(
d->index)->redo();
748 d->setIndex(
d->index + 1,
false);
761 return d->command_list.size();
789 if (!
d->macro_stack.isEmpty()) {
790 qWarning(
"QUndoStack::setIndex(): cannot set index in the middle of a macro");
796 else if (idx >
d->command_list.size())
797 idx =
d->command_list.size();
801 d->command_list.at(i++)->redo();
803 d->command_list.at(--i)->undo();
805 d->setIndex(idx,
false);
822 if (!
d->macro_stack.isEmpty())
841 if (!
d->macro_stack.isEmpty())
843 return d->index <
d->command_list.size();
855 if (!
d->macro_stack.isEmpty())
858 return d->command_list.at(
d->index - 1)->actionText();
871 if (!
d->macro_stack.isEmpty())
873 if (
d->index <
d->command_list.size())
874 return d->command_list.at(
d->index)->actionText();
898 result->
setTextFormat(
tr(
"Undo %1"),
tr(
"Undo",
"Default text for undo action"));
903 result,
SLOT(setEnabled(
bool)));
928 result->
setTextFormat(
tr(
"Redo %1"),
tr(
"Redo",
"Default text for redo action"));
933 result,
SLOT(setEnabled(
bool)));
940 #endif // QT_NO_ACTION 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)
983 d->command_list.append(cmd);
985 d->macro_stack.last()->d->child_list.append(cmd);
987 d->macro_stack.append(cmd);
989 if (
d->macro_stack.count() == 1) {
1009 if (
d->macro_stack.isEmpty()) {
1010 qWarning(
"QUndoStack::endMacro(): no matching beginMacro()");
1014 d->macro_stack.removeLast();
1016 if (
d->macro_stack.isEmpty()) {
1017 d->checkUndoLimit();
1018 d->setIndex(
d->index + 1,
false);
1041 if (index < 0 || index >=
d->command_list.count())
1043 return d->command_list.at(index);
1056 if (idx < 0 || idx >=
d->command_list.size())
1058 return d->command_list.at(idx)->text();
1083 if (!
d->command_list.isEmpty()) {
1084 qWarning(
"QUndoStack::setUndoLimit(): an undo limit can only be set when the stack is empty");
1088 if (limit ==
d->undo_limit)
1090 d->undo_limit = limit;
1091 d->checkUndoLimit();
1098 return d->undo_limit;
1123 #ifdef QT_NO_UNDOGROUP 1128 if (
d->group != 0) {
1130 d->group->setActiveStack(
this);
1131 else if (
d->group->activeStack() ==
this)
1132 d->group->setActiveStack(0);
1139 #ifdef QT_NO_UNDOGROUP 1143 return d->group == 0 ||
d->group->activeStack() ==
this;
1223 #endif // QT_NO_UNDOSTACK
QString text(int idx) const
Returns the text of the command at index idx.
void clear()
Clears the command stack by deleting all commands on it, and returns the stack to the clean state...
void indexChanged(int idx)
This signal is emitted whenever a command modifies the state of the document.
#define QT_END_NAMESPACE
This macro expands to.
void setText(const QString &text)
Sets the command's text to be the text specified.
virtual bool mergeWith(const QUndoCommand *other)
Attempts to merge this command with command.
QList< QUndoCommand * > child_list
const QUndoCommand * command(int index) const
Returns a const pointer to the command at index.
virtual void redo()
Applies a change to the document.
void redoTextChanged(const QString &redoText)
This signal is emitted whenever the value of redoText() changes.
void undo()
Undoes the command below the current command by calling QUndoCommand::undo().
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
void setActive(bool active=true)
int count(const T &t) const
Returns the number of occurrences of value in the list.
void canUndoChanged(bool canUndo)
This signal is emitted whenever the value of canUndo() changes.
The QString class provides a Unicode character string.
The QObject class is the base class of all Qt objects.
QString redoText() const
Returns the text of the command which will be redone in the next call to redo().
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
virtual ~QUndoCommand()
Destroys the QUndoCommand object and all child commands.
void setTextFormat(const QString &textFormat, const QString &defaultText)
const QUndoCommand * child(int index) const
Returns the child command at index.
The QUndoGroup class is a group of QUndoStack objects.
void append(const T &t)
Inserts value at the end of the list.
#define QT_BEGIN_NAMESPACE
This macro expands to.
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
void endMacro()
Ends composition of a macro command.
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...
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
QUndoCommand(QUndoCommand *parent=0)
Constructs a QUndoCommand object with parent parent.
const T & at(int i) const
Returns the item at index position i in the list.
void push(QUndoCommand *cmd)
Pushes cmd on the stack or merges it with the most recently executed command.
Q_CORE_EXPORT void qWarning(const char *,...)
void setText(const QString &text)
QAction * createRedoAction(QObject *parent, const QString &prefix=QString()) const
Creates an redo QAction object with the given parent.
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
int childCount() const
Returns the number of child commands in this command.
QString undoText() const
Returns the text of the command which will be undone in the next call to undo().
The QUndoStack class is a stack of QUndoCommand objects.
void beginMacro(const QString &text)
Begins composition of a macro command with the given text description.
virtual void undo()
Reverts a change to the document.
void undoTextChanged(const QString &undoText)
This signal is emitted whenever the value of undoText() changes.
bool active
the active status of this stack.
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...
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
bool checkUndoLimit()
If the number of commands on the stack exceedes the undo limit, deletes commands from the bottom of t...
QString & append(QChar c)
void setClean()
Marks the stack as clean and emits cleanChanged() if the stack was not already clean.
QAction * createUndoAction(QObject *parent, const QString &prefix=QString()) const
Creates an undo QAction object with the given parent.
int cleanIndex() const
Returns the clean index.
QObject * parent() const
Returns a pointer to the parent object.
T & last()
Returns a reference to the last item in the list.
void setPrefixedText(const QString &text)
int size() const
Returns the number of items in the list.
void canRedoChanged(bool canRedo)
This signal is emitted whenever the value of canRedo() changes.
int count() const
Returns the number of commands on the stack.
bool isClean() const
If the stack is in the clean state, returns true; otherwise returns false.
void setIndex(int idx)
Repeatedly calls undo() or redo() until the current command index reaches idx.
void setIndex(int idx, bool clean)
Sets the current index to idx, emitting appropriate signals.
bool canUndo() const
Returns true if there is a command available for undo; otherwise returns false.
QUndoStack(QObject *parent=0)
Constructs an empty undo stack with the parent parent.
bool canRedo() const
Returns true if there is a command available for redo; otherwise returns false.
void cleanChanged(bool clean)
This signal is emitted whenever the stack enters or leaves the clean state.
QUndoAction(const QString &prefix, QObject *parent=0)
void setUndoLimit(int limit)
void redo()
Redoes the current command by calling QUndoCommand::redo().
int index() const
Returns the index of the current command.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
~QUndoStack()
Destroys the undo stack, deleting any commands that are on it.
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
The QUndoCommand class is the base class of all commands stored on a QUndoStack.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
virtual int id() const
Returns the ID of this command.
The QAction class provides an abstract user interface action that can be inserted into widgets...
QString text() const
Returns a short text string describing what this command does; for example, "insert text"...
QString actionText() const
Returns a short text string describing what this command does; for example, "insert text"...