CodeStyle: Remove edit pop-up

Removed the Edit pop-up on the CodeStyle page.
Now editing is available immediately after opening
CodeStyle tab.
I left a preview on the project CodeStyle page and
added a link to global settings.

ToDo:
In project CodeStyle page replace preview with CodeStyleEditor.
I'm not 100% sure that it is needed. If you have some thoughts
please write a comment.

Change-Id: I31032a97b9668b4f6b06fc6c5c704700fb44ee4f
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2022-08-01 17:28:42 +02:00
parent 4f0ac5678f
commit 74ac09346f
12 changed files with 113 additions and 179 deletions

View File

@@ -41,8 +41,11 @@
#include <cppeditor/cpphighlighter.h>
#include <cppeditor/cppcodestylesettings.h>
#include <cppeditor/cppcodestylesnippets.h>
#include <cppeditor/cpptoolssettings.h>
#include <cppeditor/cppcodestylepreferences.h>
#include <projectexplorer/project.h>
#include <projectexplorer/editorconfiguration.h>
#include <projectexplorer/session.h>
#include <texteditor/displaysettings.h>
@@ -83,19 +86,18 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
, m_project(project)
, m_checks(std::make_unique<Ui::ClangFormatChecksWidget>())
{
resize(489, 305);
m_config = std::make_unique<ClangFormatFile>(filePathToCurrentSettings(codeStyle->currentPreferences()));
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),
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_checksScrollArea->setMaximumWidth(600);
m_checksWidget->setEnabled(!codeStyle->isReadOnly());
FilePath fileName;
@@ -125,6 +127,11 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
initOverrideCheckBox();
connect(codeStyle, &TextEditor::ICodeStylePreferences::currentPreferencesChanged,
this, &ClangFormatConfigWidget::slotCodeStyleChanged);
slotCodeStyleChanged(codeStyle->currentPreferences());
showOrHideWidgets();
fillTable();
updatePreview();
@@ -134,6 +141,21 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
ClangFormatConfigWidget::~ClangFormatConfigWidget() = default;
void ClangFormatConfigWidget::slotCodeStyleChanged(
TextEditor::ICodeStylePreferences *codeStyle)
{
if (!codeStyle)
return;
m_config.reset(new ClangFormatFile(filePathToCurrentSettings(codeStyle)));
m_config->setIsReadOnly(codeStyle->isReadOnly());
m_style = m_config->style();
m_checksWidget->setEnabled(!codeStyle->isReadOnly());
fillTable();
updatePreview();
}
void ClangFormatConfigWidget::initOverrideCheckBox()
{
if (m_project) {
@@ -296,9 +318,15 @@ static void fillComboBoxOrLineEdit(QObject *object, const std::string &text, siz
std::string value = text.substr(valueStart + 1, valueEnd - valueStart - 1);
trim(value);
if (comboBox)
if (comboBox) {
if (comboBox->findText(QString::fromStdString(value)) == -1) {
comboBox->setCurrentIndex(0);
return;
}
comboBox->setCurrentText(QString::fromStdString(value));
else
return;
}
lineEdit->setText(QString::fromStdString(value));
}
@@ -414,7 +442,15 @@ void ClangFormatConfigWidget::apply()
if (!m_checksWidget->isVisible() && !m_checksWidget->isEnabled())
return;
saveChanges(this);
m_style = m_config->style();
}
void ClangFormatConfigWidget::finish()
{
if (!m_checksWidget->isVisible() && !m_checksWidget->isEnabled())
return;
m_config->setStyle(m_style);
}
} // namespace ClangFormat

View File

