From 705232c7372eccc37d92b54706737829c8308945 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Thu, 31 Aug 2023 08:50:11 +0200 Subject: [PATCH] TextEditor: Allow overwriting undo handling In preparation for CompilerExplorer undo / redo handling. Change-Id: Iefad5cc497c8e439f626e375fe8862526b52a36e Reviewed-by: David Schulz Reviewed-by: --- src/plugins/texteditor/texteditor.h | 4 +- .../texteditor/texteditoractionhandler.cpp | 38 +++++++++++++++++-- .../texteditor/texteditoractionhandler.h | 6 +++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/plugins/texteditor/texteditor.h b/src/plugins/texteditor/texteditor.h index 0540c86be57..4f0ea99c8d4 100644 --- a/src/plugins/texteditor/texteditor.h +++ b/src/plugins/texteditor/texteditor.h @@ -434,8 +434,8 @@ public: void indent(); void unindent(); - void undo(); - void redo(); + virtual void undo(); + virtual void redo(); void openLinkUnderCursor(); void openLinkUnderCursorInNextSplit(); diff --git a/src/plugins/texteditor/texteditoractionhandler.cpp b/src/plugins/texteditor/texteditoractionhandler.cpp index d22f8c96570..d8405d33469 100644 --- a/src/plugins/texteditor/texteditoractionhandler.cpp +++ b/src/plugins/texteditor/texteditoractionhandler.cpp @@ -100,6 +100,9 @@ public: void updateCurrentEditor(Core::IEditor *editor); + void setCanUndoCallback(const TextEditorActionHandler::Predicate &callback); + void setCanRedoCallback(const TextEditorActionHandler::Predicate &callback); + public: TextEditorActionHandler::TextEditorWidgetResolver m_findTextWidget; QAction *m_undoAction = nullptr; @@ -124,8 +127,12 @@ public: uint m_optionalActions = TextEditorActionHandler::None; QPointer m_currentEditorWidget; + QPointer m_currentEditor; Utils::Id m_editorId; Utils::Id m_contextId; + + TextEditorActionHandler::Predicate m_canUndoCallback; + TextEditorActionHandler::Predicate m_canRedoCallback; }; TextEditorActionHandlerPrivate::TextEditorActionHandlerPrivate @@ -463,9 +470,17 @@ void TextEditorActionHandlerPrivate::updateActions() m_textWrappingAction->setChecked(m_currentEditorWidget->displaySettings().m_textWrapping); } - updateRedoAction(m_currentEditorWidget && m_currentEditorWidget->document()->isRedoAvailable()); - updateUndoAction(m_currentEditorWidget && m_currentEditorWidget->document()->isUndoAvailable()); - updateCopyAction(m_currentEditorWidget && m_currentEditorWidget->textCursor().hasSelection()); + if (m_currentEditorWidget) { + updateRedoAction(m_canRedoCallback ? m_canRedoCallback(m_currentEditor) + : 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(); } @@ -523,6 +538,8 @@ void TextEditorActionHandlerPrivate::updateCurrentEditor(Core::IEditor *editor) m_currentEditorWidget->disconnect(this); m_currentEditorWidget = nullptr; + m_currentEditor = editor; + if (editor && editor->document()->id() == m_editorId) { TextEditorWidget *editorWidget = m_findTextWidget(editor); QTC_ASSERT(editorWidget, return); // editor has our id, so shouldn't happen @@ -570,4 +587,19 @@ void TextEditorActionHandler::updateCurrentEditor() 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 diff --git a/src/plugins/texteditor/texteditoractionhandler.h b/src/plugins/texteditor/texteditoractionhandler.h index ce19e8535f9..0886b6a909b 100644 --- a/src/plugins/texteditor/texteditoractionhandler.h +++ b/src/plugins/texteditor/texteditoractionhandler.h @@ -50,6 +50,12 @@ public: ~TextEditorActionHandler(); void updateCurrentEditor(); + void updateActions(); + + using Predicate = std::function; + + void setCanUndoCallback(const Predicate &callback); + void setCanRedoCallback(const Predicate &callback); private: Internal::TextEditorActionHandlerPrivate *d;