PropertyParser.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "PropertyParser.h"
  2. #include "../Model/Property.h"
  3. using namespace WallpaperEngine::Data::Parsers;
  4. using namespace WallpaperEngine::Data::Model;
  5. PropertySharedPtr PropertyParser::parse (const JSON& it, const std::string& name) {
  6. // type might not be included, in which case means the same as a group
  7. const auto type = it.optional ("type");
  8. if (type == "color") {
  9. return parseColor (it, name);
  10. }
  11. if (type == "bool") {
  12. return parseBoolean (it, name);
  13. }
  14. if (type == "slider") {
  15. return parseSlider (it, name);
  16. }
  17. if (type == "combo") {
  18. return parseCombo (it, name);
  19. }
  20. if (type == "text") {
  21. return parseText (it, name);
  22. }
  23. if (type == "scenetexture") {
  24. return parseSceneTexture (it, name);
  25. }
  26. if (type == "file") {
  27. return parseFile (it, name);
  28. }
  29. if (type == "textinput") {
  30. return parseTextInput (it, name);
  31. }
  32. if (type.has_value () && type != "group") {
  33. // show the error and ignore this property
  34. sLog.error ("Unexpected type for property: ", type);
  35. sLog.error (it.dump ());
  36. }
  37. return nullptr;
  38. }
  39. PropertySharedPtr PropertyParser::parseCombo (const JSON& it, const std::string& name) {
  40. std::map <std::string, std::string> optionsMap = {};
  41. const auto options = it.require ("options", "Combo property must have options");
  42. if (!options.is_array ()) {
  43. sLog.exception ("Property combo options should be an array");
  44. }
  45. for (auto& cur : options) {
  46. if (!cur.is_object ()) {
  47. continue;
  48. }
  49. const auto value = cur.require ("value", "Combo option must have a value");
  50. optionsMap.emplace (
  51. cur.require ("label", "Combo option must have a label"),
  52. value.is_number () ? std::to_string (value.get <int> ()) : value.get <std::string> ()
  53. );
  54. }
  55. const auto value = it.require ("value", "Combo property must have a value");
  56. return std::make_shared <PropertyCombo> (PropertyData {
  57. .name = name,
  58. .text = it.optional <std::string> ("text", ""),
  59. }, ComboData {
  60. .values = optionsMap
  61. }, value.is_number () ? std::to_string (value.get <int> ()) : value.get <std::string> ());
  62. }
  63. PropertySharedPtr PropertyParser::parseColor (const JSON& it, const std::string& name) {
  64. return std::make_shared <PropertyColor> (PropertyData {
  65. .name = name,
  66. .text = it.optional <std::string> ("text", ""),
  67. }, it.require ("value", "Property must have a value"));
  68. }
  69. PropertySharedPtr PropertyParser::parseBoolean (const JSON& it, const std::string& name) {
  70. return std::make_shared <PropertyBoolean> (PropertyData {
  71. .name = name,
  72. .text = it.optional <std::string> ("text", ""),
  73. }, it.require ("value", "Property must have a value"));
  74. }
  75. PropertySharedPtr PropertyParser::parseSlider (const JSON& it, const std::string& name) {
  76. return std::make_shared <PropertySlider> (PropertyData {
  77. .name = name,
  78. .text = it.optional <std::string> ("text", ""),
  79. }, SliderData {
  80. .min = it.optional ("min", 0.0f),
  81. .max = it.optional ("max", 0.0f),
  82. .step = it.optional ("step", 0.0f),
  83. }, it.require ("value", "Property must have a value"));
  84. }
  85. PropertySharedPtr PropertyParser::parseText (const JSON& it, const std::string& name) {
  86. return std::make_shared <PropertyText> (PropertyData {
  87. .name = name,
  88. .text = it.optional <std::string> ("text", ""),
  89. });
  90. }
  91. PropertySharedPtr PropertyParser::parseSceneTexture (const JSON& it, const std::string& name) {
  92. return std::make_shared <PropertySceneTexture> (PropertyData {
  93. .name = name,
  94. .text = it.optional <std::string> ("text", ""),
  95. }, it.require ("value", "Property must have a value"));
  96. }
  97. PropertySharedPtr PropertyParser::parseFile (const JSON& it, const std::string& name) {
  98. return std::make_shared <PropertyFile> (PropertyData {
  99. .name = name,
  100. .text = it.optional <std::string> ("text", ""),
  101. }, it.optional <std::string> ("value", ""));
  102. }
  103. PropertySharedPtr PropertyParser::parseTextInput (const JSON& it, const std::string& name) {
  104. return std::make_shared <PropertyTextInput> (PropertyData {
  105. .name = name,
  106. .text = it.optional <std::string> ("text", ""),
  107. }, it.require ("value", "Property must have a value").dump ());
  108. }