Model.h 7.8 KB

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