GModel.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #include <AK/AKString.h>
  3. #include <AK/Badge.h>
  4. #include <AK/Function.h>
  5. #include <AK/HashTable.h>
  6. #include <AK/RefCounted.h>
  7. #include <LibGUI/GModelIndex.h>
  8. #include <LibGUI/GVariant.h>
  9. #include <LibDraw/TextAlignment.h>
  10. class Font;
  11. class GAbstractView;
  12. enum class GSortOrder {
  13. None,
  14. Ascending,
  15. Descending
  16. };
  17. class GModelNotification {
  18. public:
  19. enum Type {
  20. Invalid = 0,
  21. ModelUpdated,
  22. };
  23. explicit GModelNotification(Type type, const GModelIndex& index = GModelIndex())
  24. : m_type(type)
  25. , m_index(index)
  26. {
  27. }
  28. Type type() const { return m_type; }
  29. GModelIndex index() const { return m_index; }
  30. private:
  31. Type m_type { Invalid };
  32. GModelIndex m_index;
  33. };
  34. class GModel : public RefCounted<GModel> {
  35. public:
  36. struct ColumnMetadata {
  37. int preferred_width { 0 };
  38. TextAlignment text_alignment { TextAlignment::CenterLeft };
  39. const Font* font { nullptr };
  40. enum class Sortable { False, True };
  41. Sortable sortable { Sortable::True };
  42. };
  43. enum class Role {
  44. Display,
  45. Sort,
  46. Custom,
  47. ForegroundColor,
  48. BackgroundColor,
  49. Icon
  50. };
  51. virtual ~GModel();
  52. virtual int row_count(const GModelIndex& = GModelIndex()) const = 0;
  53. virtual int column_count(const GModelIndex& = GModelIndex()) const = 0;
  54. virtual String row_name(int) const { return {}; }
  55. virtual String column_name(int) const { return {}; }
  56. virtual ColumnMetadata column_metadata(int) const { return {}; }
  57. virtual GVariant data(const GModelIndex&, Role = Role::Display) const = 0;
  58. virtual void update() = 0;
  59. virtual GModelIndex parent_index(const GModelIndex&) const { return {}; }
  60. virtual GModelIndex index(int row, int column = 0, const GModelIndex& = GModelIndex()) const { return create_index(row, column); }
  61. virtual GModelIndex sibling(int row, int column, const GModelIndex& parent) const;
  62. virtual bool is_editable(const GModelIndex&) const { return false; }
  63. virtual void set_data(const GModelIndex&, const GVariant&) {}
  64. bool is_valid(const GModelIndex& index) const
  65. {
  66. return index.row() >= 0 && index.row() < row_count() && index.column() >= 0 && index.column() < column_count();
  67. }
  68. void set_selected_index(const GModelIndex&);
  69. GModelIndex selected_index() const { return m_selected_index; }
  70. virtual int key_column() const { return -1; }
  71. virtual GSortOrder sort_order() const { return GSortOrder::None; }
  72. virtual void set_key_column_and_sort_order(int, GSortOrder) {}
  73. void register_view(Badge<GAbstractView>, GAbstractView&);
  74. void unregister_view(Badge<GAbstractView>, GAbstractView&);
  75. Function<void(GModel&)> on_model_update;
  76. Function<void(const GModelIndex&)> on_selection_changed;
  77. protected:
  78. GModel();
  79. void for_each_view(Function<void(GAbstractView&)>);
  80. void did_update();
  81. GModelIndex create_index(int row, int column, void* data = nullptr) const;
  82. private:
  83. HashTable<GAbstractView*> m_views;
  84. GModelIndex m_selected_index;
  85. };
  86. inline GModelIndex GModelIndex::parent() const
  87. {
  88. return m_model ? m_model->parent_index(*this) : GModelIndex();
  89. }