📄 qundostack.cpp
字号:
/*! 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. \sa index() canRedo()*/bool QUndoStack::canUndo() const{ Q_D(const QUndoStack); if (!d->macro_stack.isEmpty()) return false; return d->index > 0;}/*! 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(). \sa index() canUndo()*/bool QUndoStack::canRedo() const{ Q_D(const QUndoStack); if (!d->macro_stack.isEmpty()) return false; return d->index < d->command_list.size();}/*! Returns the text of the command which will be undone in the next call to undo(). \sa QUndoCommand::text() redoText()*/QString QUndoStack::undoText() const{ Q_D(const QUndoStack); if (!d->macro_stack.isEmpty()) return QString(); if (d->index > 0) return d->command_list.at(d->index - 1)->text(); return QString();}/*! Returns the text of the command which will be redone in the next call to redo(). \sa QUndoCommand::text() undoText()*/QString QUndoStack::redoText() const{ Q_D(const QUndoStack); if (!d->macro_stack.isEmpty()) return QString(); if (d->index < d->command_list.size()) return d->command_list.at(d->index)->text(); return QString();}#ifndef QT_NO_ACTION/*! Creates an undo QAction object with the given \a 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 \a prefix. If there is no command available for undo, this action will be disabled. If \a prefix is empty, the default prefix "Undo" is used. \sa createRedoAction(), canUndo(), QUndoCommand::text()*/QAction *QUndoStack::createUndoAction(QObject *parent, const QString &prefix) const{ QString pref = prefix.isEmpty() ? tr("Undo") : prefix; QUndoAction *result = new QUndoAction(pref, parent); result->setEnabled(canUndo()); result->setPrefixedText(undoText()); connect(this, SIGNAL(canUndoChanged(bool)), result, SLOT(setEnabled(bool))); connect(this, SIGNAL(undoTextChanged(QString)), result, SLOT(setPrefixedText(QString))); connect(result, SIGNAL(triggered()), this, SLOT(undo())); return result;}/*! Creates an redo QAction object with the given \a 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 \a prefix. If there is no command available for redo, this action will be disabled. If \a prefix is empty, the default prefix "Redo" is used. \sa createUndoAction(), canRedo(), QUndoCommand::text()*/QAction *QUndoStack::createRedoAction(QObject *parent, const QString &prefix) const{ QString pref = prefix.isEmpty() ? tr("Redo") : prefix; QUndoAction *result = new QUndoAction(pref, parent); result->setEnabled(canRedo()); result->setPrefixedText(redoText()); connect(this, SIGNAL(canRedoChanged(bool)), result, SLOT(setEnabled(bool))); connect(this, SIGNAL(redoTextChanged(QString)), result, SLOT(setPrefixedText(QString))); connect(result, SIGNAL(triggered()), this, SLOT(redo())); return result;}#endif // QT_NO_ACTION/*! Begins composition of a macro command with the given \a text description. An empty command described by the specified \a 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: \list \i indexChanged() and cleanChanged() are not emitted, \i canUndo() and canRedo() return false, \i calling undo() or redo() has no effect, \i the undo/redo actions are disabled. \endlist The stack becomes enabled and appropriate signals are emitted when endMacro() is called for the outermost macro. \code 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 \endcode This code is equivalent to: \code 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); \endcode \sa endMacro()*/void QUndoStack::beginMacro(const QString &text){ Q_D(QUndoStack); QUndoCommand *cmd = new QUndoCommand(); cmd->setText(text); if (d->macro_stack.isEmpty()) { 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 d->command_list.append(cmd); } else { d->macro_stack.last()->d->child_list.append(cmd); } d->macro_stack.append(cmd); if (d->macro_stack.count() == 1) { emit canUndoChanged(false); emit undoTextChanged(QString()); emit canRedoChanged(false); emit redoTextChanged(QString()); }}/*! 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. \sa beginMacro()*/void QUndoStack::endMacro(){ Q_D(QUndoStack); if (d->macro_stack.isEmpty()) { qWarning("QUndoStack::endMacro(): no matching beginMacro()"); return; } d->macro_stack.removeLast(); if (d->macro_stack.isEmpty()) { d->checkUndoLimit(); d->setIndex(d->index + 1, false); }}/*! Returns the text of the command at index \a idx. \sa beginMacro()*/QString QUndoStack::text(int idx) const{ Q_D(const QUndoStack); if (idx < 0 || idx >= d->command_list.size()) return QString(); return d->command_list.at(idx)->text();}/*! \property QUndoStack::undoLimit \brief 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.*/void QUndoStack::setUndoLimit(int limit){ Q_D(QUndoStack); if (!d->command_list.isEmpty()) { qWarning("QUndoStack::setUndoLimit(): an undo limit can only be set when the stack is empty"); return; } if (limit == d->undo_limit) return; d->undo_limit = limit; d->checkUndoLimit();}int QUndoStack::undoLimit() const{ Q_D(const QUndoStack); return d->undo_limit;}/*! \property QUndoStack::active \brief 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. \sa QUndoGroup*/void QUndoStack::setActive(bool active){#ifdef QT_NO_UNDOGROUP Q_UNUSED(active);#else Q_D(QUndoStack); if (d->group != 0) { if (active) d->group->setActiveStack(this); else if (d->group->activeStack() == this) d->group->setActiveStack(0); }#endif}bool QUndoStack::isActive() const{#ifdef QT_NO_UNDOGROUP return true;#else Q_D(const QUndoStack); return d->group == 0 || d->group->activeStack() == this;#endif}/*! \fn void QUndoStack::indexChanged(int idx) 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. \a idx specifies the index of the current command, ie. the command which will be executed on the next call to redo(). \sa index() setIndex()*//*! \fn void QUndoStack::cleanChanged(bool clean) This signal is emitted whenever the stack enters or leaves the clean state. If \a clean is true, the stack is in a clean state; otherwise this signal indicates that it has left the clean state. \sa isClean() setClean()*//*! \fn void QUndoStack::undoTextChanged(const QString &undoText) 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(). \a undoText specifies the new text.*//*! \fn void QUndoStack::canUndoChanged(bool canUndo) This signal is emitted whenever the value of canUndo() changes. It is used to enable or disable the undo action returned by createUndoAction(). \a canUndo specifies the new value.*//*! \fn void QUndoStack::redoTextChanged(const QString &redoText) 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(). \a redoText specifies the new text.*//*! \fn void QUndoStack::canRedoChanged(bool canUndo) This signal is emitted whenever the value of canRedo() changes. It is used to enable or disable the redo action returned by createRedoAction(). \a canUndo specifies the new value.*/#endif // QT_NO_UNDOSTACK
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -