From 5c1d8c52a57200a80e8a3156eba777d754ab8366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sat, 10 Aug 2024 10:40:20 +0200 Subject: [PATCH] Start refactoring setting system for plugins/extensions. --- include/uibase/extensions/extensionsetting.h | 89 ++++++++++++++++++++ include/uibase/imodinterface.h | 22 ++--- include/uibase/iplugin.h | 21 +++-- include/uibase/pluginsetting.h | 22 +---- src/CMakeLists.txt | 1 + src/extension.cpp | 6 +- 6 files changed, 122 insertions(+), 39 deletions(-) create mode 100644 include/uibase/extensions/extensionsetting.h diff --git a/include/uibase/extensions/extensionsetting.h b/include/uibase/extensions/extensionsetting.h new file mode 100644 index 00000000..cd1ee7f6 --- /dev/null +++ b/include/uibase/extensions/extensionsetting.h @@ -0,0 +1,89 @@ +#pragma once + +#include +#include + +namespace MOBase +{ + +// class representing a group of settings +// +class SettingGroup +{ +public: + SettingGroup(QString const& name, QString const& title, QString const& description) + : m_Name{name}, m_Title{title}, m_Description{description} + {} + + // return the (internal) name of this group, localization independent + // + const auto& name() const { return m_Name; } + + // retrieve the title of this group, can be localized + // + const auto& title() const { return m_Title; } + + // retrieve the description of this group, can be localized + // + const auto& description() const { return m_Description; } + +private: + QString m_Name, m_Title, m_Description; +}; + +// class that represents an extension or a plugin setting +// +class Setting +{ +public: + // deprecated constructor that was previously available as PluginSettin + // + [[deprecated]] Setting(const QString& name, const QString& description, + const QVariant& defaultValue) + : m_Name{name}, m_Title{name}, m_Description{description}, m_Group{}, + m_DefaultValue{defaultValue} + {} + + Setting(const QString& name, const QString& title, const QString& description, + const QVariant& defaultValue) + : m_Name{name}, m_Title{title}, m_Description{description}, m_Group{}, + m_DefaultValue{defaultValue} + {} + + Setting(const QString& name, const QString& title, const QString& description, + const QString& group, const QVariant& defaultValue) + : m_Name{name}, m_Title{title}, m_Description{description}, m_Group{group}, + m_DefaultValue{defaultValue} + {} + +public: + // return the (internal) name of this setting, localization independent + // + const auto& name() const { return m_Name; } + + // retrieve the title of this setting, can be localized + // + const auto& title() const { return m_Title; } + + // retrieve the description of this setting, can be localized + // + const auto& description() const { return m_Description; } + + // retrieve the name of the group this settings belongs to or an empty string if there + // is none + // + const auto& group() const { return m_Group; } + + // retrieve the default value of this setting + // + const auto& defaultValue() const { return m_DefaultValue; } + +private: + QString m_Name; + QString m_Title; + QString m_Description; + QString m_Group; + QVariant m_DefaultValue; +}; + +} // namespace MOBase diff --git a/include/uibase/imodinterface.h b/include/uibase/imodinterface.h index ca8c6ff6..b093aea5 100644 --- a/include/uibase/imodinterface.h +++ b/include/uibase/imodinterface.h @@ -269,17 +269,17 @@ class IModInterface virtual void setUrl(const QString& url) = 0; public: // Plugin operations: - /** - * @brief Retrieve the specified setting in this mod for a plugin. - * - * @param pluginName Name of the plugin for which to retrieve a setting. This should - * always be IPlugin::name() unless you have a really good reason to access - * settings of another plugin. - * @param key Identifier of the setting. - * @param defaultValue The default value to return if the setting does not exist. - * - * @return the setting, if found, or the default value. - */ + /** + * @brief Retrieve the specified setting in this mod for a plugin. + * + * @param pluginName Name of the plugin for which to retrieve a setting. This should + * always be IPlugin::name() unless you have a really good reason to access + * settings of another plugin. + * @param key Identifier of the setting. + * @param defaultValue The default value to return if the setting does not exist. + * + * @return the setting, if found, or the default value. + */ virtual QVariant pluginSetting(const QString& pluginName, const QString& key, const QVariant& defaultValue = QVariant()) const = 0; diff --git a/include/uibase/iplugin.h b/include/uibase/iplugin.h index c86d774b..5f8b0cb5 100644 --- a/include/uibase/iplugin.h +++ b/include/uibase/iplugin.h @@ -21,14 +21,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #ifndef IPLUGIN_H #define IPLUGIN_H -#include "imoinfo.h" -#include "pluginrequirements.h" -#include "pluginsetting.h" -#include "versioninfo.h" +#include + #include #include #include -#include + +#include "extensions/extensionsetting.h" +#include "imoinfo.h" +#include "pluginrequirements.h" + +// deprecated header +#include "pluginsetting.h" namespace MOBase { @@ -88,7 +92,12 @@ class IPlugin * @note Plugin can store "hidden" (from the user) settings using * IOrganizer::persistent / IOrganizer::setPersistent. */ - virtual QList settings() const = 0; + virtual QList settings() const = 0; + + /** + * @return the list of groups for settings. + */ + virtual QList settingGroups() const { return {}; } /** * @return whether the plugin should be enabled by default diff --git a/include/uibase/pluginsetting.h b/include/uibase/pluginsetting.h index 6844f4d0..0b760d55 100644 --- a/include/uibase/pluginsetting.h +++ b/include/uibase/pluginsetting.h @@ -21,29 +21,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #ifndef PLUGINSETTING_H #define PLUGINSETTING_H -#include -#include -#include +#include "extensions/extensionsetting.h" namespace MOBase { -/** - * @brief struct to hold the user-configurable parameters a plugin accepts. The purpose - * of this struct is only to inform the application what settings to offer to the user, - * it does not hold the actual value - */ -struct PluginSetting -{ - PluginSetting(const QString& key, const QString& description, - const QVariant& defaultValue) - : key(key), description(description), defaultValue(defaultValue) - {} - - QString key; - QString description; - QVariant defaultValue; -}; +// deprecated alias +using PluginSetting [[deprecated]] = Setting; } // namespace MOBase diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ebd77919..73d4b318 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,6 +35,7 @@ set(root_headers ) set(extension_headers ../include/uibase/extensions/extension.h + ../include/uibase/extensions/extensionsetting.h ../include/uibase/extensions/iextensionlist.h ../include/uibase/extensions/ipluginloader.h ../include/uibase/extensions/requirements.h diff --git a/src/extension.cpp b/src/extension.cpp index bc50efdc..898beabb 100644 --- a/src/extension.cpp +++ b/src/extension.cpp @@ -96,10 +96,10 @@ ExtensionMetaData::ExtensionMetaData(std::filesystem::path const& path, Version::ParseMode::SemVer); } catch (InvalidVersionException const& ex) { throw InvalidExtensionMetaDataException( - std::format("invalid or missing version '{}'", jsonData["version"].toString())); + std::format("invalid or missing version '{}': {}", + jsonData["version"].toString(), ex.what())); } - // TODO: name of the key // translation context m_TranslationContext = jsonData["translation-context"].toString(""); @@ -337,7 +337,7 @@ TranslationExtension::parseTranslation(std::filesystem::path const& extensionFol QLocale locale(identifier); name = QString("%1 (%2)") .arg(locale.nativeLanguageName()) - .arg(locale.nativeCountryName()); + .arg(locale.nativeTerritoryName()); } return std::make_shared(identifier.toStdString(), name.toStdString(),