📄 qtundo.cpp
字号:
else return QString();}void QtUndoStack::setCurrent(){ QtUndoManager::manager()->setCurrentStack(this);}/*! Clears all the commands on this undo stack. \sa push()*/void QtUndoStack::clear(){ QtUndoState state; beforeChange(state); while (!isEmpty()) delete takeLast(); m_macro_nest = 0; m_num_commands = 0; m_have_clean_command = true; m_clean_command = 0; afterChange(state);}/*! Returns a list of descriptions of all the commands up to the current command in this stack. \sa redoList()*/QStringList QtUndoStack::undoList() const{ QStringList result; if (m_macro_nest > 0) return result; if (m_current_iter == -1) return result; for (int it = 0; it <= m_current_iter; ++it) { QtCommand *command = commandAt(it); result.append(command->description()); Q_ASSERT(!command->isMacroEnd()); if (command->isMacroBegin()) it = findMacroEnd(it); } return result;}/*! Returns a list of descriptions of all the commands preceding the current command in this stack. \sa undoList()*/QStringList QtUndoStack::redoList() const{ QStringList result; if (m_macro_nest > 0) return result; for (int it = m_current_iter + 1; it < size(); ++it) { QtCommand *command = commandAt(it); result.append(command->description()); Q_ASSERT(!command->isMacroEnd()); if (command->isMacroBegin()) it = findMacroEnd(it); } return result;}/*! Creates a QAction object connected to the QtUndoStack's undo() slot. The \a parent becomes the owner and parent of the QAction. This is significant, since any accelerators that are assigned to the QAction will only work within the \a parent. Unlike QtUndoManager::createUndoAction(), the returned QAction is connected directly to this stack's undo() slot. This is useful if you want each of your target windows to have it's own undo button. The returned QAction will keep its text property in sync with undoDescription() and disable itself whenever no commands are available for undo. If the application's default QMimeSourceFactory contains a pixmap called "undo" or "undo.png", this pixmap is assigned to the QAction. \sa undo() undoDescription() createRedoAction()*/QAction *QtUndoStack::createUndoAction(QObject *parent) const{ UndoRedoAction *undo_action = new UndoRedoAction(parent); connect(undo_action, SIGNAL(triggered()), this, SLOT(undo())); connect(this, SIGNAL(undoDescriptionChanged(QString)), undo_action, SLOT(setTextSlot(QString))); connect(this, SIGNAL(canUndoChanged(bool)), undo_action, SLOT(setEnabled(bool))); undo_action->setEnabled(canUndo()); undo_action->setText(undoDescription()); return undo_action;}/*! Creates a QAction object connected to the QtUndoStack's redo() slot. The \a parent becomes the owner and parent of the QAction. This is significant, since any accelerators that are assigned to the QAction will only work within the \a parent. Unlike QtUndoManager::createRedoAction(), the returned QAction is connected directly to this stack's redo() slot. This is useful if you want each of your target windows to have it's own redo button. The returned QAction will keep its text property in sync with redoDescription() and disable itself whenever no commands are available for redo. If the application's default QMimeSourceFactory contains a pixmap called "redo" or "redo.png", this pixmap is assigned to the QAction. \sa redo() redoDescription() createUndoAction()*/QAction *QtUndoStack::createRedoAction(QObject *parent) const{ UndoRedoAction *redo_action = new UndoRedoAction(parent); connect(redo_action, SIGNAL(triggered()), this, SLOT(redo())); connect(this, SIGNAL(redoDescriptionChanged(QString)), redo_action, SLOT(setTextSlot(QString))); connect(this, SIGNAL(canRedoChanged(bool)), redo_action, SLOT(setEnabled(bool))); redo_action->setEnabled(canRedo()); redo_action->setText(redoDescription()); return redo_action;}/*! \fn void QtUndoStack::commandExecuted() This signal is emitted whenever a QtCommand on the stack is undone or redone. When macro commands are undone or redone, this signal is emitted only once, even though the macro may contain more than one command. \sa redo() undo()*//*! \fn void QtUndoStack::undoDescriptionChanged(const QString &newDescription) This signal is emitted whenever the undo description for this QtUndoStack changes. \a newDescription is the new undo description. It is useful when you want to trigger undo using a custom widget, rather than using the QAction returned by createUndoAction(). \sa undoDescription() canUndoChanged() redoDescriptionChanged()*//*! \fn void QtUndoStack::redoDescriptionChanged(const QString &newDescription) This signal is emitted whenever the redo description for this QtUndoStack changes. \a newDescription is the new redo description. It is useful when you want to trigger undo using a custom widget, rather than using the QAction returned by createRedoAction(). \sa redoDescription() canRedoChanged() undoDescriptionChanged()*//*! \fn void QtUndoStack::canUndoChanged(bool enabled) This signal is emitted whenever the state reported by canUndo() changes. \a enabled is the new state. This function is useful if you want to trigger undo with a custom widget, rather than the QAction returned by createUndoAction(). \sa canUndo() undoDescriptionChanged() canRedoChanged()*//*! \fn void QtUndoStack::canRedoChanged(bool enabled) This signal is emitted whenever the state reported by canRedo() changes. \a enabled is the new state. This function is useful if you want to trigger redo with a custom widget, rather than the QAction returned by createRedoAction(). \sa canRedo() redoDescriptionChanged() canUndoChanged()*//*! \class QtUndoManager \brief The QtUndoManager class manages command stacks in an undo/redo framework based on the Command design pattern. For an overview of the Qt Undo/Redo framework, see the \link overview.html overview\endlink. QtUndoManager keeps a list of QtUndoStack objects. Each is a list of QtCommand objects and a pointer to the last executed command (the undo stack's \e current command). Undo is invoked by calling the current command's QtCommand::undo() function and making the previous command in the stack the current command. Redo is invoked by making the next command in the stack the current command and calling its QtCommand::redo() function. An application has one global QtUndoManager, accessed through the static function QtUndoManager::manager() (which creates the QtUndoManager when it is called for the first time). Undo and redo are requested through the undo() and redo() slots. QtUndoManager also provides the functions createUndoAction() and createRedoAction() for creating QAction objects that trigger undo and redo. These QActions have the additional benefit of keeping their text properties in sync with undoDescription() and redoDescription(), as well as disabling themselves whenever no commands are available for undo or redo. \code MainWindow::MainWindow(QWidget *parent, const char *name) : QMainWindow(parent, name) { ... QtUndoManager *manager = QtUndoManager::manager(); QAction *undo_action = manager->createUndoAction(this); QAction *redo_action = manager->createRedoAction(this); undo_action->setAccel(QKeySequence("Ctrl+Z")); redo_action->setAccel(QKeySequence("Shift+Ctrl+Z")); QToolBar *toolbar = new QToolBar(this); undo_action->addTo(toolbar); redo_action->addTo(toolbar); QPopupMenu *editmenu = new QPopupMenu(this); undo_action->addTo(editmenu); redo_action->addTo(editmenu); ... } \endcode A single application can have multiple undo stacks, typically one for each editor window in an MDI application. Each stack is associated with a widget, called the stack's \e target. undo() and redo() requests are directed to a stack whenever its target, or a child of its target, has the keyboard focus. A target is associated with a stack in QtUndoStack's constructor. Additional targets may be associated with a stack using associateView(). This is useful when two or more editor windows edit the same underlying object. An SDI aplication typically has a single stack associated with the application's main window. Whenever the widget with the keyboard focus has no targets in its parent chain, the QAction objects created using createUndoAction() and createRedoAction() are disabled. \img qtundo-menu.png <p> \img qtundo-toolbar.png \sa QtCommand QtUndoStack*//*! \internal */QtUndoManager::QtUndoManager(){ m_current_stack = 0; m_can_undo = false; m_can_redo = false; updateActions();}/*! Creates a QAction object connected to the QtUndoManager's undo() slot. The \a parent becomes the owner and parent of the QAction. This is significant, since any accelerators that are assigned to the QAction will only work within the \a parent. The returned QAction will keep its text property in sync with undoDescription() and disable itself whenever no commands are available for undo. If the application's default QMimeSourceFactory contains a pixmap called "undo" or "undo.png", this pixmap is assigned to the QAction. \sa undo() undoDescription() createRedoAction()*/QAction *QtUndoManager::createUndoAction(QObject *parent) const{ UndoRedoAction *undo_action = new UndoRedoAction(parent); connect(undo_action, SIGNAL(triggered()), this, SLOT(undo())); connect(this, SIGNAL(undoDescriptionChanged(QString)), undo_action, SLOT(setTextSlot(QString))); connect(this, SIGNAL(canUndoChanged(bool)), undo_action, SLOT(setEnabled(bool))); undo_action->setEnabled(m_can_undo); undo_action->setText(m_undo_description); return undo_action;}/*! Creates a QAction object connected to the QtUndoManager's redo() slot. The \a parent becomes the owner and parent of the QAction. This is significant, since any accelerators that are assigned to the QAction will only work within the \a parent. The returned QAction will keep its text property in sync with redoDescription() and disable itself whenever no commands are available for redo. If the application's default QMimeSourceFactory contains a pixmap called "redo" or "redo.png", this pixmap is assigned to the QAction. \sa redo() redoDescription() createUndoAction()*/QAction *QtUndoManager::createRedoAction(QObject *parent) const{ UndoRedoAction *redo_action = new UndoRedoAction(parent); connect(redo_action, SIGNAL(triggered()), this, SLOT(redo())); connect(this, SIGNAL(redoDescriptionChanged(QString)), redo_action, SLOT(setTextSlot(QString))); connect(this, SIGNAL(canRedoChanged(bool)), redo_action, SLOT(setEnabled(bool))); redo_action->setEnabled(m_can_redo); redo_action->setText(m_redo_description); return redo_action;}/*! \internal */void QtUndoManager::updateActions(){ QtUndoStack *stack = currentStack(); bool undo_enabled = stack != 0 && stack->canUndo(); QString undo_description = tr("Undo"); if (undo_enabled) undo_description += QLatin1String(" ") + stack->undoDescription(); if (undo_enabled != m_can_undo) { m_can_undo = undo_enabled; emit canUndoChanged(undo_enabled); } if (undo_description != m_undo_description) { m_undo_description = undo_description; emit undoDescriptionChanged(undo_description); } bool redo_enabled = stack != 0 && stack->canRedo(); QString redo_description = tr("Redo"); if (redo_enabled) redo_description += QLatin1String(" ") + stack->redoDescription(); if (redo_enabled != m_can_redo) { m_can_redo = redo_enabled; emit canRedoChanged(redo_enabled); } if (redo_description != m_redo_description) { m_redo_description = redo_description; emit redoDescriptionChanged(redo_description); }}/*! Returns true if a command is available for redo; otherwise returns false. Redo is not possible if the widget with the keyboard focus has no targets in its parent chain, or if a target is found but the associated stack is empty, or if the last command on the stack has already been undone. The QAction returned by createUndoAction() disables itself whenever canUndo() is false. \sa undo() canRedo()*/bool QtUndoManager::canUndo() const{ return m_can_undo;}/*! Returns true if a command is available for undo; otherwise returns false. Undo is not possible if the widget with the keyboard focus has no targets in its parent chain, or if a target is found but the associated stack is empty, or if the first command on the stack has already been redone. A QAction returned by createRedoAction() disables itself whenever canRedo() is false. \sa redo() canUndo()*/bool QtUndoManager::canRedo() const{ return m_can_redo;}/*! \internal */void QtUndoManager::stackDestroyed(QObject *stack){ if (m_current_stack == stack) m_current_stack = 0; // remove all views associated with that stack from the map StackMap::iterator it = m_stack_map.begin(); while (it != m_stack_map.end()) { if (*it == stack) { disconnect(it.key(), 0, this, 0); StackMap::iterator tmp = it; // iterator invalidation ++tmp; m_stack_map.erase(it);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -