CppEditor: Use new construction pattern for project panels

Change-Id: I60dffd8fb7c00671054b5e229ba8c0a0cb622b1d
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2023-11-15 14:55:20 +01:00
parent d739a14950
commit 20bc29face
7 changed files with 128 additions and 114 deletions

View File

@@ -12,6 +12,9 @@
#include <coreplugin/icore.h>
#include <coreplugin/session.h>
#include <projectexplorer/projectpanelfactory.h>
#include <projectexplorer/projectsettingswidget.h>
#include <utils/algorithm.h>
#include <utils/infolabel.h>
#include <utils/itemviews.h>
@@ -36,6 +39,8 @@
#include <limits>
using namespace ProjectExplorer;
namespace CppEditor::Internal {
class CppCodeModelSettingsWidget final : public Core::IOptionsPageWidget
@@ -578,24 +583,16 @@ ClangdSettingsPage::ClangdSettingsPage()
setWidgetCreator([] { return new ClangdSettingsPageWidget; });
}
class ClangdProjectSettingsWidget::Private
class ClangdProjectSettingsWidget : public ProjectSettingsWidget
{
public:
Private(const ClangdProjectSettings &s) : settings(s), widget(s.settings(), true) {}
ClangdProjectSettings settings;
ClangdSettingsWidget widget;
QCheckBox useGlobalSettingsCheckBox;
};
ClangdProjectSettingsWidget::ClangdProjectSettingsWidget(const ClangdProjectSettings &settings)
: d(new Private(settings))
ClangdProjectSettingsWidget(const ClangdProjectSettings &settings)
: m_settings(settings), m_widget(settings.settings(), true)
{
setGlobalSettingsId(Constants::CPP_CLANGD_SETTINGS_ID);
const auto layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(&d->widget);
layout->addWidget(&m_widget);
const auto updateGlobalSettingsCheckBox = [this] {
if (ClangdSettings::instance().granularity() == ClangdSettings::Granularity::Session) {
@@ -603,9 +600,9 @@ ClangdProjectSettingsWidget::ClangdProjectSettingsWidget(const ClangdProjectSett
setUseGlobalSettings(true);
} else {
setUseGlobalSettingsCheckBoxEnabled(true);
setUseGlobalSettings(d->settings.useGlobalSettings());
setUseGlobalSettings(m_settings.useGlobalSettings());
}
d->widget.setEnabled(!useGlobalSettings());
m_widget.setEnabled(!useGlobalSettings());
};
updateGlobalSettingsCheckBox();
@@ -614,20 +611,39 @@ ClangdProjectSettingsWidget::ClangdProjectSettingsWidget(const ClangdProjectSett
connect(this, &ProjectSettingsWidget::useGlobalSettingsChanged, this,
[this](bool checked) {
d->widget.setEnabled(!checked);
d->settings.setUseGlobalSettings(checked);
m_widget.setEnabled(!checked);
m_settings.setUseGlobalSettings(checked);
if (!checked)
d->settings.setSettings(d->widget.settingsData());
m_settings.setSettings(m_widget.settingsData());
});
connect(&d->widget, &ClangdSettingsWidget::settingsDataChanged, this, [this] {
d->settings.setSettings(d->widget.settingsData());
connect(&m_widget, &ClangdSettingsWidget::settingsDataChanged, this, [this] {
m_settings.setSettings(m_widget.settingsData());
});
}
ClangdProjectSettingsWidget::~ClangdProjectSettingsWidget()
private:
ClangdProjectSettings m_settings;
ClangdSettingsWidget m_widget;
};
class ClangdProjectSettingsPanelFactory final : public ProjectPanelFactory
{
delete d;
public:
ClangdProjectSettingsPanelFactory()
{
setPriority(100);
setDisplayName(Tr::tr("Clangd"));
setCreateWidgetFunction([](Project *project) {
return new ClangdProjectSettingsWidget(project);
});
ProjectPanelFactory::registerFactory(this);
}
};
void setupClangdProjectSettingsPanel()
{
static ClangdProjectSettingsPanelFactory theClangdProjectSettingsPanelFactory;
}
} // CppEditor::Internal

View File

@@ -4,7 +4,6 @@
#pragma once
#include "cppcodemodelsettings.h"
#include <projectexplorer/projectsettingswidget.h>
#include <coreplugin/dialogs/ioptionspage.h>
@@ -40,17 +39,6 @@ private:
Private * const d;
};
class ClangdProjectSettingsWidget : public ProjectExplorer::ProjectSettingsWidget
{
Q_OBJECT
void setupClangdProjectSettingsPanel();
public:
ClangdProjectSettingsWidget(const ClangdProjectSettings &settings);
~ClangdProjectSettingsWidget();
private:
class Private;
Private * const d;
};
} // CppEditor::Internal namespace
} // CppEditor::Internal

