Model.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // NOTE: This is a public version of create_index() which is normally protected,
  84. // but this can be used when creating a model translator like in Ladybird.
  85. ModelIndex unsafe_create_index(int row, int column, void const* data = nullptr) const
  86. {
  87. return create_index(row, column, data);
  88. }
  89. protected:
  90. Model();
  91. void for_each_view(Function<void(AbstractView&)>);
  92. void for_each_client(Function<void(ModelClient&)>);
  93. void did_update(unsigned flags = UpdateFlag::InvalidateAllIndices);
  94. static bool string_matches(StringView str, StringView needle, unsigned flags)
  95. {
  96. auto case_sensitivity = (flags & CaseInsensitive) ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive;
  97. if (flags & MatchFull)
  98. return str.length() == needle.length() && str.starts_with(needle, case_sensitivity);
  99. if (flags & MatchAtStart)
  100. return str.starts_with(needle, case_sensitivity);
  101. return str.contains(needle, case_sensitivity);
  102. }
  103. ModelIndex create_index(int row, int column, void const* data = nullptr) const;
  104. void begin_insert_rows(ModelIndex const& parent, int first, int last);
  105. void begin_insert_columns(ModelIndex const& parent, int first, int last);
  106. void begin_move_rows(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index);
  107. void begin_move_columns(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index);
  108. void begin_delete_rows(ModelIndex const& parent, int first, int last);
  109. void begin_delete_columns(ModelIndex const& parent, int first, int last);
  110. void end_insert_rows();
  111. void end_insert_columns();
  112. void end_move_rows();
  113. void end_move_columns();
  114. void end_delete_rows();
  115. void end_delete_columns();
  116. void change_persistent_index_list(Vector<ModelIndex> const& old_indices, Vector<ModelIndex> const& new_indices);
  117. private:
  118. enum class OperationType {
  119. Invalid = 0,
  120. Insert,
  121. Move,
  122. Delete,
  123. Reset
  124. };
  125. enum class Direction {
  126. Row,
  127. Column
  128. };
  129. struct Operation {
  130. OperationType type { OperationType::Invalid };
  131. Direction direction { Direction::Row };
  132. ModelIndex source_parent;
  133. int first { 0 };
  134. int last { 0 };
  135. ModelIndex target_parent;
  136. int target { 0 };
  137. Operation(OperationType type)
  138. : type(type)
  139. {
  140. }
  141. Operation(OperationType type, Direction direction, ModelIndex const& parent, int first, int last)
  142. : type(type)
  143. , direction(direction)
  144. , source_parent(parent)
  145. , first(first)
  146. , last(last)
  147. {
  148. }
  149. Operation(OperationType type, Direction direction, ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target)
  150. : type(type)
  151. , direction(direction)
  152. , source_parent(source_parent)
  153. , first(first)
  154. , last(last)
  155. , target_parent(target_parent)
  156. , target(target)
  157. {
  158. }
  159. };
  160. void handle_insert(Operation const&);
  161. void handle_move(Operation const&);
  162. void handle_delete(Operation const&);
  163. template<bool IsRow>
  164. void save_deleted_indices(ModelIndex const& parent, int first, int last);
  165. HashMap<ModelIndex, OwnPtr<PersistentHandle>> m_persistent_handles;
  166. Vector<Operation> m_operation_stack;
  167. // NOTE: We need to save which indices have been deleted before the delete
  168. // actually happens, because we can't figure out which persistent handles
  169. // belong to us in end_delete_rows/columns (because accessing the parents of
  170. // the indices might be impossible).
  171. Vector<Vector<ModelIndex>> m_deleted_indices_stack;
  172. HashTable<AbstractView*> m_views;
  173. HashTable<ModelClient*> m_clients;
  174. };
  175. inline ModelIndex ModelIndex::parent() const
  176. {
  177. return m_model ? m_model->parent_index(*this) : ModelIndex();
  178. }
  179. }