GTableModel.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 GTableView;
  10. class GModelNotification {
  11. public:
  12. enum Type {
  13. Invalid = 0,
  14. ModelUpdated,
  15. };
  16. explicit GModelNotification(Type type, const GModelIndex& index = GModelIndex())
  17. : m_type(type)
  18. , m_index(index)
  19. {
  20. }
  21. Type type() const { return m_type; }
  22. GModelIndex index() const { return m_index; }
  23. private:
  24. Type m_type { Invalid };
  25. GModelIndex m_index;
  26. };
  27. class GTableModel {
  28. public:
  29. struct ColumnMetadata {
  30. int preferred_width { 0 };
  31. TextAlignment text_alignment { TextAlignment::CenterLeft };
  32. };
  33. virtual ~GTableModel();
  34. virtual int row_count() const = 0;
  35. virtual int column_count() const = 0;
  36. virtual String row_name(int) const { return { }; }
  37. virtual String column_name(int) const { return { }; }
  38. virtual ColumnMetadata column_metadata(int) const { return { }; }
  39. virtual GVariant data(int row, int column) const = 0;
  40. virtual void update() = 0;
  41. virtual void activate(const GModelIndex&) { }
  42. bool is_valid(GModelIndex index) const
  43. {
  44. return index.row() >= 0 && index.row() < row_count() && index.column() >= 0 && index.column() < column_count();
  45. }
  46. void set_selected_index(const GModelIndex& index)
  47. {
  48. if (is_valid(index))
  49. m_selected_index = index;
  50. }
  51. GModelIndex selected_index() const { return m_selected_index; }
  52. void register_view(Badge<GTableView>, GTableView&);
  53. void unregister_view(Badge<GTableView>, GTableView&);
  54. protected:
  55. GTableModel();
  56. void for_each_view(Function<void(GTableView&)>);
  57. void did_update();
  58. private:
  59. HashTable<GTableView*> m_views;
  60. GModelIndex m_selected_index;
  61. };