From 2d6a36742b6dc4e9620d106327e32949592cd6ea Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Tue, 3 May 2022 11:01:24 +0200 Subject: [PATCH] MinimizableInfoBars: Remove dependency on CppToolsSettings They were only used in the info bars, so move them there. Change-Id: I584c98c70d1db2a5b741842da9d629469f0de73c Reviewed-by: Qt CI Bot Reviewed-by: Christian Kandeler --- src/plugins/cppeditor/cppeditorconstants.h | 1 - src/plugins/cppeditor/cppeditordocument.cpp | 2 + .../cppeditor/cppminimizableinfobars.cpp | 58 +++++++++++++------ .../cppeditor/cppminimizableinfobars.h | 10 +++- src/plugins/cppeditor/cpptoolssettings.cpp | 21 ------- src/plugins/cppeditor/cpptoolssettings.h | 5 -- 6 files changed, 52 insertions(+), 45 deletions(-) diff --git a/src/plugins/cppeditor/cppeditorconstants.h b/src/plugins/cppeditor/cppeditorconstants.h index 19d6fa6de2c..11883d2fd6e 100644 --- a/src/plugins/cppeditor/cppeditorconstants.h +++ b/src/plugins/cppeditor/cppeditorconstants.h @@ -99,7 +99,6 @@ const char CPPEDITOR_SETTINGSGROUP[] = "CppTools"; const char LOWERCASE_CPPFILES_KEY[] = "LowerCaseFiles"; const bool LOWERCASE_CPPFILES_DEFAULT = true; const char CPPEDITOR_SORT_EDITOR_DOCUMENT_OUTLINE[] = "SortedMethodOverview"; -const char CPPEDITOR_SHOW_INFO_BAR_FOR_FOR_NO_PROJECT[] = "ShowInfoBarForNoProject"; const char CPPEDITOR_MODEL_MANAGER_PCH_USAGE[] = "PCHUsage"; const char CPPEDITOR_INTERPRET_AMBIGIUOUS_HEADERS_AS_C_HEADERS[] = "InterpretAmbiguousHeadersAsCHeaders"; diff --git a/src/plugins/cppeditor/cppeditordocument.cpp b/src/plugins/cppeditor/cppeditordocument.cpp index 452d5da5fa1..b14c2dae469 100644 --- a/src/plugins/cppeditor/cppeditordocument.cpp +++ b/src/plugins/cppeditor/cppeditordocument.cpp @@ -128,6 +128,8 @@ CppEditorDocument::CppEditorDocument() connect(&m_parseContextModel, &ParseContextModel::preferredParseContextChanged, this, &CppEditorDocument::reparseWithPreferredParseContext); + m_minimizableInfoBars.setSettingsGroup(Constants::CPPEDITOR_SETTINGSGROUP); + // See also onFilePathChanged() for more initialization } diff --git a/src/plugins/cppeditor/cppminimizableinfobars.cpp b/src/plugins/cppeditor/cppminimizableinfobars.cpp index 638dd04a8de..5e0da9db5a1 100644 --- a/src/plugins/cppeditor/cppminimizableinfobars.cpp +++ b/src/plugins/cppeditor/cppminimizableinfobars.cpp @@ -25,42 +25,50 @@ #include "cppminimizableinfobars.h" -#include "cpptoolssettings.h" - #include +#include #include #include #include const char NO_PROJECT_CONFIGURATION[] = "CppEditor.NoProjectConfiguration"; +const char CPPEDITOR_SHOW_INFO_BAR_FOR_FOR_NO_PROJECT[] = "ShowInfoBarForNoProject"; +const bool kShowInInfoBarDefault = true; +using namespace Core; using namespace Utils; namespace CppEditor { namespace Internal { -static CppToolsSettings *settings() { return CppToolsSettings::instance(); } - -MinimizableInfoBars::MinimizableInfoBars(InfoBar &infoBar, QObject *parent) - : QObject(parent) - , m_infoBar(infoBar) +MinimizableInfoBars::MinimizableInfoBars(InfoBar &infoBar) + : m_infoBar(infoBar) { - connect(settings(), &CppToolsSettings::showNoProjectInfoBarChanged, - this, &MinimizableInfoBars::updateNoProjectConfiguration); createActions(); } +void MinimizableInfoBars::setSettingsGroup(const QString &settingsGroup) +{ + m_settingsGroup = settingsGroup; +} + void MinimizableInfoBars::createActions() { 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()); + connect(action, &QAction::triggered, this, [this]() { setShowNoProjectInfoBar(true); }); + action->setVisible(!showNoProjectInfoBar()); m_actions.insert(NO_PROJECT_CONFIGURATION, action); } +QString MinimizableInfoBars::settingsKey(const QString &id) const +{ + QTC_CHECK(!m_settingsGroup.isEmpty()); + return m_settingsGroup + '/' + id; +} + void MinimizableInfoBars::createShowInfoBarActions(const ActionCreator &actionCreator) const { QTC_ASSERT(actionCreator, return ); @@ -89,7 +97,7 @@ void MinimizableInfoBars::updateNoProjectConfiguration() bool show = false; if (!m_hasProjectPart) { - if (settings()->showNoProjectInfoBar()) + if (showNoProjectInfoBar()) addNoProjectConfigurationEntry(id); else show = true; @@ -102,6 +110,7 @@ void MinimizableInfoBars::updateNoProjectConfiguration() static InfoBarEntry createMinimizableInfo(const Id &id, const QString &text, + QObject *guard, std::function minimizer) { QTC_CHECK(minimizer); @@ -111,8 +120,9 @@ static InfoBarEntry createMinimizableInfo(const Id &id, // The minimizer() might delete the "Minimize" button immediately and as // result invalid reads will happen in QToolButton::mouseReleaseEvent(). // Avoid this by running the minimizer in the next event loop iteration. - info.addCustomButton(MinimizableInfoBars::tr("Minimize"), [minimizer] { - QMetaObject::invokeMethod(settings(), [minimizer] { minimizer(); }, Qt::QueuedConnection); + info.addCustomButton(MinimizableInfoBars::tr("Minimize"), [guard, minimizer] { + QMetaObject::invokeMethod( + guard, [minimizer] { minimizer(); }, Qt::QueuedConnection); }); return info; @@ -123,9 +133,23 @@ void MinimizableInfoBars::addNoProjectConfigurationEntry(const Id &id) const QString text = tr("Warning: This file is not part of any project. " "The code model might have issues parsing this file properly."); - m_infoBar.addInfo(createMinimizableInfo(id, text, []() { - settings()->setShowNoProjectInfoBar(false); - })); + m_infoBar.addInfo( + createMinimizableInfo(id, text, this, [this]() { setShowNoProjectInfoBar(false); })); +} + +bool MinimizableInfoBars::showNoProjectInfoBar() const +{ + return ICore::settings() + ->value(settingsKey(CPPEDITOR_SHOW_INFO_BAR_FOR_FOR_NO_PROJECT), kShowInInfoBarDefault) + .toBool(); +} + +void MinimizableInfoBars::setShowNoProjectInfoBar(bool show) +{ + ICore::settings()->setValueWithDefault(settingsKey(CPPEDITOR_SHOW_INFO_BAR_FOR_FOR_NO_PROJECT), + show, + kShowInInfoBarDefault); + updateNoProjectConfiguration(); } } // namespace Internal diff --git a/src/plugins/cppeditor/cppminimizableinfobars.h b/src/plugins/cppeditor/cppminimizableinfobars.h index 3b4dba83d2a..c4d64e94903 100644 --- a/src/plugins/cppeditor/cppminimizableinfobars.h +++ b/src/plugins/cppeditor/cppminimizableinfobars.h @@ -48,8 +48,9 @@ public: using ActionCreator = std::function; public: - explicit MinimizableInfoBars(Utils::InfoBar &infoBar, QObject *parent = nullptr); + explicit MinimizableInfoBars(Utils::InfoBar &infoBar); + void setSettingsGroup(const QString &settingsGroup); void createShowInfoBarActions(const ActionCreator &actionCreator) const; void processHasProjectPart(bool hasProjectPart); @@ -57,12 +58,19 @@ public: private: void createActions(); + QString settingsKey(const QString &id) const; + bool showHeaderErrorInfoBar() const; + void setShowHeaderErrorInfoBar(bool show); + bool showNoProjectInfoBar() const; + void setShowNoProjectInfoBar(bool show); + void updateNoProjectConfiguration(); void addNoProjectConfigurationEntry(const Utils::Id &id); private: Utils::InfoBar &m_infoBar; + QString m_settingsGroup; QHash m_actions; bool m_hasProjectPart = true; diff --git a/src/plugins/cppeditor/cpptoolssettings.cpp b/src/plugins/cppeditor/cpptoolssettings.cpp index 8a11bd9369c..31b8740cc89 100644 --- a/src/plugins/cppeditor/cpptoolssettings.cpp +++ b/src/plugins/cppeditor/cpptoolssettings.cpp @@ -44,7 +44,6 @@ static const char idKey[] = "CppGlobal"; const bool kSortEditorDocumentOutlineDefault = true; -const bool kShowNoProjectInfoBarDefault = true; using namespace Core; using namespace TextEditor; @@ -264,24 +263,4 @@ void CppToolsSettings::setSortedEditorDocumentOutline(bool sorted) emit editorDocumentOutlineSortingChanged(sorted); } -static QString showNoProjectInfoBarKey() -{ - return QLatin1String(Constants::CPPEDITOR_SETTINGSGROUP) - + QLatin1Char('/') - + QLatin1String(Constants::CPPEDITOR_SHOW_INFO_BAR_FOR_FOR_NO_PROJECT); -} - -bool CppToolsSettings::showNoProjectInfoBar() const -{ - return ICore::settings()->value(showNoProjectInfoBarKey(), kShowNoProjectInfoBarDefault).toBool(); -} - -void CppToolsSettings::setShowNoProjectInfoBar(bool show) -{ - ICore::settings()->setValueWithDefault(showNoProjectInfoBarKey(), - show, - kShowNoProjectInfoBarDefault); - emit showNoProjectInfoBarChanged(show); -} - } // namespace CppEditor diff --git a/src/plugins/cppeditor/cpptoolssettings.h b/src/plugins/cppeditor/cpptoolssettings.h index 2a4a08864b4..1c5d3cbe104 100644 --- a/src/plugins/cppeditor/cpptoolssettings.h +++ b/src/plugins/cppeditor/cpptoolssettings.h @@ -58,13 +58,8 @@ public: bool sortedEditorDocumentOutline() const; void setSortedEditorDocumentOutline(bool sorted); - bool showNoProjectInfoBar() const; - void setShowNoProjectInfoBar(bool show); - signals: void editorDocumentOutlineSortingChanged(bool isSorted); - void showHeaderErrorInfoBarChanged(bool isShown); - void showNoProjectInfoBarChanged(bool isShown); private: Internal::CppToolsSettingsPrivate *d;