ClangFormat: Rename override checkbox

It improves the UX design of CodeStyle page.

Change-Id: I4458896d6787df462de18025d292bdb481d03c89
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Artem Sokolovskii
2024-01-02 14:54:11 +01:00
parent 2c8f15ba6c
commit f2cb979aa3
10 changed files with 100 additions and 61 deletions

View File

@@ -772,7 +772,7 @@ void ClangFormatBaseIndenter::autoIndent(const QTextCursor &cursor,
}
}
clang::format::FormatStyle ClangFormatBaseIndenter::overrideStyle(
clang::format::FormatStyle ClangFormatBaseIndenter::customSettingsStyle(
const Utils::FilePath &fileName) const
{
const ProjectExplorer::Project *projectForFile
@@ -820,8 +820,8 @@ const clang::format::FormatStyle &ClangFormatBaseIndenter::styleForFile() const
if (m_cachedStyle.expirationTime > time && !(m_cachedStyle.style == clang::format::getNoStyle()))
return m_cachedStyle.style;
if (getCurrentOverriddenSettings(m_fileName)) {
clang::format::FormatStyle style = overrideStyle(m_fileName);
if (getCurrentCustomSettings(m_fileName)) {
clang::format::FormatStyle style = customSettingsStyle(m_fileName);
m_cachedStyle.setCache(style, cacheTimeout);
return m_cachedStyle.style;
}

View File

@@ -87,7 +87,7 @@ private:
mutable CachedStyle m_cachedStyle;
clang::format::FormatStyle overrideStyle(const Utils::FilePath &fileName) const;
clang::format::FormatStyle customSettingsStyle(const Utils::FilePath &fileName) const;
TextEditor::ICodeStylePreferences *m_overriddenPreferences = nullptr;
};

View File

@@ -9,7 +9,7 @@ static const char SETTINGS_FILE_NAME[] = ".clang-format";
static const char SETTINGS_FILE_ALT_NAME[] = "_clang-format";
static const char SAMPLE_FILE_NAME[] = "test.cpp";
static const char SETTINGS_ID[] = "ClangFormat";
static const char OVERRIDE_FILE_ID[] = "ClangFormat.OverrideFile";
static const char USE_CUSTOM_SETTINGS_ID[] = "ClangFormat.OverrideFile";
static const char FORMAT_CODE_ON_SAVE_ID[] = "ClangFormat.FormatCodeOnSave";
static const char FORMAT_WHILE_TYPING_ID[] = "ClangFormat.FormatWhileTyping";
static const char MODE_ID[] = "ClangFormat.Mode";

View File

@@ -9,6 +9,7 @@
#include "clangformatutils.h"
#include <projectexplorer/project.h>
#include <projectexplorer/projecttree.h>
#include <texteditor/icodestylepreferences.h>
#include <utils/layoutbuilder.h>
@@ -19,8 +20,6 @@
#include <QSpinBox>
#include <QWidget>
#include <sstream>
using namespace ProjectExplorer;
using namespace Utils;
@@ -43,10 +42,16 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(
m_indentingOrFormatting = new QComboBox(this);
m_formatWhileTyping = new QCheckBox(Tr::tr("Format while typing"));
m_formatOnSave = new QCheckBox(Tr::tr("Format edited code on file save"));
m_overrideDefault = new QCheckBox(Tr::tr("Override .clang-format file"));
m_useCustomSettingsCheckBox = new QCheckBox(Tr::tr("Use custom settings"));
m_useGlobalSettings = new QCheckBox(Tr::tr("Use global settings"));
m_useGlobalSettings->hide();
m_overrideDefaultFile = ClangFormatSettings::instance().overrideDefaultFile();
m_useCustomSettings = ClangFormatSettings::instance().useCustomSettings();
m_currentProjectLabel = new QLabel(
Tr::tr("Please note that the current project includes a .clang-format file, which will be "
"used for code indenting and formatting."));
m_currentProjectLabel->setStyleSheet("QLabel { color : red; }");
m_currentProjectLabel->setWordWrap(true);
using namespace Layouting;
@@ -64,7 +69,8 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(
m_formatWhileTyping,
m_formatOnSave,
m_projectHasClangFormat,
m_overrideDefault
m_useCustomSettingsCheckBox,
m_currentProjectLabel
}
};
@@ -75,9 +81,10 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(
initCheckBoxes();
initIndentationOrFormattingCombobox();
initOverrideCheckBox();
initCustomSettingsCheckBox();
initUseGlobalSettingsCheckBox();
initFileSizeThresholdSpinBox();
initCurrentProjectLabel();
if (project) {
m_formatOnSave->hide();
@@ -139,7 +146,7 @@ void ClangFormatGlobalConfigWidget::initUseGlobalSettingsCheckBox()
isDisabled
|| (m_indentingOrFormatting->currentIndex()
== static_cast<int>(ClangFormatSettings::Mode::Disable)));
m_overrideDefault->setDisabled(isDisabled
m_useCustomSettingsCheckBox->setDisabled(isDisabled
|| (m_indentingOrFormatting->currentIndex()
== static_cast<int>(ClangFormatSettings::Mode::Disable)));
};
@@ -173,6 +180,30 @@ void ClangFormatGlobalConfigWidget::initFileSizeThresholdSpinBox()
});
}
void ClangFormatGlobalConfigWidget::initCurrentProjectLabel()
{
auto setCurrentProjectLabelVisible = [this]() {
ProjectExplorer::Project *currentProject
= m_project ? m_project : ProjectExplorer::ProjectTree::currentProject();
if (currentProject) {
Utils::FilePath settingsFilePath = currentProject->projectDirectory()
/ Constants::SETTINGS_FILE_NAME;
Utils::FilePath settingsAltFilePath = currentProject->projectDirectory()
/ Constants::SETTINGS_FILE_ALT_NAME;
if ((settingsFilePath.exists() || settingsAltFilePath.exists())
&& m_useCustomSettingsCheckBox->checkState() == Qt::CheckState::Unchecked) {
m_currentProjectLabel->show();
return;
}
}
m_currentProjectLabel->hide();
};
setCurrentProjectLabelVisible();
connect(m_useCustomSettingsCheckBox, &QCheckBox::toggled, this, setCurrentProjectLabelVisible);
}
bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists()
{
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder
@@ -181,7 +212,7 @@ bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists()
return styleFromProjectFolder && !(*styleFromProjectFolder == clang::format::getNoStyle());
}
void ClangFormatGlobalConfigWidget::initOverrideCheckBox()
void ClangFormatGlobalConfigWidget::initCustomSettingsCheckBox()
{
if (!m_project || !projectClangFormatFileExists()) {
m_projectHasClangFormat->hide();
@@ -195,24 +226,24 @@ void ClangFormatGlobalConfigWidget::initOverrideCheckBox()
if (m_ignoreChanges.isLocked())
return;
Utils::GuardLocker locker(m_ignoreChanges);
m_codeStyle->currentPreferences()->setTemporarilyReadOnly(!m_overrideDefault->isChecked());
m_codeStyle->currentPreferences()->setIsAdditionalTabDisabled(!m_overrideDefault->isEnabled());
m_codeStyle->currentPreferences()->setTemporarilyReadOnly(!m_useCustomSettingsCheckBox->isChecked());
m_codeStyle->currentPreferences()->setIsAdditionalTabDisabled(!m_useCustomSettingsCheckBox->isEnabled());
ClangFormatSettings::instance().write();
emit m_codeStyle->currentPreferencesChanged(m_codeStyle->currentPreferences());
};
auto setEnableOverrideCheckBox = [this, setTemporarilyReadOnly](int index) {
auto setEnableCustomSettingsCheckBox = [this, setTemporarilyReadOnly](int index) {
bool isDisable = index == static_cast<int>(ClangFormatSettings::Mode::Disable);
m_overrideDefault->setDisabled(isDisable);
m_useCustomSettingsCheckBox->setDisabled(isDisable);
m_projectHasClangFormat->setDisabled(isDisable);
setTemporarilyReadOnly();
};
setEnableOverrideCheckBox(m_indentingOrFormatting->currentIndex());
setEnableCustomSettingsCheckBox(m_indentingOrFormatting->currentIndex());
connect(m_indentingOrFormatting, &QComboBox::currentIndexChanged,
this, setEnableOverrideCheckBox);
this, setEnableCustomSettingsCheckBox);
m_overrideDefault->setToolTip("<html>"
m_useCustomSettingsCheckBox->setToolTip("<html>"
+ Tr::tr("When this option is enabled, ClangFormat will use a "
"user-specified configuration from the widget below, "
"instead of the project .clang-format file. You can "
@@ -222,14 +253,17 @@ void ClangFormatGlobalConfigWidget::initOverrideCheckBox()
"configuration, and will not modify the project "
".clang-format file."));
m_overrideDefault->setChecked(getProjectOverriddenSettings(m_project));
m_useCustomSettingsCheckBox->setChecked(getProjectCustomSettings(m_project));
setTemporarilyReadOnly();
connect(m_overrideDefault, &QCheckBox::toggled, this, [this, setTemporarilyReadOnly](bool checked) {
connect(m_useCustomSettingsCheckBox,
&QCheckBox::toggled,
this,
[this, setTemporarilyReadOnly](bool checked) {
if (m_project) {
m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, checked);
m_project->setNamedSettings(Constants::USE_CUSTOM_SETTINGS_ID, checked);
} else {
ClangFormatSettings::instance().setOverrideDefaultFile(checked);
ClangFormatSettings::instance().setUseCustomSettings(checked);
setTemporarilyReadOnly();
}
});
@@ -249,18 +283,18 @@ void ClangFormatGlobalConfigWidget::apply()
if (!m_project) {
settings.setMode(
static_cast<ClangFormatSettings::Mode>(m_indentingOrFormatting->currentIndex()));
settings.setOverrideDefaultFile(m_overrideDefault->isChecked());
settings.setUseCustomSettings(m_useCustomSettingsCheckBox->isChecked());
settings.setFileSizeThreshold(m_fileSizeThresholdSpinBox->value());
m_overrideDefaultFile = m_overrideDefault->isChecked();
m_useCustomSettings = m_useCustomSettingsCheckBox->isChecked();
}
settings.write();
}
void ClangFormatGlobalConfigWidget::finish()
{
ClangFormatSettings::instance().setOverrideDefaultFile(m_overrideDefaultFile);
ClangFormatSettings::instance().setUseCustomSettings(m_useCustomSettings);
m_codeStyle->currentPreferences()->setTemporarilyReadOnly(
!ClangFormatSettings::instance().overrideDefaultFile());
!ClangFormatSettings::instance().useCustomSettings());
}
} // namespace ClangFormat

