From 2a5292732c43807463c8d844ee595bbf6e8ec40e Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 7 Oct 2014 18:01:32 -0700 Subject: [PATCH] DomainServerSettings handles any types of objects --- .../resources/describe-settings.json | 28 +--- .../src/DomainServerSettingsManager.cpp | 127 +++++++++++------- .../src/DomainServerSettingsManager.h | 2 +- 3 files changed, 82 insertions(+), 75 deletions(-) diff --git a/domain-server/resources/describe-settings.json b/domain-server/resources/describe-settings.json index 2c0b0cebf4..d567e76a80 100644 --- a/domain-server/resources/describe-settings.json +++ b/domain-server/resources/describe-settings.json @@ -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", diff --git a/domain-server/src/DomainServerSettingsManager.cpp b/domain-server/src/DomainServerSettingsManager.cpp index 6055b917fe..022e42bf02 100644 --- a/domain-server/src/DomainServerSettingsManager.cpp +++ b/domain-server/src/DomainServerSettingsManager.cpp @@ -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(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(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(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); } } } diff --git a/domain-server/src/DomainServerSettingsManager.h b/domain-server/src/DomainServerSettingsManager.h index c2e8a7d90d..d735b77c1c 100644 --- a/domain-server/src/DomainServerSettingsManager.h +++ b/domain-server/src/DomainServerSettingsManager.h @@ -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;