TextEditor: Allow overwriting undo handling

In preparation for CompilerExplorer undo / redo handling.

Change-Id: Iefad5cc497c8e439f626e375fe8862526b52a36e
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Marcus Tillmanns
2023-08-31 08:50:11 +02:00
parent 37022ff0df
commit 705232c737
3 changed files with 43 additions and 5 deletions

View File

@@ -434,8 +434,8 @@ public:
void indent(); void indent();
void unindent(); void unindent();
void undo(); virtual void undo();
void redo(); virtual void redo();
void openLinkUnderCursor(); void openLinkUnderCursor();
void openLinkUnderCursorInNextSplit(); void openLinkUnderCursorInNextSplit();

View File

@@ -100,6 +100,9 @@ public:
void updateCurrentEditor(Core::IEditor *editor); void updateCurrentEditor(Core::IEditor *editor);
void setCanUndoCallback(const TextEditorActionHandler::Predicate &callback);
void setCanRedoCallback(const TextEditorActionHandler::Predicate &callback);
public: public:
TextEditorActionHandler::TextEditorWidgetResolver m_findTextWidget; TextEditorActionHandler::TextEditorWidgetResolver m_findTextWidget;
QAction *m_undoAction = nullptr; QAction *m_undoAction = nullptr;
@@ -124,8 +127,12 @@ public:
uint m_optionalActions = TextEditorActionHandler::None; uint m_optionalActions = TextEditorActionHandler::None;
QPointer<TextEditorWidget> m_currentEditorWidget; QPointer<TextEditorWidget> m_currentEditorWidget;
QPointer<Core::IEditor> m_currentEditor;
Utils::Id m_editorId; Utils::Id m_editorId;
Utils::Id m_contextId; Utils::Id m_contextId;
TextEditorActionHandler::Predicate m_canUndoCallback;
TextEditorActionHandler::Predicate m_canRedoCallback;
}; };
TextEditorActionHandlerPrivate::TextEditorActionHandlerPrivate TextEditorActionHandlerPrivate::TextEditorActionHandlerPrivate
@@ -463,9 +470,17 @@ void TextEditorActionHandlerPrivate::updateActions()
m_textWrappingAction->setChecked(m_currentEditorWidget->displaySettings().m_textWrapping); m_textWrappingAction->setChecked(m_currentEditorWidget->displaySettings().m_textWrapping);
} }
updateRedoAction(m_currentEditorWidget && m_currentEditorWidget->document()->isRedoAvailable()); if (m_currentEditorWidget) {
updateUndoAction(m_currentEditorWidget && m_currentEditorWidget->document()->isUndoAvailable()); updateRedoAction(m_canRedoCallback ? m_canRedoCallback(m_currentEditor)
updateCopyAction(m_currentEditorWidget && m_currentEditorWidget->textCursor().hasSelection()); : m_currentEditorWidget->document()->isRedoAvailable());
updateUndoAction(m_canUndoCallback ? m_canUndoCallback(m_currentEditor)
: m_currentEditorWidget->document()->isUndoAvailable());
updateCopyAction(m_currentEditorWidget->textCursor().hasSelection());
} else {
updateRedoAction(false);
updateUndoAction(false);
updateCopyAction(false);
}
updateOptionalActions(); updateOptionalActions();
} }
@@ -523,6 +538,8 @@ void TextEditorActionHandlerPrivate::updateCurrentEditor(Core::IEditor *editor)
m_currentEditorWidget->disconnect(this); m_currentEditorWidget->disconnect(this);
m_currentEditorWidget = nullptr; m_currentEditorWidget = nullptr;
m_currentEditor = editor;
if (editor && editor->document()->id() == m_editorId) { if (editor && editor->document()->id() == m_editorId) {
TextEditorWidget *editorWidget = m_findTextWidget(editor); TextEditorWidget *editorWidget = m_findTextWidget(editor);
QTC_ASSERT(editorWidget, return); // editor has our id, so shouldn't happen QTC_ASSERT(editorWidget, return); // editor has our id, so shouldn't happen
@@ -570,4 +587,19 @@ void TextEditorActionHandler::updateCurrentEditor()
d->updateCurrentEditor(Core::EditorManager::currentEditor()); d->updateCurrentEditor(Core::EditorManager::currentEditor());
} }
void TextEditorActionHandler::updateActions()
{
d->updateActions();
}
void TextEditorActionHandler::setCanUndoCallback(const Predicate &callback)
{
d->m_canUndoCallback = callback;
}
void TextEditorActionHandler::setCanRedoCallback(const Predicate &callback)
{
d->m_canRedoCallback = callback;
}
} // namespace TextEditor } // namespace TextEditor

View File

@@ -50,6 +50,12 @@ public:
~TextEditorActionHandler(); ~TextEditorActionHandler();
void updateCurrentEditor(); void updateCurrentEditor();
void updateActions();
using Predicate = std::function<bool(Core::IEditor *editor)>;
void setCanUndoCallback(const Predicate &callback);
void setCanRedoCallback(const Predicate &callback);
private: private:
Internal::TextEditorActionHandlerPrivate *d; Internal::TextEditorActionHandlerPrivate *d;