View File

@@ -72,7 +72,6 @@
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectmanager.h>
#include <projectexplorer/projectpanelfactory.h>
#include <projectexplorer/projecttree.h>
#include <texteditor/colorpreviewhoverhandler.h>
@@ -524,32 +523,12 @@ void CppEditorPlugin::addGlobalActions()
void CppEditorPlugin::setupProjectPanels()
{
const auto quickFixSettingsPanelFactory = new ProjectPanelFactory;
quickFixSettingsPanelFactory->setPriority(100);
quickFixSettingsPanelFactory->setId(Constants::QUICK_FIX_PROJECT_PANEL_ID);
quickFixSettingsPanelFactory->setDisplayName(Tr::tr(Constants::QUICK_FIX_SETTINGS_DISPLAY_NAME));
quickFixSettingsPanelFactory->setCreateWidgetFunction([](Project *project) {
return new CppQuickFixProjectSettingsWidget(project);
});
ProjectPanelFactory::registerFactory(quickFixSettingsPanelFactory);
const auto fileNamesPanelFactory = new ProjectPanelFactory;
fileNamesPanelFactory->setPriority(99);
fileNamesPanelFactory->setDisplayName(Tr::tr("C++ File Naming"));
fileNamesPanelFactory->setCreateWidgetFunction([](Project *project) {
return new CppFileSettingsForProjectWidget(project);
});
ProjectPanelFactory::registerFactory(fileNamesPanelFactory);
setupCppQuickFixProjectPanel();
setupCppFileSettingsProjectPanel();
if (CppModelManager::isClangCodeModelActive()) {
d->m_clangdSettingsPage = new ClangdSettingsPage;
const auto clangdPanelFactory = new ProjectPanelFactory;
clangdPanelFactory->setPriority(100);
clangdPanelFactory->setDisplayName(Tr::tr("Clangd"));
clangdPanelFactory->setCreateWidgetFunction([](Project *project) {
return new ClangdProjectSettingsWidget(project);
});
ProjectPanelFactory::registerFactory(clangdPanelFactory);
setupClangdProjectSettingsPanel();
}
}

View File

@@ -10,6 +10,7 @@
#include <coreplugin/editormanager/editormanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectpanelfactory.h>
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
@@ -21,7 +22,6 @@
#include <QCheckBox>
#include <QComboBox>
#include <QCoreApplication>
#include <QFile>
#include <QGuiApplication>
#include <QLineEdit>
@@ -29,6 +29,7 @@
#include <QTextStream>
#include <QVBoxLayout>
using namespace ProjectExplorer;
using namespace Utils;
namespace CppEditor::Internal {
@@ -586,6 +587,25 @@ void CppFileSettingsForProjectWidget::Private::maybeClearHeaderSourceCache()
}
}
class CppFileSettingsProjectPanelFactory final : public ProjectPanelFactory
{
public:
CppFileSettingsProjectPanelFactory()
{
setPriority(99);
setDisplayName(Tr::tr("C++ File Naming"));
setCreateWidgetFunction([](Project *project) {
return new CppFileSettingsForProjectWidget(project);
});
ProjectPanelFactory::registerFactory(this);
}
};
void setupCppFileSettingsProjectPanel()
{
static CppFileSettingsProjectPanelFactory theCppFileSettingsProjectPanelFactory;
}
} // namespace CppEditor::Internal
#include <cppfilesettingspage.moc>

