Beautifier: Base Beautifier::GeneralSettings on Core::PagedSettings

This also uses the "Enable auto format on file save" bool directly
for the now-checked group box.

Change-Id: I7f3f392828f6ccfda99fa1d757f49cb9e3b6bc1b
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
hjk
2023-05-10 16:30:11 +02:00
parent 0672314844
commit 7dd27301ab
7 changed files with 75 additions and 242 deletions

View File

@@ -19,7 +19,6 @@ add_qtc_plugin(Beautifier
configurationdialog.cpp configurationdialog.h
configurationeditor.cpp configurationeditor.h
configurationpanel.cpp configurationpanel.h
generaloptionspage.cpp generaloptionspage.h
generalsettings.cpp generalsettings.h
uncrustify/uncrustify.cpp uncrustify/uncrustify.h
uncrustify/uncrustifyconstants.h

View File

@@ -25,8 +25,6 @@ QtcPlugin {
"configurationeditor.h",
"configurationpanel.cpp",
"configurationpanel.h",
"generaloptionspage.cpp",
"generaloptionspage.h",
"generalsettings.cpp",
"generalsettings.h",
]

View File

@@ -5,7 +5,6 @@
#include "beautifierconstants.h"
#include "beautifiertr.h"
#include "generaloptionspage.h"
#include "generalsettings.h"
#include "artisticstyle/artisticstyle.h"
@@ -80,12 +79,6 @@ public:
&uncrustifyBeautifier,
&clangFormatBeautifier
};
GeneralOptionsPage optionsPage {{
artisticStyleBeautifier.id(),
uncrustifyBeautifier.id(),
clangFormatBeautifier.id()
}};
};
static BeautifierPluginPrivate *dd = nullptr;
@@ -112,6 +105,9 @@ ExtensionSystem::IPlugin::ShutdownFlag BeautifierPlugin::aboutToShutdown()
BeautifierPluginPrivate::BeautifierPluginPrivate()
{
for (BeautifierAbstractTool *tool : m_tools)
generalSettings.autoFormatTools.addOption(tool->id());
updateActions();
const Core::EditorManager *editorManager = Core::EditorManager::instance();
@@ -129,14 +125,14 @@ void BeautifierPluginPrivate::updateActions(Core::IEditor *editor)
void BeautifierPluginPrivate::autoFormatOnSave(Core::IDocument *document)
{
if (!generalSettings.autoFormatOnSave())
if (!generalSettings.autoFormatOnSave.value())
return;
if (!isAutoFormatApplicable(document, generalSettings.autoFormatMime()))
if (!isAutoFormatApplicable(document, generalSettings.allowedMimeTypes()))
return;
// Check if file is contained in the current project (if wished)
if (generalSettings.autoFormatOnlyCurrentProject()) {
if (generalSettings.autoFormatOnlyCurrentProject.value()) {
const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject();
if (!pro
|| pro->files([document](const ProjectExplorer::Node *n) {
@@ -149,7 +145,7 @@ void BeautifierPluginPrivate::autoFormatOnSave(Core::IDocument *document)
}
// Find tool to use by id and format file!
const QString id = generalSettings.autoFormatTool();
const QString id = generalSettings.autoFormatTools.stringValue();
auto tool = std::find_if(std::begin(m_tools), std::end(m_tools),
[&id](const BeautifierAbstractTool *t){return t->id() == id;});
if (tool != std::end(m_tools)) {

View File

@@ -1,103 +0,0 @@
// Copyright (C) 2016 Lorenz Haas
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "generaloptionspage.h"
#include "beautifierconstants.h"
#include "beautifiertr.h"
#include "generalsettings.h"
#include <utils/layoutbuilder.h>
#include <QApplication>
#include <QCheckBox>
#include <QComboBox>
#include <QLabel>
#include <QLineEdit>
namespace Beautifier::Internal {
class GeneralOptionsPageWidget : public Core::IOptionsPageWidget
{
public:
explicit GeneralOptionsPageWidget(const QStringList &toolIds);
private:
void apply() final;
QCheckBox *m_autoFormat;
QComboBox *m_autoFormatTool;
QLineEdit *m_autoFormatMime;
QCheckBox *m_autoFormatOnlyCurrentProject;
};
GeneralOptionsPageWidget::GeneralOptionsPageWidget(const QStringList &toolIds)
{
auto settings = GeneralSettings::instance();
m_autoFormat = new QCheckBox(Tr::tr("Enable auto format on file save"));
m_autoFormat->setChecked(settings->autoFormatOnSave());
auto toolLabel = new QLabel(Tr::tr("Tool:"));
toolLabel->setEnabled(false);
auto mimeLabel = new QLabel(Tr::tr("Restrict to MIME types:"));
mimeLabel->setEnabled(false);
m_autoFormatMime = new QLineEdit(settings->autoFormatMimeAsString());
m_autoFormatMime->setEnabled(false);
m_autoFormatOnlyCurrentProject =
new QCheckBox(Tr::tr("Restrict to files contained in the current project"));
m_autoFormatOnlyCurrentProject->setEnabled(false);
m_autoFormatOnlyCurrentProject->setChecked(settings->autoFormatOnlyCurrentProject());
m_autoFormatTool = new QComboBox;
m_autoFormatTool->setEnabled(false);
m_autoFormatTool->addItems(toolIds);
const int index = m_autoFormatTool->findText(settings->autoFormatTool());
m_autoFormatTool->setCurrentIndex(qMax(index, 0));
using namespace Layouting;
Column {
Group {
title(Tr::tr("Automatic Formatting on File Save")),
Form {
Span(2, m_autoFormat), br,
toolLabel, m_autoFormatTool, br,
mimeLabel, m_autoFormatMime, br,
Span(2, m_autoFormatOnlyCurrentProject)
}
},
st
}.attachTo(this);
connect(m_autoFormat, &QCheckBox::toggled, m_autoFormatTool, &QComboBox::setEnabled);
connect(m_autoFormat, &QCheckBox::toggled, m_autoFormatMime, &QLineEdit::setEnabled);
connect(m_autoFormat, &QCheckBox::toggled, toolLabel, &QLabel::setEnabled);
connect(m_autoFormat, &QCheckBox::toggled, mimeLabel, &QLabel::setEnabled);
connect(m_autoFormat, &QCheckBox::toggled, m_autoFormatOnlyCurrentProject, &QCheckBox::setEnabled);
}
void GeneralOptionsPageWidget::apply()
{
auto settings = GeneralSettings::instance();
settings->setAutoFormatOnSave(m_autoFormat->isChecked());
settings->setAutoFormatTool(m_autoFormatTool->currentText());
settings->setAutoFormatMime(m_autoFormatMime->text());
settings->setAutoFormatOnlyCurrentProject(m_autoFormatOnlyCurrentProject->isChecked());
settings->save();
}
GeneralOptionsPage::GeneralOptionsPage(const QStringList &toolIds)
{
setId(Constants::OPTION_GENERAL_ID);
setDisplayName(Tr::tr("General"));
setCategory(Constants::OPTION_CATEGORY);
setDisplayCategory(Tr::tr("Beautifier"));
setWidgetCreator([toolIds] { return new GeneralOptionsPageWidget(toolIds); });
setCategoryIconPath(":/beautifier/images/settingscategory_beautifier.png");
}
} // Beautifier::Internal

View File

@@ -1,16 +0,0 @@
// Copyright (C) 2016 Lorenz Haas
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include <coreplugin/dialogs/ioptionspage.h>
namespace Beautifier::Internal {
class GeneralOptionsPage final : public Core::IOptionsPage
{
public:
explicit GeneralOptionsPage(const QStringList &toolIds);
};
} // Beautifier::Internal

View File

@@ -3,114 +3,88 @@
#include "generalsettings.h"
#include <coreplugin/icore.h>
#include "beautifierconstants.h"
#include "beautifiertr.h"
#include <utils/algorithm.h>
#include <utils/genericconstants.h>
#include <utils/mimeutils.h>
#include <utils/layoutbuilder.h>
using namespace Utils;
namespace Beautifier::Internal {
const char AUTO_FORMAT_TOOL[] = "autoFormatTool";
const char AUTO_FORMAT_MIME[] = "autoFormatMime";
const char AUTO_FORMAT_ONLY_CURRENT_PROJECT[] = "autoFormatOnlyCurrentProject";
static GeneralSettings *m_instance;
GeneralSettings::GeneralSettings()
{
m_instance = this;
read();
}
GeneralSettings *GeneralSettings::instance()
{
return m_instance;
}
void GeneralSettings::read()
GeneralSettings::GeneralSettings()
{
QSettings *s = Core::ICore::settings();
s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP);
s->beginGroup(Utils::Constants::BEAUTIFIER_GENERAL_GROUP);
m_autoFormatOnSave = s->value(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE, false).toBool();
m_autoFormatTool = s->value(AUTO_FORMAT_TOOL, QString()).toString();
setAutoFormatMime(s->value(AUTO_FORMAT_MIME, "text/x-c++src;text/x-c++hdr").toString());
m_autoFormatOnlyCurrentProject = s->value(AUTO_FORMAT_ONLY_CURRENT_PROJECT, true).toBool();
s->endGroup();
s->endGroup();
m_instance = this;
setId(Constants::OPTION_GENERAL_ID);
setDisplayName(Tr::tr("General"));
setCategory(Constants::OPTION_CATEGORY);
setDisplayCategory(Tr::tr("Beautifier"));
setCategoryIconPath(":/beautifier/images/settingscategory_beautifier.png");
setSettingsGroups("Beautifier", "General");
setSettings(this);
setAutoApply(false);
registerAspect(&autoFormatOnSave);
autoFormatOnSave.setSettingsKey(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE);
autoFormatOnSave.setDefaultValue(false);
autoFormatOnSave.setLabelText(Tr::tr("Enable auto format on file save"));
registerAspect(&autoFormatOnlyCurrentProject);
autoFormatOnlyCurrentProject.setSettingsKey("autoFormatOnlyCurrentProject");
autoFormatOnlyCurrentProject.setDefaultValue(true);
autoFormatOnlyCurrentProject.setLabelText(Tr::tr("Restrict to files contained in the current project"));
registerAspect(&autoFormatTools);
autoFormatTools.setSettingsKey("autoFormatTool");
autoFormatTools.setLabelText(Tr::tr("Tool:"));
autoFormatTools.setDefaultValue(0);
autoFormatTools.setDisplayStyle(SelectionAspect::DisplayStyle::ComboBox);
registerAspect(&autoFormatMime);
autoFormatMime.setSettingsKey("autoFormatMime");
autoFormatMime.setDefaultValue("text/x-c++src;text/x-c++hdr");
autoFormatMime.setLabelText(Tr::tr("Restrict to MIME types:"));
autoFormatMime.setDisplayStyle(StringAspect::LineEditDisplay);
setLayouter([this](QWidget *widget) {
using namespace Layouting;
Column {
Group {
title(Tr::tr("Automatic Formatting on File Save")),
autoFormatOnSave.groupChecker(),
Form {
autoFormatTools, br,
autoFormatMime, br,
Span(2, autoFormatOnlyCurrentProject)
}
},
st
}.attachTo(widget);
});
}
void GeneralSettings::save()
QList<MimeType> GeneralSettings::allowedMimeTypes() const
{
QSettings *s = Core::ICore::settings();
s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP);
s->beginGroup(Utils::Constants::BEAUTIFIER_GENERAL_GROUP);
s->setValue(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE, m_autoFormatOnSave);
s->setValue(AUTO_FORMAT_TOOL, m_autoFormatTool);
s->setValue(AUTO_FORMAT_MIME, autoFormatMimeAsString());
s->setValue(AUTO_FORMAT_ONLY_CURRENT_PROJECT, m_autoFormatOnlyCurrentProject);
s->endGroup();
s->endGroup();
}
const QStringList stringTypes = autoFormatMime.value().split(';');
bool GeneralSettings::autoFormatOnSave() const
{
return m_autoFormatOnSave;
}
void GeneralSettings::setAutoFormatOnSave(bool autoFormatOnSave)
{
m_autoFormatOnSave = autoFormatOnSave;
}
QString GeneralSettings::autoFormatTool() const
{
return m_autoFormatTool;
}
void GeneralSettings::setAutoFormatTool(const QString &autoFormatTool)
{
m_autoFormatTool = autoFormatTool;
}
QList<Utils::MimeType> GeneralSettings::autoFormatMime() const
{
return m_autoFormatMime;
}
QString GeneralSettings::autoFormatMimeAsString() const
{
return Utils::transform(m_autoFormatMime, &Utils::MimeType::name).join("; ");
}
void GeneralSettings::setAutoFormatMime(const QList<Utils::MimeType> &autoFormatMime)
{
m_autoFormatMime = autoFormatMime;
}
void GeneralSettings::setAutoFormatMime(const QString &mimeList)
{
const QStringList stringTypes = mimeList.split(';');
QList<Utils::MimeType> types;
types.reserve(stringTypes.count());
QList<MimeType> types;
for (QString t : stringTypes) {
t = t.trimmed();
const Utils::MimeType mime = Utils::mimeTypeForName(t);
const MimeType mime = Utils::mimeTypeForName(t);
if (mime.isValid())
types << mime;
}
setAutoFormatMime(types);
}
bool GeneralSettings::autoFormatOnlyCurrentProject() const
{
return m_autoFormatOnlyCurrentProject;
}
void GeneralSettings::setAutoFormatOnlyCurrentProject(bool autoFormatOnlyCurrentProject)
{
m_autoFormatOnlyCurrentProject = autoFormatOnlyCurrentProject;
return types;
}
} // Beautifier::Internal

View File

@@ -5,38 +5,23 @@
#include <utils/mimeutils.h>
#include <QList>
#include <coreplugin/dialogs/ioptionspage.h>
namespace Beautifier::Internal {
class GeneralSettings
class GeneralSettings : public Core::PagedSettings
{
public:
explicit GeneralSettings();
GeneralSettings();
static GeneralSettings *instance();
void read();
void save();
QList<Utils::MimeType> allowedMimeTypes() const;
bool autoFormatOnSave() const;
void setAutoFormatOnSave(bool autoFormatOnSave);
QString autoFormatTool() const;
void setAutoFormatTool(const QString &autoFormatTool);
QList<Utils::MimeType> autoFormatMime() const;
QString autoFormatMimeAsString() const;
void setAutoFormatMime(const QList<Utils::MimeType> &autoFormatMime);
void setAutoFormatMime(const QString &mimeList);
bool autoFormatOnlyCurrentProject() const;
void setAutoFormatOnlyCurrentProject(bool autoFormatOnlyCurrentProject);
private:
bool m_autoFormatOnSave = false;
bool m_autoFormatOnlyCurrentProject = true;
QString m_autoFormatTool;
QList<Utils::MimeType> m_autoFormatMime;
Utils::BoolAspect autoFormatOnSave;
Utils::BoolAspect autoFormatOnlyCurrentProject;
Utils::SelectionAspect autoFormatTools;
Utils::StringAspect autoFormatMime;
};
} // Beautifier::Internal