aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/beautifier/generalsettings.cpp
blob: 413e80698fb053265b8e61d4abf669690be6663d (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
// Copyright (C) 2016 Lorenz Haas
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "generalsettings.h"

#include "beautifierconstants.h"
#include "beautifiertr.h"

#include <coreplugin/dialogs/ioptionspage.h>

#include <utils/algorithm.h>
#include <utils/genericconstants.h>
#include <utils/layoutbuilder.h>

using namespace Utils;

namespace Beautifier::Internal {

GeneralSettings &generalSettings()
{
    static GeneralSettings theSettings;
    return theSettings;
}

GeneralSettings::GeneralSettings()
{
    setAutoApply(false);
    setSettingsGroups("Beautifier", "General");

    autoFormatOnSave.setSettingsKey(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE);
    autoFormatOnSave.setDefaultValue(false);
    autoFormatOnSave.setLabelText(Tr::tr("Enable auto format on file save"));

    autoFormatOnlyCurrentProject.setSettingsKey("autoFormatOnlyCurrentProject");
    autoFormatOnlyCurrentProject.setDefaultValue(true);
    autoFormatOnlyCurrentProject.setLabelText(Tr::tr("Restrict to files contained in the current project"));

    autoFormatTools.setSettingsKey("autoFormatTool");
    autoFormatTools.setLabelText(Tr::tr("Tool:"));
    autoFormatTools.setDefaultValue(0);
    autoFormatTools.setDisplayStyle(SelectionAspect::DisplayStyle::ComboBox);

    autoFormatMime.setSettingsKey("autoFormatMime");
    autoFormatMime.setDefaultValue("text/x-c++src;text/x-c++hdr");
    autoFormatMime.setLabelText(Tr::tr("Restrict to MIME types:"));
    autoFormatMime.setDisplayStyle(StringAspect::LineEditDisplay);

    setLayouter([this] {
        using namespace Layouting;
        return Column {
            Group {
                title(Tr::tr("Automatic Formatting on File Save")),
                groupChecker(autoFormatOnSave.groupChecker()),
                Form {
                    autoFormatTools, br,
                    autoFormatMime, br,
                    Span(2, autoFormatOnlyCurrentProject)
                }
            },
            st
        };
    });
    readSettings();
}

QList<MimeType> GeneralSettings::allowedMimeTypes() const
{
    const QStringList stringTypes = autoFormatMime().split(';');

    QList<MimeType> types;
    for (QString t : stringTypes) {
        t = t.trimmed();
        const MimeType mime = Utils::mimeTypeForName(t);
        if (mime.isValid())
            types << mime;
    }
    return types;
}

class GeneralSettingsPage final : public Core::IOptionsPage
{
public:
    GeneralSettingsPage()
    {
        setId(Constants::OPTION_GENERAL_ID);
        setDisplayName(Tr::tr("General"));
        setCategory(Constants::OPTION_CATEGORY);
        setDisplayCategory(Tr::tr("Beautifier"));
        setCategoryIconPath(":/beautifier/images/settingscategory_beautifier.png");
        setSettingsProvider([] { return &generalSettings(); });
    }
};

const GeneralSettingsPage settingsPage;

} // Beautifier::Internal