GJsonArrayModel.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #pragma once
  27. #include <AK/JsonArray.h>
  28. #include <AK/JsonObject.h>
  29. #include <LibGUI/GModel.h>
  30. class GJsonArrayModel final : public GModel {
  31. public:
  32. struct FieldSpec {
  33. FieldSpec(const String& a_column_name, TextAlignment a_text_alignment, Function<GVariant(const JsonObject&)>&& a_massage_for_display, Function<GVariant(const JsonObject&)>&& a_massage_for_sort = {}, Function<GVariant(const JsonObject&)>&& a_massage_for_custom = {})
  34. : column_name(a_column_name)
  35. , text_alignment(a_text_alignment)
  36. , massage_for_display(move(a_massage_for_display))
  37. , massage_for_sort(move(a_massage_for_sort))
  38. , massage_for_custom(move(a_massage_for_custom))
  39. {
  40. }
  41. FieldSpec(const String& a_json_field_name, const String& a_column_name, TextAlignment a_text_alignment)
  42. : json_field_name(a_json_field_name)
  43. , column_name(a_column_name)
  44. , text_alignment(a_text_alignment)
  45. {
  46. }
  47. String json_field_name;
  48. String column_name;
  49. TextAlignment text_alignment;
  50. Function<GVariant(const JsonObject&)> massage_for_display;
  51. Function<GVariant(const JsonObject&)> massage_for_sort;
  52. Function<GVariant(const JsonObject&)> massage_for_custom;
  53. };
  54. static NonnullRefPtr<GJsonArrayModel> create(const String& json_path, Vector<FieldSpec>&& fields)
  55. {
  56. return adopt(*new GJsonArrayModel(json_path, move(fields)));
  57. }
  58. virtual ~GJsonArrayModel() override {}
  59. virtual int row_count(const GModelIndex& = GModelIndex()) const override { return m_array.size(); }
  60. virtual int column_count(const GModelIndex& = GModelIndex()) const override { return m_fields.size(); }
  61. virtual String column_name(int column) const override { return m_fields[column].column_name; }
  62. virtual ColumnMetadata column_metadata(int) const override;
  63. virtual GVariant data(const GModelIndex&, Role = Role::Display) const override;
  64. virtual void update() override;
  65. const String& json_path() const { return m_json_path; }
  66. void set_json_path(const String& json_path);
  67. private:
  68. GJsonArrayModel(const String& json_path, Vector<FieldSpec>&& fields)
  69. : m_json_path(json_path)
  70. , m_fields(move(fields))
  71. {
  72. }
  73. String m_json_path;
  74. Vector<FieldSpec> m_fields;
  75. JsonArray m_array;
  76. };