GModel.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. };
  41. enum class Role {
  42. Display,
  43. Sort,
  44. Custom,
  45. ForegroundColor,
  46. BackgroundColor,
  47. Icon
  48. };
  49. virtual ~GModel();
  50. virtual int row_count(const GModelIndex& = GModelIndex()) const = 0;
  51. virtual int column_count(const GModelIndex& = GModelIndex()) const = 0;
  52. virtual String row_name(int) const { return {}; }
  53. virtual String column_name(int) const { return {}; }
  54. virtual ColumnMetadata column_metadata(int) const { return {}; }
  55. virtual GVariant data(const GModelIndex&, Role = Role::Display) const = 0;
  56. virtual void update() = 0;
  57. virtual GModelIndex parent_index(const GModelIndex&) const { return {}; }
  58. virtual GModelIndex index(int row, int column = 0, const GModelIndex& = GModelIndex()) const { return create_index(row, column); }
  59. virtual GModelIndex sibling(int row, int column, const GModelIndex& parent) const;
  60. virtual bool is_editable(const GModelIndex&) const { return false; }
  61. virtual void set_data(const GModelIndex&, const GVariant&) {}
  62. bool is_valid(const GModelIndex& index) const
  63. {
  64. return index.row() >= 0 && index.row() < row_count() && index.column() >= 0 && index.column() < column_count();
  65. }
  66. void set_selected_index(const GModelIndex&);
  67. GModelIndex selected_index() const { return m_selected_index; }
  68. virtual int key_column() const { return -1; }
  69. virtual GSortOrder sort_order() const { return GSortOrder::None; }
  70. virtual void set_key_column_and_sort_order(int, GSortOrder) {}
  71. void register_view(Badge<GAbstractView>, GAbstractView&);
  72. void unregister_view(Badge<GAbstractView>, GAbstractView&);
  73. Function<void(GModel&)> on_model_update;
  74. Function<void(const GModelIndex&)> on_selection_changed;
  75. protected:
  76. GModel();
  77. void for_each_view(Function<void(GAbstractView&)>);
  78. void did_update();
  79. GModelIndex create_index(int row, int column, void* data = nullptr) const;
  80. private:
  81. HashTable<GAbstractView*> m_views;
  82. GModelIndex m_selected_index;
  83. };
  84. inline GModelIndex GModelIndex::parent() const
  85. {
  86. return m_model ? m_model->parent_index(*this) : GModelIndex();
  87. }