diff options
author | hjk <hjk@qt.io> | 2020-01-09 16:19:52 +0100 |
---|---|---|
committer | hjk <hjk@qt.io> | 2020-01-10 09:25:27 +0000 |
commit | 6f210259cd118d13e04f4af313789122f12c76c2 (patch) | |
tree | b11599ed18d2c62572bee404a45e265c560f43cd /src/plugins/beautifier | |
parent | 6629ff4a86fdbfed82cafabe17ccaa87adb8cf48 (diff) | |
download | qt-creator-6f210259cd118d13e04f4af313789122f12c76c2.tar.gz |
Beautifier: Use new IOptionsPager convenience functions
Change-Id: I90510d9f7b77bf286fd342cdfea88964d12571b2
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Diffstat (limited to 'src/plugins/beautifier')
11 files changed, 196 insertions, 400 deletions
diff --git a/src/plugins/beautifier/artisticstyle/artisticstyleoptionspage.cpp b/src/plugins/beautifier/artisticstyle/artisticstyleoptionspage.cpp index 01deb00b4d..73b2645eff 100644 --- a/src/plugins/beautifier/artisticstyle/artisticstyleoptionspage.cpp +++ b/src/plugins/beautifier/artisticstyle/artisticstyleoptionspage.cpp @@ -33,89 +33,73 @@ #include "../beautifierconstants.h" #include "../beautifierplugin.h" -#include <coreplugin/icore.h> - namespace Beautifier { namespace Internal { namespace ArtisticStyle { -ArtisticStyleOptionsPageWidget::ArtisticStyleOptionsPageWidget(ArtisticStyleSettings *settings) - : ui(new Ui::ArtisticStyleOptionsPage), m_settings(settings) +class ArtisticStyleOptionsPageWidget : public Core::IOptionsPageWidget { - ui->setupUi(this); - ui->useHomeFile->setText(ui->useHomeFile->text().replace( - "HOME", QDir::toNativeSeparators(QDir::home().absolutePath()))); - ui->specificConfigFile->setExpectedKind(Utils::PathChooser::File); - ui->specificConfigFile->setPromptDialogFilter(tr("AStyle (*.astylerc)")); - ui->command->setExpectedKind(Utils::PathChooser::ExistingCommand); - ui->command->setCommandVersionArguments({"--version"}); - ui->command->setPromptDialogTitle(BeautifierPlugin::msgCommandPromptDialogTitle( - ArtisticStyle::tr(Constants::ArtisticStyle::DISPLAY_NAME))); - connect(ui->command, &Utils::PathChooser::validChanged, ui->options, &QWidget::setEnabled); - ui->configurations->setSettings(m_settings); -} + Q_DECLARE_TR_FUNCTIONS(Beautifier::Internal::ArtisticStyle) -ArtisticStyleOptionsPageWidget::~ArtisticStyleOptionsPageWidget() -{ - delete ui; -} +public: + explicit ArtisticStyleOptionsPageWidget(ArtisticStyleSettings *settings); + + void apply() final; + void finish() final {} + +private: + Ui::ArtisticStyleOptionsPage ui; + ArtisticStyleSettings *m_settings; +}; -void ArtisticStyleOptionsPageWidget::restore() +ArtisticStyleOptionsPageWidget::ArtisticStyleOptionsPageWidget(ArtisticStyleSettings *settings) + : m_settings(settings) { - ui->command->setFileName(m_settings->command()); - ui->mime->setText(m_settings->supportedMimeTypesAsString()); - ui->useOtherFiles->setChecked(m_settings->useOtherFiles()); - ui->useSpecificConfigFile->setChecked(m_settings->useSpecificConfigFile()); - ui->specificConfigFile->setFileName(m_settings->specificConfigFile()); - ui->useHomeFile->setChecked(m_settings->useHomeFile()); - ui->useCustomStyle->setChecked(m_settings->useCustomStyle()); - ui->configurations->setCurrentConfiguration(m_settings->customStyle()); + ui.setupUi(this); + ui.useHomeFile->setText(ui.useHomeFile->text().replace( + "HOME", QDir::toNativeSeparators(QDir::home().absolutePath()))); + ui.specificConfigFile->setExpectedKind(Utils::PathChooser::File); + ui.specificConfigFile->setPromptDialogFilter(tr("AStyle (*.astylerc)")); + ui.command->setExpectedKind(Utils::PathChooser::ExistingCommand); + ui.command->setCommandVersionArguments({"--version"}); + ui.command->setPromptDialogTitle(BeautifierPlugin::msgCommandPromptDialogTitle( + ArtisticStyle::tr(Constants::ArtisticStyle::DISPLAY_NAME))); + connect(ui.command, &Utils::PathChooser::validChanged, ui.options, &QWidget::setEnabled); + ui.configurations->setSettings(m_settings); + + ui.command->setFileName(m_settings->command()); + ui.mime->setText(m_settings->supportedMimeTypesAsString()); + ui.useOtherFiles->setChecked(m_settings->useOtherFiles()); + ui.useSpecificConfigFile->setChecked(m_settings->useSpecificConfigFile()); + ui.specificConfigFile->setFileName(m_settings->specificConfigFile()); + ui.useHomeFile->setChecked(m_settings->useHomeFile()); + ui.useCustomStyle->setChecked(m_settings->useCustomStyle()); + ui.configurations->setCurrentConfiguration(m_settings->customStyle()); } void ArtisticStyleOptionsPageWidget::apply() { - m_settings->setCommand(ui->command->path()); - m_settings->setSupportedMimeTypes(ui->mime->text()); - m_settings->setUseOtherFiles(ui->useOtherFiles->isChecked()); - m_settings->setUseSpecificConfigFile(ui->useSpecificConfigFile->isChecked()); - m_settings->setSpecificConfigFile(ui->specificConfigFile->fileName()); - m_settings->setUseHomeFile(ui->useHomeFile->isChecked()); - m_settings->setUseCustomStyle(ui->useCustomStyle->isChecked()); - m_settings->setCustomStyle(ui->configurations->currentConfiguration()); + m_settings->setCommand(ui.command->path()); + m_settings->setSupportedMimeTypes(ui.mime->text()); + m_settings->setUseOtherFiles(ui.useOtherFiles->isChecked()); + m_settings->setUseSpecificConfigFile(ui.useSpecificConfigFile->isChecked()); + m_settings->setSpecificConfigFile(ui.specificConfigFile->fileName()); + m_settings->setUseHomeFile(ui.useHomeFile->isChecked()); + m_settings->setUseCustomStyle(ui.useCustomStyle->isChecked()); + m_settings->setCustomStyle(ui.configurations->currentConfiguration()); m_settings->save(); // update since not all MIME types are accepted (invalids or duplicates) - ui->mime->setText(m_settings->supportedMimeTypesAsString()); + ui.mime->setText(m_settings->supportedMimeTypesAsString()); } ArtisticStyleOptionsPage::ArtisticStyleOptionsPage(ArtisticStyleSettings *settings, QObject *parent) : - IOptionsPage(parent), - m_settings(settings) + IOptionsPage(parent) { setId(Constants::ArtisticStyle::OPTION_ID); setDisplayName(tr("Artistic Style")); setCategory(Constants::OPTION_CATEGORY); -} - -QWidget *ArtisticStyleOptionsPage::widget() -{ - m_settings->read(); - - if (!m_widget) - m_widget = new ArtisticStyleOptionsPageWidget(m_settings); - m_widget->restore(); - - return m_widget; -} - -void ArtisticStyleOptionsPage::apply() -{ - if (m_widget) - m_widget->apply(); -} - -void ArtisticStyleOptionsPage::finish() -{ + setWidgetCreator([settings] { return new ArtisticStyleOptionsPageWidget(settings); }); } } // namespace ArtisticStyle diff --git a/src/plugins/beautifier/artisticstyle/artisticstyleoptionspage.h b/src/plugins/beautifier/artisticstyle/artisticstyleoptionspage.h index e1e9141bcb..8aea9fb3f1 100644 --- a/src/plugins/beautifier/artisticstyle/artisticstyleoptionspage.h +++ b/src/plugins/beautifier/artisticstyle/artisticstyleoptionspage.h @@ -27,45 +27,16 @@ #include <coreplugin/dialogs/ioptionspage.h> -#include <QPointer> -#include <QWidget> - namespace Beautifier { namespace Internal { namespace ArtisticStyle { class ArtisticStyleSettings; -namespace Ui { class ArtisticStyleOptionsPage; } - -class ArtisticStyleOptionsPageWidget : public QWidget -{ - Q_OBJECT - -public: - explicit ArtisticStyleOptionsPageWidget(ArtisticStyleSettings *settings); - ~ArtisticStyleOptionsPageWidget() override; - void restore(); - void apply(); - -private: - Ui::ArtisticStyleOptionsPage *ui; - ArtisticStyleSettings *m_settings; -}; - -class ArtisticStyleOptionsPage : public Core::IOptionsPage +class ArtisticStyleOptionsPage final : public Core::IOptionsPage { - Q_OBJECT - public: - explicit ArtisticStyleOptionsPage(ArtisticStyleSettings *settings, QObject *parent = nullptr); - QWidget *widget() override; - void apply() override; - void finish() override; - -private: - QPointer<ArtisticStyleOptionsPageWidget> m_widget; - ArtisticStyleSettings *m_settings; + ArtisticStyleOptionsPage(ArtisticStyleSettings *settings, QObject *parent); }; } // namespace ArtisticStyle diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp index 598d5a2388..621c366175 100644 --- a/src/plugins/beautifier/beautifierplugin.cpp +++ b/src/plugins/beautifier/beautifierplugin.cpp @@ -97,7 +97,7 @@ public: void autoFormatOnSave(Core::IDocument *document); - QSharedPointer<GeneralSettings> m_generalSettings; + GeneralSettings generalSettings; ArtisticStyle::ArtisticStyle artisticStyleBeautifier; ClangFormat::ClangFormat clangFormatBeautifier; @@ -133,8 +133,7 @@ BeautifierPluginPrivate::BeautifierPluginPrivate() for (BeautifierAbstractTool *tool : m_tools) toolIds << tool->id(); - m_generalSettings.reset(new GeneralSettings); - new GeneralOptionsPage(m_generalSettings, toolIds, this); + new GeneralOptionsPage(toolIds, this); updateActions(); @@ -153,14 +152,14 @@ void BeautifierPluginPrivate::updateActions(Core::IEditor *editor) void BeautifierPluginPrivate::autoFormatOnSave(Core::IDocument *document) { - if (!m_generalSettings->autoFormatOnSave()) + if (!generalSettings.autoFormatOnSave()) return; - if (!isAutoFormatApplicable(document, m_generalSettings->autoFormatMime())) + if (!isAutoFormatApplicable(document, generalSettings.autoFormatMime())) return; // Check if file is contained in the current project (if wished) - if (m_generalSettings->autoFormatOnlyCurrentProject()) { + if (generalSettings.autoFormatOnlyCurrentProject()) { const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject(); if (!pro || pro->files([document](const ProjectExplorer::Node *n) { @@ -173,7 +172,7 @@ void BeautifierPluginPrivate::autoFormatOnSave(Core::IDocument *document) } // Find tool to use by id and format file! - const QString id = m_generalSettings->autoFormatTool(); + const QString id = generalSettings.autoFormatTool(); auto tool = std::find_if(m_tools.constBegin(), m_tools.constEnd(), [&id](const BeautifierAbstractTool *t){return t->id() == id;}); if (tool != m_tools.constEnd()) { diff --git a/src/plugins/beautifier/clangformat/clangformatoptionspage.cpp b/src/plugins/beautifier/clangformat/clangformatoptionspage.cpp index 9c04198418..632727c840 100644 --- a/src/plugins/beautifier/clangformat/clangformatoptionspage.cpp +++ b/src/plugins/beautifier/clangformat/clangformatoptionspage.cpp @@ -32,103 +32,84 @@ #include "../beautifierconstants.h" #include "../beautifierplugin.h" -#include <coreplugin/icore.h> - namespace Beautifier { namespace Internal { namespace ClangFormat { -ClangFormatOptionsPageWidget::ClangFormatOptionsPageWidget(ClangFormatSettings *settings, - QWidget *parent) : - QWidget(parent), - ui(new Ui::ClangFormatOptionsPage), - m_settings(settings) +class ClangFormatOptionsPageWidget : public Core::IOptionsPageWidget +{ + Q_DECLARE_TR_FUNCTIONS(Beautifier::Internal::ClangFormat) + +public: + explicit ClangFormatOptionsPageWidget(ClangFormatSettings *settings); + + void apply() final; + void finish() final {} + +private: + Ui::ClangFormatOptionsPage ui; + ClangFormatSettings *m_settings; +}; + +ClangFormatOptionsPageWidget::ClangFormatOptionsPageWidget(ClangFormatSettings *settings) + : m_settings(settings) { - ui->setupUi(this); - ui->options->setEnabled(false); - ui->predefinedStyle->addItems(m_settings->predefinedStyles()); - ui->fallbackStyle->addItems(m_settings->fallbackStyles()); - ui->command->setExpectedKind(Utils::PathChooser::ExistingCommand); - ui->command->setCommandVersionArguments({"--version"}); - ui->command->setPromptDialogTitle( + ui.setupUi(this); + ui.options->setEnabled(false); + ui.predefinedStyle->addItems(m_settings->predefinedStyles()); + ui.fallbackStyle->addItems(m_settings->fallbackStyles()); + ui.command->setExpectedKind(Utils::PathChooser::ExistingCommand); + ui.command->setCommandVersionArguments({"--version"}); + ui.command->setPromptDialogTitle( BeautifierPlugin::msgCommandPromptDialogTitle("Clang Format")); - connect(ui->command, &Utils::PathChooser::validChanged, ui->options, &QWidget::setEnabled); - connect(ui->predefinedStyle, &QComboBox::currentTextChanged, [this](const QString &item) { - ui->fallbackStyle->setEnabled(item == "File"); + connect(ui.command, &Utils::PathChooser::validChanged, ui.options, &QWidget::setEnabled); + connect(ui.predefinedStyle, &QComboBox::currentTextChanged, [this](const QString &item) { + ui.fallbackStyle->setEnabled(item == "File"); }); - connect(ui->usePredefinedStyle, &QRadioButton::toggled, [this](bool checked) { - ui->fallbackStyle->setEnabled(checked && ui->predefinedStyle->currentText() == "File"); - ui->predefinedStyle->setEnabled(checked); + connect(ui.usePredefinedStyle, &QRadioButton::toggled, [this](bool checked) { + ui.fallbackStyle->setEnabled(checked && ui.predefinedStyle->currentText() == "File"); + ui.predefinedStyle->setEnabled(checked); }); - ui->configurations->setSettings(m_settings); -} + ui.configurations->setSettings(m_settings); -ClangFormatOptionsPageWidget::~ClangFormatOptionsPageWidget() -{ - delete ui; -} - -void ClangFormatOptionsPageWidget::restore() -{ - ui->command->setFileName(m_settings->command()); - ui->mime->setText(m_settings->supportedMimeTypesAsString()); - const int predefinedStyleIndex = ui->predefinedStyle->findText(m_settings->predefinedStyle()); + ui.command->setFileName(m_settings->command()); + ui.mime->setText(m_settings->supportedMimeTypesAsString()); + const int predefinedStyleIndex = ui.predefinedStyle->findText(m_settings->predefinedStyle()); if (predefinedStyleIndex != -1) - ui->predefinedStyle->setCurrentIndex(predefinedStyleIndex); - const int fallbackStyleIndex = ui->fallbackStyle->findText(m_settings->fallbackStyle()); + ui.predefinedStyle->setCurrentIndex(predefinedStyleIndex); + const int fallbackStyleIndex = ui.fallbackStyle->findText(m_settings->fallbackStyle()); if (fallbackStyleIndex != -1) - ui->fallbackStyle->setCurrentIndex(fallbackStyleIndex); - ui->configurations->setSettings(m_settings); - ui->configurations->setCurrentConfiguration(m_settings->customStyle()); + ui.fallbackStyle->setCurrentIndex(fallbackStyleIndex); + ui.configurations->setSettings(m_settings); + ui.configurations->setCurrentConfiguration(m_settings->customStyle()); if (m_settings->usePredefinedStyle()) - ui->usePredefinedStyle->setChecked(true); + ui.usePredefinedStyle->setChecked(true); else - ui->useCustomizedStyle->setChecked(true); + ui.useCustomizedStyle->setChecked(true); } void ClangFormatOptionsPageWidget::apply() { - m_settings->setCommand(ui->command->path()); - m_settings->setSupportedMimeTypes(ui->mime->text()); - m_settings->setUsePredefinedStyle(ui->usePredefinedStyle->isChecked()); - m_settings->setPredefinedStyle(ui->predefinedStyle->currentText()); - m_settings->setFallbackStyle(ui->fallbackStyle->currentText()); - m_settings->setCustomStyle(ui->configurations->currentConfiguration()); + m_settings->setCommand(ui.command->path()); + m_settings->setSupportedMimeTypes(ui.mime->text()); + m_settings->setUsePredefinedStyle(ui.usePredefinedStyle->isChecked()); + m_settings->setPredefinedStyle(ui.predefinedStyle->currentText()); + m_settings->setFallbackStyle(ui.fallbackStyle->currentText()); + m_settings->setCustomStyle(ui.configurations->currentConfiguration()); m_settings->save(); // update since not all MIME types are accepted (invalids or duplicates) - ui->mime->setText(m_settings->supportedMimeTypesAsString()); + ui.mime->setText(m_settings->supportedMimeTypesAsString()); } ClangFormatOptionsPage::ClangFormatOptionsPage(ClangFormatSettings *settings, QObject *parent) : - IOptionsPage(parent), - m_settings(settings) + IOptionsPage(parent) { setId(Constants::ClangFormat::OPTION_ID); - setDisplayName(tr("Clang Format")); + setDisplayName(ClangFormatOptionsPageWidget::tr("Clang Format")); setCategory(Constants::OPTION_CATEGORY); -} - -QWidget *ClangFormatOptionsPage::widget() -{ - m_settings->read(); - - if (!m_widget) - m_widget = new ClangFormatOptionsPageWidget(m_settings); - m_widget->restore(); - - return m_widget; -} - -void ClangFormatOptionsPage::apply() -{ - if (m_widget) - m_widget->apply(); -} - -void ClangFormatOptionsPage::finish() -{ + setWidgetCreator([settings] { return new ClangFormatOptionsPageWidget(settings); }); } } // namespace ClangFormat diff --git a/src/plugins/beautifier/clangformat/clangformatoptionspage.h b/src/plugins/beautifier/clangformat/clangformatoptionspage.h index 2db6aa30c2..a0384bbfd1 100644 --- a/src/plugins/beautifier/clangformat/clangformatoptionspage.h +++ b/src/plugins/beautifier/clangformat/clangformatoptionspage.h @@ -27,45 +27,16 @@ #include <coreplugin/dialogs/ioptionspage.h> -#include <QPointer> -#include <QWidget> - namespace Beautifier { namespace Internal { namespace ClangFormat { class ClangFormatSettings; -namespace Ui { class ClangFormatOptionsPage; } - -class ClangFormatOptionsPageWidget : public QWidget -{ - Q_OBJECT - -public: - explicit ClangFormatOptionsPageWidget(ClangFormatSettings *settings, QWidget *parent = nullptr); - ~ClangFormatOptionsPageWidget() override; - void restore(); - void apply(); - -private: - Ui::ClangFormatOptionsPage *ui; - ClangFormatSettings *m_settings; -}; - -class ClangFormatOptionsPage : public Core::IOptionsPage +class ClangFormatOptionsPage final : public Core::IOptionsPage { - Q_OBJECT - public: - explicit ClangFormatOptionsPage(ClangFormatSettings *settings, QObject *parent = nullptr); - QWidget *widget() override; - void apply() override; - void finish() override; - -private: - QPointer<ClangFormatOptionsPageWidget> m_widget; - ClangFormatSettings *m_settings; + ClangFormatOptionsPage(ClangFormatSettings *settings, QObject *parent); }; } // namespace ClangFormat diff --git a/src/plugins/beautifier/generaloptionspage.cpp b/src/plugins/beautifier/generaloptionspage.cpp index ae832f0c98..a98c1546b8 100644 --- a/src/plugins/beautifier/generaloptionspage.cpp +++ b/src/plugins/beautifier/generaloptionspage.cpp @@ -29,54 +29,46 @@ #include "beautifierconstants.h" #include "generalsettings.h" -#include <coreplugin/icore.h> - -#include <QTextStream> - namespace Beautifier { namespace Internal { -GeneralOptionsPageWidget::GeneralOptionsPageWidget(const QSharedPointer<GeneralSettings> &settings, - const QStringList &toolIds) : - ui(new Ui::GeneralOptionsPage), - m_settings(settings) +class GeneralOptionsPageWidget : public Core::IOptionsPageWidget { - ui->setupUi(this); - ui->autoFormatTool->addItems(toolIds); - restore(); -} +public: + explicit GeneralOptionsPageWidget(const QStringList &toolIds); -GeneralOptionsPageWidget::~GeneralOptionsPageWidget() -{ - delete ui; -} +private: + void apply() final; + void finish() final {} -void GeneralOptionsPageWidget::restore() + Ui::GeneralOptionsPage ui; +}; + +GeneralOptionsPageWidget::GeneralOptionsPageWidget(const QStringList &toolIds) { - ui->autoFormat->setChecked(m_settings->autoFormatOnSave()); - const int index = ui->autoFormatTool->findText(m_settings->autoFormatTool()); - ui->autoFormatTool->setCurrentIndex(qMax(index, 0)); - ui->autoFormatMime->setText(m_settings->autoFormatMimeAsString()); - ui->autoFormatOnlyCurrentProject->setChecked(m_settings->autoFormatOnlyCurrentProject()); + ui.setupUi(this); + ui.autoFormatTool->addItems(toolIds); + + auto settings = GeneralSettings::instance(); + ui.autoFormat->setChecked(settings->autoFormatOnSave()); + const int index = ui.autoFormatTool->findText(settings->autoFormatTool()); + ui.autoFormatTool->setCurrentIndex(qMax(index, 0)); + ui.autoFormatMime->setText(settings->autoFormatMimeAsString()); + ui.autoFormatOnlyCurrentProject->setChecked(settings->autoFormatOnlyCurrentProject()); } -void GeneralOptionsPageWidget::apply(bool *autoFormatChanged) +void GeneralOptionsPageWidget::apply() { - if (autoFormatChanged) - *autoFormatChanged = m_settings->autoFormatOnSave() != ui->autoFormat->isChecked(); - - m_settings->setAutoFormatOnSave(ui->autoFormat->isChecked()); - m_settings->setAutoFormatTool(ui->autoFormatTool->currentText()); - m_settings->setAutoFormatMime(ui->autoFormatMime->text()); - m_settings->setAutoFormatOnlyCurrentProject(ui->autoFormatOnlyCurrentProject->isChecked()); - m_settings->save(); + auto settings = GeneralSettings::instance(); + settings->setAutoFormatOnSave(ui.autoFormat->isChecked()); + settings->setAutoFormatTool(ui.autoFormatTool->currentText()); + settings->setAutoFormatMime(ui.autoFormatMime->text()); + settings->setAutoFormatOnlyCurrentProject(ui.autoFormatOnlyCurrentProject->isChecked()); + settings->save(); } -GeneralOptionsPage::GeneralOptionsPage(const QSharedPointer<GeneralSettings> &settings, - const QStringList &toolIds, QObject *parent) : - IOptionsPage(parent), - m_settings(settings), - m_toolIds(toolIds) +GeneralOptionsPage::GeneralOptionsPage(const QStringList &toolIds, QObject *parent) : + IOptionsPage(parent) { setId(Constants::OPTION_GENERAL_ID); setDisplayName(tr("General")); @@ -84,31 +76,7 @@ GeneralOptionsPage::GeneralOptionsPage(const QSharedPointer<GeneralSettings> &se setDisplayCategory(QCoreApplication::translate("Beautifier", "Beautifier")); setCategoryIcon(Utils::Icon({{":/beautifier/images/settingscategory_beautifier.png", Utils::Theme::PanelTextColorDark}}, Utils::Icon::Tint)); -} - -QWidget *GeneralOptionsPage::widget() -{ - m_settings->read(); - - if (!m_widget) - m_widget = new GeneralOptionsPageWidget(m_settings, m_toolIds); - m_widget->restore(); - - return m_widget; -} - -void GeneralOptionsPage::apply() -{ - if (m_widget) { - bool autoFormat = false; - m_widget->apply(&autoFormat); - if (autoFormat) - emit autoFormatChanged(); - } -} - -void GeneralOptionsPage::finish() -{ + setWidgetCreator([toolIds] { return new GeneralOptionsPageWidget(toolIds); }); } } // namespace Internal diff --git a/src/plugins/beautifier/generaloptionspage.h b/src/plugins/beautifier/generaloptionspage.h index b6335f68ad..4721db481d 100644 --- a/src/plugins/beautifier/generaloptionspage.h +++ b/src/plugins/beautifier/generaloptionspage.h @@ -27,51 +27,13 @@ #include <coreplugin/dialogs/ioptionspage.h> -#include <QPointer> -#include <QSharedPointer> -#include <QWidget> - namespace Beautifier { namespace Internal { -class GeneralSettings; - -namespace Ui { class GeneralOptionsPage; } - -class GeneralOptionsPageWidget : public QWidget -{ - Q_OBJECT - -public: - explicit GeneralOptionsPageWidget(const QSharedPointer<GeneralSettings> &settings, - const QStringList &toolIds); - ~GeneralOptionsPageWidget() override; - void restore(); - void apply(bool *autoFormatChanged); - -private: - Ui::GeneralOptionsPage *ui; - QSharedPointer<GeneralSettings> m_settings; -}; - class GeneralOptionsPage : public Core::IOptionsPage { - Q_OBJECT - public: - explicit GeneralOptionsPage(const QSharedPointer<GeneralSettings> &settings, - const QStringList &toolIds, QObject *parent = nullptr); - QWidget *widget() override; - void apply() override; - void finish() override; - -signals: - void autoFormatChanged(); - -private: - QPointer<GeneralOptionsPageWidget> m_widget; - QSharedPointer<GeneralSettings> m_settings; - QStringList m_toolIds; + GeneralOptionsPage(const QStringList &toolIds, QObject *parent); }; } // namespace Internal diff --git a/src/plugins/beautifier/generalsettings.cpp b/src/plugins/beautifier/generalsettings.cpp index 9546ec5d25..df16001aa0 100644 --- a/src/plugins/beautifier/generalsettings.cpp +++ b/src/plugins/beautifier/generalsettings.cpp @@ -41,11 +41,19 @@ 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() { QSettings *s = Core::ICore::settings(); diff --git a/src/plugins/beautifier/generalsettings.h b/src/plugins/beautifier/generalsettings.h index 791c30d61d..254a13c7c2 100644 --- a/src/plugins/beautifier/generalsettings.h +++ b/src/plugins/beautifier/generalsettings.h @@ -36,6 +36,8 @@ class GeneralSettings { public: explicit GeneralSettings(); + static GeneralSettings *instance(); + void read(); void save(); diff --git a/src/plugins/beautifier/uncrustify/uncrustifyoptionspage.cpp b/src/plugins/beautifier/uncrustify/uncrustifyoptionspage.cpp index 2fe042daa3..4739d07b45 100644 --- a/src/plugins/beautifier/uncrustify/uncrustifyoptionspage.cpp +++ b/src/plugins/beautifier/uncrustify/uncrustifyoptionspage.cpp @@ -33,97 +33,76 @@ #include "../beautifierconstants.h" #include "../beautifierplugin.h" -#include <coreplugin/icore.h> - -#include <QTextStream> - namespace Beautifier { namespace Internal { namespace Uncrustify { -UncrustifyOptionsPageWidget::UncrustifyOptionsPageWidget(UncrustifySettings *settings, - QWidget *parent) : - QWidget(parent), - ui(new Ui::UncrustifyOptionsPage), - m_settings(settings) +class UncrustifyOptionsPageWidget : public Core::IOptionsPageWidget { - ui->setupUi(this); - ui->useHomeFile->setText(ui->useHomeFile->text().replace( - "HOME", QDir::toNativeSeparators(QDir::home().absolutePath()))); - ui->uncrusifyFilePath->setExpectedKind(Utils::PathChooser::File); - ui->uncrusifyFilePath->setPromptDialogFilter(tr("Uncrustify file (*.cfg)")); + Q_DECLARE_TR_FUNCTIONS(Beautifier::Internal::ClangFormat) - ui->command->setExpectedKind(Utils::PathChooser::ExistingCommand); - ui->command->setCommandVersionArguments({"--version"}); - ui->command->setPromptDialogTitle(BeautifierPlugin::msgCommandPromptDialogTitle( - Uncrustify::tr(Constants::Uncrustify::DISPLAY_NAME))); - connect(ui->command, &Utils::PathChooser::validChanged, ui->options, &QWidget::setEnabled); - ui->configurations->setSettings(m_settings); -} +public: + explicit UncrustifyOptionsPageWidget(UncrustifySettings *settings); -UncrustifyOptionsPageWidget::~UncrustifyOptionsPageWidget() -{ - delete ui; -} + void apply() final; + void finish() final {} + +private: + Ui::UncrustifyOptionsPage ui; + UncrustifySettings *m_settings; +}; -void UncrustifyOptionsPageWidget::restore() +UncrustifyOptionsPageWidget::UncrustifyOptionsPageWidget(UncrustifySettings *settings) + : m_settings(settings) { - ui->command->setFileName(m_settings->command()); - ui->mime->setText(m_settings->supportedMimeTypesAsString()); - ui->useOtherFiles->setChecked(m_settings->useOtherFiles()); - ui->useHomeFile->setChecked(m_settings->useHomeFile()); - ui->useSpecificFile->setChecked(m_settings->useSpecificConfigFile()); - ui->uncrusifyFilePath->setFileName(m_settings->specificConfigFile()); - ui->useCustomStyle->setChecked(m_settings->useCustomStyle()); - ui->configurations->setCurrentConfiguration(m_settings->customStyle()); - ui->formatEntireFileFallback->setChecked(m_settings->formatEntireFileFallback()); + ui.setupUi(this); + ui.useHomeFile->setText(ui.useHomeFile->text().replace( + "HOME", QDir::toNativeSeparators(QDir::home().absolutePath()))); + ui.uncrusifyFilePath->setExpectedKind(Utils::PathChooser::File); + ui.uncrusifyFilePath->setPromptDialogFilter(tr("Uncrustify file (*.cfg)")); + + ui.command->setExpectedKind(Utils::PathChooser::ExistingCommand); + ui.command->setCommandVersionArguments({"--version"}); + ui.command->setPromptDialogTitle(BeautifierPlugin::msgCommandPromptDialogTitle( + Uncrustify::tr(Constants::Uncrustify::DISPLAY_NAME))); + connect(ui.command, &Utils::PathChooser::validChanged, ui.options, &QWidget::setEnabled); + ui.configurations->setSettings(m_settings); + + ui.command->setFileName(m_settings->command()); + ui.mime->setText(m_settings->supportedMimeTypesAsString()); + ui.useOtherFiles->setChecked(m_settings->useOtherFiles()); + ui.useHomeFile->setChecked(m_settings->useHomeFile()); + ui.useSpecificFile->setChecked(m_settings->useSpecificConfigFile()); + ui.uncrusifyFilePath->setFileName(m_settings->specificConfigFile()); + ui.useCustomStyle->setChecked(m_settings->useCustomStyle()); + ui.configurations->setCurrentConfiguration(m_settings->customStyle()); + ui.formatEntireFileFallback->setChecked(m_settings->formatEntireFileFallback()); } void UncrustifyOptionsPageWidget::apply() { - m_settings->setCommand(ui->command->path()); - m_settings->setSupportedMimeTypes(ui->mime->text()); - m_settings->setUseOtherFiles(ui->useOtherFiles->isChecked()); - m_settings->setUseHomeFile(ui->useHomeFile->isChecked()); - m_settings->setUseSpecificConfigFile(ui->useSpecificFile->isChecked()); - m_settings->setSpecificConfigFile(ui->uncrusifyFilePath->fileName()); - m_settings->setUseCustomStyle(ui->useCustomStyle->isChecked()); - m_settings->setCustomStyle(ui->configurations->currentConfiguration()); - m_settings->setFormatEntireFileFallback(ui->formatEntireFileFallback->isChecked()); + m_settings->setCommand(ui.command->path()); + m_settings->setSupportedMimeTypes(ui.mime->text()); + m_settings->setUseOtherFiles(ui.useOtherFiles->isChecked()); + m_settings->setUseHomeFile(ui.useHomeFile->isChecked()); + m_settings->setUseSpecificConfigFile(ui.useSpecificFile->isChecked()); + m_settings->setSpecificConfigFile(ui.uncrusifyFilePath->fileName()); + m_settings->setUseCustomStyle(ui.useCustomStyle->isChecked()); + m_settings->setCustomStyle(ui.configurations->currentConfiguration()); + m_settings->setFormatEntireFileFallback(ui.formatEntireFileFallback->isChecked()); m_settings->save(); // update since not all MIME types are accepted (invalids or duplicates) - ui->mime->setText(m_settings->supportedMimeTypesAsString()); + ui.mime->setText(m_settings->supportedMimeTypesAsString()); } UncrustifyOptionsPage::UncrustifyOptionsPage(UncrustifySettings *settings, QObject *parent) : - IOptionsPage(parent), - m_settings(settings) + IOptionsPage(parent) { setId(Constants::Uncrustify::OPTION_ID); - setDisplayName(tr("Uncrustify")); + setDisplayName(UncrustifyOptionsPageWidget::tr("Uncrustify")); setCategory(Constants::OPTION_CATEGORY); -} - -QWidget *UncrustifyOptionsPage::widget() -{ - m_settings->read(); - - if (!m_widget) - m_widget = new UncrustifyOptionsPageWidget(m_settings); - m_widget->restore(); - - return m_widget; -} - -void UncrustifyOptionsPage::apply() -{ - if (m_widget) - m_widget->apply(); -} - -void UncrustifyOptionsPage::finish() -{ + setWidgetCreator([settings] { return new UncrustifyOptionsPageWidget(settings); }); } } // namespace Uncrustify diff --git a/src/plugins/beautifier/uncrustify/uncrustifyoptionspage.h b/src/plugins/beautifier/uncrustify/uncrustifyoptionspage.h index e8c8c11684..83f8973535 100644 --- a/src/plugins/beautifier/uncrustify/uncrustifyoptionspage.h +++ b/src/plugins/beautifier/uncrustify/uncrustifyoptionspage.h @@ -27,45 +27,16 @@ #include <coreplugin/dialogs/ioptionspage.h> -#include <QPointer> -#include <QWidget> - namespace Beautifier { namespace Internal { namespace Uncrustify { class UncrustifySettings; -namespace Ui { class UncrustifyOptionsPage; } - -class UncrustifyOptionsPageWidget : public QWidget -{ - Q_OBJECT - -public: - explicit UncrustifyOptionsPageWidget(UncrustifySettings *settings, QWidget *parent = nullptr); - ~UncrustifyOptionsPageWidget() override; - void restore(); - void apply(); - -private: - Ui::UncrustifyOptionsPage *ui; - UncrustifySettings *m_settings; -}; - -class UncrustifyOptionsPage : public Core::IOptionsPage +class UncrustifyOptionsPage final : public Core::IOptionsPage { - Q_OBJECT - public: - explicit UncrustifyOptionsPage(UncrustifySettings *settings, QObject *parent = nullptr); - QWidget *widget() override; - void apply() override; - void finish() override; - -private: - QPointer<UncrustifyOptionsPageWidget> m_widget; - UncrustifySettings *m_settings; + UncrustifyOptionsPage(UncrustifySettings *settings, QObject *parent); }; } // namespace Uncrustify |