@@ -27,6 +27,8 @@
#include <cppeditor/cppcodestylesettingspage.h>
#include <clang/Format/Format.h>
#include <QScrollArea>
#include <memory>
@@ -57,6 +59,7 @@ public:
QWidget *parent = nullptr);
~ClangFormatConfigWidget() override;
void apply() override;
void finish() override;
void setCodeStyleSettings(const CppEditor::CppCodeStyleSettings &settings) override;
void setTabSettings(const TextEditor::TabSettings &settings) override;
void synchronize() override;
@@ -67,7 +70,7 @@ private:
bool eventFilter(QObject *object, QEvent *event) override;
void showOrHideWidgets();
void initChecksAndPreview(bool enabled);
void initChecksAndPreview();
void initOverrideCheckBox();
void connectChecks();
@@ -75,6 +78,7 @@ private:
void saveChanges(QObject *sender);
void updatePreview();
void slotCodeStyleChanged(TextEditor::ICodeStylePreferences *currentPreferences);
ProjectExplorer::Project *m_project;
QWidget *m_checksWidget;
@@ -82,6 +86,7 @@ private:
TextEditor::SnippetEditorWidget *m_preview;
std::unique_ptr<ClangFormatFile> m_config;
std::unique_ptr<Ui::ClangFormatChecksWidget> m_checks;
clang::format::FormatStyle m_style;
bool m_disableTableUpdate = false;

View File

