diff options
author | Marcus Tillmanns <marcus.tillmanns@qt.io> | 2024-04-18 08:49:23 +0200 |
---|---|---|
committer | Marcus Tillmanns <marcus.tillmanns@qt.io> | 2024-04-18 09:31:40 +0000 |
commit | 9019520d621124fb8e7ab66484166987f4605f02 (patch) | |
tree | 190bd702d1133659ac9d1718172f846e6ff36dd6 | |
parent | c9299cad64924adf10c923b338dbf8e96452fd69 (diff) |
CompilerExplorer: Fix Undo/Redo actions
Change-Id: Ib3f55467764600d0b1e4ef25b53874a4d37a7ba7
Reviewed-by: David Schulz <david.schulz@qt.io>
-rw-r--r-- | src/plugins/compilerexplorer/compilerexplorereditor.cpp | 41 | ||||
-rw-r--r-- | src/plugins/compilerexplorer/compilerexplorereditor.h | 9 | ||||
-rw-r--r-- | src/plugins/texteditor/texteditor.cpp | 32 | ||||
-rw-r--r-- | src/plugins/texteditor/texteditor.h | 5 |
4 files changed, 62 insertions, 25 deletions
diff --git a/src/plugins/compilerexplorer/compilerexplorereditor.cpp b/src/plugins/compilerexplorer/compilerexplorereditor.cpp index 706958da98..e1e564c733 100644 --- a/src/plugins/compilerexplorer/compilerexplorereditor.cpp +++ b/src/plugins/compilerexplorer/compilerexplorereditor.cpp @@ -52,6 +52,7 @@ using namespace std::chrono_literals; using namespace Aggregation; using namespace TextEditor; using namespace Utils; +using namespace Core; namespace CompilerExplorer { @@ -65,7 +66,11 @@ constexpr char SourceEditorHoverLine[] = "SourceEditor.HoveredLine"; CodeEditorWidget::CodeEditorWidget(const std::shared_ptr<SourceSettings> &settings, QUndoStack *undoStack) : m_settings(settings) - , m_undoStack(undoStack){}; + , m_undoStack(undoStack) +{ + connect(undoStack, &QUndoStack::canUndoChanged, this, [this]() { updateUndoRedoActions(); }); + connect(undoStack, &QUndoStack::canRedoChanged, this, [this]() { updateUndoRedoActions(); }); +}; void CodeEditorWidget::updateHighlighter() { @@ -417,9 +422,9 @@ CompilerWidget::CompilerWidget(const std::shared_ptr<SourceSettings> &sourceSett m_spinner = new SpinnerSolution::Spinner(SpinnerSolution::SpinnerSize::Large, this); } -Core::SearchableTerminal *CompilerWidget::createTerminal() +SearchableTerminal *CompilerWidget::createTerminal() { - m_resultTerminal = new Core::SearchableTerminal(); + m_resultTerminal = new SearchableTerminal(); m_resultTerminal->setAllowBlinkingCursor(false); std::array<QColor, 20> colors{Utils::creatorTheme()->color(Utils::Theme::TerminalAnsi0), Utils::creatorTheme()->color(Utils::Theme::TerminalAnsi1), @@ -553,7 +558,7 @@ void CompilerWidget::doCompile() const QList<QTextEdit::ExtraSelection> links = m_asmDocument->setCompileResult(r); m_asmEditor->setExtraSelections(AsmEditorLinks, links); } catch (const std::exception &e) { - Core::MessageManager::writeDisrupting( + MessageManager::writeDisrupting( Tr::tr("Failed to compile: \"%1\".").arg(QString::fromUtf8(e.what()))); } }); @@ -830,8 +835,22 @@ TextEditor::TextEditorWidget *EditorWidget::focusedEditorWidget() const Editor::Editor() : m_document(new JsonSettingsDocument(&m_undoStack)) { - setContext(Core::Context(Constants::CE_EDITOR_ID)); + setContext(Context(Constants::CE_EDITOR_ID)); setWidget(new EditorWidget(m_document, &m_undoStack)); + + m_undoAction = ActionBuilder(this, Core::Constants::UNDO) + .setContext(m_context) + .addOnTriggered([this] { m_undoStack.undo(); }) + .setScriptable(true) + .contextAction(); + m_redoAction = ActionBuilder(this, Core::Constants::REDO) + .setContext(m_context) + .addOnTriggered([this] { m_undoStack.redo(); }) + .setScriptable(true) + .contextAction(); + + connect(&m_undoStack, &QUndoStack::canUndoChanged, m_undoAction, &QAction::setEnabled); + connect(&m_undoStack, &QUndoStack::canRedoChanged, m_redoAction, &QAction::setEnabled); } Editor::~Editor() @@ -839,18 +858,6 @@ Editor::~Editor() delete widget(); } -static bool childHasFocus(QWidget *parent) -{ - if (parent->hasFocus()) - return true; - - for (QWidget *child : parent->findChildren<QWidget *>()) - if (childHasFocus(child)) - return true; - - return false; -} - QWidget *Editor::toolBar() { if (!m_toolBar) { diff --git a/src/plugins/compilerexplorer/compilerexplorereditor.h b/src/plugins/compilerexplorer/compilerexplorereditor.h index 56eef3fd36..7aa7b185ef 100644 --- a/src/plugins/compilerexplorer/compilerexplorereditor.h +++ b/src/plugins/compilerexplorer/compilerexplorereditor.h @@ -38,6 +38,9 @@ public: void undo() override { m_undoStack->undo(); } void redo() override { m_undoStack->redo(); } + bool isUndoAvailable() const override { return m_undoStack->canUndo(); } + bool isRedoAvailable() const override { return m_undoStack->canRedo(); } + void focusInEvent(QFocusEvent *event) override { TextEditorWidget::focusInEvent(event); @@ -76,6 +79,7 @@ public: { TextEditorWidget::focusInEvent(event); emit gotFocus(); + updateUndoRedoActions(); } void findLinkAt(const QTextCursor &, @@ -86,6 +90,9 @@ public: void undo() override { m_undoStack->undo(); } void redo() override { m_undoStack->redo(); } + bool isUndoAvailable() const override { return m_undoStack->canUndo(); } + bool isRedoAvailable() const override { return m_undoStack->canRedo(); } + protected: void mouseMoveEvent(QMouseEvent *event) override; void leaveEvent(QEvent *event) override; @@ -268,6 +275,8 @@ public: std::shared_ptr<JsonSettingsDocument> m_document; QUndoStack m_undoStack; std::unique_ptr<QToolBar> m_toolBar; + QAction *m_undoAction = nullptr; + QAction *m_redoAction = nullptr; }; class EditorFactory : public Core::IEditorFactory diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 089c483d2e..749e7bf2f7 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -745,8 +745,8 @@ public: void registerActions(); void updateActions(); void updateOptionalActions(); - void updateRedoAction(bool on); - void updateUndoAction(bool on); + void updateRedoAction(); + void updateUndoAction(); void updateCopyAction(bool on); public: @@ -1936,6 +1936,12 @@ void TextEditorWidget::setVisualIndentOffset(int offset) d->m_visualIndentOffset = qMax(0, offset); } +void TextEditorWidget::updateUndoRedoActions() +{ + d->updateUndoAction(); + d->updateRedoAction(); +} + void TextEditorWidgetPrivate::updateCannotDecodeInfo() { q->setReadOnly(m_document->hasDecodingError()); @@ -2587,6 +2593,16 @@ void TextEditorWidget::redo() QPlainTextEdit::redo(); } +bool TextEditorWidget::isUndoAvailable() const +{ + return document()->isUndoAvailable(); +} + +bool TextEditorWidget::isRedoAvailable() const +{ + return document()->isRedoAvailable(); +} + void TextEditorWidget::openLinkUnderCursor() { d->openLinkUnderCursor(alwaysOpenLinksInNextSplit()); @@ -4340,8 +4356,8 @@ void TextEditorWidgetPrivate::updateActions() m_visualizeWhitespaceAction->setChecked(m_displaySettings.m_visualizeWhitespace); m_textWrappingAction->setChecked(m_displaySettings.m_textWrapping); - updateRedoAction(q->document()->isRedoAvailable()); - updateUndoAction(q->document()->isUndoAvailable()); + updateRedoAction(); + updateUndoAction(); updateCopyAction(q->textCursor().hasSelection()); updateOptionalActions(); @@ -4368,14 +4384,14 @@ void TextEditorWidgetPrivate::updateOptionalActions() m_autoFormatAction->setEnabled(formatEnabled); } -void TextEditorWidgetPrivate::updateRedoAction(bool on) +void TextEditorWidgetPrivate::updateRedoAction() { - m_redoAction->setEnabled(on); + m_redoAction->setEnabled(q->isRedoAvailable()); } -void TextEditorWidgetPrivate::updateUndoAction(bool on) +void TextEditorWidgetPrivate::updateUndoAction() { - m_undoAction->setEnabled(on); + m_undoAction->setEnabled(q->isUndoAvailable()); } void TextEditorWidgetPrivate::updateCopyAction(bool hasCopyableText) diff --git a/src/plugins/texteditor/texteditor.h b/src/plugins/texteditor/texteditor.h index 2d54231b1c..78dc0e0b82 100644 --- a/src/plugins/texteditor/texteditor.h +++ b/src/plugins/texteditor/texteditor.h @@ -457,6 +457,9 @@ public: virtual void undo(); virtual void redo(); + virtual bool isUndoAvailable() const; + virtual bool isRedoAvailable() const; + void openLinkUnderCursor(); void openLinkUnderCursorInNextSplit(); void openTypeUnderCursor(); @@ -584,6 +587,8 @@ protected: void setVisualIndentOffset(int offset); + void updateUndoRedoActions(); + public: QString selectedText() const; |