From ef8323ee96dd59d3c0b1b778e3bd937554eed473 Mon Sep 17 00:00:00 2001 From: hjk Date: Wed, 17 Jan 2024 14:48:52 +0100 Subject: [PATCH] TextEditor: Move bookmark related action setup ... to BookMarkManager. For less coupling. Change-Id: I51c88fc9ee9a3456b1075bbe4c7ad039fc6f1889 Reviewed-by: Reviewed-by: David Schulz --- src/plugins/texteditor/bookmarkmanager.cpp | 118 +++++++++++++++++- src/plugins/texteditor/bookmarkmanager.h | 26 ++-- src/plugins/texteditor/texteditorplugin.cpp | 126 +------------------- 3 files changed, 134 insertions(+), 136 deletions(-) diff --git a/src/plugins/texteditor/bookmarkmanager.cpp b/src/plugins/texteditor/bookmarkmanager.cpp index ae21a75be88..650cfc32d54 100644 --- a/src/plugins/texteditor/bookmarkmanager.cpp +++ b/src/plugins/texteditor/bookmarkmanager.cpp @@ -4,9 +4,11 @@ #include "bookmarkmanager.h" #include "bookmark.h" +#include "texteditor.h" #include "texteditorconstants.h" #include "texteditortr.h" +#include #include #include #include @@ -15,11 +17,12 @@ #include #include +#include +#include #include #include -#include #include -#include +#include #include #include @@ -318,12 +321,103 @@ void BookmarkView::gotoBookmark(const QModelIndex &index) BookmarkManager::BookmarkManager() : m_selectionModel(new QItemSelectionModel(this, this)) { + m_editBookmarkAction.setText(Tr::tr("Edit Bookmark")); + m_bookmarkMarginAction.setText(Tr::tr("Toggle Bookmark")); + connect(ICore::instance(), &ICore::contextChanged, this, &BookmarkManager::updateActionStatus); connect(SessionManager::instance(), &SessionManager::sessionLoaded, this, &BookmarkManager::loadBookmarks); + const Id bookmarkMenuId = "Bookmarks.Menu"; + const Context editorManagerContext(Core::Constants::C_EDITORMANAGER); + + m_bookmarkMenu.setId(bookmarkMenuId); + m_bookmarkMenu.setTitle(Tr::tr("&Bookmarks")); + m_bookmarkMenu.setContainer(Core::Constants::M_TOOLS); + + connect(&m_editBookmarkAction, &QAction::triggered, this, [this] { + editByFileAndLine(m_marginActionFileName, m_marginActionLineNumber); + }); + + connect(&m_bookmarkMarginAction, &QAction::triggered, this, [this] { + toggleBookmark(m_marginActionFileName, m_marginActionLineNumber); + }); + + ActionBuilder toggleAction(this, "Bookmarks.Toggle"); + toggleAction.setContext(editorManagerContext); + toggleAction.setText(Tr::tr("Toggle Bookmark")); + toggleAction.setDefaultKeySequence(Tr::tr("Meta+M"), Tr::tr("Ctrl+M")); + toggleAction.setTouchBarIcon(Icons::MACOS_TOUCHBAR_BOOKMARK.icon()); + toggleAction.addToContainer(bookmarkMenuId); + toggleAction.bindContextAction(&m_toggleAction); + toggleAction.addOnTriggered(this, [this] { + IEditor *editor = EditorManager::currentEditor(); + auto widget = TextEditorWidget::fromEditor(editor); + if (widget && editor && !editor->document()->isTemporary()) + toggleBookmark(editor->document()->filePath(), editor->currentLine()); + }); + + ActionBuilder editAction(this, "Bookmarks.Edit"); + editAction.setContext(editorManagerContext); + editAction.setText(Tr::tr("Edit Bookmark")); + editAction.setDefaultKeySequence(Tr::tr("Meta+Shift+M"), Tr::tr("Ctrl+Shift+M")); + editAction.addToContainer(bookmarkMenuId); + editAction.bindContextAction(&m_editAction); + editAction.addOnTriggered(this, [this] { + IEditor *editor = EditorManager::currentEditor(); + auto widget = TextEditorWidget::fromEditor(editor); + if (widget && editor && !editor->document()->isTemporary()) { + const FilePath filePath = editor->document()->filePath(); + const int line = editor->currentLine(); + if (!hasBookmarkInPosition(filePath, line)) + toggleBookmark(filePath, line); + editByFileAndLine(filePath, line); + } + }); + + m_bookmarkMenu.addSeparator(); + + ActionBuilder prevAction(this, Constants::BOOKMARKS_PREV_ACTION); + prevAction.setContext(editorManagerContext); + prevAction.setText(Tr::tr("Previous Bookmark")); + prevAction.setDefaultKeySequence(Tr::tr("Meta+,"), Tr::tr("Ctrl+,")); + prevAction.addToContainer(bookmarkMenuId); + prevAction.setIcon(Icons::PREV_TOOLBAR.icon()); + prevAction.setIconVisibleInMenu(false); + prevAction.bindContextAction(&m_prevAction); + prevAction.addOnTriggered(this, [this] { prev(); }); + + ActionBuilder nextAction(this, Constants::BOOKMARKS_NEXT_ACTION); + nextAction.setContext(editorManagerContext); + nextAction.setText(Tr::tr("Next Bookmark")); + nextAction.setIcon(Icons::NEXT_TOOLBAR.icon()); + nextAction.setIconVisibleInMenu(false); + nextAction.setDefaultKeySequence(Tr::tr("Meta+."), Tr::tr("Ctrl+.")); + nextAction.addToContainer(bookmarkMenuId); + nextAction.bindContextAction(&m_nextAction); + nextAction.addOnTriggered(this, [this] { next(); }); + + m_bookmarkMenu.addSeparator(); + + ActionBuilder docPrevAction(this, "Bookmarks.PreviousDocument"); + docPrevAction.setContext(editorManagerContext); + docPrevAction.setText(Tr::tr("Previous Bookmark in Document")); + docPrevAction.addToContainer(bookmarkMenuId); + docPrevAction.bindContextAction(&m_docPrevAction); + docPrevAction.addOnTriggered(this, [this] { prevInDocument(); }); + + ActionBuilder docNextAction(this, "Bookmarks.NextDocument"); + docNextAction.setContext(Core::Constants::C_EDITORMANAGER); + docNextAction.setText(Tr::tr("Next Bookmark in Document")); + docNextAction.addToContainer(bookmarkMenuId); + docNextAction.bindContextAction(&m_docNextAction); + docNextAction.addOnTriggered(this, [this] { nextInDocument(); }); + + ActionContainer *touchBar = ActionManager::actionContainer(Core::Constants::TOUCH_BAR); + touchBar->addAction(toggleAction.command(), Core::Constants::G_TOUCHBAR_EDITOR); + updateActionStatus(); } @@ -559,6 +653,16 @@ bool BookmarkManager::gotoBookmark(const Bookmark *bookmark) const return false; } +void BookmarkManager::requestContextMenu(const FilePath &filePath, int lineNumber, QMenu *menu) +{ + m_marginActionLineNumber = lineNumber; + m_marginActionFileName = filePath; + + menu->addAction(&m_bookmarkMarginAction); + if (hasBookmarkInPosition(m_marginActionFileName, m_marginActionLineNumber)) + menu->addAction(&m_editBookmarkAction); +} + void BookmarkManager::nextInDocument() { documentPrevNext(true); @@ -677,7 +781,15 @@ void BookmarkManager::updateActionStatus() IEditor *editor = EditorManager::currentEditor(); const bool enableToggle = editor && !editor->document()->isTemporary(); - emit updateActions(enableToggle, state()); + const bool hasbm = state() >= BookmarkManager::HasBookMarks; + const bool hasdocbm = state() == BookmarkManager::HasBookmarksInDocument; + + m_toggleAction->setEnabled(enableToggle); + m_editAction->setEnabled(enableToggle); + m_prevAction->setEnabled(hasbm); + m_nextAction->setEnabled(hasbm); + m_docPrevAction->setEnabled(hasdocbm); + m_docNextAction->setEnabled(hasdocbm); } void BookmarkManager::move(Bookmark* mark, int newRow) diff --git a/src/plugins/texteditor/bookmarkmanager.h b/src/plugins/texteditor/bookmarkmanager.h index c83763d870f..2e36445e69e 100644 --- a/src/plugins/texteditor/bookmarkmanager.h +++ b/src/plugins/texteditor/bookmarkmanager.h @@ -3,9 +3,11 @@ #pragma once +#include +#include + #include #include -#include #include #include @@ -14,13 +16,9 @@ #include #include -namespace Core { class IContext; } - namespace TextEditor::Internal { class Bookmark; -class BookmarksPlugin; -class BookmarkContext; class BookmarkManager final : public QAbstractItemModel { @@ -79,9 +77,7 @@ public: void editByFileAndLine(const Utils::FilePath &fileName, int lineNumber); bool gotoBookmark(const Bookmark *bookmark) const; -signals: - void updateActions(bool enableToggle, int state); - void currentIndexChanged(const QModelIndex &); + void requestContextMenu(const Utils::FilePath &filePath, int lineNumber, QMenu *menu); private: void updateActionStatus(); @@ -101,6 +97,20 @@ private: QList m_bookmarksList; QItemSelectionModel *m_selectionModel; + Core::Menu m_bookmarkMenu; + + QAction *m_toggleAction = nullptr; + QAction *m_editAction = nullptr; + QAction *m_prevAction = nullptr; + QAction *m_nextAction = nullptr; + QAction *m_docPrevAction = nullptr; + QAction *m_docNextAction = nullptr; + + QAction m_editBookmarkAction; + QAction m_bookmarkMarginAction; + + int m_marginActionLineNumber = 0; + Utils::FilePath m_marginActionFileName; }; class BookmarkViewFactory : public Core::INavigationWidgetFactory diff --git a/src/plugins/texteditor/texteditorplugin.cpp b/src/plugins/texteditor/texteditorplugin.cpp index c416434693f..223c746f087 100644 --- a/src/plugins/texteditor/texteditorplugin.cpp +++ b/src/plugins/texteditor/texteditorplugin.cpp @@ -86,20 +86,6 @@ public: BookmarkFilter m_bookmarkFilter{&m_bookmarkManager}; BookmarkViewFactory m_bookmarkViewFactory{&m_bookmarkManager}; - Menu m_bookmarkMenu; - QAction *m_toggleAction = nullptr; - QAction *m_editAction = nullptr; - QAction *m_prevAction = nullptr; - QAction *m_nextAction = nullptr; - QAction *m_docPrevAction = nullptr; - QAction *m_docNextAction = nullptr; - - QAction m_editBookmarkAction{Tr::tr("Edit Bookmark")}; - QAction m_bookmarkMarginAction{Tr::tr("Toggle Bookmark")}; - - int m_marginActionLineNumber = 0; - FilePath m_marginActionFileName; - TextEditorSettings settings; FindInFiles findInFilesFilter; @@ -113,98 +99,6 @@ public: TextEditorPluginPrivate::TextEditorPluginPrivate() { - const Id bookmarkMenuId = "Bookmarks.Menu"; - const Context editorManagerContext(Core::Constants::C_EDITORMANAGER); - - m_bookmarkMenu.setId(bookmarkMenuId); - m_bookmarkMenu.setTitle(Tr::tr("&Bookmarks")); - m_bookmarkMenu.setContainer(Core::Constants::M_TOOLS); - - ActionBuilder toggleAction(this, "Bookmarks.Toggle"); - toggleAction.setContext(editorManagerContext); - toggleAction.setText(Tr::tr("Toggle Bookmark")); - toggleAction.setDefaultKeySequence(Tr::tr("Meta+M"), Tr::tr("Ctrl+M")); - toggleAction.setTouchBarIcon(Icons::MACOS_TOUCHBAR_BOOKMARK.icon()); - toggleAction.addToContainer(bookmarkMenuId); - toggleAction.bindContextAction(&m_toggleAction); - toggleAction.addOnTriggered(this, [this] { - IEditor *editor = EditorManager::currentEditor(); - auto widget = TextEditorWidget::fromEditor(editor); - if (widget && editor && !editor->document()->isTemporary()) - m_bookmarkManager.toggleBookmark(editor->document()->filePath(), editor->currentLine()); - }); - - ActionBuilder editAction(this, "Bookmarks.Edit"); - editAction.setContext(editorManagerContext); - editAction.setText(Tr::tr("Edit Bookmark")); - editAction.setDefaultKeySequence(Tr::tr("Meta+Shift+M"), Tr::tr("Ctrl+Shift+M")); - editAction.addToContainer(bookmarkMenuId); - editAction.bindContextAction(&m_editAction); - editAction.addOnTriggered(this, [this] { - IEditor *editor = EditorManager::currentEditor(); - auto widget = TextEditorWidget::fromEditor(editor); - if (widget && editor && !editor->document()->isTemporary()) { - const FilePath filePath = editor->document()->filePath(); - const int line = editor->currentLine(); - if (!m_bookmarkManager.hasBookmarkInPosition(filePath, line)) - m_bookmarkManager.toggleBookmark(filePath, line); - m_bookmarkManager.editByFileAndLine(filePath, line); - } - }); - - m_bookmarkMenu.addSeparator(); - - ActionBuilder prevAction(this, BOOKMARKS_PREV_ACTION); - prevAction.setContext(editorManagerContext); - prevAction.setText(Tr::tr("Previous Bookmark")); - prevAction.setDefaultKeySequence(Tr::tr("Meta+,"), Tr::tr("Ctrl+,")); - prevAction.addToContainer(bookmarkMenuId); - prevAction.setIcon(Icons::PREV_TOOLBAR.icon()); - prevAction.setIconVisibleInMenu(false); - prevAction.bindContextAction(&m_prevAction); - prevAction.addOnTriggered(this, [this] { m_bookmarkManager.prev(); }); - - ActionBuilder nextAction(this, BOOKMARKS_NEXT_ACTION); - nextAction.setContext(editorManagerContext); - nextAction.setText(Tr::tr("Next Bookmark")); - nextAction.setIcon(Icons::NEXT_TOOLBAR.icon()); - nextAction.setIconVisibleInMenu(false); - nextAction.setDefaultKeySequence(Tr::tr("Meta+."), Tr::tr("Ctrl+.")); - nextAction.addToContainer(bookmarkMenuId); - nextAction.bindContextAction(&m_nextAction); - nextAction.addOnTriggered(this, [this] { m_bookmarkManager.next(); }); - - m_bookmarkMenu.addSeparator(); - - ActionBuilder docPrevAction(this, "Bookmarks.PreviousDocument"); - docPrevAction.setContext(editorManagerContext); - docPrevAction.setText(Tr::tr("Previous Bookmark in Document")); - docPrevAction.addToContainer(bookmarkMenuId); - docPrevAction.bindContextAction(&m_docPrevAction); - docPrevAction.addOnTriggered(this, [this] { m_bookmarkManager.prevInDocument(); }); - - ActionBuilder docNextAction(this, "Bookmarks.NextDocument"); - docNextAction.setContext(Core::Constants::C_EDITORMANAGER); - docNextAction.setText(Tr::tr("Next Bookmark in Document")); - docNextAction.addToContainer(bookmarkMenuId); - docNextAction.bindContextAction(&m_docNextAction); - docNextAction.addOnTriggered(this, [this] { m_bookmarkManager.nextInDocument(); }); - - connect(&m_editBookmarkAction, &QAction::triggered, this, [this] { - m_bookmarkManager.editByFileAndLine(m_marginActionFileName, m_marginActionLineNumber); - }); - - connect(&m_bookmarkManager, &BookmarkManager::updateActions, - this, &TextEditorPluginPrivate::updateActions); - updateActions(false, m_bookmarkManager.state()); - - connect(&m_bookmarkMarginAction, &QAction::triggered, this, [this] { - m_bookmarkManager.toggleBookmark(m_marginActionFileName, m_marginActionLineNumber); - }); - - ActionContainer *touchBar = ActionManager::actionContainer(Core::Constants::TOUCH_BAR); - touchBar->addAction(toggleAction.command(), Core::Constants::G_TOUCHBAR_EDITOR); - // EditorManager connect(EditorManager::instance(), &EditorManager::editorAboutToClose, this, &TextEditorPluginPrivate::editorAboutToClose); @@ -212,19 +106,6 @@ TextEditorPluginPrivate::TextEditorPluginPrivate() this, &TextEditorPluginPrivate::editorOpened); } -void TextEditorPluginPrivate::updateActions(bool enableToggle, int state) -{ - const bool hasbm = state >= BookmarkManager::HasBookMarks; - const bool hasdocbm = state == BookmarkManager::HasBookmarksInDocument; - - m_toggleAction->setEnabled(enableToggle); - m_editAction->setEnabled(enableToggle); - m_prevAction->setEnabled(hasbm); - m_nextAction->setEnabled(hasbm); - m_docPrevAction->setEnabled(hasdocbm); - m_docNextAction->setEnabled(hasdocbm); -} - void TextEditorPluginPrivate::editorOpened(IEditor *editor) { if (auto widget = TextEditorWidget::fromEditor(editor)) { @@ -253,12 +134,7 @@ void TextEditorPluginPrivate::requestContextMenu(TextEditorWidget *widget, if (widget->textDocument()->isTemporary()) return; - m_marginActionLineNumber = lineNumber; - m_marginActionFileName = widget->textDocument()->filePath(); - - menu->addAction(&m_bookmarkMarginAction); - if (m_bookmarkManager.hasBookmarkInPosition(m_marginActionFileName, m_marginActionLineNumber)) - menu->addAction(&m_editBookmarkAction); + m_bookmarkManager.requestContextMenu(widget->textDocument()->filePath(), lineNumber, menu); } static class TextEditorPlugin *m_instance = nullptr;