DomainServerSettings handles any types of objects

This commit is contained in:
Atlante45 2014-10-07 18:01:32 -07:00
parent 4fd6094ea4
commit 2a5292732c
3 changed files with 82 additions and 75 deletions

View file

@ -74,44 +74,24 @@
},
"columns": [
{
"name": "x-range",
"name": "x_range",
"label": "X range",
"can_set": true,
"placeholder": "0-16384"
},
{
"name": "y-range",
"name": "y_range",
"label": "Y range",
"can_set": true,
"placeholder": "0-16384"
},
{
"name": "z-range",
"name": "z_range",
"label": "Z range",
"can_set": true,
"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",

View file

@ -29,6 +29,7 @@ const QString SETTINGS_DESCRIPTION_RELATIVE_PATH = "/resources/describe-settings
const QString DESCRIPTION_SETTINGS_KEY = "settings";
const QString SETTING_DEFAULT_KEY = "default";
const QString DESCRIPTION_NAME_KEY = "name";
const QString SETTING_DESCRIPTION_TYPE_KEY = "type";
DomainServerSettingsManager::DomainServerSettingsManager() :
_descriptionArray(),
@ -232,61 +233,87 @@ QJsonObject DomainServerSettingsManager::responseObjectForType(const QString& ty
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,
QVariantMap& settingsVariant,
QJsonArray descriptionArray) {
qDebug() << "Posted object:" << postedObject;
foreach(const QString& key, postedObject.keys()) {
QJsonValue rootValue = postedObject[key];
qDebug() << "Key:" << key;
const QJsonArray& descriptionArray) {
foreach(const QString& groupKey, postedObject.keys()) {
QJsonValue groupValue = postedObject[groupKey];
// we don't continue if this key is not present in our descriptionObject
foreach(const QJsonValue& groupValue, descriptionArray) {
if (groupValue.toObject()[DESCRIPTION_NAME_KEY].toString() == key) {
QJsonObject groupObject = groupValue.toObject();
if (rootValue.isString()) {
if (rootValue.toString().isEmpty()) {
// this is an empty value, clear it in settings variant so the default is sent
settingsVariant.remove(key);
} else {
QString settingType = groupObject[SETTING_DESCRIPTION_TYPE_KEY].toString();
const QString INPUT_DOUBLE_TYPE = "double";
// make sure the resulting json value has the right type
if (settingType == INPUT_DOUBLE_TYPE) {
settingsVariant[key] = rootValue.toString().toDouble();
} else {
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);
}
foreach(const QString& settingKey, groupValue.toObject().keys()) {
QJsonValue settingValue = groupValue.toObject()[settingKey];
QJsonValue thisDescription;
if (settingExists(groupKey, settingKey, descriptionArray, thisDescription)) {
if (!settingsVariant.contains(groupKey)) {
// we don't have a map below this key yet, so set it up now
settingsVariant[groupKey] = QVariantMap();
}
QVariantMap& thisMap = *reinterpret_cast<QVariantMap*>(settingsVariant[groupKey].data());
updateSetting(settingKey, settingValue,
thisMap,
thisDescription);
if (settingsVariant[groupKey].toMap().empty()) {
// we've cleared all of the settings below this value, so remove this one too
settingsVariant.remove(groupKey);
}
}
}

View file

@ -32,7 +32,7 @@ public:
private:
QJsonObject responseObjectForType(const QString& typeValue, bool isAuthenticated = false);
void recurseJSONObjectAndOverwriteSettings(const QJsonObject& postedObject, QVariantMap& settingsVariant,
QJsonArray descriptionArray);
const QJsonArray& descriptionArray);
void persistToFile();
QJsonArray _descriptionArray;