PropertyParser.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. value.is_number () ? std::to_string (value.get<int> ()) : value.get<std::string> (),
  52. cur.require ("label", "Combo option must have a label")
  53. );
  54. }
  55. const auto value = it.require ("value", "Combo property must have a value");
  56. return std::make_shared<PropertyCombo> (
  57. PropertyData {
  58. .name = name,
  59. .text = it.optional<std::string> ("text", ""),
  60. },
  61. ComboData { .values = optionsMap },
  62. value.is_number () ? std::to_string (value.get<int> ()) : value.get<std::string> ()
  63. );
  64. }
  65. PropertySharedPtr PropertyParser::parseColor (const JSON& it, const std::string& name) {
  66. return std::make_shared<PropertyColor> (
  67. PropertyData {
  68. .name = name,
  69. .text = it.optional<std::string> ("text", ""),
  70. },
  71. it.require ("value", "Property must have a value")
  72. );
  73. }
  74. PropertySharedPtr PropertyParser::parseBoolean (const JSON& it, const std::string& name) {
  75. return std::make_shared<PropertyBoolean> (
  76. PropertyData {
  77. .name = name,
  78. .text = it.optional<std::string> ("text", ""),
  79. },
  80. it.require ("value", "Property must have a value")
  81. );
  82. }
  83. PropertySharedPtr PropertyParser::parseSlider (const JSON& it, const std::string& name) {
  84. return std::make_shared<PropertySlider> (
  85. PropertyData {
  86. .name = name,
  87. .text = it.optional<std::string> ("text", ""),
  88. },
  89. SliderData {
  90. .min = it.optional ("min", 0.0f),
  91. .max = it.optional ("max", 0.0f),
  92. .step = it.optional ("step", 0.0f),
  93. },
  94. it.require ("value", "Property must have a value")
  95. );
  96. }
  97. PropertySharedPtr PropertyParser::parseText (const JSON& it, const std::string& name) {
  98. return std::make_shared<PropertyText> (PropertyData {
  99. .name = name,
  100. .text = it.optional<std::string> ("text", ""),
  101. });
  102. }
  103. PropertySharedPtr PropertyParser::parseSceneTexture (const JSON& it, const std::string& name) {
  104. return std::make_shared<PropertySceneTexture> (
  105. PropertyData {
  106. .name = name,
  107. .text = it.optional<std::string> ("text", ""),
  108. },
  109. it.require ("value", "Property must have a value")
  110. );
  111. }
  112. PropertySharedPtr PropertyParser::parseFile (const JSON& it, const std::string& name) {
  113. return std::make_shared<PropertyFile> (
  114. PropertyData {
  115. .name = name,
  116. .text = it.optional<std::string> ("text", ""),
  117. },
  118. it.optional<std::string> ("value", "")
  119. );
  120. }
  121. PropertySharedPtr PropertyParser::parseTextInput (const JSON& it, const std::string& name) {
  122. return std::make_shared<PropertyTextInput> (
  123. PropertyData {
  124. .name = name,
  125. .text = it.optional<std::string> ("text", ""),
  126. },
  127. it.require ("value", "Property must have a value").dump ()
  128. );
  129. }