diff --git a/src/plugins/clangformat/CMakeLists.txt b/src/plugins/clangformat/CMakeLists.txt index 3c648da5923..ebe0bebc71b 100644 --- a/src/plugins/clangformat/CMakeLists.txt +++ b/src/plugins/clangformat/CMakeLists.txt @@ -6,6 +6,7 @@ add_qtc_plugin(ClangFormat clangformatbaseindenter.cpp clangformatbaseindenter.h clangformatchecks.ui clangformatconfigwidget.cpp clangformatconfigwidget.h clangformatconfigwidget.ui + clangformatglobalconfigwidget.cpp clangformatglobalconfigwidget.h clangformatglobalconfigwidget.ui clangformatconstants.h clangformatfile.cpp clangformatfile.h clangformatindenter.cpp clangformatindenter.h diff --git a/src/plugins/clangformat/clangformat.qbs b/src/plugins/clangformat/clangformat.qbs index f7d60ad73d1..c6b4043698c 100644 --- a/src/plugins/clangformat/clangformat.qbs +++ b/src/plugins/clangformat/clangformat.qbs @@ -39,6 +39,9 @@ QtcPlugin { "clangformatconfigwidget.h", "clangformatconfigwidget.ui", "clangformatconstants.h", + "clangformatglobalconfigwidget.cpp", + "clangformatglobalconfigwidget.h", + "clangformatglobalconfigwidget.ui", "clangformatfile.cpp", "clangformatfile.h", "clangformatindenter.cpp", diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index 91781563910..04748563650 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -42,11 +42,8 @@ #include #include #include -#include -#include #include #include -#include #include #include #include @@ -54,7 +51,6 @@ #include #include #include -#include #include #include @@ -88,8 +84,6 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc initChecksAndPreview(!codeStyle->isReadOnly()); initOverrideCheckBox(); - initCheckBoxes(); - initIndentationOrFormattingCombobox(); showOrHideWidgets(); fillTable(); @@ -132,45 +126,6 @@ void ClangFormatConfigWidget::initChecksAndPreview(bool enabled) m_preview->textDocument()->indenter()->setFileName(fileName); } -void ClangFormatConfigWidget::initCheckBoxes() -{ - if (m_project) { - m_ui->formatOnSave->hide(); - m_ui->formatWhileTyping->hide(); - return; - } - - m_ui->formatOnSave->show(); - m_ui->formatWhileTyping->show(); - - auto setEnableCheckBoxes = [this](int index) { - bool isFormatting = index == static_cast(ClangFormatSettings::Mode::Formatting); - - m_ui->formatOnSave->setEnabled(isFormatting); - m_ui->formatWhileTyping->setEnabled(isFormatting); - }; - setEnableCheckBoxes(m_ui->indentingOrFormatting->currentIndex()); - connect(m_ui->indentingOrFormatting, &QComboBox::currentIndexChanged, - this, setEnableCheckBoxes); - - m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave()); - m_ui->formatWhileTyping->setChecked(ClangFormatSettings::instance().formatWhileTyping()); - - connect(m_ui->formatOnSave, &QCheckBox::toggled, - this, [](bool checked) { - ClangFormatSettings &settings = ClangFormatSettings::instance(); - settings.setFormatOnSave(checked); - settings.write(); - }); - - connect(m_ui->formatWhileTyping, &QCheckBox::toggled, - this, [](bool checked) { - ClangFormatSettings &settings = ClangFormatSettings::instance(); - settings.setFormatWhileTyping(checked); - settings.write(); - }); -} - void ClangFormatConfigWidget::initOverrideCheckBox() { if (m_project) { @@ -219,29 +174,6 @@ void ClangFormatConfigWidget::onTableChanged() saveChanges(sender()); } -void ClangFormatConfigWidget::initIndentationOrFormattingCombobox() -{ - m_ui->indentingOrFormatting->insertItem(static_cast(ClangFormatSettings::Mode::Indenting), - tr("Indenting only")); - m_ui->indentingOrFormatting->insertItem(static_cast(ClangFormatSettings::Mode::Formatting), - tr("Full formatting")); - m_ui->indentingOrFormatting->insertItem(static_cast(ClangFormatSettings::Mode::Disable), - tr("Disable")); - - m_ui->indentingOrFormatting->setCurrentIndex( - static_cast(ClangFormatSettings::instance().mode())); - - const bool isGlobal = !m_project; - m_ui->indentingOrFormatting->setVisible(isGlobal); - m_ui->formattingModeLabel->setVisible(isGlobal); - - connect(m_ui->indentingOrFormatting, &QComboBox::currentIndexChanged, this, [](int index) { - ClangFormatSettings &settings = ClangFormatSettings::instance(); - settings.setMode(static_cast(index)); - settings.write(); - }); -} - static bool projectConfigExists() { return Core::ICore::userResourcePath() diff --git a/src/plugins/clangformat/clangformatconfigwidget.h b/src/plugins/clangformat/clangformatconfigwidget.h index 3b6f2aa1634..edfdd8999dd 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.h +++ b/src/plugins/clangformat/clangformatconfigwidget.h @@ -65,13 +65,10 @@ private: void showOrHideWidgets(); void initChecksAndPreview(bool enabled); void initOverrideCheckBox(); - void initCheckBoxes(); void connectChecks(); void fillTable(); void saveChanges(QObject *sender); - - void initIndentationOrFormattingCombobox(); void updatePreview(); ProjectExplorer::Project *m_project; diff --git a/src/plugins/clangformat/clangformatconfigwidget.ui b/src/plugins/clangformat/clangformatconfigwidget.ui index a0a042c273e..b64026f4add 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.ui +++ b/src/plugins/clangformat/clangformatconfigwidget.ui @@ -26,47 +26,6 @@ 8 - - - - - - Formatting mode: - - - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - Format while typing - - - - - - - Format edited code on file save - - - diff --git a/src/plugins/clangformat/clangformatglobalconfigwidget.cpp b/src/plugins/clangformat/clangformatglobalconfigwidget.cpp new file mode 100644 index 00000000000..91030cb3d86 --- /dev/null +++ b/src/plugins/clangformat/clangformatglobalconfigwidget.cpp @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "clangformatglobalconfigwidget.h" + +#include "clangformatconstants.h" +#include "clangformatsettings.h" + +#include "ui_clangformatglobalconfigwidget.h" + +#include + +#include + +using namespace ProjectExplorer; + +namespace ClangFormat { + +ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(ProjectExplorer::Project *project, + QWidget *parent) + : CppCodeStyleWidget(parent) + , m_ui(std::make_unique()) +{ + m_ui->setupUi(this); + + initCheckBoxes(); + initIndentationOrFormattingCombobox(); + + if (project) { + m_ui->settingsGroupBox->hide(); + return; + } + m_ui->settingsGroupBox->show(); +} + +ClangFormatGlobalConfigWidget::~ClangFormatGlobalConfigWidget() = default; + +void ClangFormatGlobalConfigWidget::initCheckBoxes() +{ + auto setEnableCheckBoxes = [this](int index) { + bool isFormatting = index == static_cast(ClangFormatSettings::Mode::Formatting); + + m_ui->formatOnSave->setEnabled(isFormatting); + m_ui->formatWhileTyping->setEnabled(isFormatting); + }; + setEnableCheckBoxes(m_ui->indentingOrFormatting->currentIndex()); + connect(m_ui->indentingOrFormatting, &QComboBox::currentIndexChanged, + this, setEnableCheckBoxes); + + m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave()); + m_ui->formatWhileTyping->setChecked(ClangFormatSettings::instance().formatWhileTyping()); +} + +void ClangFormatGlobalConfigWidget::initIndentationOrFormattingCombobox() +{ + m_ui->indentingOrFormatting->insertItem(static_cast(ClangFormatSettings::Mode::Indenting), + tr("Indenting only")); + m_ui->indentingOrFormatting->insertItem(static_cast(ClangFormatSettings::Mode::Formatting), + tr("Full formatting")); + m_ui->indentingOrFormatting->insertItem(static_cast(ClangFormatSettings::Mode::Disable), + tr("Disable")); + + m_ui->indentingOrFormatting->setCurrentIndex( + static_cast(ClangFormatSettings::instance().mode())); +} + + +void ClangFormatGlobalConfigWidget::apply() +{ + ClangFormatSettings &settings = ClangFormatSettings::instance(); + settings.setFormatOnSave(m_ui->formatOnSave->isChecked()); + settings.setFormatWhileTyping(m_ui->formatWhileTyping->isChecked()); + settings.setMode(static_cast(m_ui->indentingOrFormatting->currentIndex())); + settings.write(); +} + +} // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatglobalconfigwidget.h b/src/plugins/clangformat/clangformatglobalconfigwidget.h new file mode 100644 index 00000000000..898a6cfa00b --- /dev/null +++ b/src/plugins/clangformat/clangformatglobalconfigwidget.h @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include + +#include + +namespace ProjectExplorer { class Project; } + +namespace ClangFormat { + +namespace Ui { class ClangFormatGlobalConfigWidget; } + +class ClangFormatGlobalConfigWidget : public CppEditor::CppCodeStyleWidget +{ + Q_OBJECT + +public: + explicit ClangFormatGlobalConfigWidget(ProjectExplorer::Project *project = nullptr, + QWidget *parent = nullptr); + ~ClangFormatGlobalConfigWidget() override; + void apply() override; + +private: + void initCheckBoxes(); + void initIndentationOrFormattingCombobox(); + + std::unique_ptr m_ui; +}; + +} // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatglobalconfigwidget.ui b/src/plugins/clangformat/clangformatglobalconfigwidget.ui new file mode 100644 index 00000000000..f4beb6ecd05 --- /dev/null +++ b/src/plugins/clangformat/clangformatglobalconfigwidget.ui @@ -0,0 +1,89 @@ + + + ClangFormat::ClangFormatGlobalConfigWidget + + + + 0 + 0 + 489 + 145 + + + + + + + + 8 + + + 8 + + + 8 + + + 8 + + + + + Qt::LeftToRight + + + ClangFormat global setting: + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + + + + + Formatting mode: + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + Format while typing + + + + + + + Format edited code on file save + + + + + + + + + + + diff --git a/src/plugins/clangformat/clangformatplugin.cpp b/src/plugins/clangformat/clangformatplugin.cpp index 0b2c2fd5a6f..560c98eee6f 100644 --- a/src/plugins/clangformat/clangformatplugin.cpp +++ b/src/plugins/clangformat/clangformatplugin.cpp @@ -26,6 +26,7 @@ #include "clangformatplugin.h" #include "clangformatconfigwidget.h" +#include "clangformatglobalconfigwidget.h" #include "clangformatconstants.h" #include "clangformatindenter.h" #include "clangformatsettings.h" @@ -95,6 +96,12 @@ public: { return {new ClangFormatConfigWidget(codeStyle, project, parent), tr("ClangFormat")}; } + + TextEditor::CodeStyleEditorWidget *createAdditionalGlobalSettings( + ProjectExplorer::Project *project, QWidget *parent) + { + return new ClangFormatGlobalConfigWidget(project, parent); + } }; static void replaceCppCodeStyle() diff --git a/src/plugins/cppeditor/cppcodestylesettingspage.cpp b/src/plugins/cppeditor/cppcodestylesettingspage.cpp index d8737a76ec8..9102263bf73 100644 --- a/src/plugins/cppeditor/cppcodestylesettingspage.cpp +++ b/src/plugins/cppeditor/cppcodestylesettingspage.cpp @@ -349,7 +349,8 @@ void CppCodeStylePreferencesWidget::addTab(CppCodeStyleWidget *page, QString tab if (!page) return; - m_ui->categoryTab->addTab(page, tabName); + m_ui->categoryTab->insertTab(0, page, tabName); + m_ui->categoryTab->setCurrentIndex(0); connect(page, &CppEditor::CppCodeStyleWidget::codeStyleSettingsChanged, this, [this](const CppEditor::CppCodeStyleSettings &settings) { diff --git a/src/plugins/texteditor/codestyleeditor.cpp b/src/plugins/texteditor/codestyleeditor.cpp index f51d32e2ab6..58cd3ecc2be 100644 --- a/src/plugins/texteditor/codestyleeditor.cpp +++ b/src/plugins/texteditor/codestyleeditor.cpp @@ -66,6 +66,10 @@ CodeStyleEditor::CodeStyleEditor(ICodeStylePreferencesFactory *factory, font.setItalic(true); label->setFont(font); label->setWordWrap(true); + m_additionalGlobalSettingsWidget = factory->createAdditionalGlobalSettings(project, parent); + if (m_additionalGlobalSettingsWidget) + m_layout->addWidget(m_additionalGlobalSettingsWidget); + m_layout->addWidget(selector); m_layout->addWidget(m_preview); m_layout->addWidget(label); @@ -98,3 +102,11 @@ void CodeStyleEditor::updatePreview() } tc.endEditBlock(); } + +void CodeStyleEditor::apply() +{ + if (!m_additionalGlobalSettingsWidget) + return; + + m_additionalGlobalSettingsWidget->apply(); +} diff --git a/src/plugins/texteditor/codestyleeditor.h b/src/plugins/texteditor/codestyleeditor.h index 688672b4896..45564ff50eb 100644 --- a/src/plugins/texteditor/codestyleeditor.h +++ b/src/plugins/texteditor/codestyleeditor.h @@ -49,6 +49,7 @@ public: ProjectExplorer::Project *project = nullptr, QWidget *parent = nullptr); + void apply() override; private: void updatePreview(); @@ -56,6 +57,7 @@ private: ICodeStylePreferencesFactory *m_factory; ICodeStylePreferences *m_codeStyle; SnippetEditorWidget *m_preview; + CodeStyleEditorWidget *m_additionalGlobalSettingsWidget; }; } // namespace TextEditor diff --git a/src/plugins/texteditor/icodestylepreferencesfactory.cpp b/src/plugins/texteditor/icodestylepreferencesfactory.cpp index 89eb961b131..14a94afc124 100644 --- a/src/plugins/texteditor/icodestylepreferencesfactory.cpp +++ b/src/plugins/texteditor/icodestylepreferencesfactory.cpp @@ -38,3 +38,9 @@ CodeStyleEditorWidget *ICodeStylePreferencesFactory::createCodeStyleEditor( { return new CodeStyleEditor(this, codeStyle, project, parent); } + +CodeStyleEditorWidget *ICodeStylePreferencesFactory::createAdditionalGlobalSettings( + ProjectExplorer::Project *, QWidget *) +{ + return nullptr; +} diff --git a/src/plugins/texteditor/icodestylepreferencesfactory.h b/src/plugins/texteditor/icodestylepreferencesfactory.h index c3d18b5514a..c10cb2239eb 100644 --- a/src/plugins/texteditor/icodestylepreferencesfactory.h +++ b/src/plugins/texteditor/icodestylepreferencesfactory.h @@ -61,6 +61,8 @@ public: virtual CodeStyleEditorWidget *createCodeStyleEditor(ICodeStylePreferences *codeStyle, ProjectExplorer::Project *project = nullptr, QWidget *parent = nullptr); + virtual CodeStyleEditorWidget *createAdditionalGlobalSettings( + ProjectExplorer::Project *project = nullptr, QWidget *parent = nullptr); virtual Utils::Id languageId() = 0; virtual QString displayName() = 0; virtual ICodeStylePreferences *createCodeStyle() const = 0;