View File

@@ -7,8 +7,6 @@
#include <utils/guard.h>
#include <memory>
QT_BEGIN_NAMESPACE
class QCheckBox;
class QComboBox;
@@ -36,16 +34,17 @@ public:
private:
void initCheckBoxes();
void initIndentationOrFormattingCombobox();
void initOverrideCheckBox();
void initCustomSettingsCheckBox();
void initUseGlobalSettingsCheckBox();
void initFileSizeThresholdSpinBox();
void initCurrentProjectLabel();
bool projectClangFormatFileExists();
ProjectExplorer::Project *m_project;
TextEditor::ICodeStylePreferences *m_codeStyle;
Utils::Guard m_ignoreChanges;
bool m_overrideDefaultFile;
bool m_useCustomSettings;
QLabel *m_projectHasClangFormat;
QLabel *m_formattingModeLabel;
@@ -54,8 +53,9 @@ private:
QComboBox *m_indentingOrFormatting;
QCheckBox *m_formatWhileTyping;
QCheckBox *m_formatOnSave;
QCheckBox *m_overrideDefault;
QCheckBox *m_useCustomSettingsCheckBox;
QCheckBox *m_useGlobalSettings;
QLabel *m_currentProjectLabel;
};
} // namespace ClangFormat

View File

@@ -22,7 +22,8 @@ ClangFormatSettings::ClangFormatSettings()
{
QtcSettings *settings = Core::ICore::settings();
settings->beginGroup(Constants::SETTINGS_ID);
m_overrideDefaultFile = settings->value(Constants::OVERRIDE_FILE_ID, false).toBool();
m_useCustomSettings
= settings->value(Constants::USE_CUSTOM_SETTINGS_ID, false).toBool();
m_formatWhileTyping = settings->value(Constants::FORMAT_WHILE_TYPING_ID, false).toBool();
m_formatOnSave = settings->value(Constants::FORMAT_CODE_ON_SAVE_ID, false).toBool();
m_fileSizeThreshold = settings->value(Constants::FILE_SIZE_THREDSHOLD,
@@ -47,7 +48,7 @@ void ClangFormatSettings::write() const
{
QtcSettings *settings = Core::ICore::settings();
settings->beginGroup(Constants::SETTINGS_ID);
settings->setValue(Constants::OVERRIDE_FILE_ID, m_overrideDefaultFile);
settings->setValue(Constants::USE_CUSTOM_SETTINGS_ID, m_useCustomSettings);
settings->setValue(Constants::FORMAT_WHILE_TYPING_ID, m_formatWhileTyping);
settings->setValue(Constants::FORMAT_CODE_ON_SAVE_ID, m_formatOnSave);
settings->setValue(Constants::MODE_ID, static_cast<int>(m_mode));
@@ -55,14 +56,14 @@ void ClangFormatSettings::write() const
settings->endGroup();
}
void ClangFormatSettings::setOverrideDefaultFile(bool enable)
void ClangFormatSettings::setUseCustomSettings(bool enable)
{
m_overrideDefaultFile = enable;
m_useCustomSettings = enable;
}
bool ClangFormatSettings::overrideDefaultFile() const
bool ClangFormatSettings::useCustomSettings() const
{
return m_overrideDefaultFile;
return m_useCustomSettings;
}
void ClangFormatSettings::setFormatWhileTyping(bool enable)

View File

@@ -15,8 +15,8 @@ public:
ClangFormatSettings();
void write() const;
void setOverrideDefaultFile(bool enable);
bool overrideDefaultFile() const;
void setUseCustomSettings(bool enable);
bool useCustomSettings() const;
enum Mode {
Indenting = 0,
@@ -38,7 +38,7 @@ public:
private:
Mode m_mode;
bool m_overrideDefaultFile = false;
bool m_useCustomSettings = false;
bool m_formatWhileTyping = false;
bool m_formatOnSave = false;
int m_fileSizeThreshold = 200;

View File

@@ -287,24 +287,25 @@ bool getProjectUseGlobalSettings(const ProjectExplorer::Project *project)
return projectUseGlobalSettings.isValid() ? projectUseGlobalSettings.toBool() : true;
}
bool getProjectOverriddenSettings(const ProjectExplorer::Project *project)
bool getProjectCustomSettings(const ProjectExplorer::Project *project)
{
const QVariant projectOverride = project ? project->namedSettings(Constants::OVERRIDE_FILE_ID)
const QVariant projectCustomSettings = project ? project->namedSettings(
Constants::USE_CUSTOM_SETTINGS_ID)
: QVariant();
return projectOverride.isValid()
? projectOverride.toBool()
: ClangFormatSettings::instance().overrideDefaultFile();
return projectCustomSettings.isValid()
? projectCustomSettings.toBool()
: ClangFormatSettings::instance().useCustomSettings();
}
bool getCurrentOverriddenSettings(const Utils::FilePath &filePath)
bool getCurrentCustomSettings(const Utils::FilePath &filePath)
{
const ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::projectForFile(
filePath);
return getProjectUseGlobalSettings(project)
? ClangFormatSettings::instance().overrideDefaultFile()
: getProjectOverriddenSettings(project);
? ClangFormatSettings::instance().useCustomSettings()
: getProjectCustomSettings(project);
}
ClangFormatSettings::Mode getProjectIndentationOrFormattingSettings(
@@ -348,7 +349,7 @@ Utils::FilePath findConfig(const Utils::FilePath &fileName)
Utils::FilePath configForFile(const Utils::FilePath &fileName)
{
if (!getCurrentOverriddenSettings(fileName))
if (!getCurrentCustomSettings(fileName))
return findConfig(fileName);
const ProjectExplorer::Project *projectForFile

View File

@@ -26,8 +26,8 @@ QString projectUniqueId(ProjectExplorer::Project *project);
bool getProjectUseGlobalSettings(const ProjectExplorer::Project *project);
bool getProjectOverriddenSettings(const ProjectExplorer::Project *project);
bool getCurrentOverriddenSettings(const Utils::FilePath &filePath);
bool getProjectCustomSettings(const ProjectExplorer::Project *project);
bool getCurrentCustomSettings(const Utils::FilePath &filePath);
ClangFormatSettings::Mode getProjectIndentationOrFormattingSettings(
const ProjectExplorer::Project *project);
@@ -40,7 +40,7 @@ void fromTabSettings(clang::format::FormatStyle &style, const TextEditor::TabSet
void fromCppCodeStyleSettings(clang::format::FormatStyle &style,
const CppEditor::CppCodeStyleSettings &settings);
bool getProjectOverriddenSettings(const ProjectExplorer::Project *project);
bool getProjectCustomSettings(const ProjectExplorer::Project *project);
void addQtcStatementMacros(clang::format::FormatStyle &style);
clang::format::FormatStyle qtcStyle();

View File

@@ -50,7 +50,7 @@ CodeStyleSelectorWidget::CodeStyleSelectorWidget(ICodeStylePreferencesFactory *f
Column {
Grid {
Tr::tr("Current settings:"),
Tr::tr("Custom settings:"),
m_delegateComboBox,
copyButton,
m_removeButton,
@@ -286,6 +286,9 @@ QString CodeStyleSelectorWidget::displayName(ICodeStylePreferences *codeStyle) c
name = Tr::tr("%1 [proxy: %2]").arg(name).arg(codeStyle->currentDelegate()->displayName());
if (codeStyle->isReadOnly())
name = Tr::tr("%1 [built-in]").arg(name);
else
name = Tr::tr("%1 [customizable]").arg(name);
return name;
}