Model.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. DontResizeColumns = 1 << 1,
  47. };
  48. enum MatchesFlag {
  49. AllMatching = 0,
  50. FirstMatchOnly = 1 << 0,
  51. CaseInsensitive = 1 << 1,
  52. MatchAtStart = 1 << 2,
  53. MatchFull = 1 << 3,
  54. };
  55. struct MatchResult {
  56. TriState matched { TriState::Unknown };
  57. int score { 0 };
  58. };
  59. virtual ~Model();
  60. virtual int row_count(ModelIndex const& = ModelIndex()) const = 0;
  61. virtual int column_count(ModelIndex const& = ModelIndex()) const = 0;
  62. virtual ErrorOr<String> column_name(int) const { return String {}; }
  63. virtual Variant data(ModelIndex const&, ModelRole = ModelRole::Display) const = 0;
  64. virtual MatchResult data_matches(ModelIndex const&, Variant const&) const { return {}; }
  65. virtual void invalidate();
  66. virtual ModelIndex parent_index(ModelIndex const&) const { return {}; }
  67. virtual ModelIndex index(int row, int column = 0, ModelIndex const& parent = ModelIndex()) const;
  68. virtual bool is_editable(ModelIndex const&) const { return false; }
  69. virtual bool is_searchable() const { return false; }
  70. virtual void set_data(ModelIndex const&, Variant const&) { }
  71. virtual int tree_column() const { return 0; }
  72. virtual bool accepts_drag(ModelIndex const&, Vector<String> const& mime_types) const;
  73. virtual Vector<ModelIndex> matches(StringView, unsigned = MatchesFlag::AllMatching, ModelIndex const& = ModelIndex()) { return {}; }
  74. virtual bool is_column_sortable([[maybe_unused]] int column_index) const { return true; }
  75. virtual void sort([[maybe_unused]] int column, SortOrder) { }
  76. bool is_within_range(ModelIndex const& index) const
  77. {
  78. auto parent_index = this->parent_index(index);
  79. return index.row() >= 0 && index.row() < row_count(parent_index) && index.column() >= 0 && index.column() < column_count(parent_index);
  80. }
  81. virtual StringView drag_data_type() const { return {}; }
  82. virtual RefPtr<Core::MimeData> mime_data(ModelSelection const&) const;
  83. void register_view(Badge<AbstractView>, AbstractView&);
  84. void unregister_view(Badge<AbstractView>, AbstractView&);
  85. void register_client(ModelClient&);
  86. void unregister_client(ModelClient&);
  87. WeakPtr<PersistentHandle> register_persistent_index(Badge<PersistentModelIndex>, ModelIndex const&);
  88. // NOTE: This is a public version of create_index() which is normally protected,
  89. // but this can be used when creating a model translator like in Ladybird.
  90. ModelIndex unsafe_create_index(int row, int column, void const* data = nullptr) const
  91. {
  92. return create_index(row, column, data);
  93. }
  94. protected:
  95. Model();
  96. void for_each_view(Function<void(AbstractView&)>);
  97. void for_each_client(Function<void(ModelClient&)>);
  98. void did_update(unsigned flags = UpdateFlag::InvalidateAllIndices);
  99. static bool string_matches(StringView str, StringView needle, unsigned flags)
  100. {
  101. auto case_sensitivity = (flags & CaseInsensitive) ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive;
  102. if (flags & MatchFull)
  103. return str.length() == needle.length() && str.starts_with(needle, case_sensitivity);
  104. if (flags & MatchAtStart)
  105. return str.starts_with(needle, case_sensitivity);
  106. return str.contains(needle, case_sensitivity);
  107. }
  108. ModelIndex create_index(int row, int column, void const* data = nullptr) const;
  109. void begin_insert_rows(ModelIndex const& parent, int first, int last);
  110. void begin_insert_columns(ModelIndex const& parent, int first, int last);
  111. void begin_move_rows(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index);
  112. void begin_move_columns(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index);
  113. void begin_delete_rows(ModelIndex const& parent, int first, int last);
  114. void begin_delete_columns(ModelIndex const& parent, int first, int last);
  115. void end_insert_rows();
  116. void end_insert_columns();
  117. void end_move_rows();
  118. void end_move_columns();
  119. void end_delete_rows();
  120. void end_delete_columns();
  121. void change_persistent_index_list(Vector<ModelIndex> const& old_indices, Vector<ModelIndex> const& new_indices);
  122. private:
  123. enum class OperationType {
  124. Invalid = 0,
  125. Insert,
  126. Move,
  127. Delete,
  128. Reset
  129. };
  130. enum class Direction {
  131. Row,
  132. Column
  133. };
  134. struct Operation {
  135. OperationType type { OperationType::Invalid };
  136. Direction direction { Direction::Row };
  137. ModelIndex source_parent;
  138. int first { 0 };
  139. int last { 0 };
  140. ModelIndex target_parent;
  141. int target { 0 };
  142. Operation(OperationType type)
  143. : type(type)
  144. {
  145. }
  146. Operation(OperationType type, Direction direction, ModelIndex const& parent, int first, int last)
  147. : type(type)
  148. , direction(direction)
  149. , source_parent(parent)
  150. , first(first)
  151. , last(last)
  152. {
  153. }
  154. Operation(OperationType type, Direction direction, ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target)
  155. : type(type)
  156. , direction(direction)
  157. , source_parent(source_parent)
  158. , first(first)
  159. , last(last)
  160. , target_parent(target_parent)
  161. , target(target)
  162. {
  163. }
  164. };
  165. void handle_insert(Operation const&);
  166. void handle_move(Operation const&);
  167. void handle_delete(Operation const&);
  168. template<bool IsRow>
  169. void save_deleted_indices(ModelIndex const& parent, int first, int last);
  170. HashMap<ModelIndex, OwnPtr<PersistentHandle>> m_persistent_handles;
  171. Vector<Operation> m_operation_stack;
  172. // NOTE: We need to save which indices have been deleted before the delete
  173. // actually happens, because we can't figure out which persistent handles
  174. // belong to us in end_delete_rows/columns (because accessing the parents of
  175. // the indices might be impossible).
  176. Vector<Vector<ModelIndex>> m_deleted_indices_stack;
  177. HashTable<AbstractView*> m_views;
  178. HashTable<ModelClient*> m_clients;
  179. };
  180. inline ModelIndex ModelIndex::parent() const
  181. {
  182. return m_model ? m_model->parent_index(*this) : ModelIndex();
  183. }
  184. }