Core: Add a static IContext::attach() function

... to simplify the user code.

Some, but not all, potential beneficiaries are adapted.

Change-Id: Ia7d514a0114a4f99f8ee745523435f5b990bd095
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2024-07-03 14:55:42 +02:00
parent d58780213d
commit 2c40765e86
28 changed files with 44 additions and 115 deletions

View File

@@ -7,19 +7,15 @@
#include "testtreemodel.h" #include "testtreemodel.h"
#include <coreplugin/icontext.h> #include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
namespace Autotest { namespace Autotest {
namespace Internal { namespace Internal {
TestTreeView::TestTreeView(QWidget *parent) TestTreeView::TestTreeView(QWidget *parent)
: NavigationTreeView(parent), : NavigationTreeView(parent)
m_context(new Core::IContext(this))
{ {
setExpandsOnDoubleClick(false); setExpandsOnDoubleClick(false);
m_context->setWidget(this); Core::IContext::attach(this, Core::Context(Constants::AUTOTEST_CONTEXT));
m_context->setContext(Core::Context(Constants::AUTOTEST_CONTEXT));
Core::ICore::addContextObject(m_context);
} }
static void changeCheckStateAll(const Qt::CheckState checkState) static void changeCheckStateAll(const Qt::CheckState checkState)

View File

@@ -5,10 +5,6 @@
#include <utils/navigationtreeview.h> #include <utils/navigationtreeview.h>
namespace Core {
class IContext;
}
namespace Autotest { namespace Autotest {
namespace Internal { namespace Internal {
@@ -21,9 +17,6 @@ public:
void selectAll() override; void selectAll() override;
void deselectAll(); void deselectAll();
private:
Core::IContext *m_context;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -66,14 +66,10 @@ EditMode::EditMode() :
this, &EditMode::grabEditorManager); this, &EditMode::grabEditorManager);
m_splitter->setFocusProxy(editorPlaceHolder); m_splitter->setFocusProxy(editorPlaceHolder);
auto modeContextObject = new IContext(this); IContext::attach(m_splitter, Context(Constants::C_EDITORMANAGER));
modeContextObject->setContext(Context(Constants::C_EDITORMANAGER));
modeContextObject->setWidget(m_splitter);
ICore::addContextObject(modeContextObject);
setWidget(m_splitter); setWidget(m_splitter);
setContext(Context(Constants::C_EDIT_MODE, setContext(Context(Constants::C_EDIT_MODE, Constants::C_NAVIGATION_PANE));
Constants::C_NAVIGATION_PANE));
} }
EditMode::~EditMode() EditMode::~EditMode()

View File

@@ -8,7 +8,6 @@
#include "../coreconstants.h" #include "../coreconstants.h"
#include "../icontext.h" #include "../icontext.h"
#include "../icore.h"
#include "../idocument.h" #include "../idocument.h"
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
@@ -20,10 +19,7 @@ namespace Internal {
EditorArea::EditorArea() EditorArea::EditorArea()
{ {
m_context = new IContext; IContext::attach(this, Context(Constants::C_EDITORMANAGER));
m_context->setContext(Context(Constants::C_EDITORMANAGER));
m_context->setWidget(this);
ICore::addContextObject(m_context);
setCurrentView(view()); setCurrentView(view());
updateCloseSplitButton(); updateCloseSplitButton();
@@ -39,8 +35,6 @@ EditorArea::~EditorArea()
setCurrentView(nullptr); setCurrentView(nullptr);
disconnect(qApp, &QApplication::focusChanged, disconnect(qApp, &QApplication::focusChanged,
this, &EditorArea::focusChanged); this, &EditorArea::focusChanged);
delete m_context;
} }
IDocument *EditorArea::currentDocument() const IDocument *EditorArea::currentDocument() const

View File

@@ -7,11 +7,7 @@
#include <QPointer> #include <QPointer>
namespace Core { namespace Core::Internal {
class IContext;
namespace Internal {
class EditorArea : public SplitterOrView class EditorArea : public SplitterOrView
{ {
@@ -35,10 +31,8 @@ private:
void updateCloseSplitButton(); void updateCloseSplitButton();
void hideEvent(QHideEvent *) override; void hideEvent(QHideEvent *) override;
IContext *m_context;
QPointer<EditorView> m_currentView; QPointer<EditorView> m_currentView;
QPointer<IDocument> m_currentDocument; QPointer<IDocument> m_currentDocument;
}; };
} // Internal } // Core::Internal
} // Core

View File

@@ -247,10 +247,7 @@ void Find::initialize()
d->m_currentDocumentFind = new Internal::CurrentDocumentFind; d->m_currentDocumentFind = new Internal::CurrentDocumentFind;
d->m_findToolBar = new Internal::FindToolBar(d->m_currentDocumentFind); d->m_findToolBar = new Internal::FindToolBar(d->m_currentDocumentFind);
auto *findToolBarContext = new IContext(m_instance); IContext::attach(d->m_findToolBar, Context(Constants::C_FINDTOOLBAR));
findToolBarContext->setWidget(d->m_findToolBar);
findToolBarContext->setContext(Context(Constants::C_FINDTOOLBAR));
ICore::addContextObject(findToolBarContext);
d->m_findDialog = new Internal::FindToolWindow; d->m_findDialog = new Internal::FindToolWindow;
d->m_searchResultWindow = new SearchResultWindow(d->m_findDialog); d->m_searchResultWindow = new SearchResultWindow(d->m_findDialog);

View File

@@ -256,10 +256,7 @@ FolderNavigationWidget::FolderNavigationWidget(QWidget *parent) : QWidget(parent
m_crumbContainer(new QWidget(this)), m_crumbContainer(new QWidget(this)),
m_crumbLabel(new DelayedFileCrumbLabel(this)) m_crumbLabel(new DelayedFileCrumbLabel(this))
{ {
auto context = new IContext(this); IContext::attach(this, Context(C_FOLDERNAVIGATIONWIDGET));
context->setContext(Context(C_FOLDERNAVIGATIONWIDGET));
context->setWidget(this);
ICore::addContextObject(context);
setBackgroundRole(QPalette::Base); setBackgroundRole(QPalette::Base);
setAutoFillBackground(true); setAutoFillBackground(true);

View File

@@ -3,9 +3,21 @@
#include "icontext.h" #include "icontext.h"
#include "icore.h"
#include <QDebug> #include <QDebug>
namespace Core { namespace Core {
void IContext::attach(QWidget *widget, const Context &context, const HelpItem &help)
{
auto icontext = new IContext(widget); // As QObject parent.
icontext->setContext(context);
icontext->setWidget(widget);
icontext->setContextHelp(help);
ICore::addContextObject(icontext);
}
QDebug operator<<(QDebug debug, const Core::Context &context) QDebug operator<<(QDebug debug, const Core::Context &context)
{ {
debug.nospace() << "Context("; debug.nospace() << "Context(";
@@ -23,6 +35,7 @@ QDebug operator<<(QDebug debug, const Core::Context &context)
return debug; return debug;
} }
} // namespace Core } // namespace Core
/*! /*!

View File

@@ -60,6 +60,10 @@ public:
virtual void setWidget(QWidget *widget) { m_widget = widget; } virtual void setWidget(QWidget *widget) { m_widget = widget; }
virtual void setContextHelp(const HelpItem &id) { m_contextHelp = id; } virtual void setContextHelp(const HelpItem &id) { m_contextHelp = id; }
static void attach(QWidget *widget,
const Context &context,
const HelpItem &contextHelp = {});
friend CORE_EXPORT QDebug operator<<(QDebug debug, const Core::Context &context); friend CORE_EXPORT QDebug operator<<(QDebug debug, const Core::Context &context);
protected: protected:

View File

@@ -87,8 +87,6 @@ protected:
void setupContext(const Context &context, QWidget *widget); void setupContext(const Context &context, QWidget *widget);
void setZoomButtonsEnabled(bool enabled); void setZoomButtonsEnabled(bool enabled);
IContext *m_context = nullptr;
private: private:
virtual void updateFilter(); virtual void updateFilter();

View File

@@ -224,11 +224,7 @@ void IOutputPane::setupContext(const char *context, QWidget *widget)
void IOutputPane::setupContext(const Context &context, QWidget *widget) void IOutputPane::setupContext(const Context &context, QWidget *widget)
{ {
QTC_ASSERT(!m_context, return); IContext::attach(widget, context);
m_context = new IContext(this);
m_context->setContext(context);
m_context->setWidget(widget);
ICore::addContextObject(m_context);
ActionBuilder(this, Constants::ZOOM_IN) ActionBuilder(this, Constants::ZOOM_IN)
.setContext(context) .setContext(context)

View File

@@ -98,10 +98,7 @@ OutputWindow::OutputWindow(Context context, const Key &settingsKey, QWidget *par
d->settingsKey = settingsKey; d->settingsKey = settingsKey;
auto outputWindowContext = new IContext(this); IContext::attach(this, context);
outputWindowContext->setContext(context);
outputWindowContext->setWidget(this);
ICore::addContextObject(outputWindowContext);
auto undoAction = new QAction(this); auto undoAction = new QAction(this);
auto redoAction = new QAction(this); auto redoAction = new QAction(this);

View File

@@ -34,10 +34,7 @@ WindowSupport::WindowSupport(QWidget *window, const Context &context, const Cont
{ {
m_window->installEventFilter(this); m_window->installEventFilter(this);
m_contextObject = new IContext(this); IContext::attach(window, context);
m_contextObject->setWidget(window);
m_contextObject->setContext(context);
ICore::addContextObject(m_contextObject);
const Context ac = actionContext.isEmpty() ? context : actionContext; const Context ac = actionContext.isEmpty() ? context : actionContext;
if (useMacShortcuts) { if (useMacShortcuts) {

View File

@@ -54,7 +54,6 @@ private:
void updateFullScreenAction(); void updateFullScreenAction();
QWidget *m_window; QWidget *m_window;
IContext *m_contextObject;
QAction *m_minimizeAction; QAction *m_minimizeAction;
QAction *m_zoomAction; QAction *m_zoomAction;
QAction *m_closeAction; QAction *m_closeAction;

View File

@@ -683,7 +683,6 @@ public:
QStringList m_arguments; QStringList m_arguments;
QList<IOptionsPage *> m_optionPages; QList<IOptionsPage *> m_optionPages;
IContext m_debugModeContext;
Perspective m_perspective{Constants::PRESET_PERSPECTIVE_ID, Tr::tr("Debugger")}; Perspective m_perspective{Constants::PRESET_PERSPECTIVE_ID, Tr::tr("Debugger")};
Perspective m_perspectiveDap{Constants::DAP_PERSPECTIVE_ID, Tr::tr("DAP")}; Perspective m_perspectiveDap{Constants::DAP_PERSPECTIVE_ID, Tr::tr("DAP")};
@@ -1165,9 +1164,7 @@ DebuggerPluginPrivate::DebuggerPluginPrivate(const QStringList &arguments)
// Debug mode setup // Debug mode setup
m_mode = new DebugMode; m_mode = new DebugMode;
m_debugModeContext.setContext(Context(CC::C_EDITORMANAGER)); IContext::attach(m_mode->widget(), Context(CC::C_EDITORMANAGER));
m_debugModeContext.setWidget(m_mode->widget());
ICore::addContextObject(&m_debugModeContext);
// //
// Connections // Connections

View File

@@ -88,10 +88,7 @@ DescriptionEditorWidget::DescriptionEditorWidget(QWidget *parent)
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
auto context = new IContext(this); IContext::attach(this, Context(Constants::C_DIFF_EDITOR_DESCRIPTION));
context->setWidget(this);
context->setContext(Context(Constants::C_DIFF_EDITOR_DESCRIPTION));
ICore::addContextObject(context);
textDocument()->resetSyntaxHighlighter([] { return new SyntaxHighlighter(); }); textDocument()->resetSyntaxHighlighter([] { return new SyntaxHighlighter(); });
} }

View File

@@ -716,10 +716,8 @@ SideBySideDiffEditorWidget::SideBySideDiffEditorWidget(QWidget *parent)
connect(m_editor[side]->horizontalScrollBar(), &QAbstractSlider::rangeChanged, connect(m_editor[side]->horizontalScrollBar(), &QAbstractSlider::rangeChanged,
this, &SideBySideDiffEditorWidget::syncHorizontalScrollBarPolicy); this, &SideBySideDiffEditorWidget::syncHorizontalScrollBarPolicy);
auto context = new IContext(this); IContext::attach(m_editor[side],
context->setWidget(m_editor[side]); Context(Id(Constants::SIDE_BY_SIDE_VIEW_ID).withSuffix(side + 1)));
context->setContext(Context(Id(Constants::SIDE_BY_SIDE_VIEW_ID).withSuffix(side + 1)));
ICore::addContextObject(context);
}; };
setupEditor(LeftSide); setupEditor(LeftSide);
setupEditor(RightSide); setupEditor(RightSide);

View File

@@ -43,10 +43,7 @@ UnifiedDiffEditorWidget::UnifiedDiffEditorWidget(QWidget *parent)
connect(this, &QPlainTextEdit::cursorPositionChanged, connect(this, &QPlainTextEdit::cursorPositionChanged,
this, &UnifiedDiffEditorWidget::slotCursorPositionChangedInEditor); this, &UnifiedDiffEditorWidget::slotCursorPositionChangedInEditor);
auto context = new IContext(this); IContext::attach(this, Context(Constants::UNIFIED_VIEW_ID));
context->setWidget(this);
context->setContext(Context(Constants::UNIFIED_VIEW_ID));
ICore::addContextObject(context);
} }
UnifiedDiffEditorWidget::~UnifiedDiffEditorWidget() = default; UnifiedDiffEditorWidget::~UnifiedDiffEditorWidget() = default;

View File

@@ -142,11 +142,8 @@ public:
setDragDropMode(QAbstractItemView::DragDrop); setDragDropMode(QAbstractItemView::DragDrop);
viewport()->setAcceptDrops(true); viewport()->setAcceptDrops(true);
setDropIndicatorShown(true); setDropIndicatorShown(true);
auto context = new IContext(this);
context->setContext(Context(ProjectExplorer::Constants::C_PROJECT_TREE));
context->setWidget(this);
ICore::addContextObject(context); IContext::attach(this, Context(ProjectExplorer::Constants::C_PROJECT_TREE));
connect(this, &ProjectTreeView::expanded, connect(this, &ProjectTreeView::expanded,
this, &ProjectTreeView::invalidateSize); this, &ProjectTreeView::invalidateSize);

View File

@@ -7,7 +7,6 @@
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreplugintr.h> #include <coreplugin/coreplugintr.h>
#include <coreplugin/icore.h>
#include <projectexplorer/projectexplorerconstants.h> #include <projectexplorer/projectexplorerconstants.h>
@@ -30,14 +29,11 @@
namespace QmlDesigner { namespace QmlDesigner {
BindingEditorWidget::BindingEditorWidget() BindingEditorWidget::BindingEditorWidget()
: m_context(new Core::IContext(this))
{ {
Core::Context context(BINDINGEDITOR_CONTEXT_ID, Core::Context context(BINDINGEDITOR_CONTEXT_ID,
ProjectExplorer::Constants::QMLJS_LANGUAGE_ID); ProjectExplorer::Constants::QMLJS_LANGUAGE_ID);
m_context->setWidget(this); Core::IContext::attach(this, context);
m_context->setContext(context);
Core::ICore::addContextObject(m_context);
Utils::TransientScrollAreaSupport::support(this); Utils::TransientScrollAreaSupport::support(this);

View File

@@ -39,7 +39,6 @@ signals:
public: public:
QmlJSEditor::QmlJSEditorDocument *qmljsdocument = nullptr; QmlJSEditor::QmlJSEditorDocument *qmljsdocument = nullptr;
Core::IContext *m_context = nullptr;
QAction *m_completionAction = nullptr; QAction *m_completionAction = nullptr;
bool m_isMultiline = false; bool m_isMultiline = false;
}; };

View File

@@ -15,7 +15,6 @@
#include <coreplugin/editormanager/ieditorfactory.h> #include <coreplugin/editormanager/ieditorfactory.h>
#include <coreplugin/editortoolbar.h> #include <coreplugin/editortoolbar.h>
#include <coreplugin/icontext.h> #include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h> #include <coreplugin/idocument.h>
#include <coreplugin/minisplitter.h> #include <coreplugin/minisplitter.h>
#include <coreplugin/modemanager.h> #include <coreplugin/modemanager.h>
@@ -218,10 +217,7 @@ void ScxmlEditorData::fullInit()
Context scxmlContexts = m_contexts; Context scxmlContexts = m_contexts;
scxmlContexts.add(Core::Constants::C_EDITORMANAGER); scxmlContexts.add(Core::Constants::C_EDITORMANAGER);
auto context = new IContext(this); IContext::attach(m_modeWidget, scxmlContexts);
context->setContext(scxmlContexts);
context->setWidget(m_modeWidget);
ICore::addContextObject(context);
DesignMode::registerDesignWidget(m_modeWidget, QStringList(Utils::Constants::SCXML_MIMETYPE), m_contexts); DesignMode::registerDesignWidget(m_modeWidget, QStringList(Utils::Constants::SCXML_MIMETYPE), m_contexts);
} }

View File

@@ -11,7 +11,6 @@
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icontext.h> #include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/fancylineedit.h> #include <utils/fancylineedit.h>
@@ -23,12 +22,9 @@ namespace Squish::Internal {
SquishTestTreeView::SquishTestTreeView(QWidget *parent) SquishTestTreeView::SquishTestTreeView(QWidget *parent)
: Utils::NavigationTreeView(parent) : Utils::NavigationTreeView(parent)
, m_context(new Core::IContext(this))
{ {
setExpandsOnDoubleClick(false); setExpandsOnDoubleClick(false);
m_context->setWidget(this); Core::IContext::attach(this, Core::Context(Constants::SQUISH_CONTEXT));
m_context->setContext(Core::Context(Constants::SQUISH_CONTEXT));
Core::ICore::addContextObject(m_context);
} }
void SquishTestTreeView::resizeEvent(QResizeEvent *event) void SquishTestTreeView::resizeEvent(QResizeEvent *event)

View File

@@ -8,8 +8,6 @@
#include <QModelIndex> #include <QModelIndex>
#include <QStyledItemDelegate> #include <QStyledItemDelegate>
namespace Core { class IContext; }
namespace Squish { namespace Squish {
namespace Internal { namespace Internal {
@@ -29,7 +27,6 @@ signals:
void recordTestCase(const QString &suiteName, const QString &testCaseName); void recordTestCase(const QString &suiteName, const QString &testCaseName);
private: private:
Core::IContext *m_context;
QModelIndex m_lastMousePressedIndex; QModelIndex m_lastMousePressedIndex;
}; };

View File

@@ -809,10 +809,7 @@ WelcomeMode::WelcomeMode()
m_modeWidget = new QWidget; m_modeWidget = new QWidget;
m_modeWidget->setLayout(boxLayout); m_modeWidget->setLayout(boxLayout);
boxLayout->addWidget(m_quickWidget); boxLayout->addWidget(m_quickWidget);
auto context = new IContext(m_modeWidget); IContext::attach(m_modeWidget, {}, "Qt Design Studio Manual");
context->setWidget(m_modeWidget);
context->setContextHelp("Qt Design Studio Manual");
ICore::addContextObject(context);
setWidget(m_modeWidget); setWidget(m_modeWidget);
QStringList designStudioQchPathes QStringList designStudioQchPathes

View File

@@ -52,15 +52,13 @@ using namespace Utils::Terminal;
using namespace Core; using namespace Core;
namespace Terminal { namespace Terminal {
TerminalWidget::TerminalWidget(QWidget *parent, const OpenTerminalParameters &openParameters) TerminalWidget::TerminalWidget(QWidget *parent, const OpenTerminalParameters &openParameters)
: Core::SearchableTerminal(parent) : Core::SearchableTerminal(parent)
, m_context(Utils::Id("TerminalWidget_").withSuffix(QString::number((uintptr_t) this))) , m_context(Utils::Id("TerminalWidget_").withSuffix(QString::number((uintptr_t) this)))
, m_openParameters(openParameters) , m_openParameters(openParameters)
{ {
auto contextObj = new IContext(this); IContext::attach(this, m_context);
contextObj->setWidget(this);
contextObj->setContext(m_context);
ICore::addContextObject(contextObj);
setupFont(); setupFont();
setupColors(); setupColors();

View File

@@ -192,19 +192,14 @@ protected:
void keyPressEvent(QKeyEvent *event) final; void keyPressEvent(QKeyEvent *event) final;
private: private:
Core::IContext *m_bookmarkContext;
QModelIndex m_contextMenuIndex; QModelIndex m_contextMenuIndex;
}; };
BookmarkView::BookmarkView() BookmarkView::BookmarkView()
: m_bookmarkContext(new IContext(this))
{ {
setWindowTitle(Tr::tr("Bookmarks")); setWindowTitle(Tr::tr("Bookmarks"));
m_bookmarkContext->setWidget(this); IContext::attach(this, Context(BOOKMARKS_CONTEXT));
m_bookmarkContext->setContext(Context(BOOKMARKS_CONTEXT));
ICore::addContextObject(m_bookmarkContext);
ListView::setModel(&bookmarkManager()); ListView::setModel(&bookmarkManager());

View File

@@ -125,10 +125,8 @@ public:
&TextEditorWidget::addCurrentStateToNavigationHistory, &TextEditorWidget::addCurrentStateToNavigationHistory,
this, this,
&MarkdownEditor::addCurrentStateToNavigationHistory); &MarkdownEditor::addCurrentStateToNavigationHistory);
auto context = new IContext(this);
context->setWidget(m_textEditorWidget); IContext::attach(m_textEditorWidget, Context(MARKDOWNVIEWER_TEXT_CONTEXT));
context->setContext(Context(MARKDOWNVIEWER_TEXT_CONTEXT));
ICore::addContextObject(context);
m_splitter->addWidget(m_textEditorWidget); // sets splitter->focusWidget() on non-Windows m_splitter->addWidget(m_textEditorWidget); // sets splitter->focusWidget() on non-Windows
m_splitter->addWidget(m_previewWidget); m_splitter->addWidget(m_previewWidget);