diff options
author | hjk <hjk@qt.io> | 2023-05-10 16:30:11 +0200 |
---|---|---|
committer | hjk <hjk@qt.io> | 2023-05-12 13:12:10 +0000 |
commit | 7dd27301ab2ea3b5d049d167d51dca8b9b5461d5 (patch) | |
tree | 7f34b91ff4f1f860e2c3226d7411ef5aa9c0852a | |
parent | 06723148447ea244267be1ec3eb3e3a22e931bf1 (diff) | |
download | qt-creator-7dd27301ab2ea3b5d049d167d51dca8b9b5461d5.tar.gz |
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>
-rw-r--r-- | src/plugins/beautifier/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/plugins/beautifier/beautifier.qbs | 2 | ||||
-rw-r--r-- | src/plugins/beautifier/beautifierplugin.cpp | 18 | ||||
-rw-r--r-- | src/plugins/beautifier/generaloptionspage.cpp | 103 | ||||
-rw-r--r-- | src/plugins/beautifier/generaloptionspage.h | 16 | ||||
-rw-r--r-- | src/plugins/beautifier/generalsettings.cpp | 142 | ||||
-rw-r--r-- | src/plugins/beautifier/generalsettings.h | 33 |
7 files changed, 74 insertions, 241 deletions
diff --git a/src/plugins/beautifier/CMakeLists.txt b/src/plugins/beautifier/CMakeLists.txt index d721f1ef20..cdfacccce1 100644 --- a/src/plugins/beautifier/CMakeLists.txt +++ b/src/plugins/beautifier/CMakeLists.txt @@ -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 diff --git a/src/plugins/beautifier/beautifier.qbs b/src/plugins/beautifier/beautifier.qbs index d1160ae5f5..0f85ca29b5 100644 --- a/src/plugins/beautifier/beautifier.qbs +++ b/src/plugins/beautifier/beautifier.qbs @@ -25,8 +25,6 @@ QtcPlugin { "configurationeditor.h", "configurationpanel.cpp", "configurationpanel.h", - "generaloptionspage.cpp", - "generaloptionspage.h", "generalsettings.cpp", "generalsettings.h", ] diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp index 3057f2034a..b849976a05 100644 --- a/src/plugins/beautifier/beautifierplugin.cpp +++ b/src/plugins/beautifier/beautifierplugin.cpp @@ -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)) { diff --git a/src/plugins/beautifier/generaloptionspage.cpp b/src/plugins/beautifier/generaloptionspage.cpp deleted file mode 100644 index 5e03499a99..0000000000 --- a/src/plugins/beautifier/generaloptionspage.cpp +++ /dev/null @@ -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 diff --git a/src/plugins/beautifier/generaloptionspage.h b/src/plugins/beautifier/generaloptionspage.h deleted file mode 100644 index 885a27cb19..0000000000 --- a/src/plugins/beautifier/generaloptionspage.h +++ /dev/null @@ -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 diff --git a/src/plugins/beautifier/generalsettings.cpp b/src/plugins/beautifier/generalsettings.cpp index 218f6963f3..d9ae27b3fc 100644 --- a/src/plugins/beautifier/generalsettings.cpp +++ b/src/plugins/beautifier/generalsettings.cpp @@ -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> -namespace Beautifier::Internal { +using namespace Utils; -const char AUTO_FORMAT_TOOL[] = "autoFormatTool"; -const char AUTO_FORMAT_MIME[] = "autoFormatMime"; -const char AUTO_FORMAT_ONLY_CURRENT_PROJECT[] = "autoFormatOnlyCurrentProject"; +namespace Beautifier::Internal { static GeneralSettings *m_instance; -GeneralSettings::GeneralSettings() -{ - m_instance = this; - read(); -} - GeneralSettings *GeneralSettings::instance() { return m_instance; } -void GeneralSettings::read() -{ - 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(); -} - -void GeneralSettings::save() -{ - 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(); -} - -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 +GeneralSettings::GeneralSettings() { - return m_autoFormatMime; -} + m_instance = this; -QString GeneralSettings::autoFormatMimeAsString() const -{ - return Utils::transform(m_autoFormatMime, &Utils::MimeType::name).join("; "); + 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::setAutoFormatMime(const QList<Utils::MimeType> &autoFormatMime) +QList<MimeType> GeneralSettings::allowedMimeTypes() const { - m_autoFormatMime = autoFormatMime; -} + const QStringList stringTypes = autoFormatMime.value().split(';'); -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 diff --git a/src/plugins/beautifier/generalsettings.h b/src/plugins/beautifier/generalsettings.h index b381dacb26..2c6743301b 100644 --- a/src/plugins/beautifier/generalsettings.h +++ b/src/plugins/beautifier/generalsettings.h @@ -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(); - static GeneralSettings *instance(); - - void read(); - void save(); - - bool autoFormatOnSave() const; - void setAutoFormatOnSave(bool autoFormatOnSave); + GeneralSettings(); - 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); + static GeneralSettings *instance(); - bool autoFormatOnlyCurrentProject() const; - void setAutoFormatOnlyCurrentProject(bool autoFormatOnlyCurrentProject); + QList<Utils::MimeType> allowedMimeTypes() const; -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 |