JsonArrayModel.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/Model.h>
  30. namespace GUI {
  31. class JsonArrayModel final : public Model {
  32. public:
  33. struct FieldSpec {
  34. FieldSpec(const String& a_column_name, Gfx::TextAlignment a_text_alignment, Function<Variant(const JsonObject&)>&& a_massage_for_display, Function<Variant(const JsonObject&)>&& a_massage_for_sort = {}, Function<Variant(const JsonObject&)>&& a_massage_for_custom = {})
  35. : column_name(a_column_name)
  36. , text_alignment(a_text_alignment)
  37. , massage_for_display(move(a_massage_for_display))
  38. , massage_for_sort(move(a_massage_for_sort))
  39. , massage_for_custom(move(a_massage_for_custom))
  40. {
  41. }
  42. FieldSpec(const String& a_json_field_name, const String& a_column_name, Gfx::TextAlignment a_text_alignment)
  43. : json_field_name(a_json_field_name)
  44. , column_name(a_column_name)
  45. , text_alignment(a_text_alignment)
  46. {
  47. }
  48. String json_field_name;
  49. String column_name;
  50. Gfx::TextAlignment text_alignment;
  51. Function<Variant(const JsonObject&)> massage_for_display;
  52. Function<Variant(const JsonObject&)> massage_for_sort;
  53. Function<Variant(const JsonObject&)> massage_for_custom;
  54. };
  55. static NonnullRefPtr<JsonArrayModel> create(const String& json_path, Vector<FieldSpec>&& fields)
  56. {
  57. return adopt(*new JsonArrayModel(json_path, move(fields)));
  58. }
  59. virtual ~JsonArrayModel() override {}
  60. virtual int row_count(const ModelIndex& = ModelIndex()) const override { return m_array.size(); }
  61. virtual int column_count(const ModelIndex& = ModelIndex()) const override { return m_fields.size(); }
  62. virtual String column_name(int column) const override { return m_fields[column].column_name; }
  63. virtual ColumnMetadata column_metadata(int) const override;
  64. virtual Variant data(const ModelIndex&, Role = Role::Display) const override;
  65. virtual void update() override;
  66. const String& json_path() const { return m_json_path; }
  67. void set_json_path(const String& json_path);
  68. bool add(const Vector<JsonValue>&& fields);
  69. bool remove(int row);
  70. bool store();
  71. private:
  72. JsonArrayModel(const String& json_path, Vector<FieldSpec>&& fields)
  73. : m_json_path(json_path)
  74. , m_fields(move(fields))
  75. {
  76. }
  77. String m_json_path;
  78. Vector<FieldSpec> m_fields;
  79. JsonArray m_array;
  80. };
  81. }