main.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/JsonArray.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/JsonValue.h>
  29. #include <AK/LogStream.h>
  30. #include <AK/StringBuilder.h>
  31. #include <LibCore/File.h>
  32. #include <stdio.h>
  33. int main(int argc, char** argv)
  34. {
  35. if (argc != 2) {
  36. printf("usage: %s <form-file>\n", argv[0]);
  37. return 0;
  38. }
  39. auto file = Core::File::construct(argv[1]);
  40. if (!file->open(Core::IODevice::ReadOnly)) {
  41. fprintf(stderr, "Error: Cannot open %s: %s\n", argv[1], file->error_string());
  42. return 1;
  43. }
  44. auto file_contents = file->read_all();
  45. auto json = JsonValue::from_string(file_contents);
  46. if (!json.is_object()) {
  47. fprintf(stderr, "Malformed input\n");
  48. return 1;
  49. }
  50. auto name = json.as_object().get("name").to_string();
  51. auto widgets = json.as_object().get("widgets");
  52. if (!widgets.is_array()) {
  53. fprintf(stderr, "Malformed input\n");
  54. return 1;
  55. }
  56. dbg() << "#pragma once";
  57. widgets.as_array().for_each([&](auto& value) {
  58. const JsonObject& widget_object = value.as_object();
  59. auto class_name = widget_object.get("class").to_string();
  60. StringBuilder builder;
  61. builder.append('G');
  62. auto parts = class_name.split(':');
  63. builder.append(parts.last());
  64. dbg() << "#include <LibGUI/" << builder.to_string() << ".h>";
  65. });
  66. dbg() << "struct UI_" << name << " {";
  67. dbg() << " RefPtr<GUI::Widget> main_widget;";
  68. widgets.as_array().for_each([&](auto& value) {
  69. ASSERT(value.is_object());
  70. const JsonObject& widget_object = value.as_object();
  71. auto name = widget_object.get("name").to_string();
  72. auto class_name = widget_object.get("class").to_string();
  73. dbg() << " RefPtr<" << class_name << "> " << name << ";";
  74. });
  75. dbg() << " UI_" << name << "();";
  76. dbg() << "};";
  77. dbg() << "UI_" << name << "::UI_" << name << "()";
  78. dbg() << "{";
  79. dbg() << " main_widget = GUI::Widget::construct(nullptr);";
  80. dbg() << " main_widget->set_fill_with_background_color(true);";
  81. widgets.as_array().for_each([&](auto& value) {
  82. ASSERT(value.is_object());
  83. const JsonObject& widget_object = value.as_object();
  84. auto name = widget_object.get("name").to_string();
  85. auto class_name = widget_object.get("class").to_string();
  86. dbg() << " " << name << " = " << class_name << "::construct(main_widget);";
  87. widget_object.for_each_member([&](auto& property_name, const JsonValue& property_value) {
  88. if (property_name == "class")
  89. return;
  90. String value;
  91. if (property_value.is_null())
  92. value = "{}";
  93. else
  94. value = property_value.serialized<StringBuilder>();
  95. dbg() << " " << name << "->set_" << property_name << "(" << value << ");";
  96. });
  97. dbg() << "";
  98. });
  99. dbg() << "}";
  100. return 0;
  101. }