TextEditor: Move bookmark related action setup

... to BookMarkManager. For less coupling.

Change-Id: I51c88fc9ee9a3456b1075bbe4c7ad039fc6f1889
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2024-01-17 14:48:52 +01:00
parent 89d5c69a79
commit ef8323ee96
3 changed files with 134 additions and 136 deletions

View File

@@ -4,9 +4,11 @@
#include "bookmarkmanager.h" #include "bookmarkmanager.h"
#include "bookmark.h" #include "bookmark.h"
#include "texteditor.h"
#include "texteditorconstants.h" #include "texteditorconstants.h"
#include "texteditortr.h" #include "texteditortr.h"
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h> #include <coreplugin/actionmanager/command.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
@@ -15,11 +17,12 @@
#include <coreplugin/session.h> #include <coreplugin/session.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/checkablemessagebox.h>
#include <utils/dropsupport.h>
#include <utils/icon.h> #include <utils/icon.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/checkablemessagebox.h>
#include <utils/theme/theme.h> #include <utils/theme/theme.h>
#include <utils/dropsupport.h> #include <utils/utilsicons.h>
#include <QAction> #include <QAction>
#include <QContextMenuEvent> #include <QContextMenuEvent>
@@ -318,12 +321,103 @@ void BookmarkView::gotoBookmark(const QModelIndex &index)
BookmarkManager::BookmarkManager() : BookmarkManager::BookmarkManager() :
m_selectionModel(new QItemSelectionModel(this, this)) 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, connect(ICore::instance(), &ICore::contextChanged,
this, &BookmarkManager::updateActionStatus); this, &BookmarkManager::updateActionStatus);
connect(SessionManager::instance(), &SessionManager::sessionLoaded, connect(SessionManager::instance(), &SessionManager::sessionLoaded,
this, &BookmarkManager::loadBookmarks); 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(); updateActionStatus();
} }
@@ -559,6 +653,16 @@ bool BookmarkManager::gotoBookmark(const Bookmark *bookmark) const
return false; 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() void BookmarkManager::nextInDocument()
{ {
documentPrevNext(true); documentPrevNext(true);
@@ -677,7 +781,15 @@ void BookmarkManager::updateActionStatus()
IEditor *editor = EditorManager::currentEditor(); IEditor *editor = EditorManager::currentEditor();
const bool enableToggle = editor && !editor->document()->isTemporary(); 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) void BookmarkManager::move(Bookmark* mark, int newRow)

View File

@@ -3,9 +3,11 @@
#pragma once #pragma once
#include <coreplugin/inavigationwidgetfactory.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <utils/itemviews.h> #include <utils/itemviews.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <coreplugin/inavigationwidgetfactory.h>
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QMultiMap> #include <QMultiMap>
@@ -14,13 +16,9 @@
#include <QPixmap> #include <QPixmap>
#include <QStyledItemDelegate> #include <QStyledItemDelegate>
namespace Core { class IContext; }
namespace TextEditor::Internal { namespace TextEditor::Internal {
class Bookmark; class Bookmark;
class BookmarksPlugin;
class BookmarkContext;
class BookmarkManager final : public QAbstractItemModel class BookmarkManager final : public QAbstractItemModel
{ {
@@ -79,9 +77,7 @@ public:
void editByFileAndLine(const Utils::FilePath &fileName, int lineNumber); void editByFileAndLine(const Utils::FilePath &fileName, int lineNumber);
bool gotoBookmark(const Bookmark *bookmark) const; bool gotoBookmark(const Bookmark *bookmark) const;
signals: void requestContextMenu(const Utils::FilePath &filePath, int lineNumber, QMenu *menu);
void updateActions(bool enableToggle, int state);
void currentIndexChanged(const QModelIndex &);
private: private:
void updateActionStatus(); void updateActionStatus();
@@ -101,6 +97,20 @@ private:
QList<Bookmark *> m_bookmarksList; QList<Bookmark *> m_bookmarksList;
QItemSelectionModel *m_selectionModel; 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 class BookmarkViewFactory : public Core::INavigationWidgetFactory

View File

@@ -86,20 +86,6 @@ public:
BookmarkFilter m_bookmarkFilter{&m_bookmarkManager}; BookmarkFilter m_bookmarkFilter{&m_bookmarkManager};
BookmarkViewFactory m_bookmarkViewFactory{&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; TextEditorSettings settings;
FindInFiles findInFilesFilter; FindInFiles findInFilesFilter;
@@ -113,98 +99,6 @@ public:
TextEditorPluginPrivate::TextEditorPluginPrivate() 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 // EditorManager
connect(EditorManager::instance(), &EditorManager::editorAboutToClose, connect(EditorManager::instance(), &EditorManager::editorAboutToClose,
this, &TextEditorPluginPrivate::editorAboutToClose); this, &TextEditorPluginPrivate::editorAboutToClose);
@@ -212,19 +106,6 @@ TextEditorPluginPrivate::TextEditorPluginPrivate()
this, &TextEditorPluginPrivate::editorOpened); 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) void TextEditorPluginPrivate::editorOpened(IEditor *editor)
{ {
if (auto widget = TextEditorWidget::fromEditor(editor)) { if (auto widget = TextEditorWidget::fromEditor(editor)) {
@@ -253,12 +134,7 @@ void TextEditorPluginPrivate::requestContextMenu(TextEditorWidget *widget,
if (widget->textDocument()->isTemporary()) if (widget->textDocument()->isTemporary())
return; return;
m_marginActionLineNumber = lineNumber; m_bookmarkManager.requestContextMenu(widget->textDocument()->filePath(), lineNumber, menu);
m_marginActionFileName = widget->textDocument()->filePath();
menu->addAction(&m_bookmarkMarginAction);
if (m_bookmarkManager.hasBookmarkInPosition(m_marginActionFileName, m_marginActionLineNumber))
menu->addAction(&m_editBookmarkAction);
} }
static class TextEditorPlugin *m_instance = nullptr; static class TextEditorPlugin *m_instance = nullptr;