GModel.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <LibGUI/GModelIndex.h>
  7. #include <LibGUI/GVariant.h>
  8. #include <SharedGraphics/TextAlignment.h>
  9. class Font;
  10. class GAbstractView;
  11. enum class GSortOrder { None, Ascending, Descending };
  12. class GModelNotification {
  13. public:
  14. enum Type {
  15. Invalid = 0,
  16. ModelUpdated,
  17. };
  18. explicit GModelNotification(Type type, const GModelIndex& index = GModelIndex())
  19. : m_type(type)
  20. , m_index(index)
  21. {
  22. }
  23. Type type() const { return m_type; }
  24. GModelIndex index() const { return m_index; }
  25. private:
  26. Type m_type { Invalid };
  27. GModelIndex m_index;
  28. };
  29. class GModel : public Retainable<GModel> {
  30. public:
  31. struct ColumnMetadata {
  32. int preferred_width { 0 };
  33. TextAlignment text_alignment { TextAlignment::CenterLeft };
  34. const Font* font { nullptr };
  35. };
  36. enum class Role { Display, Sort, Custom, ForegroundColor, BackgroundColor, Icon };
  37. virtual ~GModel();
  38. virtual int row_count(const GModelIndex& = GModelIndex()) const = 0;
  39. virtual int column_count(const GModelIndex& = GModelIndex()) const = 0;
  40. virtual String row_name(int) const { return { }; }
  41. virtual String column_name(int) const { return { }; }
  42. virtual ColumnMetadata column_metadata(int) const { return { }; }
  43. virtual GVariant data(const GModelIndex&, Role = Role::Display) const = 0;
  44. virtual void update() = 0;
  45. virtual GModelIndex parent_index(const GModelIndex&) const { return { }; }
  46. virtual GModelIndex index(int row, int column = 0, const GModelIndex& = GModelIndex()) const { return create_index(row, column); }
  47. virtual void activate(const GModelIndex&) { }
  48. bool is_valid(const GModelIndex& index) const
  49. {
  50. return index.row() >= 0 && index.row() < row_count() && index.column() >= 0 && index.column() < column_count();
  51. }
  52. void set_selected_index(const GModelIndex&);
  53. GModelIndex selected_index() const { return m_selected_index; }
  54. bool activates_on_selection() const { return m_activates_on_selection; }
  55. void set_activates_on_selection(bool b) { m_activates_on_selection = b; }
  56. virtual int key_column() const { return -1; }
  57. virtual GSortOrder sort_order() const { return GSortOrder::None; }
  58. virtual void set_key_column_and_sort_order(int, GSortOrder) { }
  59. void register_view(Badge<GAbstractView>, GAbstractView&);
  60. void unregister_view(Badge<GAbstractView>, GAbstractView&);
  61. Function<void(GModel&)> on_model_update;
  62. Function<void(const GModelIndex&)> on_selection_changed;
  63. protected:
  64. GModel();
  65. void for_each_view(Function<void(GAbstractView&)>);
  66. void did_update();
  67. GModelIndex create_index(int row, int column, void* data = nullptr) const;
  68. private:
  69. HashTable<GAbstractView*> m_views;
  70. GModelIndex m_selected_index;
  71. bool m_activates_on_selection { false };
  72. };
  73. inline GModelIndex GModelIndex::parent() const
  74. {
  75. return m_model ? m_model->parent_index(*this) : GModelIndex();
  76. }