Skip to content

Commit

Permalink
Start refactoring setting system for plugins/extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Holt59 committed Aug 10, 2024
1 parent 3f5e093 commit 47b1b3c
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 35 deletions.
89 changes: 89 additions & 0 deletions include/uibase/extensionsetting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#pragma once

#include <QString>
#include <QVariant>

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
22 changes: 11 additions & 11 deletions include/uibase/imodinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 7 additions & 2 deletions include/uibase/iplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef IPLUGIN_H
#define IPLUGIN_H

#include "extensionsetting.h"
#include "imoinfo.h"
#include "pluginrequirements.h"
#include "pluginsetting.h"
#include "versioninfo.h"
#include <QList>
#include <QObject>
Expand Down Expand Up @@ -88,7 +88,12 @@ class IPlugin
* @note Plugin can store "hidden" (from the user) settings using
* IOrganizer::persistent / IOrganizer::setPersistent.
*/
virtual QList<PluginSetting> settings() const = 0;
virtual QList<Setting> settings() const = 0;

/**
* @return the list of groups for settings.
*/
virtual QList<SettingGroup> settingGroups() const { return {}; }

/**
* @return whether the plugin should be enabled by default
Expand Down
22 changes: 3 additions & 19 deletions include/uibase/pluginsetting.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef PLUGINSETTING_H
#define PLUGINSETTING_H

#include <QList>
#include <QString>
#include <QVariant>
#include "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

Expand Down
6 changes: 3 additions & 3 deletions src/extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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("");

Expand Down Expand Up @@ -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<Translation>(identifier.toStdString(), name.toStdString(),
Expand Down

0 comments on commit 47b1b3c

Please sign in to comment.