View File

@@ -85,4 +85,6 @@ private:
Private * const d;
};
void setupCppFileSettingsProjectPanel();
} // namespace CppEditor::Internal

View File

@@ -5,17 +5,35 @@
#include "cppeditorconstants.h"
#include "cppeditortr.h"
#include "cppquickfixprojectsettings.h"
#include "cppquickfixsettingswidget.h"
#include <projectexplorer/projectpanelfactory.h>
#include <QFile>
#include <QGridLayout>
#include <QPushButton>
using namespace ProjectExplorer;
namespace CppEditor::Internal {
CppQuickFixProjectSettingsWidget::CppQuickFixProjectSettingsWidget(ProjectExplorer::Project *project,
QWidget *parent)
: ProjectExplorer::ProjectSettingsWidget(parent)
class CppQuickFixProjectSettingsWidget : public ProjectSettingsWidget
{
public:
explicit CppQuickFixProjectSettingsWidget(Project *project);
private:
void currentItemChanged(bool useGlobalSettings);
void buttonCustomClicked();
CppQuickFixSettingsWidget *m_settingsWidget;
CppQuickFixProjectsSettings::CppQuickFixProjectsSettingsPtr m_projectSettings;
QPushButton *m_pushButton;
};
CppQuickFixProjectSettingsWidget::CppQuickFixProjectSettingsWidget(Project *project)
{
setGlobalSettingsId(CppEditor::Constants::QUICK_FIX_SETTINGS_ID);
m_projectSettings = CppQuickFixProjectsSettings::getSettings(project);
@@ -50,8 +68,6 @@ CppQuickFixProjectSettingsWidget::CppQuickFixProjectSettingsWidget(ProjectExplor
});
}
CppQuickFixProjectSettingsWidget::~CppQuickFixProjectSettingsWidget() = default;
void CppQuickFixProjectSettingsWidget::currentItemChanged(bool useGlobalSettings)
{
if (useGlobalSettings) {
@@ -88,4 +104,24 @@ void CppQuickFixProjectSettingsWidget::buttonCustomClicked()
}
}
class CppQuickFixProjectPanelFactory final : public ProjectPanelFactory
{
public:
CppQuickFixProjectPanelFactory()
{
setPriority(100);
setId(Constants::QUICK_FIX_PROJECT_PANEL_ID);
setDisplayName(Tr::tr(Constants::QUICK_FIX_SETTINGS_DISPLAY_NAME));
setCreateWidgetFunction([](Project *project) {
return new CppQuickFixProjectSettingsWidget(project);
});
ProjectPanelFactory::registerFactory(this);
}
};
void setupCppQuickFixProjectPanel()
{
static CppQuickFixProjectPanelFactory theCppQuickFixProjectPanelFactory;
}
} // CppEditor::Internal

View File

@@ -3,35 +3,8 @@
#pragma once
#include "cppquickfixprojectsettings.h"
#include <projectexplorer/projectsettingswidget.h>
QT_BEGIN_NAMESPACE
class QPushButton;
QT_END_NAMESPACE
namespace ProjectExplorer { class Project; }
namespace CppEditor::Internal {
class CppQuickFixSettingsWidget;
class CppQuickFixProjectSettingsWidget : public ProjectExplorer::ProjectSettingsWidget
{
Q_OBJECT
public:
explicit CppQuickFixProjectSettingsWidget(ProjectExplorer::Project *project,
QWidget *parent = nullptr);
~CppQuickFixProjectSettingsWidget();
private:
void currentItemChanged(bool useGlobalSettings);
void buttonCustomClicked();
CppQuickFixSettingsWidget *m_settingsWidget;
CppQuickFixProjectsSettings::CppQuickFixProjectsSettingsPtr m_projectSettings;
QPushButton *m_pushButton;
};
void setupCppQuickFixProjectPanel();
} // CppEditor::Internal