ClangFormat: Move override checkbox to global settings

Moved override checkbox to global settings groupbox.

Change-Id: I70c97486a61f69274d059edc5095fe02c86fb04d
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2022-08-19 15:20:22 +02:00
parent d3354d1999
commit 2d375eb776
4 changed files with 65 additions and 67 deletions

View File

@@ -46,7 +46,6 @@
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <projectexplorer/editorconfiguration.h> #include <projectexplorer/editorconfiguration.h>
#include <projectexplorer/session.h>
#include <texteditor/displaysettings.h> #include <texteditor/displaysettings.h>
#include <texteditor/icodestylepreferences.h> #include <texteditor/icodestylepreferences.h>
@@ -88,8 +87,6 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
m_config = std::make_unique<ClangFormatFile>(filePathToCurrentSettings(codeStyle->currentPreferences())); m_config = std::make_unique<ClangFormatFile>(filePathToCurrentSettings(codeStyle->currentPreferences()));
resize(489, 305); resize(489, 305);
m_projectHasClangFormat = new QLabel(this);
m_overrideDefault = new QCheckBox(tr("Override Clang Format configuration file"));
m_fallbackConfig = new QLabel(tr("Clang-Format Style")); m_fallbackConfig = new QLabel(tr("Clang-Format Style"));
m_checksScrollArea = new QScrollArea(); m_checksScrollArea = new QScrollArea();
m_checksWidget = new QWidget; m_checksWidget = new QWidget;
@@ -118,14 +115,10 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
using namespace Layouting; using namespace Layouting;
Column { Column {
m_projectHasClangFormat,
m_overrideDefault,
m_fallbackConfig, m_fallbackConfig,
Row { m_checksScrollArea, m_preview } Row { m_checksScrollArea, m_preview }
}.attachTo(this); }.attachTo(this);
initOverrideCheckBox();
connect(codeStyle, &TextEditor::ICodeStylePreferences::currentPreferencesChanged, connect(codeStyle, &TextEditor::ICodeStylePreferences::currentPreferencesChanged,
this, &ClangFormatConfigWidget::slotCodeStyleChanged); this, &ClangFormatConfigWidget::slotCodeStyleChanged);
@@ -155,29 +148,6 @@ void ClangFormatConfigWidget::slotCodeStyleChanged(
updatePreview(); updatePreview();
} }
void ClangFormatConfigWidget::initOverrideCheckBox()
{
if (m_project) {
m_overrideDefault->setChecked(
m_project->namedSettings(Constants::OVERRIDE_FILE_ID).toBool());
} else {
m_overrideDefault->setChecked(ClangFormatSettings::instance().overrideDefaultFile());
m_overrideDefault->setToolTip(
tr("Override Clang Format configuration file with the fallback configuration."));
}
connect(m_overrideDefault, &QCheckBox::toggled,
this, &ClangFormatConfigWidget::showOrHideWidgets);
connect(m_overrideDefault, &QCheckBox::toggled, this, [this](bool checked) {
if (m_project)
m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, checked);
else {
ClangFormatSettings::instance().setOverrideDefaultFile(checked);
ClangFormatSettings::instance().write();
}
});
}
void ClangFormatConfigWidget::connectChecks() void ClangFormatConfigWidget::connectChecks()
{ {
auto doSaveChanges = [this](QObject *sender) { auto doSaveChanges = [this](QObject *sender) {
@@ -200,19 +170,8 @@ void ClangFormatConfigWidget::connectChecks()
} }
} }
static bool projectConfigExists()
{
return Core::ICore::userResourcePath()
.pathAppended("clang-format")
.pathAppended(currentProjectUniqueId())
.pathAppended(Constants::SETTINGS_FILE_NAME)
.exists();
}
void ClangFormatConfigWidget::showOrHideWidgets() void ClangFormatConfigWidget::showOrHideWidgets()
{ {
m_projectHasClangFormat->hide();
auto verticalLayout = qobject_cast<QVBoxLayout *>(layout()); auto verticalLayout = qobject_cast<QVBoxLayout *>(layout());
QTC_ASSERT(verticalLayout, return); QTC_ASSERT(verticalLayout, return);
@@ -220,31 +179,10 @@ void ClangFormatConfigWidget::showOrHideWidgets()
if (lastItem->spacerItem()) if (lastItem->spacerItem())
verticalLayout->removeItem(lastItem); verticalLayout->removeItem(lastItem);
if (!m_overrideDefault->isChecked() && m_project) {
// Show the fallback configuration only globally.
m_fallbackConfig->hide();
m_checksScrollArea->hide();
m_preview->hide();
verticalLayout->addStretch(1);
return;
}
createStyleFileIfNeeded(!m_project); createStyleFileIfNeeded(!m_project);
m_fallbackConfig->show(); m_fallbackConfig->show();
m_checksScrollArea->show(); m_checksScrollArea->show();
m_preview->show(); m_preview->show();
if (!m_project) {
const Project *currentProject = SessionManager::startupProject();
if (!currentProject || !projectConfigExists()) {
m_projectHasClangFormat->hide();
} else {
m_projectHasClangFormat->show();
m_projectHasClangFormat->setText(
tr("Current project has its own overridden .clang-format file "
"and can be configured in Projects > Code Style > C++."));
}
}
} }
void ClangFormatConfigWidget::updatePreview() void ClangFormatConfigWidget::updatePreview()

View File

