GModel.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/Badge.h>
  28. #include <AK/Function.h>
  29. #include <AK/HashTable.h>
  30. #include <AK/RefCounted.h>
  31. #include <AK/String.h>
  32. #include <LibDraw/TextAlignment.h>
  33. #include <LibGUI/GModelIndex.h>
  34. #include <LibGUI/GVariant.h>
  35. class Font;
  36. class GAbstractView;
  37. enum class GSortOrder {
  38. None,
  39. Ascending,
  40. Descending
  41. };
  42. class GModel : public RefCounted<GModel> {
  43. public:
  44. struct ColumnMetadata {
  45. int preferred_width { 0 };
  46. TextAlignment text_alignment { TextAlignment::CenterLeft };
  47. const Font* font { nullptr };
  48. enum class Sortable {
  49. False,
  50. True,
  51. };
  52. Sortable sortable { Sortable::True };
  53. };
  54. enum class Role {
  55. Display,
  56. Sort,
  57. Custom,
  58. ForegroundColor,
  59. BackgroundColor,
  60. Icon,
  61. Font,
  62. DragData,
  63. };
  64. virtual ~GModel();
  65. virtual int row_count(const GModelIndex& = GModelIndex()) const = 0;
  66. virtual int column_count(const GModelIndex& = GModelIndex()) const = 0;
  67. virtual String row_name(int) const { return {}; }
  68. virtual String column_name(int) const { return {}; }
  69. virtual ColumnMetadata column_metadata(int) const { return {}; }
  70. virtual GVariant data(const GModelIndex&, Role = Role::Display) const = 0;
  71. virtual void update() = 0;
  72. virtual GModelIndex parent_index(const GModelIndex&) const { return {}; }
  73. virtual GModelIndex index(int row, int column = 0, const GModelIndex& = GModelIndex()) const { return create_index(row, column); }
  74. virtual GModelIndex sibling(int row, int column, const GModelIndex& parent) const;
  75. virtual bool is_editable(const GModelIndex&) const { return false; }
  76. virtual void set_data(const GModelIndex&, const GVariant&) {}
  77. virtual int tree_column() const { return 0; }
  78. bool is_valid(const GModelIndex& index) const
  79. {
  80. auto parent_index = this->parent_index(index);
  81. return index.row() >= 0 && index.row() < row_count(parent_index) && index.column() >= 0 && index.column() < column_count(parent_index);
  82. }
  83. virtual int key_column() const { return -1; }
  84. virtual GSortOrder sort_order() const { return GSortOrder::None; }
  85. virtual void set_key_column_and_sort_order(int, GSortOrder) {}
  86. virtual StringView drag_data_type() const { return {}; }
  87. void register_view(Badge<GAbstractView>, GAbstractView&);
  88. void unregister_view(Badge<GAbstractView>, GAbstractView&);
  89. Function<void()> on_update;
  90. protected:
  91. GModel();
  92. void for_each_view(Function<void(GAbstractView&)>);
  93. void did_update();
  94. GModelIndex create_index(int row, int column, const void* data = nullptr) const;
  95. private:
  96. HashTable<GAbstractView*> m_views;
  97. };
  98. inline GModelIndex GModelIndex::parent() const
  99. {
  100. return m_model ? m_model->parent_index(*this) : GModelIndex();
  101. }