From 8bb63ebc9a89e907d40b420e8991fea691c958d1 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 2 May 2022 17:11:18 +0200 Subject: [PATCH] MinimizableInfoBars: Move toolbar visibility handling Remove some coupling between CppEditorWidget and MinimizableInfoBars by handing the visibility of the toolbar buttons solely within MinimizableInfoBars. Change-Id: I5abf59187ea9b37fd13c91d63b2c82a1caa67275 Reviewed-by: Qt CI Bot Reviewed-by: Reviewed-by: Christian Kandeler --- src/plugins/cppeditor/cppeditorwidget.cpp | 15 +------ src/plugins/cppeditor/cppeditorwidget.h | 2 - .../cppeditor/cppminimizableinfobars.cpp | 41 +++++++++++-------- .../cppeditor/cppminimizableinfobars.h | 11 +++-- 4 files changed, 32 insertions(+), 37 deletions(-) diff --git a/src/plugins/cppeditor/cppeditorwidget.cpp b/src/plugins/cppeditor/cppeditorwidget.cpp index ad0a3a77dc1..d900071c3c6 100644 --- a/src/plugins/cppeditor/cppeditorwidget.cpp +++ b/src/plugins/cppeditor/cppeditorwidget.cpp @@ -424,7 +424,6 @@ public: QAction *m_parseContextAction = nullptr; ParseContextWidget *m_parseContextWidget = nullptr; QToolButton *m_preprocessorButton = nullptr; - MinimizableInfoBars::Actions m_showInfoBarActions; CppLocalRenaming m_localRenaming; CppUseSelectionsUpdater m_useSelectionsUpdater; @@ -555,11 +554,8 @@ void CppEditorWidget::finalizeInitialization() } // Toolbar: Actions to show minimized info bars - d->m_showInfoBarActions = MinimizableInfoBars::createShowInfoBarActions([this](QWidget *w) { - return this->insertExtraToolBarWidget(TextEditorWidget::Left, w); - }); - connect(&cppEditorDocument()->minimizableInfoBars(), &MinimizableInfoBars::showAction, - this, &CppEditorWidget::onShowInfoBarAction); + d->m_cppEditorDocument->minimizableInfoBars().createShowInfoBarActions( + [this](QWidget *w) { return this->insertExtraToolBarWidget(TextEditorWidget::Left, w); }); d->m_outlineTimer.setInterval(5000); d->m_outlineTimer.setSingleShot(true); @@ -672,13 +668,6 @@ void CppEditorWidget::onIfdefedOutBlocksUpdated(unsigned revision, textDocument()->setIfdefedOutBlocks(ifdefedOutBlocks); } -void CppEditorWidget::onShowInfoBarAction(const Id &id, bool show) -{ - QAction *action = d->m_showInfoBarActions.value(id); - QTC_ASSERT(action, return); - action->setVisible(show); -} - static QString getDocumentLine(QTextDocument *document, int line) { if (document) diff --git a/src/plugins/cppeditor/cppeditorwidget.h b/src/plugins/cppeditor/cppeditorwidget.h index 0593fbab4d6..21f4a8bee10 100644 --- a/src/plugins/cppeditor/cppeditorwidget.h +++ b/src/plugins/cppeditor/cppeditorwidget.h @@ -136,8 +136,6 @@ private: void onIfdefedOutBlocksUpdated(unsigned revision, const QList ifdefedOutBlocks); - void onShowInfoBarAction(const Utils::Id &id, bool show); - void updateSemanticInfo(const SemanticInfo &semanticInfo, bool updateUseSelectionSynchronously = false); void updatePreprocessorButtonTooltip(); diff --git a/src/plugins/cppeditor/cppminimizableinfobars.cpp b/src/plugins/cppeditor/cppminimizableinfobars.cpp index eabf52c2886..94c0410aa5a 100644 --- a/src/plugins/cppeditor/cppminimizableinfobars.cpp +++ b/src/plugins/cppeditor/cppminimizableinfobars.cpp @@ -47,26 +47,33 @@ MinimizableInfoBars::MinimizableInfoBars(InfoBar &infoBar, QObject *parent) { connect(settings(), &CppToolsSettings::showNoProjectInfoBarChanged, this, &MinimizableInfoBars::updateNoProjectConfiguration); + createActions(); } -MinimizableInfoBars::Actions MinimizableInfoBars::createShowInfoBarActions( - const ActionCreator &actionCreator) +void MinimizableInfoBars::createActions() { - Actions result; - QTC_ASSERT(actionCreator, return result); - - // No project configuration available - auto *button = new QToolButton(); - button->setToolTip(tr("File is not part of any project.")); - button->setIcon(Utils::Icons::WARNING_TOOLBAR.pixmap()); - connect(button, &QAbstractButton::clicked, []() { - settings()->setShowNoProjectInfoBar(true); - }); - QAction *action = actionCreator(button); + auto action = new QAction(this); + action->setToolTip(tr("File is not part of any project.")); + action->setIcon(Icons::WARNING_TOOLBAR.pixmap()); + connect(action, &QAction::triggered, []() { settings()->setShowNoProjectInfoBar(true); }); action->setVisible(!settings()->showNoProjectInfoBar()); - result.insert(Constants::NO_PROJECT_CONFIGURATION, action); + m_actions.insert(Constants::NO_PROJECT_CONFIGURATION, action); - return result; +} + +void MinimizableInfoBars::createShowInfoBarActions(const ActionCreator &actionCreator) const +{ + QTC_ASSERT(actionCreator, return ); + + for (QAction *action : m_actions) { + auto *button = new QToolButton(); + button->setDefaultAction(action); + QAction *toolbarAction = actionCreator(button); + connect(action, &QAction::changed, toolbarAction, [action, toolbarAction] { + toolbarAction->setVisible(action->isVisible()); + }); + toolbarAction->setVisible(action->isVisible()); + } } void MinimizableInfoBars::processHasProjectPart(bool hasProjectPart) @@ -88,7 +95,9 @@ void MinimizableInfoBars::updateNoProjectConfiguration() show = true; } - emit showAction(id, show); + QAction *action = m_actions.value(id); + if (QTC_GUARD(action)) + action->setVisible(show); } static InfoBarEntry createMinimizableInfo(const Id &id, diff --git a/src/plugins/cppeditor/cppminimizableinfobars.h b/src/plugins/cppeditor/cppminimizableinfobars.h index 59f6fc17c01..3b4dba83d2a 100644 --- a/src/plugins/cppeditor/cppminimizableinfobars.h +++ b/src/plugins/cppeditor/cppminimizableinfobars.h @@ -46,25 +46,24 @@ class MinimizableInfoBars : public QObject public: using ActionCreator = std::function; - using Actions = QHash; - - static Actions createShowInfoBarActions(const ActionCreator &actionCreator); public: explicit MinimizableInfoBars(Utils::InfoBar &infoBar, QObject *parent = nullptr); + void createShowInfoBarActions(const ActionCreator &actionCreator) const; + void processHasProjectPart(bool hasProjectPart); -signals: - void showAction(const Utils::Id &id, bool show); - private: + void createActions(); + void updateNoProjectConfiguration(); void addNoProjectConfigurationEntry(const Utils::Id &id); private: Utils::InfoBar &m_infoBar; + QHash m_actions; bool m_hasProjectPart = true; };