From 47b5613c532f9f38f3d79e5389d358a734a59828 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Thu, 17 Nov 2022 09:16:19 +0100 Subject: [PATCH] CppCodeStyleSettingsPage: Use Layouting Change-Id: Ied8991d32d5799af944c94a65ff3e85f3f454b26 Reviewed-by: Alessandro Portale Reviewed-by: --- src/plugins/cppeditor/CMakeLists.txt | 2 +- .../cppeditor/cppcodestylesettingspage.cpp | 416 +++++++++++----- .../cppeditor/cppcodestylesettingspage.h | 7 +- .../cppeditor/cppcodestylesettingspage.ui | 455 ------------------ src/plugins/cppeditor/cppcodestylesnippets.h | 332 ++++++------- src/plugins/cppeditor/cppeditor.qbs | 1 - 6 files changed, 476 insertions(+), 737 deletions(-) delete mode 100644 src/plugins/cppeditor/cppcodestylesettingspage.ui diff --git a/src/plugins/cppeditor/CMakeLists.txt b/src/plugins/cppeditor/CMakeLists.txt index 48c52c456cd..fea990b4829 100644 --- a/src/plugins/cppeditor/CMakeLists.txt +++ b/src/plugins/cppeditor/CMakeLists.txt @@ -28,7 +28,7 @@ add_qtc_plugin(CppEditor cppcodestylepreferences.cpp cppcodestylepreferences.h cppcodestylepreferencesfactory.cpp cppcodestylepreferencesfactory.h cppcodestylesettings.cpp cppcodestylesettings.h - cppcodestylesettingspage.cpp cppcodestylesettingspage.h cppcodestylesettingspage.ui + cppcodestylesettingspage.cpp cppcodestylesettingspage.h cppcodestylesnippets.h cppcompletionassist.cpp cppcompletionassist.h cppcompletionassistprocessor.cpp cppcompletionassistprocessor.h diff --git a/src/plugins/cppeditor/cppcodestylesettingspage.cpp b/src/plugins/cppeditor/cppcodestylesettingspage.cpp index 225f8fcfdb3..37cdc81c7f6 100644 --- a/src/plugins/cppeditor/cppcodestylesettingspage.cpp +++ b/src/plugins/cppeditor/cppcodestylesettingspage.cpp @@ -3,23 +3,26 @@ #include "cppcodestylesettingspage.h" +#include "cppcodeformatter.h" #include "cppcodestylepreferences.h" #include "cppcodestylesnippets.h" #include "cppeditorconstants.h" #include "cpppointerdeclarationformatter.h" -#include "cppqtstyleindenter.h" #include "cpptoolssettings.h" -#include #include #include #include +#include #include #include -#include -#include #include +#include +#include +#include +#include #include +#include #include #include @@ -27,8 +30,9 @@ #include +#include +#include #include -#include using namespace TextEditor; @@ -74,74 +78,263 @@ static void applyRefactorings(QTextDocument *textDocument, TextEditorWidget *edi // ------------------ CppCodeStyleSettingsWidget +class CppCodeStylePreferencesWidgetPrivate +{ + Q_DECLARE_TR_FUNCTIONS(CppEditor::Internal::CppCodeStyleSettingsPage) + +public: + CppCodeStylePreferencesWidgetPrivate(CppCodeStylePreferencesWidget *widget) + : q(widget) + , m_indentAccessSpecifiers(createCheckBox(tr("\"public\", \"protected\" and\n" + "\"private\" within class body"))) + , m_indentDeclarationsRelativeToAccessSpecifiers( + createCheckBox(tr("Declarations relative to \"public\",\n" + "\"protected\" and \"private\""))) + , m_indentFunctionBody(createCheckBox(tr("Statements within function body"))) + , m_indentBlockBody(createCheckBox(tr("Statements within blocks"))) + , m_indentNamespaceBody(createCheckBox(tr("Declarations within\n" + "\"namespace\" definition"))) + , m_indentClassBraces(createCheckBox(tr("Class declarations"))) + , m_indentNamespaceBraces(createCheckBox(tr("Namespace declarations"))) + , m_indentEnumBraces(createCheckBox(tr("Enum declarations"))) + , m_indentFunctionBraces(createCheckBox(tr("Function declarations"))) + , m_indentBlockBraces(createCheckBox(tr("Blocks"))) + , m_indentSwitchLabels(createCheckBox(tr("\"case\" or \"default\""))) + , m_indentCaseStatements(createCheckBox(tr("Statements relative to\n" + "\"case\" or \"default\""))) + , m_indentCaseBlocks(createCheckBox(tr("Blocks relative to\n" + "\"case\" or \"default\""))) + , m_indentCaseBreak(createCheckBox(tr("\"break\" statement relative to\n" + "\"case\" or \"default\""))) + , m_alignAssignments(createCheckBox(tr("Align after assignments"), + tr("\n" + "Enables alignment to tokens after =, += etc. When the option is disabled, regular continuation line indentation will be used.
\n" + "
\n" + "With alignment:\n" + "
\n"
+                                               "a = a +\n"
+                                               "    b\n"
+                                               "
\n" + "Without alignment:\n" + "
\n"
+                                               "a = a +\n"
+                                               "        b\n"
+                                               "
\n" + ""))) + , m_extraPaddingConditions(createCheckBox(tr("Add extra padding to conditions\n" + "if they would align to the next line"), + tr("\n" + "Adds an extra level of indentation to multiline conditions in the switch, if, while and foreach statements if they would otherwise have the same or less indentation than a nested statement.\n" + "\n" + "For four-spaces indentation only if statement conditions are affected. Without extra padding:\n" + "
\n"
+                                                     "if (a &&\n"
+                                                     "    b)\n"
+                                                     "    c;\n"
+                                                     "
\n" + "With extra padding:\n" + "
\n"
+                                                     "if (a &&\n"
+                                                     "        b)\n"
+                                                     "    c;\n"
+                                                     "
\n" + ""))) + , m_bindStarToIdentifier(createCheckBox(tr("Identifier"), + tr("This does not apply to the star and reference symbol in pointer/reference to functions and arrays, e.g.:\n" + "
   int (&rf)() = ...;\n"
+                                                   "   int (*pf)() = ...;\n"
+                                                   "\n"
+                                                   "   int (&ra)[2] = ...;\n"
+                                                   "   int (*pa)[2] = ...;\n"
+                                                   "\n"
+                                                   "
"))) + , m_bindStarToTypeName(createCheckBox(tr("Type name"))) + , m_bindStarToLeftSpecifier(createCheckBox(tr("Left const/volatile"))) + , m_bindStarToRightSpecifier(createCheckBox(tr("Right const/volatile"), + tr("This does not apply to references."))) + , m_categoryTab(new QTabWidget) + , m_tabSettingsWidget(new TabSettingsWidget) + { + m_categoryTab->setProperty("_q_custom_style_disabled", true); + + QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); + sizePolicy.setHorizontalStretch(0); + sizePolicy.setVerticalStretch(0); + sizePolicy.setHeightForWidth(m_tabSettingsWidget->sizePolicy().hasHeightForWidth()); + m_tabSettingsWidget->setSizePolicy(sizePolicy); + m_tabSettingsWidget->setFocusPolicy(Qt::TabFocus); + QObject::connect(m_tabSettingsWidget, &TabSettingsWidget::settingsChanged, + q, &CppCodeStylePreferencesWidget::slotTabSettingsChanged); + + using namespace Utils::Layouting; + + QWidget *generalTab = new QWidget; + Row { + Column { + m_tabSettingsWidget, + st + }, + createPreview(0) + }.attachTo(generalTab); + m_categoryTab->addTab(generalTab, tr("General")); + m_controllers.append(m_tabSettingsWidget); + + QWidget *contentTab = new QWidget; + Group contentGroup { + title(tr("Indent")), + Column { + m_indentAccessSpecifiers, + m_indentDeclarationsRelativeToAccessSpecifiers, + m_indentFunctionBody, + m_indentBlockBody, + m_indentNamespaceBody, + st + } + }; + Row { + contentGroup, + createPreview(1) + }.attachTo(contentTab); + m_categoryTab->addTab(contentTab, tr("Content")); + m_controllers.append(contentGroup.widget); + + QWidget *bracesTab = new QWidget; + Group bracesGroup { + title(tr("Indent Braces")), + Column { + m_indentClassBraces, + m_indentNamespaceBraces, + m_indentEnumBraces, + m_indentFunctionBraces, + m_indentBlockBraces, + st + } + }; + Row { + bracesGroup, + createPreview(2) + }.attachTo(bracesTab); + m_categoryTab->addTab(bracesTab, tr("Braces")); + m_controllers.append(bracesGroup.widget); + + QWidget *switchTab = new QWidget; + Group switchGroup { + title(tr("Indent within \"switch\"")), + Column { + m_indentSwitchLabels, + m_indentCaseStatements, + m_indentCaseBlocks, + m_indentCaseBreak, + st + } + }; + Row { + switchGroup, + createPreview(3) + }.attachTo(switchTab); + m_categoryTab->addTab(switchTab, tr("\"switch\"")); + m_controllers.append(switchGroup.widget); + + QWidget *alignmentTab = new QWidget; + Group alignmentGroup { + title(tr("Align")), + Column { + m_alignAssignments, + m_extraPaddingConditions, + st + } + }; + Row { + alignmentGroup, + createPreview(4) + }.attachTo(alignmentTab); + m_categoryTab->addTab(alignmentTab, tr("Alignment")); + m_controllers.append(alignmentGroup.widget); + + QWidget *typesTab = new QWidget; + Group typesGroup { + title(tr("Bind '*' and '&&' in types/declarations to")), + Column { + m_bindStarToIdentifier, + m_bindStarToTypeName, + m_bindStarToLeftSpecifier, + m_bindStarToRightSpecifier, + st + } + }; + Row { + typesGroup, + createPreview(5) + }.attachTo(typesTab); + m_categoryTab->addTab(typesTab, tr("Pointers and References")); + m_controllers.append(typesGroup.widget); + + Row { m_categoryTab }.attachTo(q); + } + + QCheckBox *createCheckBox(const QString &text, const QString &toolTip = {}) + { + QCheckBox *checkBox = new QCheckBox(text); + checkBox->setToolTip(toolTip); + QObject::connect(checkBox, &QCheckBox::toggled, + q, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); + return checkBox; + } + + SnippetEditorWidget *createPreview(int i) + { + SnippetEditorWidget *editor = new SnippetEditorWidget; + editor->setPlainText(QLatin1String(Constants::DEFAULT_CODE_STYLE_SNIPPETS[i])); + m_previews.append(editor); + return editor; + } + + CppCodeStylePreferencesWidget *q = nullptr; + + QCheckBox *m_indentAccessSpecifiers = nullptr; + QCheckBox *m_indentDeclarationsRelativeToAccessSpecifiers = nullptr; + QCheckBox *m_indentFunctionBody = nullptr; + QCheckBox *m_indentBlockBody = nullptr; + QCheckBox *m_indentNamespaceBody = nullptr; + QCheckBox *m_indentClassBraces = nullptr; + QCheckBox *m_indentNamespaceBraces = nullptr; + QCheckBox *m_indentEnumBraces = nullptr; + QCheckBox *m_indentFunctionBraces = nullptr; + QCheckBox *m_indentBlockBraces = nullptr; + QCheckBox *m_indentSwitchLabels = nullptr; + QCheckBox *m_indentCaseStatements = nullptr; + QCheckBox *m_indentCaseBlocks = nullptr; + QCheckBox *m_indentCaseBreak = nullptr; + QCheckBox *m_alignAssignments = nullptr; + QCheckBox *m_extraPaddingConditions = nullptr; + QCheckBox *m_bindStarToIdentifier = nullptr; + QCheckBox *m_bindStarToTypeName = nullptr; + QCheckBox *m_bindStarToLeftSpecifier = nullptr; + QCheckBox *m_bindStarToRightSpecifier = nullptr; + + QList m_previews; + QList m_controllers; + + QTabWidget *m_categoryTab = nullptr; + TabSettingsWidget *m_tabSettingsWidget = nullptr; +}; + CppCodeStylePreferencesWidget::CppCodeStylePreferencesWidget(QWidget *parent) : TextEditor::CodeStyleEditorWidget(parent), - m_ui(new Ui::CppCodeStyleSettingsPage) + d(new CppCodeStylePreferencesWidgetPrivate(this)) { - m_ui->setupUi(this); - m_ui->categoryTab->setProperty("_q_custom_style_disabled", true); - - m_previews << m_ui->previewTextEditGeneral << m_ui->previewTextEditContent - << m_ui->previewTextEditBraces << m_ui->previewTextEditSwitch - << m_ui->previewTextEditPadding << m_ui->previewTextEditPointerReferences; - for (int i = 0; i < m_previews.size(); ++i) - m_previews[i]->setPlainText(QLatin1String(Constants::DEFAULT_CODE_STYLE_SNIPPETS[i])); - decorateEditors(TextEditorSettings::fontSettings()); connect(TextEditorSettings::instance(), &TextEditorSettings::fontSettingsChanged, this, &CppCodeStylePreferencesWidget::decorateEditors); setVisualizeWhitespace(true); - connect(m_ui->tabSettingsWidget, &TabSettingsWidget::settingsChanged, - this, &CppCodeStylePreferencesWidget::slotTabSettingsChanged); - connect(m_ui->indentBlockBraces, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->indentBlockBody, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->indentClassBraces, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->indentNamespaceBraces, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->indentEnumBraces, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->indentNamespaceBody, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->indentSwitchLabels, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->indentCaseStatements, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->indentCaseBlocks, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->indentCaseBreak, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->indentAccessSpecifiers, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->indentDeclarationsRelativeToAccessSpecifiers, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->indentFunctionBody, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->indentFunctionBraces, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->extraPaddingConditions, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->alignAssignments, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->bindStarToIdentifier, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->bindStarToTypeName, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->bindStarToLeftSpecifier, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - connect(m_ui->bindStarToRightSpecifier, &QCheckBox::toggled, - this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); - - m_ui->categoryTab->setCurrentIndex(0); +// m_ui->categoryTab->setCurrentIndex(0); } CppCodeStylePreferencesWidget::~CppCodeStylePreferencesWidget() { - delete m_ui; + delete d; } void CppCodeStylePreferencesWidget::setCodeStyle(CppCodeStylePreferences *codeStylePreferences) @@ -172,64 +365,64 @@ CppCodeStyleSettings CppCodeStylePreferencesWidget::cppCodeStyleSettings() const { CppCodeStyleSettings set; - set.indentBlockBraces = m_ui->indentBlockBraces->isChecked(); - set.indentBlockBody = m_ui->indentBlockBody->isChecked(); - set.indentClassBraces = m_ui->indentClassBraces->isChecked(); - set.indentEnumBraces = m_ui->indentEnumBraces->isChecked(); - set.indentNamespaceBraces = m_ui->indentNamespaceBraces->isChecked(); - set.indentNamespaceBody = m_ui->indentNamespaceBody->isChecked(); - set.indentAccessSpecifiers = m_ui->indentAccessSpecifiers->isChecked(); - set.indentDeclarationsRelativeToAccessSpecifiers = m_ui->indentDeclarationsRelativeToAccessSpecifiers->isChecked(); - set.indentFunctionBody = m_ui->indentFunctionBody->isChecked(); - set.indentFunctionBraces = m_ui->indentFunctionBraces->isChecked(); - set.indentSwitchLabels = m_ui->indentSwitchLabels->isChecked(); - set.indentStatementsRelativeToSwitchLabels = m_ui->indentCaseStatements->isChecked(); - set.indentBlocksRelativeToSwitchLabels = m_ui->indentCaseBlocks->isChecked(); - set.indentControlFlowRelativeToSwitchLabels = m_ui->indentCaseBreak->isChecked(); - set.bindStarToIdentifier = m_ui->bindStarToIdentifier->isChecked(); - set.bindStarToTypeName = m_ui->bindStarToTypeName->isChecked(); - set.bindStarToLeftSpecifier = m_ui->bindStarToLeftSpecifier->isChecked(); - set.bindStarToRightSpecifier = m_ui->bindStarToRightSpecifier->isChecked(); - set.extraPaddingForConditionsIfConfusingAlign = m_ui->extraPaddingConditions->isChecked(); - set.alignAssignments = m_ui->alignAssignments->isChecked(); + set.indentBlockBraces = d->m_indentBlockBraces->isChecked(); + set.indentBlockBody = d->m_indentBlockBody->isChecked(); + set.indentClassBraces = d->m_indentClassBraces->isChecked(); + set.indentEnumBraces = d->m_indentEnumBraces->isChecked(); + set.indentNamespaceBraces = d->m_indentNamespaceBraces->isChecked(); + set.indentNamespaceBody = d->m_indentNamespaceBody->isChecked(); + set.indentAccessSpecifiers = d->m_indentAccessSpecifiers->isChecked(); + set.indentDeclarationsRelativeToAccessSpecifiers = d->m_indentDeclarationsRelativeToAccessSpecifiers->isChecked(); + set.indentFunctionBody = d->m_indentFunctionBody->isChecked(); + set.indentFunctionBraces = d->m_indentFunctionBraces->isChecked(); + set.indentSwitchLabels = d->m_indentSwitchLabels->isChecked(); + set.indentStatementsRelativeToSwitchLabels = d->m_indentCaseStatements->isChecked(); + set.indentBlocksRelativeToSwitchLabels = d->m_indentCaseBlocks->isChecked(); + set.indentControlFlowRelativeToSwitchLabels = d->m_indentCaseBreak->isChecked(); + set.bindStarToIdentifier = d->m_bindStarToIdentifier->isChecked(); + set.bindStarToTypeName = d->m_bindStarToTypeName->isChecked(); + set.bindStarToLeftSpecifier = d->m_bindStarToLeftSpecifier->isChecked(); + set.bindStarToRightSpecifier = d->m_bindStarToRightSpecifier->isChecked(); + set.extraPaddingForConditionsIfConfusingAlign = d->m_extraPaddingConditions->isChecked(); + set.alignAssignments = d->m_alignAssignments->isChecked(); return set; } void CppCodeStylePreferencesWidget::setTabSettings(const TabSettings &settings) { - m_ui->tabSettingsWidget->setTabSettings(settings); + d->m_tabSettingsWidget->setTabSettings(settings); } TextEditor::TabSettings CppCodeStylePreferencesWidget::tabSettings() const { - return m_ui->tabSettingsWidget->tabSettings(); + return d->m_tabSettingsWidget->tabSettings(); } void CppCodeStylePreferencesWidget::setCodeStyleSettings(const CppCodeStyleSettings &s, bool preview) { const bool wasBlocked = m_blockUpdates; m_blockUpdates = true; - m_ui->indentBlockBraces->setChecked(s.indentBlockBraces); - m_ui->indentBlockBody->setChecked(s.indentBlockBody); - m_ui->indentClassBraces->setChecked(s.indentClassBraces); - m_ui->indentEnumBraces->setChecked(s.indentEnumBraces); - m_ui->indentNamespaceBraces->setChecked(s.indentNamespaceBraces); - m_ui->indentNamespaceBody->setChecked(s.indentNamespaceBody); - m_ui->indentAccessSpecifiers->setChecked(s.indentAccessSpecifiers); - m_ui->indentDeclarationsRelativeToAccessSpecifiers->setChecked(s.indentDeclarationsRelativeToAccessSpecifiers); - m_ui->indentFunctionBody->setChecked(s.indentFunctionBody); - m_ui->indentFunctionBraces->setChecked(s.indentFunctionBraces); - m_ui->indentSwitchLabels->setChecked(s.indentSwitchLabels); - m_ui->indentCaseStatements->setChecked(s.indentStatementsRelativeToSwitchLabels); - m_ui->indentCaseBlocks->setChecked(s.indentBlocksRelativeToSwitchLabels); - m_ui->indentCaseBreak->setChecked(s.indentControlFlowRelativeToSwitchLabels); - m_ui->bindStarToIdentifier->setChecked(s.bindStarToIdentifier); - m_ui->bindStarToTypeName->setChecked(s.bindStarToTypeName); - m_ui->bindStarToLeftSpecifier->setChecked(s.bindStarToLeftSpecifier); - m_ui->bindStarToRightSpecifier->setChecked(s.bindStarToRightSpecifier); - m_ui->extraPaddingConditions->setChecked(s.extraPaddingForConditionsIfConfusingAlign); - m_ui->alignAssignments->setChecked(s.alignAssignments); + d->m_indentBlockBraces->setChecked(s.indentBlockBraces); + d->m_indentBlockBody->setChecked(s.indentBlockBody); + d->m_indentClassBraces->setChecked(s.indentClassBraces); + d->m_indentEnumBraces->setChecked(s.indentEnumBraces); + d->m_indentNamespaceBraces->setChecked(s.indentNamespaceBraces); + d->m_indentNamespaceBody->setChecked(s.indentNamespaceBody); + d->m_indentAccessSpecifiers->setChecked(s.indentAccessSpecifiers); + d->m_indentDeclarationsRelativeToAccessSpecifiers->setChecked(s.indentDeclarationsRelativeToAccessSpecifiers); + d->m_indentFunctionBody->setChecked(s.indentFunctionBody); + d->m_indentFunctionBraces->setChecked(s.indentFunctionBraces); + d->m_indentSwitchLabels->setChecked(s.indentSwitchLabels); + d->m_indentCaseStatements->setChecked(s.indentStatementsRelativeToSwitchLabels); + d->m_indentCaseBlocks->setChecked(s.indentBlocksRelativeToSwitchLabels); + d->m_indentCaseBreak->setChecked(s.indentControlFlowRelativeToSwitchLabels); + d->m_bindStarToIdentifier->setChecked(s.bindStarToIdentifier); + d->m_bindStarToTypeName->setChecked(s.bindStarToTypeName); + d->m_bindStarToLeftSpecifier->setChecked(s.bindStarToLeftSpecifier); + d->m_bindStarToRightSpecifier->setChecked(s.bindStarToRightSpecifier); + d->m_extraPaddingConditions->setChecked(s.extraPaddingForConditionsIfConfusingAlign); + d->m_alignAssignments->setChecked(s.alignAssignments); m_blockUpdates = wasBlocked; if (preview) updatePreview(); @@ -238,12 +431,9 @@ void CppCodeStylePreferencesWidget::setCodeStyleSettings(const CppCodeStyleSetti void CppCodeStylePreferencesWidget::slotCurrentPreferencesChanged(ICodeStylePreferences *preferences, bool preview) { const bool enable = !preferences->isReadOnly(); - m_ui->tabSettingsWidget->setEnabled(enable); - m_ui->contentGroupBox->setEnabled(enable); - m_ui->bracesGroupBox->setEnabled(enable); - m_ui->switchGroupBox->setEnabled(enable); - m_ui->alignmentGroupBox->setEnabled(enable); - m_ui->pointerReferencesGroupBox->setEnabled(enable); + for (QWidget *widget : d->m_controllers) + widget->setEnabled(enable); + if (preview) updatePreview(); } @@ -285,7 +475,7 @@ void CppCodeStylePreferencesWidget::updatePreview() const CppCodeStyleSettings ccss = cppCodeStylePreferences->currentCodeStyleSettings(); const TabSettings ts = cppCodeStylePreferences->currentTabSettings(); QtStyleCodeFormatter formatter(ts, ccss); - for (SnippetEditorWidget *preview : std::as_const(m_previews)) { + for (SnippetEditorWidget *preview : std::as_const(d->m_previews)) { preview->textDocument()->setTabSettings(ts); preview->setCodeStyle(cppCodeStylePreferences); @@ -307,7 +497,7 @@ void CppCodeStylePreferencesWidget::updatePreview() void CppCodeStylePreferencesWidget::decorateEditors(const FontSettings &fontSettings) { - for (SnippetEditorWidget *editor : std::as_const(m_previews)) { + for (SnippetEditorWidget *editor : std::as_const(d->m_previews)) { editor->textDocument()->setFontSettings(fontSettings); SnippetProvider::decorateEditor(editor, CppEditor::Constants::CPP_SNIPPETS_GROUP_ID); } @@ -315,7 +505,7 @@ void CppCodeStylePreferencesWidget::decorateEditors(const FontSettings &fontSett void CppCodeStylePreferencesWidget::setVisualizeWhitespace(bool on) { - for (SnippetEditorWidget *editor : std::as_const(m_previews)) { + for (SnippetEditorWidget *editor : std::as_const(d->m_previews)) { DisplaySettings displaySettings = editor->displaySettings(); displaySettings.m_visualizeWhitespace = on; editor->setDisplaySettings(displaySettings); @@ -327,8 +517,8 @@ void CppCodeStylePreferencesWidget::addTab(CppCodeStyleWidget *page, QString tab if (!page) return; - m_ui->categoryTab->insertTab(0, page, tabName); - m_ui->categoryTab->setCurrentIndex(0); + d->m_categoryTab->insertTab(0, page, tabName); + d->m_categoryTab->setCurrentIndex(0); connect(page, &CppEditor::CppCodeStyleWidget::codeStyleSettingsChanged, this, [this](const CppEditor::CppCodeStyleSettings &settings) { diff --git a/src/plugins/cppeditor/cppcodestylesettingspage.h b/src/plugins/cppeditor/cppcodestylesettingspage.h index 844cfc0337e..0cb69163699 100644 --- a/src/plugins/cppeditor/cppcodestylesettingspage.h +++ b/src/plugins/cppeditor/cppcodestylesettingspage.h @@ -4,7 +4,6 @@ #pragma once #include "cppcodestylesettings.h" -#include "cppcodeformatter.h" #include #include @@ -42,7 +41,7 @@ signals: namespace Internal { -namespace Ui { class CppCodeStyleSettingsPage; } +class CppCodeStylePreferencesWidgetPrivate; class CppCodeStylePreferencesWidget : public TextEditor::CodeStyleEditorWidget { @@ -70,9 +69,9 @@ private: CppCodeStyleSettings cppCodeStyleSettings() const; CppCodeStylePreferences *m_preferences = nullptr; - Ui::CppCodeStyleSettingsPage *m_ui; - QList m_previews; + CppCodeStylePreferencesWidgetPrivate *d = nullptr; bool m_blockUpdates = false; + friend class CppCodeStylePreferencesWidgetPrivate; signals: void codeStyleSettingsChanged(const CppEditor::CppCodeStyleSettings &); void tabSettingsChanged(const TextEditor::TabSettings &); diff --git a/src/plugins/cppeditor/cppcodestylesettingspage.ui b/src/plugins/cppeditor/cppcodestylesettingspage.ui deleted file mode 100644 index cb5a75d6a34..00000000000 --- a/src/plugins/cppeditor/cppcodestylesettingspage.ui +++ /dev/null @@ -1,455 +0,0 @@ - - - CppEditor::Internal::CppCodeStyleSettingsPage - - - - 0 - 0 - 463 - 317 - - - - - - - - - - 0 - - - - General - - - - - - - 0 - 0 - - - - Qt::TabFocus - - - - - - - - - - - - - - Qt::Vertical - - - - 20 - 347 - - - - - - - - - Content - - - - - - Indent - - - - - - "public", "protected" and -"private" within class body - - - - - - - Declarations relative to "public", -"protected" and "private" - - - - - - - Statements within function body - - - - - - - Statements within blocks - - - - - - - Declarations within -"namespace" definition - - - - - - - Qt::Vertical - - - - 17 - 114 - - - - - - - - - - - - - - - - - - - Braces - - - - - - Indent Braces - - - - - - Class declarations - - - - - - - Namespace declarations - - - - - - - Enum declarations - - - - - - - Function declarations - - - - - - - Blocks - - - - - - - Qt::Vertical - - - - 20 - 195 - - - - - - - - - - - - - - - - - - - "switch" - - - - - - Indent within "switch" - - - - - - "case" or "default" - - - - - - - Statements relative to -"case" or "default" - - - - - - - Blocks relative to -"case" or "default" - - - - - - - "break" statement relative to -"case" or "default" - - - - - - - Qt::Vertical - - - - 20 - 143 - - - - - - - - - - - - - - - - - - - Alignment - - - - - - Align - - - - - - <html><head/><body> -Enables alignment to tokens after =, += etc. When the option is disabled, regular continuation line indentation will be used.<br> -<br> -With alignment: -<pre> -a = a + - b -</pre> -Without alignment: -<pre> -a = a + - b -</pre> -</body></html> - - - Align after assignments - - - - - - - <html><head/><body> -Adds an extra level of indentation to multiline conditions in the switch, if, while and foreach statements if they would otherwise have the same or less indentation than a nested statement. - -For four-spaces indentation only if statement conditions are affected. Without extra padding: -<pre> -if (a && - b) - c; -</pre> -With extra padding: -<pre> -if (a && - b) - c; -</pre> -</body></html> - - - Add extra padding to conditions -if they would align to the next line - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - - - - - - - - - - - - Pointers and References - - - - - - Bind '*' and '&&' in types/declarations to - - - - - - <html><head/><body>This does not apply to the star and reference symbol in pointer/reference to functions and arrays, e.g.: -<pre> int (&rf)() = ...; - int (*pf)() = ...; - - int (&ra)[2] = ...; - int (*pa)[2] = ...; - -</pre></body></html> - - - Identifier - - - - - - - Type name - - - - - - - Left const/volatile - - - - - - - This does not apply to references. - - - Right const/volatile - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - - - - - - - - - - - - - - - - TextEditor::TabSettingsWidget - QWidget -
texteditor/tabsettingswidget.h
- 1 -
- - TextEditor::SnippetEditorWidget - QPlainTextEdit -
texteditor/snippets/snippeteditor.h
-
-
- - -
diff --git a/src/plugins/cppeditor/cppcodestylesnippets.h b/src/plugins/cppeditor/cppcodestylesnippets.h index e329ffbb40d..ad234f1ee1b 100644 --- a/src/plugins/cppeditor/cppcodestylesnippets.h +++ b/src/plugins/cppeditor/cppcodestylesnippets.h @@ -6,169 +6,175 @@ namespace CppEditor { namespace Constants { -static const char *DEFAULT_CODE_STYLE_SNIPPETS[] - = {"#include \n" - "\n" - "class Complex\n" - " {\n" - "public:\n" - " Complex(double re, double im)\n" - " : _re(re), _im(im)\n" - " {}\n" - " double modulus() const\n" - " {\n" - " return sqrt(_re * _re + _im * _im);\n" - " }\n" - "private:\n" - " double _re;\n" - " double _im;\n" - " };\n" - "\n" - "void bar(int i)\n" - " {\n" - " static int counter = 0;\n" - " counter += i;\n" - " }\n" - "\n" - "namespace Foo\n" - " {\n" - " namespace Bar\n" - " {\n" - " void foo(int a, int b)\n" - " {\n" - " for (int i = 0; i < a; i++)\n" - " {\n" - " if (i < b)\n" - " bar(i);\n" - " else\n" - " {\n" - " bar(i);\n" - " bar(b);\n" - " }\n" - " }\n" - " }\n" - " } // namespace Bar\n" - " } // namespace Foo\n", - "#include \n" - "\n" - "class Complex\n" - " {\n" - "public:\n" - " Complex(double re, double im)\n" - " : _re(re), _im(im)\n" - " {}\n" - " double modulus() const\n" - " {\n" - " return sqrt(_re * _re + _im * _im);\n" - " }\n" - "private:\n" - " double _re;\n" - " double _im;\n" - " };\n" - "\n" - "void bar(int i)\n" - " {\n" - " static int counter = 0;\n" - " counter += i;\n" - " }\n" - "\n" - "namespace Foo\n" - " {\n" - " namespace Bar\n" - " {\n" - " void foo(int a, int b)\n" - " {\n" - " for (int i = 0; i < a; i++)\n" - " {\n" - " if (i < b)\n" - " bar(i);\n" - " else\n" - " {\n" - " bar(i);\n" - " bar(b);\n" - " }\n" - " }\n" - " }\n" - " } // namespace Bar\n" - " } // namespace Foo\n", - "namespace Foo\n" - "{\n" - "namespace Bar\n" - "{\n" - "class FooBar\n" - " {\n" - "public:\n" - " FooBar(int a)\n" - " : _a(a)\n" - " {}\n" - " int calculate() const\n" - " {\n" - " if (a > 10)\n" - " {\n" - " int b = 2 * a;\n" - " return a * b;\n" - " }\n" - " return -a;\n" - " }\n" - "private:\n" - " int _a;\n" - " };\n" - "enum class E\n" - "{\n" - " V1,\n" - " V2,\n" - " V3\n" - "};\n" - "}\n" - "}\n", - "#include \"bar.h\"\n" - "\n" - "int foo(int a)\n" - " {\n" - " switch (a)\n" - " {\n" - " case 1:\n" - " bar(1);\n" - " break;\n" - " case 2:\n" - " {\n" - " bar(2);\n" - " break;\n" - " }\n" - " case 3:\n" - " default:\n" - " bar(3);\n" - " break;\n" - " }\n" - " return 0;\n" - " }\n", - "void foo() {\n" - " if (a &&\n" - " b)\n" - " c;\n" - "\n" - " while (a ||\n" - " b)\n" - " break;\n" - " a = b +\n" - " c;\n" - " myInstance.longMemberName +=\n" - " foo;\n" - " myInstance.longMemberName += bar +\n" - " foo;\n" - "}\n", - "int *foo(const Bar &b1, Bar &&b2, int*, int *&rpi)\n" - "{\n" - " int*pi = 0;\n" - " int*const*const cpcpi = π\n" - " int*const*pcpi = π\n" - " int**const cppi = π\n" - "\n" - " void (*foo)(char *s) = 0;\n" - " int (*bar)[] = 0;\n" - "\n" - " return pi;\n" - "}\n"}; +static const char *DEFAULT_CODE_STYLE_SNIPPETS[] = { +R"==(#include + +class Complex + { +public: + Complex(double re, double im) + : _re(re), _im(im) + {} + double modulus() const + { + return sqrt(_re * _re + _im * _im); + } +private: + double _re; + double _im; + }; + +void bar(int i) + { + static int counter = 0; + counter += i; + } + +namespace Foo + { + namespace Bar + { + void foo(int a, int b) + { + for (int i = 0; i < a; i++) + { + if (i < b) + bar(i); + else + { + bar(i); + bar(b); + } + } + } + } // namespace Bar + } // namespace Foo +)==", +R"==(#include + +class Complex + { +public: + Complex(double re, double im) + : _re(re), _im(im) + {} + double modulus() const + { + return sqrt(_re * _re + _im * _im); + } +private: + double _re; + double _im; + }; + +void bar(int i) + { + static int counter = 0; + counter += i; + } + +namespace Foo + { + namespace Bar + { + void foo(int a, int b) + { + for (int i = 0; i < a; i++) + { + if (i < b) + bar(i); + else + { + bar(i); + bar(b); + } + } + } + } // namespace Bar + } // namespace Foo +)==", +R"==(namespace Foo +{ +namespace Bar +{ +class FooBar + { +public: + FooBar(int a) + : _a(a) + {} + int calculate() const + { + if (a > 10) + { + int b = 2 * a; + return a * b; + } + return -a; + } +private: + int _a; + }; +enum class E +{ + V1, + V2, + V3 +}; +} +} +)==", +R"==(#include "bar.h" + +int foo(int a) + { + switch (a) + { + case 1: + bar(1); + break; + case 2: + { + bar(2); + break; + } + case 3: + default: + bar(3); + break; + } + return 0; + } +)==", +R"==(void foo() { + if (a && + b) + c; + + while (a || + b) + break; + a = b + + c; + myInstance.longMemberName += + foo; + myInstance.longMemberName += bar + + foo; +} +)==", +R"==(int *foo(const Bar &b1, Bar &&b2, int*, int *&rpi) +{ + int*pi = 0; + int*const*const cpcpi = π + int*const*pcpi = π + int**const cppi = π + + void (*foo)(char *s) = 0; + int (*bar)[] = 0; + + return pi; +} +)=="}; } // namespace Constants } // namespace CppEditor diff --git a/src/plugins/cppeditor/cppeditor.qbs b/src/plugins/cppeditor/cppeditor.qbs index 9329e4cc9ca..6539c2b2504 100644 --- a/src/plugins/cppeditor/cppeditor.qbs +++ b/src/plugins/cppeditor/cppeditor.qbs @@ -78,7 +78,6 @@ QtcPlugin { "cppcodestylesettings.h", "cppcodestylesettingspage.cpp", "cppcodestylesettingspage.h", - "cppcodestylesettingspage.ui", "cppcodestylesnippets.h", "cppcompletionassist.cpp", "cppcompletionassist.h",