@@ -71,7 +71,6 @@ private:
void showOrHideWidgets(); void showOrHideWidgets();
void initChecksAndPreview(); void initChecksAndPreview();
void initOverrideCheckBox();
void connectChecks(); void connectChecks();
void fillTable(); void fillTable();
@@ -90,8 +89,6 @@ private:
Utils::Guard m_ignoreChanges; Utils::Guard m_ignoreChanges;
QLabel *m_projectHasClangFormat;
QCheckBox *m_overrideDefault;
QLabel *m_fallbackConfig; QLabel *m_fallbackConfig;
}; };

View File

@@ -27,6 +27,7 @@
#include "clangformatconstants.h" #include "clangformatconstants.h"
#include "clangformatsettings.h" #include "clangformatsettings.h"
#include "clangformatutils.h"
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
@@ -47,13 +48,16 @@ namespace ClangFormat {
ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(ProjectExplorer::Project *project, ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(ProjectExplorer::Project *project,
QWidget *parent) QWidget *parent)
: CppCodeStyleWidget(parent) : CppCodeStyleWidget(parent)
, m_project(project)
{ {
resize(489, 305); resize(489, 305);
m_projectHasClangFormat = new QLabel(this);
m_formattingModeLabel = new QLabel(tr("Formatting mode:")); m_formattingModeLabel = new QLabel(tr("Formatting mode:"));
m_indentingOrFormatting = new QComboBox(this); m_indentingOrFormatting = new QComboBox(this);
m_formatWhileTyping = new QCheckBox(tr("Format while typing")); m_formatWhileTyping = new QCheckBox(tr("Format while typing"));
m_formatOnSave = new QCheckBox(tr("Format edited code on file save")); m_formatOnSave = new QCheckBox(tr("Format edited code on file save"));
m_overrideDefault = new QCheckBox(tr("Override Clang Format configuration file"));
using namespace Layouting; using namespace Layouting;
@@ -62,7 +66,9 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(ProjectExplorer::Pr
Column { Column {
Row { m_formattingModeLabel, m_indentingOrFormatting, st }, Row { m_formattingModeLabel, m_indentingOrFormatting, st },
m_formatWhileTyping, m_formatWhileTyping,
m_formatOnSave m_formatOnSave,
m_projectHasClangFormat,
m_overrideDefault
} }
}; };
@@ -72,9 +78,13 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(ProjectExplorer::Pr
initCheckBoxes(); initCheckBoxes();
initIndentationOrFormattingCombobox(); initIndentationOrFormattingCombobox();
initOverrideCheckBox();
if (project) { if (project) {
globalSettingsGroupBox.widget->hide(); m_formattingModeLabel->hide();
m_formatOnSave->hide();
m_formatWhileTyping->hide();
m_indentingOrFormatting->hide();
return; return;
} }
globalSettingsGroupBox.widget->show(); globalSettingsGroupBox.widget->show();
@@ -111,6 +121,52 @@ void ClangFormatGlobalConfigWidget::initIndentationOrFormattingCombobox()
static_cast<int>(ClangFormatSettings::instance().mode())); static_cast<int>(ClangFormatSettings::instance().mode()));
} }
bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists()
{
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder
= clang::format::getStyle("file", m_project->projectFilePath().path().toStdString(), "none");
return styleFromProjectFolder && !(*styleFromProjectFolder == clang::format::getNoStyle());
}
void ClangFormatGlobalConfigWidget::initOverrideCheckBox()
{
if (!m_project || !projectClangFormatFileExists()) {
m_projectHasClangFormat->hide();
} else {
m_projectHasClangFormat->show();
m_projectHasClangFormat->setText(tr("The current project has its own .clang-format file which "
"can be overridden by the settings below."));
}
auto setEnableOverrideCheckBox = [this](int index) {
bool isDisable = index == static_cast<int>(ClangFormatSettings::Mode::Disable);
m_overrideDefault->setEnabled(!isDisable);
};
setEnableOverrideCheckBox(m_indentingOrFormatting->currentIndex());
connect(m_indentingOrFormatting, &QComboBox::currentIndexChanged,
this, setEnableOverrideCheckBox);
m_overrideDefault->setToolTip(
tr("Override Clang Format configuration file with the chosen configuration."));
if (m_project)
m_overrideDefault->setChecked(
m_project->namedSettings(Constants::OVERRIDE_FILE_ID).toBool());
else
m_overrideDefault->setChecked(ClangFormatSettings::instance().overrideDefaultFile());
connect(m_overrideDefault, &QCheckBox::toggled, this, [this](bool checked) {
if (m_project)
m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, checked);
else {
ClangFormatSettings::instance().setOverrideDefaultFile(checked);
ClangFormatSettings::instance().write();
}
});
}
void ClangFormatGlobalConfigWidget::apply() void ClangFormatGlobalConfigWidget::apply()
{ {

View File

@@ -52,11 +52,18 @@ public:
private: private:
void initCheckBoxes(); void initCheckBoxes();
void initIndentationOrFormattingCombobox(); void initIndentationOrFormattingCombobox();
void initOverrideCheckBox();
bool projectClangFormatFileExists();
ProjectExplorer::Project *m_project;
QLabel *m_projectHasClangFormat;
QLabel *m_formattingModeLabel; QLabel *m_formattingModeLabel;
QComboBox *m_indentingOrFormatting; QComboBox *m_indentingOrFormatting;
QCheckBox *m_formatWhileTyping; QCheckBox *m_formatWhileTyping;
QCheckBox *m_formatOnSave; QCheckBox *m_formatOnSave;
QCheckBox *m_overrideDefault;
}; };
} // namespace ClangFormat } // namespace ClangFormat