📄 qtundo.cpp
字号:
it = tmp; } else ++it; } updateActions();}/*! \internal */void QtUndoManager::viewDestroyed(QObject *view){ StackMap::iterator it = m_stack_map.find(view); if (it == m_stack_map.end()) { qWarning("QtUndoManager::viewDestroyed(): no such view"); return; } if (*it == m_current_stack) m_current_stack = 0; m_stack_map.erase(it); updateActions();}/*! Directs an undo request to the appropriate QtUndoStack. The stack is chosen by finding the widget with the keyboard focus and searching its parent chain for a target. If a target is found, QtUndoStack::undo() is called on the associated stack. If no such target is found, this function does nothing. \a count is the number of commands that should be undone. It defaults to 1. \sa redo() canUndo()*/void QtUndoManager::undo(int count){ QtUndoStack *stack = currentStack(); if (stack == 0 || !stack->canUndo()) { qWarning("QtUndoManager::undo(): can't undo"); return; } stack->undo(count);}/*! Directs the redo request to the appropriate QtUndoStack. The stack is chosen by finding the widget with the keyboard focus and searching its parent chain for a target. If a target is found, QtUndoStack::redo() is called on the associated stack. If no such target is found, this function does nothing. \a count is the number of commands that should be redone. It defaults to 1. \sa undo() canRedo()*/void QtUndoManager::redo(int count){ QtUndoStack *stack = currentStack(); if (stack == 0 || !stack->canRedo()) { qWarning("QtUndoManager::redo(): can't redo"); return; } stack->redo(count);}QtUndoManager *QtUndoManager::m_manager = 0;uint QtUndoManager::m_undo_limit = 0;/*! Returns the application-global instance of QtUndoManager, creating it if it does not yet exist.*/QtUndoManager *QtUndoManager::manager(){ return g_manager();}/*! Disassociates \a obj from any stack. \sa associateView()*/void QtUndoManager::setCurrentStack(QtUndoStack *stack){ m_current_stack = stack; updateActions();}void QtUndoManager::disassociateView(QObject *obj){ if (obj == 0) { qWarning("QtUndoManager::disassociateView(): canot disassociate null object"); return; } StackMap::iterator it = m_stack_map.find(obj); if (it == m_stack_map.end()) { qWarning("QtUndoManager::disassociateView(): object has no associated stack"); return; } disconnect(obj, 0, this, 0); disconnect(*it, 0, this, 0); m_stack_map.erase(it);}/*! Associates the object \a obj with the given \a stack, adding \a obj the \a stack's targets. undo() and redo() requests will be directed to \a stack, whenever \a obj or one of its children has the keyboard focus. \sa disassociateView()*/void QtUndoManager::associateView(QObject *obj, QtUndoStack *stack){ if (obj == 0) { qWarning("QtUndoManager::associateView(): cannot associate a null object"); return; } if (stack == 0) { qWarning("QtUndoManager::associateView(): cannot associate a null stack"); return; } if (m_stack_map.contains(obj)) { qWarning("QtUndoManager::associateView(): view already associated with a stack"); return; } m_stack_map[obj] = stack; connect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(viewDestroyed(QObject*))); connect(stack, SIGNAL(commandExecuted()), this, SIGNAL(changed())); updateActions();}/*! Returns the maximum size that any undo stack can grow to. A size of 0 means that the stacks can grow indefinitely. \sa setUndoLimit()*/uint QtUndoManager::undoLimit() const{ return m_undo_limit;}/*! Sets the maximum size that any stack can grow to, to \a i. A size of 0 means that the stacks can grow indefinitely. \sa undoLimit()*/void QtUndoManager::setUndoLimit(uint i){ m_undo_limit = i;}/*! \internal */QtUndoStack *QtUndoManager::currentStack() const{ return m_current_stack;}/*! \fn void QtUndoManager::changed() \internal*//*! Returns the current undo description. The undo description is a string that describes what effects calling QtUndoManager::undo() will have on the edited object. It contains the text returned by QtCommand::description() for the current command on the QtUndoStack associated with the target widget that contains the keyboard focus. The QAction returned by createUndoAction() keeps its text property in sync with the undo description. This function is useful if you want to trigger undo with a custom widget, rather than with this QAction. \sa undoDescriptionChanged() createUndoAction() QtCommand::description() QtUndoStack::undoDescription()*/QString QtUndoManager::undoDescription() const{ return m_undo_description;}/*! Returns the current redo description. The redo description is a string that describes what effects calling QtUndoManager::redo() will have on the edited object. It contains the text returned by QtCommand::description() for the command preceding the current command on the QtUndoStack associated with the target widget that contains the keyboard focus. The QAction returned by createRedoAction() keeps its text property in sync with the redo description. This function is useful if you want to trigger redo with a custom widget, rather than with this QAction. \sa redoDescriptionChanged() createRedoAction() QtCommand::description() QtUndoStack::redoDescription()*/QString QtUndoManager::redoDescription() const{ return m_redo_description;}/*! \fn void QtUndoManager::redoDescriptionChanged(const QString &newDescription) This signal is emitted whenever the redo description for the QtUndoStack associated with the target widget that contains the keyboard focus changes. \a newDescription is the new redo description. It is useful when you want to trigger redo using a custom widget, rather than using the QAction returned by createRedoAction(). \sa redoDescription() canRedoChanged() undoDescriptionChanged()*//*! \fn void QtUndoManager::undoDescriptionChanged(const QString &newDescription) This signal is emitted whenever the undo description for the QtUndoStack associated with the target widget that contains the keyboard focus 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 QtUndoManager::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 QtUndoManager::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()*//*! Returns a list of descriptions of all the commands up to the current command in the stack associated with the currently focused target. If no target has focus, returns an empty list. \sa redoList()*/QStringList QtUndoManager::undoList() const{ QtUndoStack *stack = currentStack(); if (stack == 0) return QStringList(); return stack->undoList();}/*! Returns a list of descriptions of all the commands preceding the current command in the stack associated with the currently focused target. If no target has focus, returns an empty list. \sa undoList()*/QStringList QtUndoManager::redoList() const{ QtUndoStack *stack = currentStack(); if (stack == 0) return QStringList(); return stack->redoList();}/*! \class QtUndoListBox \brief The QtUndoListBox class is a QListBox which displays the commands on the QtUndoStack associated with the focused target. QtUndoListBox keeps track of changes in the stack and focus in the application, and updates itself accordingly. Selecting a command causes undo or redo until the selected command is current. Hence the history of changes can be undone or redone by traversing the list. \img qtundo-list.png*//*! Constructs a QtUndoListBox. The \a parent and \a name are passed on to the QListBox constructor.*/QtUndoListModel::QtUndoListModel(QObject *parent) : QAbstractItemModel(parent), m_undoIndex(0){ connect(QtUndoManager::manager(), SIGNAL(changed()), this, SLOT(updateItems()));}QtUndoListModel::~QtUndoListModel(){}void QtUndoListModel::updateItems(){ m_items = QtUndoManager::manager()->undoList(); m_undoIndex = m_items.count(); m_items += QtUndoManager::manager()->redoList(); emit reset();}int QtUndoListModel::rowCount(const QModelIndex &parent) const{ if (parent.isValid()) return 0; return m_items.count() + 1;}int QtUndoListModel::columnCount(const QModelIndex &parent) const{ Q_UNUSED(parent); return 1;}QModelIndex QtUndoListModel::parent(const QModelIndex &index) const{ Q_UNUSED(index); return QModelIndex();}QModelIndex QtUndoListModel::index(int row, int column, const QModelIndex &parent) const{ Q_UNUSED(parent); return createIndex(row, column, 0);}QVariant QtUndoListModel::data(const QModelIndex &index, int role) const{ switch (role) { case Qt::DisplayRole: if (index.row() < m_items.size()) return m_items.at(index.row()); else if (index.row() == m_items.size()) return QString(); else return QVariant(); default: return QVariant(); }}QtUndoListView::QtUndoListView(QWidget *parent) : QListView(parent){ QtUndoListModel *m = new QtUndoListModel(this); setModel(m); setSelectionMode(SingleSelection); setCurrentIndex(m->index(0, 0, QModelIndex())); connect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(undoOrRedo()));}QtUndoListView::~QtUndoListView(){}void QtUndoListView::undoOrRedo(){ int idx = currentIndex().row(); bool block = model()->blockSignals(true); int undoIndex = static_cast<QtUndoListModel*>(model())->undoIndex(); QtUndoManager *manager = QtUndoManager::manager(); if (idx < undoIndex) { for (int i = idx; i < undoIndex; ++i) { Q_ASSERT(manager->canUndo()); manager->undo(); } } else { for (int i = undoIndex; i < idx; ++i) { Q_ASSERT(manager->canRedo()); manager->redo(); } } model()->blockSignals(block);}void QtUndoListView::reset(){ QListView::reset(); QtUndoListModel *m = qobject_cast<QtUndoListModel*>(model()); setCurrentIndex(m->index(m->undoIndex(), 0, QModelIndex()));}#include "qtundo.moc"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -