ClangFormat: Inline *.ui files

Change-Id: Ie6bcb486c70896894121434b1030861b029a46a2
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2022-07-22 09:44:47 +02:00
committed by Artem Sokolovskii
parent 7070e89904
commit 031bf8d3a4
7 changed files with 139 additions and 146 deletions

View File

@@ -5,8 +5,8 @@ add_qtc_plugin(ClangFormat
SOURCES SOURCES
clangformatbaseindenter.cpp clangformatbaseindenter.h clangformatbaseindenter.cpp clangformatbaseindenter.h
clangformatchecks.ui clangformatchecks.ui
clangformatconfigwidget.cpp clangformatconfigwidget.h clangformatconfigwidget.ui clangformatconfigwidget.cpp clangformatconfigwidget.h
clangformatglobalconfigwidget.cpp clangformatglobalconfigwidget.h clangformatglobalconfigwidget.ui clangformatglobalconfigwidget.cpp clangformatglobalconfigwidget.h
clangformatconstants.h clangformatconstants.h
clangformatfile.cpp clangformatfile.h clangformatfile.cpp clangformatfile.h
clangformatindenter.cpp clangformatindenter.h clangformatindenter.cpp clangformatindenter.h

View File

@@ -37,11 +37,9 @@ QtcPlugin {
"clangformatchecks.ui", "clangformatchecks.ui",
"clangformatconfigwidget.cpp", "clangformatconfigwidget.cpp",
"clangformatconfigwidget.h", "clangformatconfigwidget.h",
"clangformatconfigwidget.ui",
"clangformatconstants.h", "clangformatconstants.h",
"clangformatglobalconfigwidget.cpp", "clangformatglobalconfigwidget.cpp",
"clangformatglobalconfigwidget.h", "clangformatglobalconfigwidget.h",
"clangformatglobalconfigwidget.ui",
"clangformatfile.cpp", "clangformatfile.cpp",
"clangformatfile.h", "clangformatfile.h",
"clangformatindenter.cpp", "clangformatindenter.cpp",

View File

@@ -34,30 +34,36 @@
// the file was generated by scripts/generateClangFormatChecksUI.py // the file was generated by scripts/generateClangFormatChecksUI.py
#include "ui_clangformatchecks.h" #include "ui_clangformatchecks.h"
#include "ui_clangformatconfigwidget.h"
#include <clang/Format/Format.h> #include <clang/Format/Format.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <cppeditor/cpphighlighter.h> #include <cppeditor/cpphighlighter.h>
#include <cppeditor/cppcodestylesettings.h> #include <cppeditor/cppcodestylesettings.h>
#include <cppeditor/cppcodestylesnippets.h> #include <cppeditor/cppcodestylesnippets.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <projectexplorer/session.h> #include <projectexplorer/session.h>
#include <texteditor/displaysettings.h> #include <texteditor/displaysettings.h>
#include <texteditor/icodestylepreferences.h> #include <texteditor/icodestylepreferences.h>
#include <texteditor/snippets/snippeteditor.h> #include <texteditor/snippets/snippeteditor.h>
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <texteditor/texteditorsettings.h> #include <texteditor/texteditorsettings.h>
#include <utils/executeondestruction.h> #include <utils/executeondestruction.h>
#include <utils/layoutbuilder.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <QFile> #include <QCheckBox>
#include <QMessageBox> #include <QComboBox>
#include <QLabel>
#include <QWidget>
#include <sstream> #include <sstream>
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace Utils;
namespace ClangFormat { namespace ClangFormat {
@@ -76,13 +82,47 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
: CppCodeStyleWidget(parent) : CppCodeStyleWidget(parent)
, m_project(project) , m_project(project)
, m_checks(std::make_unique<Ui::ClangFormatChecksWidget>()) , m_checks(std::make_unique<Ui::ClangFormatChecksWidget>())
, m_ui(std::make_unique<Ui::ClangFormatConfigWidget>())
{ {
m_ui->setupUi(this); 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_config = std::make_unique<ClangFormatFile>(filePathToCurrentSettings(codeStyle), m_config = std::make_unique<ClangFormatFile>(filePathToCurrentSettings(codeStyle),
codeStyle->isReadOnly()); codeStyle->isReadOnly());
m_checksScrollArea = new QScrollArea();
m_checksWidget = new QWidget;
m_checks->setupUi(m_checksWidget);
m_checksScrollArea->setWidget(m_checksWidget);
m_checksScrollArea->setMaximumWidth(500);
m_checksWidget->setEnabled(!codeStyle->isReadOnly());
FilePath fileName;
if (m_project)
fileName = m_project->projectFilePath().pathAppended("snippet.cpp");
else
fileName = Core::ICore::userResourcePath("snippet.cpp");
m_preview = new TextEditor::SnippetEditorWidget(this);
TextEditor::DisplaySettings displaySettings = m_preview->displaySettings();
displaySettings.m_visualizeWhitespace = true;
m_preview->setDisplaySettings(displaySettings);
m_preview->setPlainText(QLatin1String(CppEditor::Constants::DEFAULT_CODE_STYLE_SNIPPETS[0]));
m_preview->textDocument()->setIndenter(new ClangFormatIndenter(m_preview->document()));
m_preview->textDocument()->setFontSettings(TextEditor::TextEditorSettings::fontSettings());
m_preview->textDocument()->setSyntaxHighlighter(new CppEditor::CppHighlighter);
m_preview->textDocument()->indenter()->setFileName(fileName);
using namespace Layouting;
Column {
m_projectHasClangFormat,
m_overrideDefault,
m_fallbackConfig,
Row { m_checksScrollArea, m_preview }
}.attachTo(this);
initChecksAndPreview(!codeStyle->isReadOnly());
initOverrideCheckBox(); initOverrideCheckBox();
showOrHideWidgets(); showOrHideWidgets();
@@ -94,52 +134,20 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
ClangFormatConfigWidget::~ClangFormatConfigWidget() = default; ClangFormatConfigWidget::~ClangFormatConfigWidget() = default;
void ClangFormatConfigWidget::initChecksAndPreview(bool enabled)
{
m_checksScrollArea = new QScrollArea();
m_checksWidget = new QWidget;
m_checks->setupUi(m_checksWidget);
m_checksScrollArea->setWidget(m_checksWidget);
m_checksScrollArea->setMaximumWidth(500);
m_checksWidget->setEnabled(enabled);
m_ui->horizontalLayout_2->addWidget(m_checksScrollArea);
m_preview = new TextEditor::SnippetEditorWidget(this);
m_ui->horizontalLayout_2->addWidget(m_preview);
TextEditor::DisplaySettings displaySettings = m_preview->displaySettings();
displaySettings.m_visualizeWhitespace = true;
m_preview->setDisplaySettings(displaySettings);
m_preview->setPlainText(QLatin1String(CppEditor::Constants::DEFAULT_CODE_STYLE_SNIPPETS[0]));
m_preview->textDocument()->setIndenter(new ClangFormatIndenter(m_preview->document()));
m_preview->textDocument()->setFontSettings(TextEditor::TextEditorSettings::fontSettings());
m_preview->textDocument()->setSyntaxHighlighter(new CppEditor::CppHighlighter);
Utils::FilePath fileName;
if (m_project) {
fileName = m_project->projectFilePath().pathAppended("snippet.cpp");
} else {
fileName = Core::ICore::userResourcePath("snippet.cpp");
}
m_preview->textDocument()->indenter()->setFileName(fileName);
}
void ClangFormatConfigWidget::initOverrideCheckBox() void ClangFormatConfigWidget::initOverrideCheckBox()
{ {
if (m_project) { if (m_project) {
m_ui->overrideDefault->setChecked( m_overrideDefault->setChecked(
m_project->namedSettings(Constants::OVERRIDE_FILE_ID).toBool()); m_project->namedSettings(Constants::OVERRIDE_FILE_ID).toBool());
} else { } else {
m_ui->overrideDefault->setChecked(ClangFormatSettings::instance().overrideDefaultFile()); m_overrideDefault->setChecked(ClangFormatSettings::instance().overrideDefaultFile());
m_ui->overrideDefault->setToolTip( m_overrideDefault->setToolTip(
tr("Override Clang Format configuration file with the fallback configuration.")); tr("Override Clang Format configuration file with the fallback configuration."));
} }
connect(m_ui->overrideDefault, &QCheckBox::toggled, connect(m_overrideDefault, &QCheckBox::toggled,
this, &ClangFormatConfigWidget::showOrHideWidgets); this, &ClangFormatConfigWidget::showOrHideWidgets);
connect(m_ui->overrideDefault, &QCheckBox::toggled, this, [this](bool checked) { connect(m_overrideDefault, &QCheckBox::toggled, this, [this](bool checked) {
if (m_project) if (m_project)
m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, checked); m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, checked);
else { else {
@@ -185,33 +193,36 @@ static bool projectConfigExists()
void ClangFormatConfigWidget::showOrHideWidgets() void ClangFormatConfigWidget::showOrHideWidgets()
{ {
m_ui->projectHasClangFormat->hide(); m_projectHasClangFormat->hide();
QLayoutItem *lastItem = m_ui->verticalLayout->itemAt(m_ui->verticalLayout->count() - 1); auto verticalLayout = qobject_cast<QVBoxLayout *>(layout());
QTC_ASSERT(verticalLayout, return);
QLayoutItem *lastItem = verticalLayout->itemAt(verticalLayout->count() - 1);
if (lastItem->spacerItem()) if (lastItem->spacerItem())
m_ui->verticalLayout->removeItem(lastItem); verticalLayout->removeItem(lastItem);
if (!m_ui->overrideDefault->isChecked() && m_project) { if (!m_overrideDefault->isChecked() && m_project) {
// Show the fallback configuration only globally. // Show the fallback configuration only globally.
m_ui->fallbackConfig->hide(); m_fallbackConfig->hide();
m_checksScrollArea->hide(); m_checksScrollArea->hide();
m_preview->hide(); m_preview->hide();
m_ui->verticalLayout->addStretch(1); verticalLayout->addStretch(1);
return; return;
} }
createStyleFileIfNeeded(!m_project); createStyleFileIfNeeded(!m_project);
m_ui->fallbackConfig->show(); m_fallbackConfig->show();
m_checksScrollArea->show(); m_checksScrollArea->show();
m_preview->show(); m_preview->show();
if (!m_project) { if (!m_project) {
const Project *currentProject = SessionManager::startupProject(); const Project *currentProject = SessionManager::startupProject();
if (!currentProject || !projectConfigExists()) { if (!currentProject || !projectConfigExists()) {
m_ui->projectHasClangFormat->hide(); m_projectHasClangFormat->hide();
} else { } else {
m_ui->projectHasClangFormat->show(); m_projectHasClangFormat->show();
m_ui->projectHasClangFormat->setText( m_projectHasClangFormat->setText(
tr("Current project has its own overridden .clang-format file " tr("Current project has its own overridden .clang-format file "
"and can be configured in Projects > Code Style > C++.")); "and can be configured in Projects > Code Style > C++."));
} }

View File

@@ -31,16 +31,20 @@
#include <memory> #include <memory>
QT_BEGIN_NAMESPACE
class QCheckBox;
class QComboBox;
class QLabel;
QT_END_NAMESPACE
namespace ProjectExplorer { class Project; } namespace ProjectExplorer { class Project; }
namespace TextEditor { class SnippetEditorWidget; } namespace TextEditor { class SnippetEditorWidget; }
namespace CppEditor { class CppCodeStyleSettings; } namespace CppEditor { class CppCodeStyleSettings; }
namespace ClangFormat { namespace ClangFormat {
namespace Ui { namespace Ui { class ClangFormatChecksWidget; }
class ClangFormatConfigWidget;
class ClangFormatChecksWidget;
}
class ClangFormatFile; class ClangFormatFile;
class ClangFormatConfigWidget : public CppEditor::CppCodeStyleWidget class ClangFormatConfigWidget : public CppEditor::CppCodeStyleWidget
@@ -69,6 +73,7 @@ private:
void fillTable(); void fillTable();
void saveChanges(QObject *sender); void saveChanges(QObject *sender);
void updatePreview(); void updatePreview();
ProjectExplorer::Project *m_project; ProjectExplorer::Project *m_project;
@@ -77,8 +82,12 @@ private:
TextEditor::SnippetEditorWidget *m_preview; TextEditor::SnippetEditorWidget *m_preview;
std::unique_ptr<ClangFormatFile> m_config; std::unique_ptr<ClangFormatFile> m_config;
std::unique_ptr<Ui::ClangFormatChecksWidget> m_checks; std::unique_ptr<Ui::ClangFormatChecksWidget> m_checks;
std::unique_ptr<Ui::ClangFormatConfigWidget> m_ui;
bool m_disableTableUpdate = false; bool m_disableTableUpdate = false;
QLabel *m_projectHasClangFormat;
QCheckBox *m_overrideDefault;
QLabel *m_fallbackConfig;
}; };
} // namespace ClangFormat } // ClangFormat

View File

@@ -1,57 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ClangFormat::ClangFormatConfigWidget</class>
<widget class="QWidget" name="ClangFormat::ClangFormatConfigWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>489</width>
<height>305</height>
</rect>
</property>
<property name="windowTitle">
<string/>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>8</number>
</property>
<property name="topMargin">
<number>8</number>
</property>
<property name="rightMargin">
<number>8</number>
</property>
<property name="bottomMargin">
<number>8</number>
</property>
<item>
<widget class="QLabel" name="projectHasClangFormat">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="overrideDefault">
<property name="text">
<string>Override Clang Format configuration file</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="fallbackConfig">
<property name="text">
<string>Clang-Format Style</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@@ -28,31 +28,56 @@
#include "clangformatconstants.h" #include "clangformatconstants.h"
#include "clangformatsettings.h" #include "clangformatsettings.h"
#include "ui_clangformatglobalconfigwidget.h"
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <utils/layoutbuilder.h>
#include <QCheckBox>
#include <QComboBox>
#include <QLabel>
#include <QWidget>
#include <sstream> #include <sstream>
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace Utils;
namespace ClangFormat { namespace ClangFormat {
ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(ProjectExplorer::Project *project, ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(ProjectExplorer::Project *project,
QWidget *parent) QWidget *parent)
: CppCodeStyleWidget(parent) : CppCodeStyleWidget(parent)
, m_ui(std::make_unique<Ui::ClangFormatGlobalConfigWidget>())
{ {
m_ui->setupUi(this); resize(489, 305);
m_formattingModeLabel = new QLabel(tr("Formatting mode:"));
m_indentingOrFormatting = new QComboBox(this);
m_formatWhileTyping = new QCheckBox(tr("Format while typing"));
m_formatOnSave = new QCheckBox(tr("Format edited code on file save"));
using namespace Layouting;
Group globalSettingsGroupBox {
title(tr("ClangFormat global setting:")),
Column {
Row { m_formattingModeLabel, m_indentingOrFormatting, st },
m_formatWhileTyping,
m_formatOnSave
}
};
Column {
globalSettingsGroupBox
}.attachTo(this);
initCheckBoxes(); initCheckBoxes();
initIndentationOrFormattingCombobox(); initIndentationOrFormattingCombobox();
if (project) { if (project) {
m_ui->settingsGroupBox->hide(); globalSettingsGroupBox.widget->hide();
return; return;
} }
m_ui->settingsGroupBox->show(); globalSettingsGroupBox.widget->show();
} }
ClangFormatGlobalConfigWidget::~ClangFormatGlobalConfigWidget() = default; ClangFormatGlobalConfigWidget::~ClangFormatGlobalConfigWidget() = default;
@@ -62,27 +87,27 @@ void ClangFormatGlobalConfigWidget::initCheckBoxes()
auto setEnableCheckBoxes = [this](int index) { auto setEnableCheckBoxes = [this](int index) {
bool isFormatting = index == static_cast<int>(ClangFormatSettings::Mode::Formatting); bool isFormatting = index == static_cast<int>(ClangFormatSettings::Mode::Formatting);
m_ui->formatOnSave->setEnabled(isFormatting); m_formatOnSave->setEnabled(isFormatting);
m_ui->formatWhileTyping->setEnabled(isFormatting); m_formatWhileTyping->setEnabled(isFormatting);
}; };
setEnableCheckBoxes(m_ui->indentingOrFormatting->currentIndex()); setEnableCheckBoxes(m_indentingOrFormatting->currentIndex());
connect(m_ui->indentingOrFormatting, &QComboBox::currentIndexChanged, connect(m_indentingOrFormatting, &QComboBox::currentIndexChanged,
this, setEnableCheckBoxes); this, setEnableCheckBoxes);
m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave()); m_formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave());
m_ui->formatWhileTyping->setChecked(ClangFormatSettings::instance().formatWhileTyping()); m_formatWhileTyping->setChecked(ClangFormatSettings::instance().formatWhileTyping());
} }
void ClangFormatGlobalConfigWidget::initIndentationOrFormattingCombobox() void ClangFormatGlobalConfigWidget::initIndentationOrFormattingCombobox()
{ {
m_ui->indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Indenting), m_indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Indenting),
tr("Indenting only")); tr("Indenting only"));
m_ui->indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Formatting), m_indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Formatting),
tr("Full formatting")); tr("Full formatting"));
m_ui->indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Disable), m_indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Disable),
tr("Disable")); tr("Disable"));
m_ui->indentingOrFormatting->setCurrentIndex( m_indentingOrFormatting->setCurrentIndex(
static_cast<int>(ClangFormatSettings::instance().mode())); static_cast<int>(ClangFormatSettings::instance().mode()));
} }
@@ -90,9 +115,9 @@ void ClangFormatGlobalConfigWidget::initIndentationOrFormattingCombobox()
void ClangFormatGlobalConfigWidget::apply() void ClangFormatGlobalConfigWidget::apply()
{ {
ClangFormatSettings &settings = ClangFormatSettings::instance(); ClangFormatSettings &settings = ClangFormatSettings::instance();
settings.setFormatOnSave(m_ui->formatOnSave->isChecked()); settings.setFormatOnSave(m_formatOnSave->isChecked());
settings.setFormatWhileTyping(m_ui->formatWhileTyping->isChecked()); settings.setFormatWhileTyping(m_formatWhileTyping->isChecked());
settings.setMode(static_cast<ClangFormatSettings::Mode>(m_ui->indentingOrFormatting->currentIndex())); settings.setMode(static_cast<ClangFormatSettings::Mode>(m_indentingOrFormatting->currentIndex()));
settings.write(); settings.write();
} }

View File

@@ -29,12 +29,16 @@
#include <memory> #include <memory>
QT_BEGIN_NAMESPACE
class QCheckBox;
class QComboBox;
class QLabel;
QT_END_NAMESPACE
namespace ProjectExplorer { class Project; } namespace ProjectExplorer { class Project; }
namespace ClangFormat { namespace ClangFormat {
namespace Ui { class ClangFormatGlobalConfigWidget; }
class ClangFormatGlobalConfigWidget : public CppEditor::CppCodeStyleWidget class ClangFormatGlobalConfigWidget : public CppEditor::CppCodeStyleWidget
{ {
Q_OBJECT Q_OBJECT
@@ -49,7 +53,10 @@ private:
void initCheckBoxes(); void initCheckBoxes();
void initIndentationOrFormattingCombobox(); void initIndentationOrFormattingCombobox();
std::unique_ptr<Ui::ClangFormatGlobalConfigWidget> m_ui; QLabel *m_formattingModeLabel;
QComboBox *m_indentingOrFormatting;
QCheckBox *m_formatWhileTyping;
QCheckBox *m_formatOnSave;
}; };
} // namespace ClangFormat } // namespace ClangFormat