Model.h 8.2 KB

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