summaryrefslogtreecommitdiff
path: root/src/plugins/cmakeprojectmanager/cmakeformatteroptionspage.cpp
blob: a82e7be07af730b58a743d675386855e65d7e489 (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
// Copyright (C) 2016 Lorenz Haas
// Copyright (C) 2022 Xavier BESSON
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "cmakeformatteroptionspage.h"

#include "cmakeprojectconstants.h"
#include "cmakeprojectmanagertr.h"
#include "cmakeformattersettings.h"

#include <utils/layoutbuilder.h>
#include <utils/pathchooser.h>

#include <QApplication>
#include <QCheckBox>
#include <QComboBox>
#include <QLabel>
#include <QLineEdit>

namespace CMakeProjectManager::Internal {

class CMakeFormatterOptionsPageWidget : public Core::IOptionsPageWidget
{
public:
    explicit CMakeFormatterOptionsPageWidget();

private:
    void apply() final;

    Utils::PathChooser *m_command;
    QCheckBox *m_autoFormat;
    QLineEdit *m_autoFormatMime;
    QCheckBox *m_autoFormatOnlyCurrentProject;
};

CMakeFormatterOptionsPageWidget::CMakeFormatterOptionsPageWidget()
{
    resize(817, 631);

    auto settings = CMakeFormatterSettings::instance();

    m_autoFormat = new QCheckBox(Tr::tr("Enable auto format on file save"));
    m_autoFormat->setChecked(settings->autoFormatOnSave());

    auto mimeLabel = new QLabel(Tr::tr("Restrict to MIME types:"));
    mimeLabel->setEnabled(false);

    m_autoFormatMime = new QLineEdit(settings->autoFormatMimeAsString());
    m_autoFormatMime->setEnabled(m_autoFormat->isChecked());

    m_autoFormatOnlyCurrentProject =
        new QCheckBox(Tr::tr("Restrict to files contained in the current project"));
    m_autoFormatOnlyCurrentProject->setEnabled(m_autoFormat->isChecked());
    m_autoFormatOnlyCurrentProject->setChecked(settings->autoFormatOnlyCurrentProject());

    m_command = new Utils::PathChooser;
    m_command->setExpectedKind(Utils::PathChooser::ExistingCommand);
    m_command->setCommandVersionArguments({"--version"});
    m_command->setPromptDialogTitle(Tr::tr("%1 Command").arg(Tr::tr("Formatter")));
    m_command->setFilePath(settings->command());

    using namespace Utils::Layouting;

    Column {
        Group {
            title(Tr::tr("Automatic Formatting on File Save")),
            Form {
                Tr::tr("CMakeFormat command:"), m_command, br,
                Span(2, m_autoFormat), br,
                mimeLabel, m_autoFormatMime, br,
                Span(2, m_autoFormatOnlyCurrentProject)
            }
        },
        st
    }.attachTo(this);

    connect(m_autoFormat, &QCheckBox::toggled, m_autoFormatMime, &QLineEdit::setEnabled);
    connect(m_autoFormat, &QCheckBox::toggled, mimeLabel, &QLabel::setEnabled);
    connect(m_autoFormat, &QCheckBox::toggled, m_autoFormatOnlyCurrentProject, &QCheckBox::setEnabled);
}

void CMakeFormatterOptionsPageWidget::apply()
{
    auto settings = CMakeFormatterSettings::instance();
    settings->setCommand(m_command->filePath().toString());
    settings->setAutoFormatOnSave(m_autoFormat->isChecked());
    settings->setAutoFormatMime(m_autoFormatMime->text());
    settings->setAutoFormatOnlyCurrentProject(m_autoFormatOnlyCurrentProject->isChecked());
    settings->save();
}

CMakeFormatterOptionsPage::CMakeFormatterOptionsPage()
{
    setId(Constants::Settings::FORMATTER_ID);
    setDisplayName(Tr::tr("Formatter"));
    setDisplayCategory("CMake");
    setCategory(Constants::Settings::CATEGORY);
    setWidgetCreator([] { return new CMakeFormatterOptionsPageWidget; });
}

} // CMakeProjectManager::Internal