aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/clangcodemodel/clangprojectsettingswidget.cpp
blob: 400efd03dff385b8962333cb353b55a5bed465cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "clangprojectsettingswidget.h"

#include "clangmodelmanagersupport.h"
#include "clangprojectsettings.h"

#include <coreplugin/icore.h>

#include <cppeditor/clangdiagnosticconfig.h>
#include <cppeditor/clangdiagnosticconfigswidget.h>
#include <cppeditor/cppeditorconstants.h>
#include <cppeditor/cppcodemodelsettings.h>
#include <cppeditor/cpptoolsreuse.h>

#include <utils/hostosinfo.h>

namespace ClangCodeModel {
namespace Internal {

static Utils::Id configIdForProject(ClangProjectSettings &projectSettings)
{
    if (projectSettings.useGlobalConfig())
        return CppEditor::codeModelSettings()->clangDiagnosticConfigId();
    return projectSettings.warningConfigId();
}

ClangProjectSettingsWidget::ClangProjectSettingsWidget(ProjectExplorer::Project *project)
    : m_projectSettings(ClangModelManagerSupport::instance()->projectSettings(project))
{
    m_ui.setupUi(this);
    setGlobalSettingsId(CppEditor::Constants::CPP_CODE_MODEL_SETTINGS_ID);

    using namespace CppEditor;

    m_ui.delayedTemplateParseCheckBox->setVisible(Utils::HostOsInfo::isWindowsHost());

    connect(m_ui.clangDiagnosticConfigsSelectionWidget,
            &ClangDiagnosticConfigsSelectionWidget::changed,
            this,
            [this]() {
                // Save project's config id
                const Utils::Id currentConfigId = m_ui.clangDiagnosticConfigsSelectionWidget
                                                     ->currentConfigId();
                m_projectSettings.setWarningConfigId(currentConfigId);

                // Save global custom configs
                const ClangDiagnosticConfigs configs = m_ui.clangDiagnosticConfigsSelectionWidget
                                                           ->customConfigs();
                CppEditor::codeModelSettings()->setClangCustomDiagnosticConfigs(configs);
                CppEditor::codeModelSettings()->toSettings(Core::ICore::settings());
            });

    connect(this, &ProjectSettingsWidget::useGlobalSettingsChanged,
            this, &ClangProjectSettingsWidget::onGlobalCustomChanged);
    connect(m_ui.delayedTemplateParseCheckBox, &QCheckBox::toggled,
            this, &ClangProjectSettingsWidget::onDelayedTemplateParseClicked);
    connect(project, &ProjectExplorer::Project::aboutToSaveSettings,
            this, &ClangProjectSettingsWidget::onAboutToSaveProjectSettings);

    connect(&m_projectSettings, &ClangProjectSettings::changed,
            this, &ClangProjectSettingsWidget::syncWidgets);
    connect(CppEditor::codeModelSettings(), &CppEditor::CppCodeModelSettings::changed,
            this, &ClangProjectSettingsWidget::syncOtherWidgetsToComboBox);

    syncWidgets();
}

void ClangProjectSettingsWidget::onDelayedTemplateParseClicked(bool checked)
{
    // Don't save it when we reset the global config in code
    if (m_projectSettings.useGlobalConfig())
        return;

    const QLatin1String extraFlag{checked ? ClangProjectSettings::DelayedTemplateParsing
                                          : ClangProjectSettings::NoDelayedTemplateParsing};
    QStringList options = m_projectSettings.commandLineOptions();
    options.removeAll(QLatin1String{ClangProjectSettings::DelayedTemplateParsing});
    options.removeAll(QLatin1String{ClangProjectSettings::NoDelayedTemplateParsing});
    options.append(extraFlag);
    m_projectSettings.setCommandLineOptions(options);
}

void ClangProjectSettingsWidget::onGlobalCustomChanged(bool useGlobalSettings)
{
    m_projectSettings.setUseGlobalConfig(useGlobalSettings);
    syncOtherWidgetsToComboBox();
}

void ClangProjectSettingsWidget::onAboutToSaveProjectSettings()
{
    CppEditor::codeModelSettings()->toSettings(Core::ICore::settings());
}

void ClangProjectSettingsWidget::syncWidgets()
{
    setUseGlobalSettings(m_projectSettings.useGlobalConfig());
    syncOtherWidgetsToComboBox();
}

void ClangProjectSettingsWidget::syncOtherWidgetsToComboBox()
{
    const QStringList options = m_projectSettings.commandLineOptions();
    m_ui.delayedTemplateParseCheckBox->setChecked(
        options.contains(QLatin1String{ClangProjectSettings::DelayedTemplateParsing}));

    const bool isCustom = !m_projectSettings.useGlobalConfig();
    m_ui.delayedTemplateParseCheckBox->setEnabled(isCustom);

    for (int i = 0; i < m_ui.clangDiagnosticConfigsSelectionWidget->layout()->count(); ++i) {
        QWidget *widget = m_ui.clangDiagnosticConfigsSelectionWidget->layout()->itemAt(i)->widget();
        if (widget)
            widget->setEnabled(isCustom);
    }

    m_ui.clangDiagnosticConfigsSelectionWidget
        ->refresh(CppEditor::diagnosticConfigsModel(),
                  configIdForProject(m_projectSettings),
                  [](const CppEditor::ClangDiagnosticConfigs &configs,
                     const Utils::Id &configToSelect) {
                      return new CppEditor::ClangDiagnosticConfigsWidget(configs, configToSelect);
                  });
}

} // namespace Internal
} // namespace ClangCodeModel