@@ -35,9 +35,8 @@
using namespace ClangFormat;
ClangFormatFile::ClangFormatFile(Utils::FilePath filePath, bool isReadOnly)
ClangFormatFile::ClangFormatFile(Utils::FilePath filePath)
: m_filePath(filePath)
, m_isReadOnly(isReadOnly)
{
if (!m_filePath.exists()) {
// create file and folder
@@ -58,7 +57,7 @@ ClangFormatFile::ClangFormatFile(Utils::FilePath filePath, bool isReadOnly)
}
}
clang::format::FormatStyle ClangFormatFile::format() {
clang::format::FormatStyle ClangFormatFile::style() {
return m_style;
}
@@ -78,6 +77,11 @@ bool ClangFormatFile::isReadOnly() const
return m_isReadOnly;
}
void ClangFormatFile::setIsReadOnly(bool isReadOnly)
{
m_isReadOnly = isReadOnly;
}
void ClangFormatFile::resetStyleToQtC()
{
m_style = qtcStyle();

View File

@@ -37,8 +37,8 @@ namespace ClangFormat {
class ClangFormatFile
{
public:
explicit ClangFormatFile(Utils::FilePath file, bool isReadOnly);
clang::format::FormatStyle format();
explicit ClangFormatFile(Utils::FilePath file);
clang::format::FormatStyle style();
Utils::FilePath filePath();
void resetStyleToQtC();
@@ -55,6 +55,7 @@ public:
void fromCppCodeStyleSettings(const CppEditor::CppCodeStyleSettings &settings);
void fromTabSettings(const TextEditor::TabSettings &settings);
bool isReadOnly() const;
void setIsReadOnly(bool isReadOnly);
private:
void saveNewFormat();
@@ -63,7 +64,7 @@ private:
private:
Utils::FilePath m_filePath;
clang::format::FormatStyle m_style;
const bool m_isReadOnly;
bool m_isReadOnly;
};
} // namespace ClangFormat

View File

@@ -259,7 +259,7 @@ void CppCodeStylePreferencesWidget::setCodeStyleSettings(const CppCodeStyleSetti
void CppCodeStylePreferencesWidget::slotCurrentPreferencesChanged(ICodeStylePreferences *preferences, bool preview)
{
const bool enable = !preferences->isReadOnly() && !m_preferences->currentDelegate();
const bool enable = !preferences->isReadOnly();
m_ui->tabSettingsWidget->setEnabled(enable);
m_ui->contentGroupBox->setEnabled(enable);
m_ui->bracesGroupBox->setEnabled(enable);
@@ -369,6 +369,9 @@ void CppCodeStylePreferencesWidget::addTab(CppCodeStyleWidget *page, QString tab
connect(this, &CppCodeStylePreferencesWidget::applyEmitted,
page, &CppCodeStyleWidget::apply);
connect(this, &CppCodeStylePreferencesWidget::finishEmitted,
page, &CppCodeStyleWidget::finish);
page->synchronize();
}
@@ -377,6 +380,11 @@ void CppCodeStylePreferencesWidget::apply()
emit applyEmitted();
}
void CppCodeStylePreferencesWidget::finish()
{
emit finishEmitted();
}
// ------------------ CppCodeStyleSettingsPage
CppCodeStyleSettingsPage::CppCodeStyleSettingsPage()
@@ -431,6 +439,7 @@ void CppCodeStyleSettingsPage::apply()
void CppCodeStyleSettingsPage::finish()
{
m_widget->finish();
delete m_widget;
}

View File

@@ -76,6 +76,7 @@ public:
void setCodeStyle(CppCodeStylePreferences *codeStylePreferences);
void addTab(CppCodeStyleWidget *page, QString tabName);
void apply() override;
void finish() override;
private:
void decorateEditors(const TextEditor::FontSettings &fontSettings);
@@ -98,6 +99,7 @@ signals:
void codeStyleSettingsChanged(const CppEditor::CppCodeStyleSettings &);
void tabSettingsChanged(const TextEditor::TabSettings &);
void applyEmitted();
void finishEmitted();
};

View File

@@ -28,6 +28,8 @@
#include "editorconfiguration.h"
#include "project.h"
#include <cppeditor/cppeditorconstants.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/icodestylepreferencesfactory.h>
#include <texteditor/codestyleeditor.h>
@@ -48,8 +50,8 @@ CodeStyleSettingsWidget::CodeStyleSettingsWidget(Project *project)
auto languageComboBox = new QComboBox(this);
auto stackedWidget = new QStackedWidget(this);
setGlobalSettingsId(CppEditor::Constants::CPP_CODE_STYLE_SETTINGS_ID);
setUseGlobalSettingsCheckBoxVisible(false);
setUseGlobalSettingsLabelVisible(false);
const EditorConfiguration *config = project->editorConfiguration();

View File

@@ -52,12 +52,20 @@ CodeStyleEditor::CodeStyleEditor(ICodeStylePreferencesFactory *factory,
m_layout = new QVBoxLayout(this);
auto selector = new CodeStyleSelectorWidget(factory, project, this);
selector->setCodeStyle(codeStyle);
m_preview = new SnippetEditorWidget(this);
DisplaySettings displaySettings = m_preview->displaySettings();
displaySettings.m_visualizeWhitespace = true;
m_preview->setDisplaySettings(displaySettings);
QString groupId = factory->snippetProviderGroupId();
SnippetProvider::decorateEditor(m_preview, groupId);
m_additionalGlobalSettingsWidget = factory->createAdditionalGlobalSettings(project, parent);
if (m_additionalGlobalSettingsWidget)
m_layout->addWidget(m_additionalGlobalSettingsWidget);
m_layout->addWidget(selector);
if (!project) {
m_widget = factory->createEditor(codeStyle, project, parent);
if (m_widget)
m_layout->addWidget(m_widget);
return;
}
QLabel *label = new QLabel(
tr("Edit preview contents to see how the current settings "
"are applied to custom code snippets. Changes in the preview "
@@ -66,11 +74,14 @@ 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_preview = new SnippetEditorWidget(this);
DisplaySettings displaySettings = m_preview->displaySettings();
displaySettings.m_visualizeWhitespace = true;
m_preview->setDisplaySettings(displaySettings);
QString groupId = factory->snippetProviderGroupId();
SnippetProvider::decorateEditor(m_preview, groupId);
m_layout->addWidget(m_preview);
m_layout->addWidget(label);
connect(codeStyle, &ICodeStylePreferences::currentTabSettingsChanged,
@@ -105,8 +116,15 @@ void CodeStyleEditor::updatePreview()
void CodeStyleEditor::apply()
{
if (!m_additionalGlobalSettingsWidget)
return;
if (m_widget)
m_widget->apply();
if (m_additionalGlobalSettingsWidget)
m_additionalGlobalSettingsWidget->apply();
}
void CodeStyleEditor::finish()
{
if (m_widget)
m_widget->finish();
}

View File

@@ -50,6 +50,7 @@ public:
QWidget *parent = nullptr);
void apply() override;
void finish() override;
private:
void updatePreview();
@@ -58,6 +59,7 @@ private:
ICodeStylePreferences *m_codeStyle;
SnippetEditorWidget *m_preview;
CodeStyleEditorWidget *m_additionalGlobalSettingsWidget;
CodeStyleEditorWidget *m_widget;
};
} // namespace TextEditor

View File

@@ -49,118 +49,6 @@ using namespace TextEditor;
using namespace Utils;
namespace TextEditor {
namespace Internal {
class CodeStyleDialog : public QDialog
{
Q_OBJECT
public:
explicit CodeStyleDialog(ICodeStylePreferencesFactory *factory,
ICodeStylePreferences *codeStyle,
ProjectExplorer::Project *project = nullptr,
QWidget *parent = nullptr);
~CodeStyleDialog() override;
ICodeStylePreferences *codeStyle() const;
private:
void slotCopyClicked();
void slotDisplayNameChanged();
ICodeStylePreferences *m_codeStyle;
QLineEdit *m_lineEdit;
QDialogButtonBox *m_buttons;
QLabel *m_warningLabel = nullptr;
QPushButton *m_copyButton = nullptr;
QString m_originalDisplayName;
};
CodeStyleDialog::CodeStyleDialog(ICodeStylePreferencesFactory *factory,
ICodeStylePreferences *codeStyle,
ProjectExplorer::Project *project,
QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("Edit Code Style"));
auto layout = new QVBoxLayout(this);
QLabel *label = new QLabel(tr("Code style name:"));
m_lineEdit = new QLineEdit(codeStyle->displayName(), this);
auto nameLayout = new QHBoxLayout;
nameLayout->addWidget(label);
nameLayout->addWidget(m_lineEdit);
layout->addLayout(nameLayout);
if (codeStyle->isReadOnly()) {
auto warningLayout = new QHBoxLayout;
m_warningLabel = new QLabel(
tr("You cannot save changes to a built-in code style. "
"Copy it first to create your own version."), this);
QFont font = m_warningLabel->font();
font.setItalic(true);
m_warningLabel->setFont(font);
m_warningLabel->setWordWrap(true);
m_copyButton = new QPushButton(tr("Copy Built-in Code Style"), this);
m_copyButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(m_copyButton, &QAbstractButton::clicked, this, &CodeStyleDialog::slotCopyClicked);
warningLayout->addWidget(m_warningLabel);
warningLayout->addWidget(m_copyButton);
layout->addLayout(warningLayout);
}
m_originalDisplayName = codeStyle->displayName();
m_codeStyle = factory->createCodeStyle();
m_codeStyle->setTabSettings(codeStyle->tabSettings());
m_codeStyle->setValue(codeStyle->value());
m_codeStyle->setId(codeStyle->id());
m_codeStyle->setDisplayName(m_originalDisplayName);
m_codeStyle->setReadOnly(codeStyle->isReadOnly());
CodeStyleEditorWidget *editor = factory->createEditor(m_codeStyle, project, this);
m_buttons = new QDialogButtonBox(
QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
if (codeStyle->isReadOnly()) {
QPushButton *okButton = m_buttons->button(QDialogButtonBox::Ok);
okButton->setEnabled(false);
}
if (editor)
layout->addWidget(editor);
layout->addWidget(m_buttons);
resize(850, 600);
connect(m_lineEdit, &QLineEdit::textChanged, this, &CodeStyleDialog::slotDisplayNameChanged);
connect(m_buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_buttons, &QDialogButtonBox::accepted, editor, &TextEditor::CodeStyleEditorWidget::apply);
connect(m_buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
}
ICodeStylePreferences *CodeStyleDialog::codeStyle() const
{
return m_codeStyle;
}
void CodeStyleDialog::slotCopyClicked()
{
if (m_warningLabel)
m_warningLabel->hide();
if (m_copyButton)
m_copyButton->hide();
QPushButton *okButton = m_buttons->button(QDialogButtonBox::Ok);
okButton->setEnabled(true);
if (m_lineEdit->text() == m_originalDisplayName)
m_lineEdit->setText(tr("%1 (Copy)").arg(m_lineEdit->text()));
m_lineEdit->selectAll();
}
void CodeStyleDialog::slotDisplayNameChanged()
{
m_codeStyle->setDisplayName(m_lineEdit->text());
}
CodeStyleDialog::~CodeStyleDialog()
{
delete m_codeStyle;
}
} // Internal
CodeStyleSelectorWidget::CodeStyleSelectorWidget(ICodeStylePreferencesFactory *factory,
ProjectExplorer::Project *project,
@@ -175,7 +63,6 @@ CodeStyleSelectorWidget::CodeStyleSelectorWidget(ICodeStylePreferencesFactory *f
m_delegateComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
auto copyButton = new QPushButton(tr("Copy..."));
auto editButton = new QPushButton(tr("Edit..."));
m_removeButton = new QPushButton(tr("Remove"));
@@ -192,12 +79,8 @@ CodeStyleSelectorWidget::CodeStyleSelectorWidget(ICodeStylePreferencesFactory *f
tr("Current settings:"),
m_delegateComboBox,
copyButton,
editButton,
m_removeButton,
m_exportButton,
br,
Span(5, Space(1)),
m_importButton
},
@@ -207,8 +90,6 @@ CodeStyleSelectorWidget::CodeStyleSelectorWidget(ICodeStylePreferencesFactory *f
this, &CodeStyleSelectorWidget::slotComboBoxActivated);
connect(copyButton, &QAbstractButton::clicked,
this, &CodeStyleSelectorWidget::slotCopyClicked);
connect(editButton, &QAbstractButton::clicked,
this, &CodeStyleSelectorWidget::slotEditClicked);
connect(m_removeButton, &QAbstractButton::clicked,
this, &CodeStyleSelectorWidget::slotRemoveClicked);
connect(m_importButton, &QAbstractButton::clicked,
@@ -314,30 +195,6 @@ void CodeStyleSelectorWidget::slotCopyClicked()
}
}
void CodeStyleSelectorWidget::slotEditClicked()
{
if (!m_codeStyle)
return;
ICodeStylePreferences *codeStyle = m_codeStyle->currentPreferences();
// check if it's read-only
Internal::CodeStyleDialog dialog(m_factory, codeStyle, m_project, this);
if (dialog.exec() == QDialog::Accepted) {
ICodeStylePreferences *dialogCodeStyle = dialog.codeStyle();
if (codeStyle->isReadOnly()) {
CodeStylePool *codeStylePool = m_codeStyle->delegatingPool();
codeStyle = codeStylePool->cloneCodeStyle(dialogCodeStyle);
if (codeStyle)
m_codeStyle->setCurrentDelegate(codeStyle);
return;
}
codeStyle->setTabSettings(dialogCodeStyle->tabSettings());
codeStyle->setValue(dialogCodeStyle->value());
codeStyle->setDisplayName(dialogCodeStyle->displayName());
}
}
void CodeStyleSelectorWidget::slotRemoveClicked()
{
if (!m_codeStyle)
@@ -457,5 +314,3 @@ QString CodeStyleSelectorWidget::displayName(ICodeStylePreferences *codeStyle) c
}
} // TextEditor
#include "codestyleselectorwidget.moc"

View File

@@ -58,7 +58,6 @@ private:
void slotComboBoxActivated(int index);
void slotCurrentDelegateChanged(ICodeStylePreferences *delegate);
void slotCopyClicked();
void slotEditClicked();
void slotRemoveClicked();
void slotImportClicked();
void slotExportClicked();

View File

@@ -47,6 +47,7 @@ public:
: QWidget(parent)
{}
virtual void apply() {}
virtual void finish() {}
};
class TEXTEDITOR_EXPORT ICodeStylePreferencesFactory