aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/projectexplorer/projectcommentssettings.cpp
blob: d1b6b1ffaa1e2b5d3d7c4396f40ff9d38a6bb06e (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "projectcommentssettings.h"

#include "project.h"
#include "projectexplorertr.h"
#include "projectmanager.h"
#include "projectpanelfactory.h"
#include "projectsettingswidget.h"

#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>

#include <QVBoxLayout>

using namespace TextEditor;
using namespace Utils;

namespace ProjectExplorer::Internal {

const char kUseGlobalKey[] = "UseGlobalKey";

ProjectCommentsSettings::ProjectCommentsSettings(Project *project)
    : m_project(project)
{
    loadSettings();
}

CommentsSettings::Data ProjectCommentsSettings::settings() const
{
    return m_useGlobalSettings ? CommentsSettings::data() : m_customSettings;
}

void ProjectCommentsSettings::setSettings(const CommentsSettings::Data &settings)
{
    if (settings == m_customSettings)
        return;
    m_customSettings = settings;
    saveSettings();
    emit TextEditorSettings::instance()->commentsSettingsChanged();
}

void ProjectCommentsSettings::setUseGlobalSettings(bool useGlobal)
{
    if (useGlobal == m_useGlobalSettings)
        return;
    m_useGlobalSettings = useGlobal;
    saveSettings();
    emit TextEditorSettings::instance()->commentsSettingsChanged();
}

void ProjectCommentsSettings::loadSettings()
{
    if (!m_project)
        return;

    const QVariant entry = m_project->namedSettings(CommentsSettings::mainSettingsKey());
    if (!entry.isValid())
        return;

    const Store data = storeFromVariant(entry);
    m_useGlobalSettings = data.value(kUseGlobalKey, true).toBool();
    m_customSettings.enableDoxygen = data.value(CommentsSettings::enableDoxygenSettingsKey(),
                                                m_customSettings.enableDoxygen).toBool();
    m_customSettings.generateBrief = data.value(CommentsSettings::generateBriefSettingsKey(),
                                                m_customSettings.generateBrief).toBool();
    m_customSettings.leadingAsterisks = data.value(CommentsSettings::leadingAsterisksSettingsKey(),
                                                   m_customSettings.leadingAsterisks).toBool();
    m_customSettings.commandPrefix = static_cast<CommentsSettings::CommandPrefix>(
        data.value(CommentsSettings::commandPrefixKey(),
                   int(m_customSettings.commandPrefix)).toInt());
}

void ProjectCommentsSettings::saveSettings()
{
    if (!m_project)
        return;

    // Optimization: Don't save anything if the user never switched away from the default.
    if (m_useGlobalSettings && !m_project->namedSettings
                                (CommentsSettings::mainSettingsKey()).isValid()) {
        return;
    }

    Store data;
    data.insert(kUseGlobalKey, m_useGlobalSettings);
    data.insert(CommentsSettings::enableDoxygenSettingsKey(), m_customSettings.enableDoxygen);
    data.insert(CommentsSettings::generateBriefSettingsKey(), m_customSettings.generateBrief);
    data.insert(CommentsSettings::leadingAsterisksSettingsKey(), m_customSettings.leadingAsterisks);
    data.insert(CommentsSettings::commandPrefixKey(), int(m_customSettings.commandPrefix));
    m_project->setNamedSettings(CommentsSettings::mainSettingsKey(), variantFromStore(data));
}

class ProjectCommentsSettingsWidget final : public ProjectSettingsWidget
{
public:
    ProjectCommentsSettingsWidget(Project *project)
        : m_settings(project)
    {
        setGlobalSettingsId(TextEditor::Constants::TEXT_EDITOR_COMMENTS_SETTINGS);

        const auto layout = new QVBoxLayout(this);
        layout->setContentsMargins(0, 0, 0, 0);
        layout->addWidget(&m_widget);

        const auto updateGlobalSettingsCheckBox = [this] {
            setUseGlobalSettingsCheckBoxEnabled(true);
            setUseGlobalSettings(m_settings.useGlobalSettings());
            m_widget.setEnabled(!useGlobalSettings());
        };
        updateGlobalSettingsCheckBox();
        connect(TextEditorSettings::instance(), &TextEditorSettings::commentsSettingsChanged,
                this, updateGlobalSettingsCheckBox);
        connect(this, &ProjectSettingsWidget::useGlobalSettingsChanged, this,
                [this](bool checked) {
                    m_widget.setEnabled(!checked);
                    m_settings.setUseGlobalSettings(checked);
                    if (!checked)
                        m_settings.setSettings(m_widget.settingsData());
                });
        connect(&m_widget, &CommentsSettingsWidget::settingsChanged, this, [this] {
            m_settings.setSettings(m_widget.settingsData());
        });
    }

private:
    ProjectCommentsSettings m_settings;
    CommentsSettingsWidget m_widget{m_settings.settings()};
};

class CommentsSettingsProjectPanelFactory final : public ProjectPanelFactory
{
public:
    CommentsSettingsProjectPanelFactory()
    {
        setPriority(45);
        setDisplayName(Tr::tr("Documentation Comments"));
        setCreateWidgetFunction([](Project *project) {
            return new ProjectCommentsSettingsWidget(project);
        });

        TextEditor::TextEditorSettings::setCommentsSettingsRetriever([](const FilePath &filePath) {
            return ProjectCommentsSettings(ProjectManager::projectForFile(filePath)).settings();
        });
    }
};

void setupCommentsSettingsProjectPanel()
{
    static CommentsSettingsProjectPanelFactory theCommentsSettingsProjectPanelFactory;
}

} // ProjectExplorer::Internal