GModel.h 2.7 KB

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