mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 09:48:44 +02:00
DomainServerSettings handles any types of objects
This commit is contained in:
parent
4fd6094ea4
commit
2a5292732c
3 changed files with 82 additions and 75 deletions
|
@ -74,44 +74,24 @@
|
||||||
},
|
},
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
"name": "x-range",
|
"name": "x_range",
|
||||||
"label": "X range",
|
"label": "X range",
|
||||||
"can_set": true,
|
"can_set": true,
|
||||||
"placeholder": "0-16384"
|
"placeholder": "0-16384"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "y-range",
|
"name": "y_range",
|
||||||
"label": "Y range",
|
"label": "Y range",
|
||||||
"can_set": true,
|
"can_set": true,
|
||||||
"placeholder": "0-16384"
|
"placeholder": "0-16384"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "z-range",
|
"name": "z_range",
|
||||||
"label": "Z range",
|
"label": "Z range",
|
||||||
"can_set": true,
|
"can_set": true,
|
||||||
"placeholder": "0-16384"
|
"placeholder": "0-16384"
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
"default": {
|
|
||||||
"Zone_1":
|
|
||||||
{
|
|
||||||
"x-range": "10-82",
|
|
||||||
"y-range": "36-94",
|
|
||||||
"z-range": "38-84"
|
|
||||||
},
|
|
||||||
"Zone_2":
|
|
||||||
{
|
|
||||||
"x-range": "12-163",
|
|
||||||
"y-range": "64-134",
|
|
||||||
"z-range": "27-184"
|
|
||||||
},
|
|
||||||
"Zone_3":
|
|
||||||
{
|
|
||||||
"x-range": "463-632",
|
|
||||||
"y-range": "264-384",
|
|
||||||
"z-range": "843-937"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "enable_filter",
|
"name": "enable_filter",
|
||||||
|
|
|
@ -29,6 +29,7 @@ const QString SETTINGS_DESCRIPTION_RELATIVE_PATH = "/resources/describe-settings
|
||||||
const QString DESCRIPTION_SETTINGS_KEY = "settings";
|
const QString DESCRIPTION_SETTINGS_KEY = "settings";
|
||||||
const QString SETTING_DEFAULT_KEY = "default";
|
const QString SETTING_DEFAULT_KEY = "default";
|
||||||
const QString DESCRIPTION_NAME_KEY = "name";
|
const QString DESCRIPTION_NAME_KEY = "name";
|
||||||
|
const QString SETTING_DESCRIPTION_TYPE_KEY = "type";
|
||||||
|
|
||||||
DomainServerSettingsManager::DomainServerSettingsManager() :
|
DomainServerSettingsManager::DomainServerSettingsManager() :
|
||||||
_descriptionArray(),
|
_descriptionArray(),
|
||||||
|
@ -232,61 +233,87 @@ QJsonObject DomainServerSettingsManager::responseObjectForType(const QString& ty
|
||||||
return responseObject;
|
return responseObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool settingExists(const QString& groupName, const QString& settingName,
|
||||||
|
const QJsonArray& descriptionArray, QJsonValue& settingDescription) {
|
||||||
|
foreach(const QJsonValue& groupValue, descriptionArray) {
|
||||||
|
QJsonObject groupObject = groupValue.toObject();
|
||||||
|
if (groupObject[DESCRIPTION_NAME_KEY].toString() == groupName) {
|
||||||
|
|
||||||
|
foreach(const QJsonValue& settingValue, groupObject[DESCRIPTION_SETTINGS_KEY].toArray()) {
|
||||||
|
QJsonObject settingObject = settingValue.toObject();
|
||||||
|
if (settingObject[DESCRIPTION_NAME_KEY].toString() == settingName) {
|
||||||
|
settingDescription = settingObject[SETTING_DEFAULT_KEY];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
settingDescription = QJsonValue::Undefined;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
const QString SETTING_DESCRIPTION_TYPE_KEY = "type";
|
void updateSetting(const QString& key, const QJsonValue& newValue, QVariantMap& settingMap,
|
||||||
|
const QJsonValue& settingDescription) {
|
||||||
|
if (newValue.isString()) {
|
||||||
|
if (newValue.toString().isEmpty()) {
|
||||||
|
// this is an empty value, clear it in settings variant so the default is sent
|
||||||
|
settingMap.remove(key);
|
||||||
|
} else {
|
||||||
|
// make sure the resulting json value has the right type
|
||||||
|
const QString settingType = settingDescription.toObject()[SETTING_DESCRIPTION_TYPE_KEY].toString();
|
||||||
|
const QString INPUT_DOUBLE_TYPE = "double";
|
||||||
|
|
||||||
|
if (settingType == INPUT_DOUBLE_TYPE) {
|
||||||
|
settingMap[key] = newValue.toString().toDouble();
|
||||||
|
} else {
|
||||||
|
settingMap[key] = newValue.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (newValue.isBool()) {
|
||||||
|
settingMap[key] = newValue.toBool();
|
||||||
|
} else if (newValue.isObject()) {
|
||||||
|
if (!settingMap.contains(key)) {
|
||||||
|
// we don't have a map below this key yet, so set it up now
|
||||||
|
settingMap[key] = QVariantMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach(const QString childKey, newValue.toObject().keys()) {
|
||||||
|
updateSetting(childKey, newValue.toObject()[childKey],
|
||||||
|
*reinterpret_cast<QVariantMap*>(settingMap[key].data()),
|
||||||
|
settingDescription.toObject()[key]);
|
||||||
|
|
||||||
|
}
|
||||||
|
if (settingMap[key].toMap().isEmpty()) {
|
||||||
|
// we've cleared all of the settings below this value, so remove this one too
|
||||||
|
settingMap.remove(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void DomainServerSettingsManager::recurseJSONObjectAndOverwriteSettings(const QJsonObject& postedObject,
|
void DomainServerSettingsManager::recurseJSONObjectAndOverwriteSettings(const QJsonObject& postedObject,
|
||||||
QVariantMap& settingsVariant,
|
QVariantMap& settingsVariant,
|
||||||
QJsonArray descriptionArray) {
|
const QJsonArray& descriptionArray) {
|
||||||
|
foreach(const QString& groupKey, postedObject.keys()) {
|
||||||
qDebug() << "Posted object:" << postedObject;
|
QJsonValue groupValue = postedObject[groupKey];
|
||||||
foreach(const QString& key, postedObject.keys()) {
|
|
||||||
|
|
||||||
QJsonValue rootValue = postedObject[key];
|
|
||||||
qDebug() << "Key:" << key;
|
|
||||||
|
|
||||||
// we don't continue if this key is not present in our descriptionObject
|
foreach(const QString& settingKey, groupValue.toObject().keys()) {
|
||||||
foreach(const QJsonValue& groupValue, descriptionArray) {
|
QJsonValue settingValue = groupValue.toObject()[settingKey];
|
||||||
if (groupValue.toObject()[DESCRIPTION_NAME_KEY].toString() == key) {
|
|
||||||
QJsonObject groupObject = groupValue.toObject();
|
QJsonValue thisDescription;
|
||||||
if (rootValue.isString()) {
|
if (settingExists(groupKey, settingKey, descriptionArray, thisDescription)) {
|
||||||
if (rootValue.toString().isEmpty()) {
|
if (!settingsVariant.contains(groupKey)) {
|
||||||
// this is an empty value, clear it in settings variant so the default is sent
|
// we don't have a map below this key yet, so set it up now
|
||||||
settingsVariant.remove(key);
|
settingsVariant[groupKey] = QVariantMap();
|
||||||
} else {
|
}
|
||||||
QString settingType = groupObject[SETTING_DESCRIPTION_TYPE_KEY].toString();
|
|
||||||
|
QVariantMap& thisMap = *reinterpret_cast<QVariantMap*>(settingsVariant[groupKey].data());
|
||||||
const QString INPUT_DOUBLE_TYPE = "double";
|
updateSetting(settingKey, settingValue,
|
||||||
|
thisMap,
|
||||||
// make sure the resulting json value has the right type
|
thisDescription);
|
||||||
|
|
||||||
if (settingType == INPUT_DOUBLE_TYPE) {
|
if (settingsVariant[groupKey].toMap().empty()) {
|
||||||
settingsVariant[key] = rootValue.toString().toDouble();
|
// we've cleared all of the settings below this value, so remove this one too
|
||||||
} else {
|
settingsVariant.remove(groupKey);
|
||||||
settingsVariant[key] = rootValue.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (rootValue.isBool()) {
|
|
||||||
settingsVariant[key] = rootValue.toBool();
|
|
||||||
} else if (rootValue.isObject()) {
|
|
||||||
// there's a JSON Object to explore, so attempt to recurse into it
|
|
||||||
QJsonObject nextDescriptionObject = groupObject;
|
|
||||||
|
|
||||||
if (!settingsVariant.contains(key)) {
|
|
||||||
// we don't have a map below this key yet, so set it up now
|
|
||||||
settingsVariant[key] = QVariantMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
QVariantMap& thisMap = *reinterpret_cast<QVariantMap*>(settingsVariant[key].data());
|
|
||||||
|
|
||||||
recurseJSONObjectAndOverwriteSettings(rootValue.toObject(),
|
|
||||||
thisMap,
|
|
||||||
nextDescriptionObject[DESCRIPTION_SETTINGS_KEY].toArray());
|
|
||||||
|
|
||||||
if (thisMap.isEmpty()) {
|
|
||||||
// we've cleared all of the settings below this value, so remove this one too
|
|
||||||
settingsVariant.remove(key);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ public:
|
||||||
private:
|
private:
|
||||||
QJsonObject responseObjectForType(const QString& typeValue, bool isAuthenticated = false);
|
QJsonObject responseObjectForType(const QString& typeValue, bool isAuthenticated = false);
|
||||||
void recurseJSONObjectAndOverwriteSettings(const QJsonObject& postedObject, QVariantMap& settingsVariant,
|
void recurseJSONObjectAndOverwriteSettings(const QJsonObject& postedObject, QVariantMap& settingsVariant,
|
||||||
QJsonArray descriptionArray);
|
const QJsonArray& descriptionArray);
|
||||||
void persistToFile();
|
void persistToFile();
|
||||||
|
|
||||||
QJsonArray _descriptionArray;
|
QJsonArray _descriptionArray;
|
||||||
|
|
Loading…
Reference in a new issue