main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <AK/JsonArray.h>
  2. #include <AK/JsonObject.h>
  3. #include <AK/JsonValue.h>
  4. #include <AK/LogStream.h>
  5. #include <AK/StringBuilder.h>
  6. #include <LibCore/CFile.h>
  7. #include <stdio.h>
  8. int main(int argc, char** argv)
  9. {
  10. if (argc != 2) {
  11. printf("usage: %s <form-file>\n", argv[0]);
  12. return 0;
  13. }
  14. CFile file(argv[1]);
  15. if (!file.open(CIODevice::ReadOnly)) {
  16. fprintf(stderr, "Error: Cannot open %s: %s\n", argv[1], file.error_string());
  17. return 1;
  18. }
  19. auto file_contents = file.read_all();
  20. auto json = JsonValue::from_string(file_contents);
  21. if (!json.is_object()) {
  22. fprintf(stderr, "Malformed input\n");
  23. return 1;
  24. }
  25. auto name = json.as_object().get("name").to_string();
  26. auto widgets = json.as_object().get("widgets");
  27. if (!widgets.is_array()) {
  28. fprintf(stderr, "Malformed input\n");
  29. return 1;
  30. }
  31. dbg() << "#pragma once";
  32. widgets.as_array().for_each([&](auto& value) {
  33. const JsonObject& widget_object = value.as_object();
  34. auto class_name = widget_object.get("class").to_string();
  35. dbg() << "#include <LibGUI/" << class_name << ".h>";
  36. });
  37. dbg() << "struct UI_" << name << " {";
  38. dbg() << " ObjectPtr<GWidget> main_widget;";
  39. widgets.as_array().for_each([&](auto& value) {
  40. ASSERT(value.is_object());
  41. const JsonObject& widget_object = value.as_object();
  42. auto name = widget_object.get("name").to_string();
  43. auto class_name = widget_object.get("class").to_string();
  44. dbg() << " ObjectPtr<" << class_name << "> " << name << ";";
  45. });
  46. dbg() << " UI_" << name << "();";
  47. dbg() << "};";
  48. dbg() << "UI_" << name << "::UI_" << name << "()";
  49. dbg() << "{";
  50. dbg() << " main_widget = GWidget::construct(nullptr);";
  51. dbg() << " main_widget->set_fill_with_background_color(true);";
  52. widgets.as_array().for_each([&](auto& value) {
  53. ASSERT(value.is_object());
  54. const JsonObject& widget_object = value.as_object();
  55. auto name = widget_object.get("name").to_string();
  56. auto class_name = widget_object.get("class").to_string();
  57. dbg() << " " << name << " = " << class_name << "::construct(main_widget);";
  58. widget_object.for_each_member([&](auto& property_name, const JsonValue& property_value) {
  59. if (property_name == "class")
  60. return;
  61. String value;
  62. if (property_value.is_null())
  63. value = "{}";
  64. else
  65. value = property_value.serialized<StringBuilder>();
  66. dbg() << " " << name << "->set_" << property_name << "(" << value << ");";
  67. });
  68. dbg() << "";
  69. });
  70. dbg() << "}";
  71. return 0;
  72. }