From ed86c821a01e5738e3fb40c1b5dd1513bd3c8327 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Wed, 3 Jul 2024 11:02:46 +0200 Subject: [PATCH] Break IMode's inheritance from IContext IContext has the purpose of matching the current focus widget hierarchy to active context and context help. We do have actions that are enabled by mode context and we do have some modes that specify context help, and modes do have associated widgets. But the inheritance of IMode from IContext also forces IModes to create their widgets early which is undesirable. We already manually add the active mode's context via updateAdditionalContexts, so we already do not rely on the focus and do not need the IContext for that. Instead add a context property to IMode directly. Also, modes can just create an IContext for their widget if they need it _when_ the widget is created. Change-Id: I1178b73ffa7b6e4c25221dca0419c7def78f7bdc Reviewed-by: hjk --- src/plugins/coreplugin/imode.cpp | 24 ++++++++++++++++++- src/plugins/coreplugin/imode.h | 6 ++++- src/plugins/coreplugin/modemanager.cpp | 4 ---- .../projectexplorer/projectexplorer.cpp | 5 +++- .../studiowelcome/studiowelcomeplugin.cpp | 6 ++++- src/plugins/welcome/welcomeplugin.cpp | 5 +++- 6 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/plugins/coreplugin/imode.cpp b/src/plugins/coreplugin/imode.cpp index f3bed33aa92..29da730bfba 100644 --- a/src/plugins/coreplugin/imode.cpp +++ b/src/plugins/coreplugin/imode.cpp @@ -25,6 +25,8 @@ public: Utils::FancyMainWindow *m_mainWindow = nullptr; int m_priority = -1; Utils::Id m_id; + Context m_context; + QPointer m_widget; bool m_isEnabled = true; BoolAspect m_isVisible; }; @@ -123,7 +125,7 @@ public: Registers the mode in \QC. */ IMode::IMode(QObject *parent) - : IContext(parent) + : QObject(parent) , m_d(new Internal::IModePrivate) { m_d->m_isVisible.setDefaultValue(true); @@ -197,6 +199,16 @@ void IMode::setMenu(QMenu *menu) m_d->m_menu = menu; } +void IMode::setContext(const Context &context) +{ + m_d->m_context = context; +} + +void IMode::setWidget(QWidget *widget) +{ + m_d->m_widget = widget; +} + Utils::FancyMainWindow *IMode::mainWindow() { if (m_d->m_mainWindow) @@ -225,4 +237,14 @@ QMenu *IMode::menu() const return m_d->m_menu; } +Context IMode::context() const +{ + return m_d->m_context; +} + +QWidget *IMode::widget() const +{ + return m_d->m_widget; +} + } // namespace Core diff --git a/src/plugins/coreplugin/imode.h b/src/plugins/coreplugin/imode.h index c0b790c113a..35ae82f0fc4 100644 --- a/src/plugins/coreplugin/imode.h +++ b/src/plugins/coreplugin/imode.h @@ -22,7 +22,7 @@ namespace Internal { class IModePrivate; } -class CORE_EXPORT IMode : public IContext +class CORE_EXPORT IMode : public QObject { Q_OBJECT Q_PROPERTY(QString displayName READ displayName WRITE setDisplayName) @@ -43,6 +43,8 @@ public: bool isEnabled() const; bool isVisible() const; QMenu *menu() const; + Context context() const; + QWidget *widget() const; void setEnabled(bool enabled); void setVisible(bool visible); @@ -51,6 +53,8 @@ public: void setPriority(int priority); void setId(Utils::Id id); void setMenu(QMenu *menu); + void setContext(const Context &context); + void setWidget(QWidget *widget); Utils::FancyMainWindow *mainWindow(); void setMainWindow(Utils::FancyMainWindow *mw); diff --git a/src/plugins/coreplugin/modemanager.cpp b/src/plugins/coreplugin/modemanager.cpp index a9a70e60a67..03b38af04e3 100644 --- a/src/plugins/coreplugin/modemanager.cpp +++ b/src/plugins/coreplugin/modemanager.cpp @@ -209,8 +209,6 @@ void ModeManagerPrivate::appendMode(IMode *mode) { const int index = m_modeCommands.count(); - ICore::addContextObject(mode); - m_modeStack->insertTab(index, mode->widget(), mode->icon(), mode->displayName(), mode->menu() != nullptr); m_modeStack->setTabEnabled(index, mode->isEnabled()); @@ -266,8 +264,6 @@ void ModeManager::removeMode(IMode *mode) d->m_modeCommands.remove(index); d->m_modeStack->removeTab(index); - - ICore::removeContextObject(mode); } void ModeManagerPrivate::ensureVisibleEnabledMode() diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index fd2f33c2e58..97041931a3f 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -366,7 +366,6 @@ public: Icons::MODE_PROJECT_FLAT, Icons::MODE_PROJECT_FLAT_ACTIVE)); setPriority(Constants::P_MODE_SESSION); setId(Constants::MODE_SESSION); - setContextHelp("Managing Projects"); } }; @@ -913,6 +912,10 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er auto splitter = new MiniSplitter(Qt::Vertical); splitter->addWidget(dd->m_proWindow); splitter->addWidget(new OutputPanePlaceHolder(Constants::MODE_SESSION, splitter)); + auto context = new IContext(splitter); + context->setWidget(splitter); + context->setContextHelp("Managing Projects"); + ICore::addContextObject(context); dd->m_projectsMode.setWidget(splitter); dd->m_projectsMode.setEnabled(false); diff --git a/src/plugins/studiowelcome/studiowelcomeplugin.cpp b/src/plugins/studiowelcome/studiowelcomeplugin.cpp index a2094a0d423..0bd15bf9be3 100644 --- a/src/plugins/studiowelcome/studiowelcomeplugin.cpp +++ b/src/plugins/studiowelcome/studiowelcomeplugin.cpp @@ -66,6 +66,7 @@ #include #include +using namespace Core; using namespace ProjectExplorer; using namespace Utils; @@ -764,7 +765,6 @@ WelcomeMode::WelcomeMode() setPriority(Core::Constants::P_MODE_WELCOME); setId(Core::Constants::MODE_WELCOME); - setContextHelp("Qt Design Studio Manual"); setContext(Core::Context(Core::Constants::C_WELCOME_MODE)); QFontDatabase::addApplicationFont(":/studiofonts/TitilliumWeb-Regular.ttf"); @@ -809,6 +809,10 @@ WelcomeMode::WelcomeMode() m_modeWidget = new QWidget; m_modeWidget->setLayout(boxLayout); boxLayout->addWidget(m_quickWidget); + auto context = new IContext(m_modeWidget); + context->setWidget(m_modeWidget); + context->setContextHelp("Qt Design Studio Manual"); + ICore::addContextObject(context); setWidget(m_modeWidget); QStringList designStudioQchPathes diff --git a/src/plugins/welcome/welcomeplugin.cpp b/src/plugins/welcome/welcomeplugin.cpp index 432ce59cb96..cef07005561 100644 --- a/src/plugins/welcome/welcomeplugin.cpp +++ b/src/plugins/welcome/welcomeplugin.cpp @@ -299,7 +299,6 @@ WelcomeMode::WelcomeMode() setPriority(Constants::P_MODE_WELCOME); setId(Constants::MODE_WELCOME); - setContextHelp("Qt Creator Manual"); setContext(Context(Constants::C_WELCOME_MODE)); m_modeWidget = new ResizeSignallingWidget; @@ -346,6 +345,10 @@ WelcomeMode::WelcomeMode() spacing(0), }.attachTo(m_modeWidget); + auto context = new IContext(m_modeWidget); + context->setWidget(m_modeWidget); + context->setContextHelp("Qt Creator Manual"); + ICore::addContextObject(context); setWidget(m_modeWidget); }