From b48e10717f8914710c61491e8edc931f9d43dac2 Mon Sep 17 00:00:00 2001 From: hjk Date: Thu, 17 Aug 2023 11:02:25 +0200 Subject: [PATCH] ProjectExplorer: Self-register JsonWizardPage related factories Moves the using code closer to the common factory setup pattern. Change-Id: I2ee85b911d43b63730ff994a4b07568b23b14f00 Reviewed-by: Christian Stenger --- src/plugins/designer/formeditorplugin.cpp | 3 +- .../jsonwizard/jsonwizardfactory.cpp | 65 +++++++++++-------- .../jsonwizard/jsonwizardfactory.h | 7 -- .../jsonwizard/jsonwizardgeneratorfactory.h | 3 + .../jsonwizard/jsonwizardpagefactory.cpp | 2 - .../jsonwizard/jsonwizardpagefactory.h | 1 + .../projectexplorer/projectexplorer.cpp | 19 +++--- src/plugins/python/pythonplugin.cpp | 2 +- src/plugins/qtsupport/qtsupportplugin.cpp | 3 +- src/plugins/squish/squishplugin.cpp | 10 +-- src/plugins/vcsbase/vcsplugin.cpp | 6 +- 11 files changed, 62 insertions(+), 59 deletions(-) diff --git a/src/plugins/designer/formeditorplugin.cpp b/src/plugins/designer/formeditorplugin.cpp index e839ce2b3a2..e997b26b939 100644 --- a/src/plugins/designer/formeditorplugin.cpp +++ b/src/plugins/designer/formeditorplugin.cpp @@ -52,6 +52,7 @@ public: FormEditorFactory formEditorFactory; SettingsPageProvider settingsPageProvider; QtDesignerFormClassCodeGenerator formClassCodeGenerator; + FormPageFactory formPageFactory; }; FormEditorPlugin::~FormEditorPlugin() @@ -99,8 +100,6 @@ bool FormEditorPlugin::initialize([[maybe_unused]] const QStringList &arguments, }); #endif - ProjectExplorer::JsonWizardFactory::registerPageFactory(new Internal::FormPageFactory); - // Ensure that loading designer translations is done before FormEditorW is instantiated const QString locale = ICore::userInterfaceLanguage(); if (!locale.isEmpty()) { diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp index 80a0b8e0dcb..4d364362fd1 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp @@ -68,8 +68,17 @@ const char OPTIONS_KEY[] = "options"; const char PLATFORM_INDEPENDENT_KEY[] = "platformIndependent"; const char DEFAULT_VALUES[] = "defaultValues"; -static QList s_pageFactories; -static QList s_generatorFactories; +static QList &pageFactories() +{ + static QList thePageFactories; + return thePageFactories; +} + +static QList &generatorFactories() +{ + static QList theGeneratorFactories; + return theGeneratorFactories; +} int JsonWizardFactory::m_verbose = 0; @@ -115,11 +124,11 @@ static JsonWizardFactory::Generator parseGenerator(const QVariant &value, QStrin } Id typeId = Id::fromString(QLatin1String(Constants::GENERATOR_ID_PREFIX) + strVal); JsonWizardGeneratorFactory *factory - = findOr(s_generatorFactories, nullptr, [typeId](JsonWizardGeneratorFactory *f) { return f->canCreate(typeId); }); + = findOr(generatorFactories(), nullptr, [typeId](JsonWizardGeneratorFactory *f) { return f->canCreate(typeId); }); if (!factory) { *errorMessage = Tr::tr("TypeId \"%1\" of generator is unknown. Supported typeIds are: \"%2\".") .arg(strVal) - .arg(supportedTypeIds(s_generatorFactories).replace(QLatin1String(Constants::GENERATOR_ID_PREFIX), QLatin1String(""))); + .arg(supportedTypeIds(generatorFactories()).replace(QLatin1String(Constants::GENERATOR_ID_PREFIX), QLatin1String(""))); return gen; } @@ -133,6 +142,26 @@ static JsonWizardFactory::Generator parseGenerator(const QVariant &value, QStrin return gen; } +JsonWizardPageFactory::JsonWizardPageFactory() +{ + pageFactories().append(this); +} + +JsonWizardPageFactory::~JsonWizardPageFactory() +{ + pageFactories().removeOne(this); +} + +JsonWizardGeneratorFactory::JsonWizardGeneratorFactory() +{ + generatorFactories().append(this); +} + +JsonWizardGeneratorFactory::~JsonWizardGeneratorFactory() +{ + generatorFactories().removeOne(this); +} + //FIXME: createWizardFactories() has an almost identical loop. Make the loop return the results instead of //internal processing and create a separate function for it. Then process the results in //loadDefaultValues() and createWizardFactories() @@ -323,11 +352,11 @@ JsonWizardFactory::Page JsonWizardFactory::parsePage(const QVariant &value, QStr Id typeId = Id::fromString(QLatin1String(Constants::PAGE_ID_PREFIX) + strVal); JsonWizardPageFactory *factory - = Utils::findOr(s_pageFactories, nullptr, [typeId](JsonWizardPageFactory *f) { return f->canCreate(typeId); }); + = Utils::findOr(pageFactories(), nullptr, [typeId](JsonWizardPageFactory *f) { return f->canCreate(typeId); }); if (!factory) { *errorMessage = Tr::tr("TypeId \"%1\" of page is unknown. Supported typeIds are: \"%2\".") .arg(strVal) - .arg(supportedTypeIds(s_pageFactories).replace(QLatin1String(Constants::PAGE_ID_PREFIX), QLatin1String(""))); + .arg(supportedTypeIds(pageFactories()).replace(QLatin1String(Constants::PAGE_ID_PREFIX), QLatin1String(""))); return p; } @@ -519,18 +548,6 @@ int JsonWizardFactory::verbose() return m_verbose; } -void JsonWizardFactory::registerPageFactory(JsonWizardPageFactory *factory) -{ - QTC_ASSERT(!s_pageFactories.contains(factory), return); - s_pageFactories.append(factory); -} - -void JsonWizardFactory::registerGeneratorFactory(JsonWizardGeneratorFactory *factory) -{ - QTC_ASSERT(!s_generatorFactories.contains(factory), return); - s_generatorFactories.append(factory); -} - static QString qmlProjectName(const FilePath &folder) { FilePath currentFolder = folder; @@ -598,7 +615,7 @@ Wizard *JsonWizardFactory::runWizardImpl(const FilePath &path, QWidget *parent, continue; havePage = true; - JsonWizardPageFactory *factory = findOr(s_pageFactories, nullptr, + JsonWizardPageFactory *factory = findOr(pageFactories(), nullptr, [&data](JsonWizardPageFactory *f) { return f->canCreate(data.typeId); }); @@ -621,7 +638,7 @@ Wizard *JsonWizardFactory::runWizardImpl(const FilePath &path, QWidget *parent, for (const Generator &data : std::as_const(m_generators)) { QTC_ASSERT(data.isValid(), continue); - JsonWizardGeneratorFactory *factory = Utils::findOr(s_generatorFactories, nullptr, + JsonWizardGeneratorFactory *factory = Utils::findOr(generatorFactories(), nullptr, [&data](JsonWizardGeneratorFactory *f) { return f->canCreate(data.typeId); }); @@ -701,14 +718,6 @@ bool JsonWizardFactory::isAvailable(Id platformId) const return JsonWizard::boolFromVariant(m_enabledExpression, &expander); } -void JsonWizardFactory::destroyAllFactories() -{ - qDeleteAll(s_pageFactories); - s_pageFactories.clear(); - qDeleteAll(s_generatorFactories); - s_generatorFactories.clear(); -} - bool JsonWizardFactory::initialize(const QVariantMap &data, const FilePath &baseDir, QString *errorMessage) { QTC_ASSERT(errorMessage, return false); diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h index 95171f11dbd..118b20330f2 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h @@ -15,9 +15,6 @@ namespace ProjectExplorer { -class JsonWizardFactory; -class JsonWizardPageFactory; -class JsonWizardGeneratorFactory; class ProjectExplorerPlugin; class ProjectExplorerPluginPrivate; @@ -52,9 +49,6 @@ public: QVariant data; }; - static void registerPageFactory(JsonWizardPageFactory *factory); - static void registerGeneratorFactory(JsonWizardGeneratorFactory *factory); - static QList objectOrList(const QVariant &data, QString *errorMessage); static QString localizedString(const QVariant &value); @@ -78,7 +72,6 @@ private: static void setVerbose(int level); static int verbose(); - static void destroyAllFactories(); bool initialize(const QVariantMap &data, const Utils::FilePath &baseDir, QString *errorMessage); JsonWizardFactory::Page parsePage(const QVariant &value, QString *errorMessage); diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.h b/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.h index 199e359bfb4..2b7683f661d 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.h +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.h @@ -47,6 +47,9 @@ class PROJECTEXPLORER_EXPORT JsonWizardGeneratorFactory : public QObject Q_OBJECT public: + JsonWizardGeneratorFactory(); + ~JsonWizardGeneratorFactory() override; + bool canCreate(Utils::Id typeId) const { return m_typeIds.contains(typeId); } QList supportedIds() const { return m_typeIds; } diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory.cpp index 492b4406b4d..0961cfa9064 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory.cpp @@ -13,8 +13,6 @@ namespace ProjectExplorer { // JsonWizardPageFactory: // -------------------------------------------------------------------- -JsonWizardPageFactory::~JsonWizardPageFactory() = default; - void JsonWizardPageFactory::setTypeIdsSuffixes(const QStringList &suffixes) { m_typeIds = Utils::transform(suffixes, [](const QString &suffix) { diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory.h b/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory.h index 5558022a3e8..b6374772461 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory.h +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory.h @@ -18,6 +18,7 @@ class JsonWizard; class PROJECTEXPLORER_EXPORT JsonWizardPageFactory { public: + JsonWizardPageFactory(); virtual ~JsonWizardPageFactory(); bool canCreate(Utils::Id typeId) const { return m_typeIds.contains(typeId); } diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 8493bf275ab..2f2a9644532 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -690,6 +690,15 @@ public: DeviceCheckBuildStepFactory deviceCheckBuildStepFactory; SanitizerOutputFormatterFactory sanitizerFormatterFactory; + + // JsonWizard related + FieldPageFactory fieldPageFactory; + FilePageFactory filePageFactory; + KitsPageFactory kitsPageFactory; + ProjectPageFactory projectPageFactory; + SummaryPageFactory summaryPageFactory; + FileGeneratorFactory fileGeneratorFactory; + ScannerGeneratorFactory scannerGeneratorFactory; }; static ProjectExplorerPlugin *m_instance = nullptr; @@ -756,7 +765,6 @@ ProjectExplorerPlugin::~ProjectExplorerPlugin() QTC_ASSERT(dd, return); delete dd->m_proWindow; // Needs access to the kit manager. - JsonWizardFactory::destroyAllFactories(); // Force sequence of deletion: KitManager::destroy(); // remove all the profile information @@ -843,15 +851,6 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er : FilePath()); }); - // For JsonWizard: - JsonWizardFactory::registerPageFactory(new FieldPageFactory); - JsonWizardFactory::registerPageFactory(new FilePageFactory); - JsonWizardFactory::registerPageFactory(new KitsPageFactory); - JsonWizardFactory::registerPageFactory(new ProjectPageFactory); - JsonWizardFactory::registerPageFactory(new SummaryPageFactory); - JsonWizardFactory::registerGeneratorFactory(new FileGeneratorFactory); - JsonWizardFactory::registerGeneratorFactory(new ScannerGeneratorFactory); - dd->m_proWindow = new ProjectWindow; Context projectTreeContext(Constants::C_PROJECT_TREE); diff --git a/src/plugins/python/pythonplugin.cpp b/src/plugins/python/pythonplugin.cpp index bc95ddd2aa8..a7e8cc3c747 100644 --- a/src/plugins/python/pythonplugin.cpp +++ b/src/plugins/python/pythonplugin.cpp @@ -37,6 +37,7 @@ public: PySideBuildConfigurationFactory buildConfigFactory; SimpleTargetRunnerFactory runWorkerFactory{{runConfigFactory.runConfigurationId()}}; PythonSettings settings; + PythonWizardPageFactory pythonWizardPageFactory; }; PythonPlugin::PythonPlugin() @@ -61,7 +62,6 @@ void PythonPlugin::initialize() ProjectManager::registerProjectType(PythonMimeType); ProjectManager::registerProjectType(PythonMimeTypeLegacy); - JsonWizardFactory::registerPageFactory(new PythonWizardPageFactory); } void PythonPlugin::extensionsInitialized() diff --git a/src/plugins/qtsupport/qtsupportplugin.cpp b/src/plugins/qtsupport/qtsupportplugin.cpp index 7e93652bad6..2851c977b76 100644 --- a/src/plugins/qtsupport/qtsupportplugin.cpp +++ b/src/plugins/qtsupport/qtsupportplugin.cpp @@ -67,6 +67,8 @@ public: DesignerExternalEditor designerEditor; LinguistEditor linguistEditor; + + TranslationWizardPageFactory translationWizardPageFactory; }; QtSupportPlugin::~QtSupportPlugin() @@ -131,7 +133,6 @@ void QtSupportPlugin::initialize() new ProFileCacheManager(this); JsExpander::registerGlobalObject("QtSupport"); - ProjectExplorer::JsonWizardFactory::registerPageFactory(new TranslationWizardPageFactory); BuildPropertiesSettings::showQtSettings(); diff --git a/src/plugins/squish/squishplugin.cpp b/src/plugins/squish/squishplugin.cpp index cb2da8c9e51..e86ec531662 100644 --- a/src/plugins/squish/squishplugin.cpp +++ b/src/plugins/squish/squishplugin.cpp @@ -48,6 +48,11 @@ public: ObjectsMapEditorFactory m_objectsMapEditorFactory; SquishOutputPane *m_outputPane = nullptr; SquishTools * m_squishTools = nullptr; + + SquishToolkitsPageFactory m_squishToolkitsPageFactory; + SquishScriptLanguagePageFactory m_squishScriptLanguagePageFactory; + SquishAUTPageFactory m_squishAUTPageFactory; + SquishGeneratorFactory m_squishGeneratorFactory; }; static SquishPluginPrivate *dd = nullptr; @@ -59,11 +64,6 @@ SquishPluginPrivate::SquishPluginPrivate() m_outputPane = SquishOutputPane::instance(); m_squishTools = new SquishTools; initializeMenuEntries(); - - ProjectExplorer::JsonWizardFactory::registerPageFactory(new SquishToolkitsPageFactory); - ProjectExplorer::JsonWizardFactory::registerPageFactory(new SquishScriptLanguagePageFactory); - ProjectExplorer::JsonWizardFactory::registerPageFactory(new SquishAUTPageFactory); - ProjectExplorer::JsonWizardFactory::registerGeneratorFactory(new SquishGeneratorFactory); } SquishPluginPrivate::~SquishPluginPrivate() diff --git a/src/plugins/vcsbase/vcsplugin.cpp b/src/plugins/vcsbase/vcsplugin.cpp index 62619b18d86..ee6461e603f 100644 --- a/src/plugins/vcsbase/vcsplugin.cpp +++ b/src/plugins/vcsbase/vcsplugin.cpp @@ -72,6 +72,9 @@ public: VcsPlugin *q; QStandardItemModel *m_nickNameModel = nullptr; + + VcsConfigurationPageFactory m_vcsConfigurationPageFactory; + VcsCommandPageFactory m_vcsCommandPageFactory; }; static VcsPlugin *m_instance = nullptr; @@ -100,9 +103,6 @@ void VcsPlugin::initialize() return result; }); - JsonWizardFactory::registerPageFactory(new Internal::VcsConfigurationPageFactory); - JsonWizardFactory::registerPageFactory(new Internal::VcsCommandPageFactory); - JsExpander::registerGlobalObject("Vcs"); MacroExpander *expander = globalMacroExpander();