Model.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Badge.h>
  10. #include <AK/Function.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/HashTable.h>
  13. #include <AK/RefCounted.h>
  14. #include <AK/String.h>
  15. #include <AK/WeakPtr.h>
  16. #include <LibCore/MimeData.h>
  17. #include <LibGUI/Forward.h>
  18. #include <LibGUI/ModelIndex.h>
  19. #include <LibGUI/ModelRole.h>
  20. #include <LibGUI/ModelSelection.h>
  21. #include <LibGUI/Variant.h>
  22. #include <LibGfx/Forward.h>
  23. #include <LibGfx/TextAlignment.h>
  24. namespace GUI {
  25. enum class SortOrder {
  26. None,
  27. Ascending,
  28. Descending
  29. };
  30. class ModelClient {
  31. public:
  32. virtual ~ModelClient() = default;
  33. virtual void model_did_update(unsigned flags) = 0;
  34. virtual void model_did_insert_rows([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { }
  35. virtual void model_did_insert_columns([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { }
  36. virtual void model_did_move_rows([[maybe_unused]] ModelIndex const& source_parent, [[maybe_unused]] int first, [[maybe_unused]] int last, [[maybe_unused]] ModelIndex const& target_parent, [[maybe_unused]] int target_index) { }
  37. virtual void model_did_move_columns([[maybe_unused]] ModelIndex const& source_parent, [[maybe_unused]] int first, [[maybe_unused]] int last, [[maybe_unused]] ModelIndex const& target_parent, [[maybe_unused]] int target_index) { }
  38. virtual void model_did_delete_rows([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { }
  39. virtual void model_did_delete_columns([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { }
  40. };
  41. class Model : public RefCounted<Model> {
  42. public:
  43. enum UpdateFlag {
  44. DontInvalidateIndices = 0,
  45. InvalidateAllIndices = 1 << 0,
  46. };
  47. enum MatchesFlag {
  48. AllMatching = 0,
  49. FirstMatchOnly = 1 << 0,
  50. CaseInsensitive = 1 << 1,
  51. MatchAtStart = 1 << 2,
  52. MatchFull = 1 << 3,
  53. };
  54. virtual ~Model();
  55. virtual int row_count(ModelIndex const& = ModelIndex()) const = 0;
  56. virtual int column_count(ModelIndex const& = ModelIndex()) const = 0;
  57. virtual String column_name(int) const { return {}; }
  58. virtual Variant data(ModelIndex const&, ModelRole = ModelRole::Display) const = 0;
  59. virtual TriState data_matches(ModelIndex const&, Variant const&) const { return TriState::Unknown; }
  60. virtual void invalidate();
  61. virtual ModelIndex parent_index(ModelIndex const&) const { return {}; }
  62. virtual ModelIndex index(int row, int column = 0, ModelIndex const& parent = ModelIndex()) const;
  63. virtual bool is_editable(ModelIndex const&) const { return false; }
  64. virtual bool is_searchable() const { return false; }
  65. virtual void set_data(ModelIndex const&, Variant const&) { }
  66. virtual int tree_column() const { return 0; }
  67. virtual bool accepts_drag(ModelIndex const&, Vector<String> const& mime_types) const;
  68. virtual Vector<ModelIndex> matches(StringView, unsigned = MatchesFlag::AllMatching, ModelIndex const& = ModelIndex()) { return {}; }
  69. virtual bool is_column_sortable([[maybe_unused]] int column_index) const { return true; }
  70. virtual void sort([[maybe_unused]] int column, SortOrder) { }
  71. bool is_within_range(ModelIndex const& index) const
  72. {
  73. auto parent_index = this->parent_index(index);
  74. return index.row() >= 0 && index.row() < row_count(parent_index) && index.column() >= 0 && index.column() < column_count(parent_index);
  75. }
  76. virtual StringView drag_data_type() const { return {}; }
  77. virtual RefPtr<Core::MimeData> mime_data(ModelSelection const&) const;
  78. void register_view(Badge<AbstractView>, AbstractView&);
  79. void unregister_view(Badge<AbstractView>, AbstractView&);
  80. void register_client(ModelClient&);
  81. void unregister_client(ModelClient&);
  82. WeakPtr<PersistentHandle> register_persistent_index(Badge<PersistentModelIndex>, ModelIndex const&);
  83. protected:
  84. Model();
  85. void for_each_view(Function<void(AbstractView&)>);
  86. void for_each_client(Function<void(ModelClient&)>);
  87. void did_update(unsigned flags = UpdateFlag::InvalidateAllIndices);
  88. static bool string_matches(StringView str, StringView needle, unsigned flags)
  89. {
  90. auto case_sensitivity = (flags & CaseInsensitive) ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive;
  91. if (flags & MatchFull)
  92. return str.length() == needle.length() && str.starts_with(needle, case_sensitivity);
  93. if (flags & MatchAtStart)
  94. return str.starts_with(needle, case_sensitivity);
  95. return str.contains(needle, case_sensitivity);
  96. }
  97. ModelIndex create_index(int row, int column, void const* data = nullptr) const;
  98. void begin_insert_rows(ModelIndex const& parent, int first, int last);
  99. void begin_insert_columns(ModelIndex const& parent, int first, int last);
  100. void begin_move_rows(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index);
  101. void begin_move_columns(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index);
  102. void begin_delete_rows(ModelIndex const& parent, int first, int last);
  103. void begin_delete_columns(ModelIndex const& parent, int first, int last);
  104. void end_insert_rows();
  105. void end_insert_columns();
  106. void end_move_rows();
  107. void end_move_columns();
  108. void end_delete_rows();
  109. void end_delete_columns();
  110. void change_persistent_index_list(Vector<ModelIndex> const& old_indices, Vector<ModelIndex> const& new_indices);
  111. private:
  112. enum class OperationType {
  113. Invalid = 0,
  114. Insert,
  115. Move,
  116. Delete,
  117. Reset
  118. };
  119. enum class Direction {
  120. Row,
  121. Column
  122. };
  123. struct Operation {
  124. OperationType type { OperationType::Invalid };
  125. Direction direction { Direction::Row };
  126. ModelIndex source_parent;
  127. int first { 0 };
  128. int last { 0 };
  129. ModelIndex target_parent;
  130. int target { 0 };
  131. Operation(OperationType type)
  132. : type(type)
  133. {
  134. }
  135. Operation(OperationType type, Direction direction, ModelIndex const& parent, int first, int last)
  136. : type(type)
  137. , direction(direction)
  138. , source_parent(parent)
  139. , first(first)
  140. , last(last)
  141. {
  142. }
  143. Operation(OperationType type, Direction direction, ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target)
  144. : type(type)
  145. , direction(direction)
  146. , source_parent(source_parent)
  147. , first(first)
  148. , last(last)
  149. , target_parent(target_parent)
  150. , target(target)
  151. {
  152. }
  153. };
  154. void handle_insert(Operation const&);
  155. void handle_move(Operation const&);
  156. void handle_delete(Operation const&);
  157. template<bool IsRow>
  158. void save_deleted_indices(ModelIndex const& parent, int first, int last);
  159. HashMap<ModelIndex, OwnPtr<PersistentHandle>> m_persistent_handles;
  160. Vector<Operation> m_operation_stack;
  161. // NOTE: We need to save which indices have been deleted before the delete
  162. // actually happens, because we can't figure out which persistent handles
  163. // belong to us in end_delete_rows/columns (because accessing the parents of
  164. // the indices might be impossible).
  165. Vector<Vector<ModelIndex>> m_deleted_indices_stack;
  166. HashTable<AbstractView*> m_views;
  167. HashTable<ModelClient*> m_clients;
  168. };
  169. inline ModelIndex ModelIndex::parent() const
  170. {
  171. return m_model ? m_model->parent_index(*this) : ModelIndex();
  172. }
  173. }