📄 qundostack.cpp
字号:
QUndoAction::QUndoAction(const QString &prefix, QObject *parent) : QAction(parent){ m_prefix = prefix;}void QUndoAction::setPrefixedText(const QString &text){ QString s = m_prefix; if (!m_prefix.isEmpty() && !text.isEmpty()) s.append(QLatin1Char(' ')); s.append(text); setText(s);}#endif // QT_NO_ACTION/*! \internal Sets the current index to \a idx, emitting appropriate signals. If \a clean is true, makes \a idx the clean index as well.*/void QUndoStackPrivate::setIndex(int idx, bool clean){ Q_Q(QUndoStack); bool was_clean = index == clean_index; if (idx != index) { index = idx; emit q->indexChanged(index); emit q->canUndoChanged(q->canUndo()); emit q->undoTextChanged(q->undoText()); emit q->canRedoChanged(q->canRedo()); emit q->redoTextChanged(q->redoText()); } if (clean) clean_index = index; bool is_clean = index == clean_index; if (is_clean != was_clean) emit q->cleanChanged(is_clean);}/*! \internal If the number of commands on the stack exceedes the undo limit, deletes commands from the bottom of the stack. Returns true if commands were deleted.*/bool QUndoStackPrivate::checkUndoLimit(){ if (undo_limit <= 0 || !macro_stack.isEmpty() || undo_limit >= command_list.count()) return false; int del_count = command_list.count() - undo_limit; for (int i = 0; i < del_count; ++i) delete command_list.takeFirst(); index -= del_count; if (clean_index != -1) { if (clean_index < del_count) clean_index = -1; // we've deleted the clean command else clean_index -= del_count; } return true;}/*! Constructs an empty undo stack with the parent \a parent. The stack will initally be in the clean state. If \a parent is a QUndoGroup object, the stack is automatically added to the group. \sa push()*/QUndoStack::QUndoStack(QObject *parent) : QObject(*(new QUndoStackPrivate), parent){#ifndef QT_NO_UNDOGROUP if (QUndoGroup *group = qobject_cast<QUndoGroup*>(parent)) group->addStack(this);#endif}/*! 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. \sa QUndoStack()*/QUndoStack::~QUndoStack(){#ifndef QT_NO_UNDOGROUP Q_D(QUndoStack); if (d->group != 0) d->group->removeStack(this);#endif 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. \sa QUndoStack()*/void QUndoStack::clear(){ Q_D(QUndoStack); if (d->command_list.isEmpty()) return; bool was_clean = isClean(); d->macro_stack.clear(); qDeleteAll(d->command_list); d->command_list.clear(); d->index = 0; d->clean_index = 0; emit indexChanged(0); emit canUndoChanged(false); emit undoTextChanged(QString()); emit canRedoChanged(false); emit redoTextChanged(QString()); if (!was_clean) emit cleanChanged(true);}/*! Pushes \a cmd on the stack or merges it with the most recently executed command. In either case, executes \a cmd by calling its redo() function. If \a 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, \a cmd is deleted. In all other cases \a cmd is simply pushed on the stack. If commands were undone before \a cmd was pushed, the current command and all commands above it are deleted. Hence \a 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. \sa QUndoCommand::id() QUndoCommand::mergeWith()*/void QUndoStack::push(QUndoCommand *cmd){ Q_D(QUndoStack); cmd->redo(); bool macro = !d->macro_stack.isEmpty(); QUndoCommand *cur = 0; if (macro) { QUndoCommand *macro_cmd = d->macro_stack.last(); if (!macro_cmd->d->child_list.isEmpty()) cur = macro_cmd->d->child_list.last(); } else { if (d->index > 0) cur = d->command_list.at(d->index - 1); while (d->index < d->command_list.size()) delete d->command_list.takeLast(); if (d->clean_index > d->index) d->clean_index = -1; // we've deleted the clean state } bool try_merge = cur != 0 && cur->id() != -1 && cur->id() == cmd->id() && (macro || d->index != d->clean_index); if (try_merge && cur->mergeWith(cmd)) { delete cmd; if (!macro) { emit indexChanged(d->index); emit canUndoChanged(canUndo()); emit undoTextChanged(undoText()); emit canRedoChanged(canRedo()); emit redoTextChanged(redoText()); } } else { if (macro) { d->macro_stack.last()->d->child_list.append(cmd); } else { d->command_list.append(cmd); d->checkUndoLimit(); d->setIndex(d->index + 1, false); } }}/*! 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. \sa isClean(), cleanIndex()*/void QUndoStack::setClean(){ Q_D(QUndoStack); if (!d->macro_stack.isEmpty()) { qWarning("QUndoStack::setClean(): cannot set clean in the middle of a macro"); return; } d->setIndex(d->index, true);}/*! If the stack is in the clean state, returns true; otherwise returns false. \sa setClean() cleanIndex()*/bool QUndoStack::isClean() const{ Q_D(const QUndoStack); if (!d->macro_stack.isEmpty()) return false; return d->clean_index == d->index;}/*! 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. \sa isClean() setClean()*/int QUndoStack::cleanIndex() const{ Q_D(const QUndoStack); return d->clean_index;}/*! 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. \sa redo() index()*/void QUndoStack::undo(){ Q_D(QUndoStack); if (d->index == 0) return; if (!d->macro_stack.isEmpty()) { qWarning("QUndoStack::undo(): cannot undo in the middle of a macro"); return; } int idx = d->index - 1; d->command_list.at(idx)->undo(); d->setIndex(idx, false);}/*! 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. \sa undo() index()*/void QUndoStack::redo(){ Q_D(QUndoStack); if (d->index == d->command_list.size()) return; if (!d->macro_stack.isEmpty()) { qWarning("QUndoStack::redo(): cannot redo in the middle of a macro"); return; } d->command_list.at(d->index)->redo(); d->setIndex(d->index + 1, false);}/*! Returns the number of commands on the stack. Macro commands are counted as one command. \sa index() setIndex()*/int QUndoStack::count() const{ Q_D(const QUndoStack); return d->command_list.size();}/*! 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. \sa undo() redo() count()*/int QUndoStack::index() const{ Q_D(const QUndoStack); return d->index;}/*! Repeatedly calls undo() or redo() until the the current command index reaches \a idx. This function can be used to roll the state of the document forwards of backwards. indexChanged() is emitted only once. \sa index() count() undo() redo()*/void QUndoStack::setIndex(int idx){ Q_D(QUndoStack); if (!d->macro_stack.isEmpty()) { qWarning("QUndoStack::setIndex(): cannot set index in the middle of a macro"); return; } if (idx < 0) idx = 0; else if (idx > d->command_list.size()) idx = d->command_list.size(); int i = d->index; while (i < idx) d->command_list.at(i++)->redo(); while (i > idx) d->command_list.at(--i)->undo(); d->setIndex(idx, false);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -