ItemListModel.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2019-2020, Jesse Buhgaiar <jooster669@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullRefPtr.h>
  8. #include <AK/Vector.h>
  9. #include <LibGUI/Model.h>
  10. namespace GUI {
  11. template<typename T, typename Container = Vector<T>, typename ColumnNameListType = void>
  12. class ItemListModel : public Model {
  13. public:
  14. static constexpr auto IsTwoDimensional = requires(Container data)
  15. {
  16. requires !IsVoid<ColumnNameListType>;
  17. data.at(0).at(0);
  18. data.at(0).size();
  19. };
  20. // Substitute 'void' for a dummy u8.
  21. using ColumnNamesT = Conditional<IsVoid<ColumnNameListType>, u8, ColumnNameListType>;
  22. static NonnullRefPtr<ItemListModel> create(const Container& data, const ColumnNamesT& column_names, const Optional<size_t>& row_count = {}) requires(IsTwoDimensional)
  23. {
  24. return adopt_ref(*new ItemListModel<T, Container, ColumnNameListType>(data, column_names, row_count));
  25. }
  26. static NonnullRefPtr<ItemListModel> create(const Container& data, const Optional<size_t>& row_count = {}) requires(!IsTwoDimensional)
  27. {
  28. return adopt_ref(*new ItemListModel<T, Container>(data, row_count));
  29. }
  30. virtual ~ItemListModel() override { }
  31. virtual int row_count(const ModelIndex&) const override
  32. {
  33. return m_provided_row_count.has_value() ? *m_provided_row_count : m_data.size();
  34. }
  35. virtual int column_count(const ModelIndex& index) const override
  36. {
  37. // if it's 2D (e.g. Vector<Vector<T>>)
  38. if constexpr (IsTwoDimensional) {
  39. if (index.is_valid())
  40. return m_data.at(index.row()).size();
  41. if (m_data.size())
  42. return m_data.at(0).size();
  43. return 0;
  44. }
  45. // Otherwise, let's just assume it's 1D.
  46. return 1;
  47. }
  48. virtual String column_name(int index) const override
  49. {
  50. if constexpr (IsTwoDimensional)
  51. return m_column_names[index];
  52. return "Data";
  53. }
  54. virtual Variant data(const ModelIndex& index, ModelRole role) const override
  55. {
  56. if (role == ModelRole::TextAlignment)
  57. return Gfx::TextAlignment::CenterLeft;
  58. if (role == ModelRole::Display) {
  59. if constexpr (IsTwoDimensional)
  60. return m_data.at(index.row()).at(index.column());
  61. else
  62. return m_data.at(index.row());
  63. }
  64. return {};
  65. }
  66. virtual void update() override
  67. {
  68. did_update();
  69. }
  70. protected:
  71. explicit ItemListModel(const Container& data, Optional<size_t> row_count = {}) requires(!IsTwoDimensional)
  72. : m_data(data)
  73. , m_provided_row_count(move(row_count))
  74. {
  75. }
  76. explicit ItemListModel(const Container& data, const ColumnNamesT& column_names, Optional<size_t> row_count = {}) requires(IsTwoDimensional)
  77. : m_data(data)
  78. , m_column_names(column_names)
  79. , m_provided_row_count(move(row_count))
  80. {
  81. }
  82. const Container& m_data;
  83. ColumnNamesT m_column_names;
  84. Optional<size_t> m_provided_row_count;
  85. };
  86. }