Skip to content

Commit 47b1b3c

Browse files
committed
Start refactoring setting system for plugins/extensions.
1 parent 3f5e093 commit 47b1b3c

5 files changed

Lines changed: 113 additions & 35 deletions

File tree

include/uibase/extensionsetting.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#pragma once
2+
3+
#include <QString>
4+
#include <QVariant>
5+
6+
namespace MOBase
7+
{
8+
9+
// class representing a group of settings
10+
//
11+
class SettingGroup
12+
{
13+
public:
14+
SettingGroup(QString const& name, QString const& title, QString const& description)
15+
: m_Name{name}, m_Title{title}, m_Description{description}
16+
{}
17+
18+
// return the (internal) name of this group, localization independent
19+
//
20+
const auto& name() const { return m_Name; }
21+
22+
// retrieve the title of this group, can be localized
23+
//
24+
const auto& title() const { return m_Title; }
25+
26+
// retrieve the description of this group, can be localized
27+
//
28+
const auto& description() const { return m_Description; }
29+
30+
private:
31+
QString m_Name, m_Title, m_Description;
32+
};
33+
34+
// class that represents an extension or a plugin setting
35+
//
36+
class Setting
37+
{
38+
public:
39+
// deprecated constructor that was previously available as PluginSettin
40+
//
41+
[[deprecated]] Setting(const QString& name, const QString& description,
42+
const QVariant& defaultValue)
43+
: m_Name{name}, m_Title{name}, m_Description{description}, m_Group{},
44+
m_DefaultValue{defaultValue}
45+
{}
46+
47+
Setting(const QString& name, const QString& title, const QString& description,
48+
const QVariant& defaultValue)
49+
: m_Name{name}, m_Title{title}, m_Description{description}, m_Group{},
50+
m_DefaultValue{defaultValue}
51+
{}
52+
53+
Setting(const QString& name, const QString& title, const QString& description,
54+
const QString& group, const QVariant& defaultValue)
55+
: m_Name{name}, m_Title{title}, m_Description{description}, m_Group{group},
56+
m_DefaultValue{defaultValue}
57+
{}
58+
59+
public:
60+
// return the (internal) name of this setting, localization independent
61+
//
62+
const auto& name() const { return m_Name; }
63+
64+
// retrieve the title of this setting, can be localized
65+
//
66+
const auto& title() const { return m_Title; }
67+
68+
// retrieve the description of this setting, can be localized
69+
//
70+
const auto& description() const { return m_Description; }
71+
72+
// retrieve the name of the group this settings belongs to or an empty string if there
73+
// is none
74+
//
75+
const auto& group() const { return m_Group; }
76+
77+
// retrieve the default value of this setting
78+
//
79+
const auto& defaultValue() const { return m_DefaultValue; }
80+
81+
private:
82+
QString m_Name;
83+
QString m_Title;
84+
QString m_Description;
85+
QString m_Group;
86+
QVariant m_DefaultValue;
87+
};
88+
89+
} // namespace MOBase

include/uibase/imodinterface.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,17 @@ class IModInterface
269269
virtual void setUrl(const QString& url) = 0;
270270

271271
public: // Plugin operations:
272-
/**
273-
* @brief Retrieve the specified setting in this mod for a plugin.
274-
*
275-
* @param pluginName Name of the plugin for which to retrieve a setting. This should
276-
* always be IPlugin::name() unless you have a really good reason to access
277-
* settings of another plugin.
278-
* @param key Identifier of the setting.
279-
* @param defaultValue The default value to return if the setting does not exist.
280-
*
281-
* @return the setting, if found, or the default value.
282-
*/
272+
/**
273+
* @brief Retrieve the specified setting in this mod for a plugin.
274+
*
275+
* @param pluginName Name of the plugin for which to retrieve a setting. This should
276+
* always be IPlugin::name() unless you have a really good reason to access
277+
* settings of another plugin.
278+
* @param key Identifier of the setting.
279+
* @param defaultValue The default value to return if the setting does not exist.
280+
*
281+
* @return the setting, if found, or the default value.
282+
*/
283283
virtual QVariant pluginSetting(const QString& pluginName, const QString& key,
284284
const QVariant& defaultValue = QVariant()) const = 0;
285285

include/uibase/iplugin.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2121
#ifndef IPLUGIN_H
2222
#define IPLUGIN_H
2323

24+
#include "extensionsetting.h"
2425
#include "imoinfo.h"
2526
#include "pluginrequirements.h"
26-
#include "pluginsetting.h"
2727
#include "versioninfo.h"
2828
#include <QList>
2929
#include <QObject>
@@ -88,7 +88,12 @@ class IPlugin
8888
* @note Plugin can store "hidden" (from the user) settings using
8989
* IOrganizer::persistent / IOrganizer::setPersistent.
9090
*/
91-
virtual QList<PluginSetting> settings() const = 0;
91+
virtual QList<Setting> settings() const = 0;
92+
93+
/**
94+
* @return the list of groups for settings.
95+
*/
96+
virtual QList<SettingGroup> settingGroups() const { return {}; }
9297

9398
/**
9499
* @return whether the plugin should be enabled by default

include/uibase/pluginsetting.h

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2121
#ifndef PLUGINSETTING_H
2222
#define PLUGINSETTING_H
2323

24-
#include <QList>
25-
#include <QString>
26-
#include <QVariant>
24+
#include "extensionsetting.h"
2725

2826
namespace MOBase
2927
{
3028

31-
/**
32-
* @brief struct to hold the user-configurable parameters a plugin accepts. The purpose
33-
* of this struct is only to inform the application what settings to offer to the user,
34-
* it does not hold the actual value
35-
*/
36-
struct PluginSetting
37-
{
38-
PluginSetting(const QString& key, const QString& description,
39-
const QVariant& defaultValue)
40-
: key(key), description(description), defaultValue(defaultValue)
41-
{}
42-
43-
QString key;
44-
QString description;
45-
QVariant defaultValue;
46-
};
29+
// deprecated alias
30+
using PluginSetting [[ deprecated ]] = Setting;
4731

4832
} // namespace MOBase
4933

src/extension.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ ExtensionMetaData::ExtensionMetaData(std::filesystem::path const& path,
9696
Version::ParseMode::SemVer);
9797
} catch (InvalidVersionException const& ex) {
9898
throw InvalidExtensionMetaDataException(
99-
std::format("invalid or missing version '{}'", jsonData["version"].toString()));
99+
std::format("invalid or missing version '{}': {}",
100+
jsonData["version"].toString(), ex.what()));
100101
}
101102

102-
// TODO: name of the key
103103
// translation context
104104
m_TranslationContext = jsonData["translation-context"].toString("");
105105

@@ -337,7 +337,7 @@ TranslationExtension::parseTranslation(std::filesystem::path const& extensionFol
337337
QLocale locale(identifier);
338338
name = QString("%1 (%2)")
339339
.arg(locale.nativeLanguageName())
340-
.arg(locale.nativeCountryName());
340+
.arg(locale.nativeTerritoryName());
341341
}
342342

343343
return std::make_shared<Translation>(identifier.toStdString(), name.toStdString(),

0 commit comments

Comments
 (0)