SortingProxyModel.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <LibGUI/AbstractView.h>
  8. #include <LibGUI/SortingProxyModel.h>
  9. namespace GUI {
  10. SortingProxyModel::SortingProxyModel(NonnullRefPtr<Model> source)
  11. : m_source(move(source))
  12. {
  13. m_source->register_client(*this);
  14. invalidate();
  15. }
  16. SortingProxyModel::~SortingProxyModel()
  17. {
  18. m_source->unregister_client(*this);
  19. }
  20. void SortingProxyModel::invalidate(unsigned int flags)
  21. {
  22. if (flags == UpdateFlag::DontInvalidateIndices) {
  23. sort(m_last_key_column, m_last_sort_order);
  24. } else {
  25. m_mappings.clear();
  26. // FIXME: This is really harsh, but without precise invalidation, not much we can do.
  27. for_each_view([&](auto& view) {
  28. view.set_cursor({}, AbstractView::SelectionUpdate::None);
  29. view.selection().clear();
  30. });
  31. }
  32. did_update(flags);
  33. }
  34. void SortingProxyModel::model_did_update(unsigned flags)
  35. {
  36. invalidate(flags);
  37. }
  38. bool SortingProxyModel::accepts_drag(const ModelIndex& proxy_index, const Vector<String>& mime_types) const
  39. {
  40. return source().accepts_drag(map_to_source(proxy_index), mime_types);
  41. }
  42. int SortingProxyModel::row_count(const ModelIndex& proxy_index) const
  43. {
  44. return source().row_count(map_to_source(proxy_index));
  45. }
  46. int SortingProxyModel::column_count(const ModelIndex& proxy_index) const
  47. {
  48. return source().column_count(map_to_source(proxy_index));
  49. }
  50. ModelIndex SortingProxyModel::map_to_source(const ModelIndex& proxy_index) const
  51. {
  52. if (!proxy_index.is_valid())
  53. return {};
  54. VERIFY(proxy_index.model() == this);
  55. VERIFY(proxy_index.internal_data());
  56. auto& index_mapping = *static_cast<Mapping*>(proxy_index.internal_data());
  57. auto it = m_mappings.find(index_mapping.source_parent);
  58. VERIFY(it != m_mappings.end());
  59. auto& mapping = *it->value;
  60. if (static_cast<size_t>(proxy_index.row()) >= mapping.source_rows.size() || proxy_index.column() >= column_count())
  61. return {};
  62. int source_row = mapping.source_rows[proxy_index.row()];
  63. int source_column = proxy_index.column();
  64. return source().index(source_row, source_column, it->key);
  65. }
  66. ModelIndex SortingProxyModel::map_to_proxy(const ModelIndex& source_index) const
  67. {
  68. if (!source_index.is_valid())
  69. return {};
  70. VERIFY(source_index.model() == m_source);
  71. auto source_parent = source_index.parent();
  72. auto it = const_cast<SortingProxyModel*>(this)->build_mapping(source_parent);
  73. auto& mapping = *it->value;
  74. if (source_index.row() >= static_cast<int>(mapping.proxy_rows.size()) || source_index.column() >= column_count())
  75. return {};
  76. int proxy_row = mapping.proxy_rows[source_index.row()];
  77. int proxy_column = source_index.column();
  78. if (proxy_row < 0 || proxy_column < 0)
  79. return {};
  80. return create_index(proxy_row, proxy_column, &mapping);
  81. }
  82. String SortingProxyModel::column_name(int column) const
  83. {
  84. return source().column_name(column);
  85. }
  86. Variant SortingProxyModel::data(const ModelIndex& proxy_index, ModelRole role) const
  87. {
  88. return source().data(map_to_source(proxy_index), role);
  89. }
  90. void SortingProxyModel::update()
  91. {
  92. source().update();
  93. }
  94. StringView SortingProxyModel::drag_data_type() const
  95. {
  96. return source().drag_data_type();
  97. }
  98. bool SortingProxyModel::less_than(const ModelIndex& index1, const ModelIndex& index2) const
  99. {
  100. auto data1 = index1.data(m_sort_role);
  101. auto data2 = index2.data(m_sort_role);
  102. if (data1.is_string() && data2.is_string())
  103. return data1.as_string().to_lowercase() < data2.as_string().to_lowercase();
  104. return data1 < data2;
  105. }
  106. ModelIndex SortingProxyModel::index(int row, int column, const ModelIndex& parent) const
  107. {
  108. if (row < 0 || column < 0)
  109. return {};
  110. auto source_parent = map_to_source(parent);
  111. const_cast<SortingProxyModel*>(this)->build_mapping(source_parent);
  112. auto it = m_mappings.find(source_parent);
  113. VERIFY(it != m_mappings.end());
  114. auto& mapping = *it->value;
  115. if (row >= static_cast<int>(mapping.source_rows.size()) || column >= column_count())
  116. return {};
  117. return create_index(row, column, &mapping);
  118. }
  119. ModelIndex SortingProxyModel::parent_index(const ModelIndex& proxy_index) const
  120. {
  121. if (!proxy_index.is_valid())
  122. return {};
  123. VERIFY(proxy_index.model() == this);
  124. VERIFY(proxy_index.internal_data());
  125. auto& index_mapping = *static_cast<Mapping*>(proxy_index.internal_data());
  126. auto it = m_mappings.find(index_mapping.source_parent);
  127. VERIFY(it != m_mappings.end());
  128. return map_to_proxy(it->value->source_parent);
  129. }
  130. void SortingProxyModel::sort_mapping(Mapping& mapping, int column, SortOrder sort_order)
  131. {
  132. if (column == -1) {
  133. int row_count = source().row_count(mapping.source_parent);
  134. for (int i = 0; i < row_count; ++i) {
  135. mapping.source_rows[i] = i;
  136. mapping.proxy_rows[i] = i;
  137. }
  138. return;
  139. }
  140. auto old_source_rows = mapping.source_rows;
  141. int row_count = source().row_count(mapping.source_parent);
  142. for (int i = 0; i < row_count; ++i)
  143. mapping.source_rows[i] = i;
  144. quick_sort(mapping.source_rows, [&](auto row1, auto row2) -> bool {
  145. bool is_less_than = less_than(source().index(row1, column, mapping.source_parent), source().index(row2, column, mapping.source_parent));
  146. return sort_order == SortOrder::Ascending ? is_less_than : !is_less_than;
  147. });
  148. for (int i = 0; i < row_count; ++i)
  149. mapping.proxy_rows[mapping.source_rows[i]] = i;
  150. // FIXME: I really feel like this should be done at the view layer somehow.
  151. for_each_view([&](AbstractView& view) {
  152. // Update the view's cursor.
  153. auto cursor = view.cursor_index();
  154. if (cursor.is_valid() && cursor.parent() == mapping.source_parent) {
  155. for (size_t i = 0; i < mapping.source_rows.size(); ++i) {
  156. if (mapping.source_rows[i] == view.cursor_index().row()) {
  157. auto new_source_index = this->index(i, view.cursor_index().column(), mapping.source_parent);
  158. view.set_cursor(new_source_index, AbstractView::SelectionUpdate::None, false);
  159. break;
  160. }
  161. }
  162. }
  163. // Update the view's selection.
  164. view.selection().change_from_model({}, [&](ModelSelection& selection) {
  165. Vector<ModelIndex> selected_indices_in_source;
  166. Vector<ModelIndex> stale_indices_in_selection;
  167. selection.for_each_index([&](const ModelIndex& index) {
  168. if (index.parent() == mapping.source_parent) {
  169. stale_indices_in_selection.append(index);
  170. selected_indices_in_source.append(source().index(old_source_rows[index.row()], index.column(), mapping.source_parent));
  171. }
  172. });
  173. for (auto& index : stale_indices_in_selection) {
  174. selection.remove(index);
  175. }
  176. for (auto& index : selected_indices_in_source) {
  177. for (size_t i = 0; i < mapping.source_rows.size(); ++i) {
  178. if (mapping.source_rows[i] == index.row()) {
  179. auto new_source_index = this->index(i, index.column(), mapping.source_parent);
  180. selection.add(new_source_index);
  181. break;
  182. }
  183. }
  184. }
  185. });
  186. });
  187. }
  188. void SortingProxyModel::sort(int column, SortOrder sort_order)
  189. {
  190. for (auto& it : m_mappings) {
  191. auto& mapping = *it.value;
  192. sort_mapping(mapping, column, sort_order);
  193. }
  194. m_last_key_column = column;
  195. m_last_sort_order = sort_order;
  196. did_update(UpdateFlag::DontInvalidateIndices);
  197. }
  198. SortingProxyModel::InternalMapIterator SortingProxyModel::build_mapping(const ModelIndex& source_parent)
  199. {
  200. auto it = m_mappings.find(source_parent);
  201. if (it != m_mappings.end())
  202. return it;
  203. auto mapping = make<Mapping>();
  204. mapping->source_parent = source_parent;
  205. int row_count = source().row_count(source_parent);
  206. mapping->source_rows.resize(row_count);
  207. mapping->proxy_rows.resize(row_count);
  208. sort_mapping(*mapping, m_last_key_column, m_last_sort_order);
  209. if (source_parent.is_valid()) {
  210. auto source_grand_parent = source_parent.parent();
  211. build_mapping(source_grand_parent);
  212. }
  213. m_mappings.set(source_parent, move(mapping));
  214. return m_mappings.find(source_parent);
  215. }
  216. bool SortingProxyModel::is_column_sortable(int column_index) const
  217. {
  218. return source().is_column_sortable(column_index);
  219. }
  220. bool SortingProxyModel::is_editable(const ModelIndex& proxy_index) const
  221. {
  222. return source().is_editable(map_to_source(proxy_index));
  223. }
  224. void SortingProxyModel::set_data(const ModelIndex& proxy_index, const Variant& data)
  225. {
  226. source().set_data(map_to_source(proxy_index), data);
  227. }
  228. bool SortingProxyModel::is_searchable() const
  229. {
  230. return source().is_searchable();
  231. }
  232. Vector<ModelIndex, 1> SortingProxyModel::matches(const StringView& searching, unsigned flags, const ModelIndex& proxy_index)
  233. {
  234. auto found_indices = source().matches(searching, flags, map_to_source(proxy_index));
  235. for (size_t i = 0; i < found_indices.size(); i++)
  236. found_indices[i] = map_to_proxy(found_indices[i]);
  237. return found_indices;
  238. }
  239. }