GModel.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 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 { False, True };
  24. Sortable sortable { Sortable::True };
  25. };
  26. enum class Role {
  27. Display,
  28. Sort,
  29. Custom,
  30. ForegroundColor,
  31. BackgroundColor,
  32. Icon
  33. };
  34. virtual ~GModel();
  35. virtual int row_count(const GModelIndex& = GModelIndex()) const = 0;
  36. virtual int column_count(const GModelIndex& = GModelIndex()) const = 0;
  37. virtual String row_name(int) const { return {}; }
  38. virtual String column_name(int) const { return {}; }
  39. virtual ColumnMetadata column_metadata(int) const { return {}; }
  40. virtual GVariant data(const GModelIndex&, Role = Role::Display) const = 0;
  41. virtual void update() = 0;
  42. virtual GModelIndex parent_index(const GModelIndex&) const { return {}; }
  43. virtual GModelIndex index(int row, int column = 0, const GModelIndex& = GModelIndex()) const { return create_index(row, column); }
  44. virtual GModelIndex sibling(int row, int column, const GModelIndex& parent) const;
  45. virtual bool is_editable(const GModelIndex&) const { return false; }
  46. virtual void set_data(const GModelIndex&, const GVariant&) {}
  47. bool is_valid(const GModelIndex& index) const
  48. {
  49. return index.row() >= 0 && index.row() < row_count() && index.column() >= 0 && index.column() < column_count();
  50. }
  51. void set_selected_index(const GModelIndex&);
  52. GModelIndex selected_index() const { return m_selected_index; }
  53. virtual int key_column() const { return -1; }
  54. virtual GSortOrder sort_order() const { return GSortOrder::None; }
  55. virtual void set_key_column_and_sort_order(int, GSortOrder) {}
  56. void register_view(Badge<GAbstractView>, GAbstractView&);
  57. void unregister_view(Badge<GAbstractView>, GAbstractView&);
  58. Function<void()> on_update;
  59. Function<void(const GModelIndex&)> on_selection_changed;
  60. protected:
  61. GModel();
  62. void for_each_view(Function<void(GAbstractView&)>);
  63. void did_update();
  64. GModelIndex create_index(int row, int column, const void* data = nullptr) const;
  65. private:
  66. HashTable<GAbstractView*> m_views;
  67. GModelIndex m_selected_index;
  68. };
  69. inline GModelIndex GModelIndex::parent() const
  70. {
  71. return m_model ? m_model->parent_index(*this) : GModelIndex();
  72. }