forked from qt-creator/qt-creator
Bookmarks: Avoid use of global object pool
Modernize a bit in the process. Change-Id: I34e71c154b38ede29fc36a25762a515ea6596fb9 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -36,169 +36,184 @@
|
|||||||
#include <coreplugin/actionmanager/actionmanager.h>
|
#include <coreplugin/actionmanager/actionmanager.h>
|
||||||
#include <coreplugin/actionmanager/actioncontainer.h>
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
||||||
#include <coreplugin/actionmanager/command.h>
|
#include <coreplugin/actionmanager/command.h>
|
||||||
#include <coreplugin/id.h>
|
|
||||||
#include <extensionsystem/pluginmanager.h>
|
|
||||||
#include <texteditor/texteditor.h>
|
#include <texteditor/texteditor.h>
|
||||||
#include <texteditor/textdocument.h>
|
#include <texteditor/textdocument.h>
|
||||||
#include <texteditor/texteditorconstants.h>
|
#include <texteditor/texteditorconstants.h>
|
||||||
|
|
||||||
#include <QtPlugin>
|
#include <utils/fileutils.h>
|
||||||
|
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
|
|
||||||
using namespace Bookmarks::Constants;
|
|
||||||
using namespace Core;
|
using namespace Core;
|
||||||
using namespace TextEditor;
|
using namespace TextEditor;
|
||||||
|
|
||||||
|
using namespace Bookmarks::Constants;
|
||||||
|
|
||||||
namespace Bookmarks {
|
namespace Bookmarks {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
BookmarksPlugin::BookmarksPlugin() :
|
class BookmarksPluginRunData : public QObject
|
||||||
m_bookmarkManager(0),
|
|
||||||
m_toggleAction(0),
|
|
||||||
m_prevAction(0),
|
|
||||||
m_nextAction(0),
|
|
||||||
m_docPrevAction(0),
|
|
||||||
m_docNextAction(0),
|
|
||||||
m_editBookmarkAction(0),
|
|
||||||
m_bookmarkMarginAction(0),
|
|
||||||
m_bookmarkMarginActionLineNumber(0)
|
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
BookmarksPluginRunData();
|
||||||
|
|
||||||
|
void updateActions(bool enableToggle, int stateMask);
|
||||||
|
void editorOpened(Core::IEditor *editor);
|
||||||
|
void editorAboutToClose(Core::IEditor *editor);
|
||||||
|
|
||||||
|
void requestContextMenu(TextEditor::TextEditorWidget *widget,
|
||||||
|
int lineNumber, QMenu *menu);
|
||||||
|
|
||||||
|
BookmarkManager m_bookmarkManager;
|
||||||
|
BookmarkFilter m_bookmarkFilter;
|
||||||
|
BookmarkViewFactory m_bookmarkViewFactory;
|
||||||
|
|
||||||
|
QAction m_toggleAction{BookmarksPlugin::tr("Toggle Bookmark"), nullptr};
|
||||||
|
QAction m_prevAction{BookmarksPlugin::tr("Previous Bookmark"), nullptr};
|
||||||
|
QAction m_nextAction{BookmarksPlugin::tr("Next Bookmark"), nullptr};
|
||||||
|
QAction m_docPrevAction{BookmarksPlugin::tr("Previous Bookmark in Document"), nullptr};
|
||||||
|
QAction m_docNextAction{BookmarksPlugin::tr("Next Bookmark in Document"), nullptr};
|
||||||
|
QAction m_editBookmarkAction{BookmarksPlugin::tr("Edit Bookmark"), nullptr};
|
||||||
|
QAction m_bookmarkMarginAction{BookmarksPlugin::tr("Toggle Bookmark"), nullptr};
|
||||||
|
|
||||||
|
int m_marginActionLineNumber = 0;
|
||||||
|
Utils::FileName m_marginActionFileName;
|
||||||
|
};
|
||||||
|
|
||||||
|
BookmarksPlugin::~BookmarksPlugin()
|
||||||
|
{
|
||||||
|
delete m_runData;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BookmarksPlugin::initialize(const QStringList & /*arguments*/, QString *)
|
bool BookmarksPlugin::initialize(const QStringList &, QString *)
|
||||||
|
{
|
||||||
|
m_runData = new BookmarksPluginRunData;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
BookmarksPluginRunData::BookmarksPluginRunData()
|
||||||
|
: m_bookmarkFilter(&m_bookmarkManager)
|
||||||
|
, m_bookmarkViewFactory(&m_bookmarkManager)
|
||||||
{
|
{
|
||||||
ActionContainer *mtools = ActionManager::actionContainer(Core::Constants::M_TOOLS);
|
ActionContainer *mtools = ActionManager::actionContainer(Core::Constants::M_TOOLS);
|
||||||
ActionContainer *mbm = ActionManager::createMenu(Id(BOOKMARKS_MENU));
|
ActionContainer *mbm = ActionManager::createMenu(Id(BOOKMARKS_MENU));
|
||||||
mbm->menu()->setTitle(tr("&Bookmarks"));
|
mbm->menu()->setTitle(BookmarksPlugin::tr("&Bookmarks"));
|
||||||
mtools->addMenu(mbm);
|
mtools->addMenu(mbm);
|
||||||
|
|
||||||
const Context editorManagerContext(Core::Constants::C_EDITORMANAGER);
|
const Context editorManagerContext(Core::Constants::C_EDITORMANAGER);
|
||||||
|
|
||||||
// Toggle
|
// Toggle
|
||||||
m_toggleAction = new QAction(tr("Toggle Bookmark"), this);
|
Command *cmd = ActionManager::registerAction(&m_toggleAction, BOOKMARKS_TOGGLE_ACTION,
|
||||||
Command *cmd = ActionManager::registerAction(m_toggleAction, BOOKMARKS_TOGGLE_ACTION, editorManagerContext);
|
editorManagerContext);
|
||||||
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+M") : tr("Ctrl+M")));
|
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? BookmarksPlugin::tr("Meta+M")
|
||||||
|
: BookmarksPlugin::tr("Ctrl+M")));
|
||||||
mbm->addAction(cmd);
|
mbm->addAction(cmd);
|
||||||
|
|
||||||
mbm->addSeparator();
|
mbm->addSeparator();
|
||||||
|
|
||||||
// Previous
|
// Previous
|
||||||
m_prevAction = new QAction(tr("Previous Bookmark"), this);
|
cmd = ActionManager::registerAction(&m_prevAction, BOOKMARKS_PREV_ACTION, editorManagerContext);
|
||||||
cmd = ActionManager::registerAction(m_prevAction, BOOKMARKS_PREV_ACTION, editorManagerContext);
|
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? BookmarksPlugin::tr("Meta+,")
|
||||||
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+,") : tr("Ctrl+,")));
|
: BookmarksPlugin::tr("Ctrl+,")));
|
||||||
mbm->addAction(cmd);
|
mbm->addAction(cmd);
|
||||||
|
|
||||||
// Next
|
// Next
|
||||||
m_nextAction = new QAction(tr("Next Bookmark"), this);
|
cmd = ActionManager::registerAction(&m_nextAction, BOOKMARKS_NEXT_ACTION, editorManagerContext);
|
||||||
cmd = ActionManager::registerAction(m_nextAction, BOOKMARKS_NEXT_ACTION, editorManagerContext);
|
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? BookmarksPlugin::tr("Meta+.")
|
||||||
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+.") : tr("Ctrl+.")));
|
: BookmarksPlugin::tr("Ctrl+.")));
|
||||||
mbm->addAction(cmd);
|
mbm->addAction(cmd);
|
||||||
|
|
||||||
mbm->addSeparator();
|
mbm->addSeparator();
|
||||||
|
|
||||||
// Previous Doc
|
// Previous Doc
|
||||||
m_docPrevAction = new QAction(tr("Previous Bookmark in Document"), this);
|
cmd = ActionManager::registerAction(&m_docPrevAction, BOOKMARKS_PREVDOC_ACTION,
|
||||||
cmd = ActionManager::registerAction(m_docPrevAction, BOOKMARKS_PREVDOC_ACTION, editorManagerContext);
|
editorManagerContext);
|
||||||
mbm->addAction(cmd);
|
mbm->addAction(cmd);
|
||||||
|
|
||||||
// Next Doc
|
// Next Doc
|
||||||
m_docNextAction = new QAction(tr("Next Bookmark in Document"), this);
|
cmd = ActionManager::registerAction(&m_docNextAction, BOOKMARKS_NEXTDOC_ACTION,
|
||||||
cmd = ActionManager::registerAction(m_docNextAction, BOOKMARKS_NEXTDOC_ACTION, editorManagerContext);
|
editorManagerContext);
|
||||||
mbm->addAction(cmd);
|
mbm->addAction(cmd);
|
||||||
|
|
||||||
m_editBookmarkAction = new QAction(tr("Edit Bookmark"), this);
|
connect(&m_toggleAction, &QAction::triggered, [this] {
|
||||||
|
|
||||||
m_bookmarkManager = new BookmarkManager;
|
|
||||||
|
|
||||||
addAutoReleasedObject(new BookmarkFilter(m_bookmarkManager));
|
|
||||||
|
|
||||||
connect(m_toggleAction, &QAction::triggered, [this]() {
|
|
||||||
BaseTextEditor *editor = BaseTextEditor::currentTextEditor();
|
BaseTextEditor *editor = BaseTextEditor::currentTextEditor();
|
||||||
if (editor && !editor->document()->isTemporary())
|
if (editor && !editor->document()->isTemporary())
|
||||||
m_bookmarkManager->toggleBookmark(editor->document()->filePath(), editor->currentLine());
|
m_bookmarkManager.toggleBookmark(editor->document()->filePath(), editor->currentLine());
|
||||||
});
|
});
|
||||||
|
|
||||||
connect(m_prevAction, &QAction::triggered, m_bookmarkManager, &BookmarkManager::prev);
|
connect(&m_prevAction, &QAction::triggered, &m_bookmarkManager, &BookmarkManager::prev);
|
||||||
connect(m_nextAction, &QAction::triggered, m_bookmarkManager, &BookmarkManager::next);
|
connect(&m_nextAction, &QAction::triggered, &m_bookmarkManager, &BookmarkManager::next);
|
||||||
connect(m_docPrevAction, &QAction::triggered, m_bookmarkManager, &BookmarkManager::prevInDocument);
|
connect(&m_docPrevAction, &QAction::triggered,
|
||||||
connect(m_docNextAction, &QAction::triggered, m_bookmarkManager, &BookmarkManager::nextInDocument);
|
&m_bookmarkManager, &BookmarkManager::prevInDocument);
|
||||||
|
connect(&m_docNextAction, &QAction::triggered,
|
||||||
|
&m_bookmarkManager, &BookmarkManager::nextInDocument);
|
||||||
|
|
||||||
connect(m_editBookmarkAction, &QAction::triggered, [this]() {
|
connect(&m_editBookmarkAction, &QAction::triggered, [this] {
|
||||||
m_bookmarkManager->editByFileAndLine(m_bookmarkMarginActionFileName, m_bookmarkMarginActionLineNumber);
|
m_bookmarkManager.editByFileAndLine(m_marginActionFileName, m_marginActionLineNumber);
|
||||||
});
|
});
|
||||||
|
|
||||||
connect(m_bookmarkManager, &BookmarkManager::updateActions, this, &BookmarksPlugin::updateActions);
|
connect(&m_bookmarkManager, &BookmarkManager::updateActions,
|
||||||
updateActions(false, m_bookmarkManager->state());
|
this, &BookmarksPluginRunData::updateActions);
|
||||||
addAutoReleasedObject(new BookmarkViewFactory(m_bookmarkManager));
|
updateActions(false, m_bookmarkManager.state());
|
||||||
|
|
||||||
m_bookmarkMarginAction = new QAction(this);
|
connect(&m_bookmarkMarginAction, &QAction::triggered, [this] {
|
||||||
m_bookmarkMarginAction->setText(tr("Toggle Bookmark"));
|
m_bookmarkManager.toggleBookmark(m_marginActionFileName, m_marginActionLineNumber);
|
||||||
connect(m_bookmarkMarginAction, &QAction::triggered, [this]() {
|
|
||||||
m_bookmarkManager->toggleBookmark(m_bookmarkMarginActionFileName,
|
|
||||||
m_bookmarkMarginActionLineNumber);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// EditorManager
|
// EditorManager
|
||||||
connect(EditorManager::instance(), &EditorManager::editorAboutToClose,
|
connect(EditorManager::instance(), &EditorManager::editorAboutToClose,
|
||||||
this, &BookmarksPlugin::editorAboutToClose);
|
this, &BookmarksPluginRunData::editorAboutToClose);
|
||||||
connect(EditorManager::instance(), &EditorManager::editorOpened,
|
connect(EditorManager::instance(), &EditorManager::editorOpened,
|
||||||
this, &BookmarksPlugin::editorOpened);
|
this, &BookmarksPluginRunData::editorOpened);
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BookmarksPlugin::~BookmarksPlugin()
|
void BookmarksPluginRunData::updateActions(bool enableToggle, int state)
|
||||||
{
|
|
||||||
delete m_bookmarkManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BookmarksPlugin::updateActions(bool enableToggle, int state)
|
|
||||||
{
|
{
|
||||||
const bool hasbm = state >= BookmarkManager::HasBookMarks;
|
const bool hasbm = state >= BookmarkManager::HasBookMarks;
|
||||||
const bool hasdocbm = state == BookmarkManager::HasBookmarksInDocument;
|
const bool hasdocbm = state == BookmarkManager::HasBookmarksInDocument;
|
||||||
|
|
||||||
m_toggleAction->setEnabled(enableToggle);
|
m_toggleAction.setEnabled(enableToggle);
|
||||||
m_prevAction->setEnabled(hasbm);
|
m_prevAction.setEnabled(hasbm);
|
||||||
m_nextAction->setEnabled(hasbm);
|
m_nextAction.setEnabled(hasbm);
|
||||||
m_docPrevAction->setEnabled(hasdocbm);
|
m_docPrevAction.setEnabled(hasdocbm);
|
||||||
m_docNextAction->setEnabled(hasdocbm);
|
m_docNextAction.setEnabled(hasdocbm);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BookmarksPlugin::editorOpened(IEditor *editor)
|
void BookmarksPluginRunData::editorOpened(IEditor *editor)
|
||||||
{
|
{
|
||||||
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) {
|
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) {
|
||||||
connect(widget, &TextEditorWidget::markRequested, m_bookmarkManager,
|
connect(widget, &TextEditorWidget::markRequested,
|
||||||
[this, editor](TextEditorWidget *, int line, TextMarkRequestKind kind) {
|
this, [this, editor](TextEditorWidget *, int line, TextMarkRequestKind kind) {
|
||||||
if (kind == BookmarkRequest && !editor->document()->isTemporary())
|
if (kind == BookmarkRequest && !editor->document()->isTemporary())
|
||||||
m_bookmarkManager->toggleBookmark(editor->document()->filePath(), line);
|
m_bookmarkManager.toggleBookmark(editor->document()->filePath(), line);
|
||||||
});
|
});
|
||||||
|
|
||||||
connect(widget, &TextEditorWidget::markContextMenuRequested,
|
connect(widget, &TextEditorWidget::markContextMenuRequested,
|
||||||
this, &BookmarksPlugin::requestContextMenu);
|
this, &BookmarksPluginRunData::requestContextMenu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BookmarksPlugin::editorAboutToClose(IEditor *editor)
|
void BookmarksPluginRunData::editorAboutToClose(IEditor *editor)
|
||||||
{
|
{
|
||||||
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) {
|
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) {
|
||||||
disconnect(widget, &TextEditorWidget::markContextMenuRequested,
|
disconnect(widget, &TextEditorWidget::markContextMenuRequested,
|
||||||
this, &BookmarksPlugin::requestContextMenu);
|
this, &BookmarksPluginRunData::requestContextMenu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BookmarksPlugin::requestContextMenu(TextEditorWidget *widget,
|
void BookmarksPluginRunData::requestContextMenu(TextEditorWidget *widget,
|
||||||
int lineNumber, QMenu *menu)
|
int lineNumber, QMenu *menu)
|
||||||
{
|
{
|
||||||
if (widget->textDocument()->isTemporary())
|
if (widget->textDocument()->isTemporary())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m_bookmarkMarginActionLineNumber = lineNumber;
|
m_marginActionLineNumber = lineNumber;
|
||||||
m_bookmarkMarginActionFileName = widget->textDocument()->filePath();
|
m_marginActionFileName = widget->textDocument()->filePath();
|
||||||
|
|
||||||
menu->addAction(m_bookmarkMarginAction);
|
menu->addAction(&m_bookmarkMarginAction);
|
||||||
if (m_bookmarkManager->hasBookmarkInPosition(m_bookmarkMarginActionFileName, m_bookmarkMarginActionLineNumber))
|
if (m_bookmarkManager.hasBookmarkInPosition(m_marginActionFileName, m_marginActionLineNumber))
|
||||||
menu->addAction(m_editBookmarkAction);
|
menu->addAction(&m_editBookmarkAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
|
@@ -26,24 +26,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <extensionsystem/iplugin.h>
|
#include <extensionsystem/iplugin.h>
|
||||||
#include <utils/fileutils.h>
|
|
||||||
|
|
||||||
#include <QObject>
|
|
||||||
#include <QMultiMap>
|
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
|
||||||
class QAction;
|
|
||||||
class QMenu;
|
|
||||||
QT_END_NAMESPACE
|
|
||||||
|
|
||||||
namespace Core { class IEditor; }
|
|
||||||
|
|
||||||
namespace TextEditor { class TextEditorWidget; }
|
|
||||||
|
|
||||||
namespace Bookmarks {
|
namespace Bookmarks {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
class BookmarkManager;
|
class BookmarksPluginRunData;
|
||||||
|
|
||||||
class BookmarksPlugin : public ExtensionSystem::IPlugin
|
class BookmarksPlugin : public ExtensionSystem::IPlugin
|
||||||
{
|
{
|
||||||
@@ -51,32 +38,14 @@ class BookmarksPlugin : public ExtensionSystem::IPlugin
|
|||||||
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Bookmarks.json")
|
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Bookmarks.json")
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BookmarksPlugin();
|
BookmarksPlugin() {}
|
||||||
~BookmarksPlugin();
|
~BookmarksPlugin() final;
|
||||||
|
|
||||||
bool initialize(const QStringList &arguments, QString *errorMessage);
|
|
||||||
void extensionsInitialized() {}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void updateActions(bool enableToggle, int stateMask);
|
bool initialize(const QStringList &arguments, QString *errorMessage) final;
|
||||||
void editorOpened(Core::IEditor *editor);
|
void extensionsInitialized() final {}
|
||||||
void editorAboutToClose(Core::IEditor *editor);
|
|
||||||
|
|
||||||
void requestContextMenu(TextEditor::TextEditorWidget *widget,
|
BookmarksPluginRunData *m_runData;
|
||||||
int lineNumber, QMenu *menu);
|
|
||||||
|
|
||||||
BookmarkManager *m_bookmarkManager;
|
|
||||||
|
|
||||||
QAction *m_toggleAction;
|
|
||||||
QAction *m_prevAction;
|
|
||||||
QAction *m_nextAction;
|
|
||||||
QAction *m_docPrevAction;
|
|
||||||
QAction *m_docNextAction;
|
|
||||||
QAction *m_editBookmarkAction;
|
|
||||||
|
|
||||||
QAction *m_bookmarkMarginAction;
|
|
||||||
int m_bookmarkMarginActionLineNumber;
|
|
||||||
Utils::FileName m_bookmarkMarginActionFileName;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
|
Reference in New Issue
Block a user