SortingProxyModel.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/QuickSort.h>
  27. #include <LibGUI/AbstractView.h>
  28. #include <LibGUI/SortingProxyModel.h>
  29. namespace GUI {
  30. SortingProxyModel::SortingProxyModel(NonnullRefPtr<Model> source)
  31. : m_source(move(source))
  32. {
  33. m_source->register_client(*this);
  34. invalidate();
  35. }
  36. SortingProxyModel::~SortingProxyModel()
  37. {
  38. m_source->unregister_client(*this);
  39. }
  40. void SortingProxyModel::invalidate(unsigned int flags)
  41. {
  42. if (flags == UpdateFlag::DontInvalidateIndexes) {
  43. sort(m_last_key_column, m_last_sort_order);
  44. } else {
  45. m_mappings.clear();
  46. // FIXME: This is really harsh, but without precise invalidation, not much we can do.
  47. for_each_view([&](auto& view) {
  48. view.set_cursor({}, AbstractView::SelectionUpdate::None);
  49. view.selection().clear();
  50. });
  51. }
  52. did_update(flags);
  53. }
  54. void SortingProxyModel::model_did_update(unsigned flags)
  55. {
  56. invalidate(flags);
  57. }
  58. bool SortingProxyModel::accepts_drag(const ModelIndex& proxy_index, const Vector<String>& mime_types) const
  59. {
  60. return source().accepts_drag(map_to_source(proxy_index), mime_types);
  61. }
  62. int SortingProxyModel::row_count(const ModelIndex& proxy_index) const
  63. {
  64. return source().row_count(map_to_source(proxy_index));
  65. }
  66. int SortingProxyModel::column_count(const ModelIndex& proxy_index) const
  67. {
  68. return source().column_count(map_to_source(proxy_index));
  69. }
  70. ModelIndex SortingProxyModel::map_to_source(const ModelIndex& proxy_index) const
  71. {
  72. if (!proxy_index.is_valid())
  73. return {};
  74. VERIFY(proxy_index.model() == this);
  75. VERIFY(proxy_index.internal_data());
  76. auto& index_mapping = *static_cast<Mapping*>(proxy_index.internal_data());
  77. auto it = m_mappings.find(index_mapping.source_parent);
  78. VERIFY(it != m_mappings.end());
  79. auto& mapping = *it->value;
  80. if (static_cast<size_t>(proxy_index.row()) >= mapping.source_rows.size() || proxy_index.column() >= column_count())
  81. return {};
  82. int source_row = mapping.source_rows[proxy_index.row()];
  83. int source_column = proxy_index.column();
  84. return source().index(source_row, source_column, it->key);
  85. }
  86. ModelIndex SortingProxyModel::map_to_proxy(const ModelIndex& source_index) const
  87. {
  88. if (!source_index.is_valid())
  89. return {};
  90. VERIFY(source_index.model() == m_source);
  91. auto source_parent = source_index.parent();
  92. auto it = const_cast<SortingProxyModel*>(this)->build_mapping(source_parent);
  93. auto& mapping = *it->value;
  94. if (source_index.row() >= static_cast<int>(mapping.proxy_rows.size()) || source_index.column() >= column_count())
  95. return {};
  96. int proxy_row = mapping.proxy_rows[source_index.row()];
  97. int proxy_column = source_index.column();
  98. if (proxy_row < 0 || proxy_column < 0)
  99. return {};
  100. return create_index(proxy_row, proxy_column, &mapping);
  101. }
  102. String SortingProxyModel::column_name(int column) const
  103. {
  104. return source().column_name(column);
  105. }
  106. Variant SortingProxyModel::data(const ModelIndex& proxy_index, ModelRole role) const
  107. {
  108. return source().data(map_to_source(proxy_index), role);
  109. }
  110. void SortingProxyModel::update()
  111. {
  112. source().update();
  113. }
  114. StringView SortingProxyModel::drag_data_type() const
  115. {
  116. return source().drag_data_type();
  117. }
  118. bool SortingProxyModel::less_than(const ModelIndex& index1, const ModelIndex& index2) const
  119. {
  120. auto data1 = index1.data(m_sort_role);
  121. auto data2 = index2.data(m_sort_role);
  122. if (data1.is_string() && data2.is_string())
  123. return data1.as_string().to_lowercase() < data2.as_string().to_lowercase();
  124. return data1 < data2;
  125. }
  126. ModelIndex SortingProxyModel::index(int row, int column, const ModelIndex& parent) const
  127. {
  128. if (row < 0 || column < 0)
  129. return {};
  130. auto source_parent = map_to_source(parent);
  131. const_cast<SortingProxyModel*>(this)->build_mapping(source_parent);
  132. auto it = m_mappings.find(source_parent);
  133. VERIFY(it != m_mappings.end());
  134. auto& mapping = *it->value;
  135. if (row >= static_cast<int>(mapping.source_rows.size()) || column >= column_count())
  136. return {};
  137. return create_index(row, column, &mapping);
  138. }
  139. ModelIndex SortingProxyModel::parent_index(const ModelIndex& proxy_index) const
  140. {
  141. if (!proxy_index.is_valid())
  142. return {};
  143. VERIFY(proxy_index.model() == this);
  144. VERIFY(proxy_index.internal_data());
  145. auto& index_mapping = *static_cast<Mapping*>(proxy_index.internal_data());
  146. auto it = m_mappings.find(index_mapping.source_parent);
  147. VERIFY(it != m_mappings.end());
  148. return map_to_proxy(it->value->source_parent);
  149. }
  150. void SortingProxyModel::sort_mapping(Mapping& mapping, int column, SortOrder sort_order)
  151. {
  152. if (column == -1) {
  153. int row_count = source().row_count(mapping.source_parent);
  154. for (int i = 0; i < row_count; ++i) {
  155. mapping.source_rows[i] = i;
  156. mapping.proxy_rows[i] = i;
  157. }
  158. return;
  159. }
  160. auto old_source_rows = mapping.source_rows;
  161. int row_count = source().row_count(mapping.source_parent);
  162. for (int i = 0; i < row_count; ++i)
  163. mapping.source_rows[i] = i;
  164. quick_sort(mapping.source_rows, [&](auto row1, auto row2) -> bool {
  165. bool is_less_than = less_than(source().index(row1, column, mapping.source_parent), source().index(row2, column, mapping.source_parent));
  166. return sort_order == SortOrder::Ascending ? is_less_than : !is_less_than;
  167. });
  168. for (int i = 0; i < row_count; ++i)
  169. mapping.proxy_rows[mapping.source_rows[i]] = i;
  170. // FIXME: I really feel like this should be done at the view layer somehow.
  171. for_each_view([&](AbstractView& view) {
  172. // Update the view's cursor.
  173. auto cursor = view.cursor_index();
  174. if (cursor.is_valid() && cursor.parent() == mapping.source_parent) {
  175. for (size_t i = 0; i < mapping.source_rows.size(); ++i) {
  176. if (mapping.source_rows[i] == view.cursor_index().row()) {
  177. auto new_source_index = this->index(i, view.cursor_index().column(), mapping.source_parent);
  178. view.set_cursor(new_source_index, AbstractView::SelectionUpdate::None, false);
  179. break;
  180. }
  181. }
  182. }
  183. // Update the view's selection.
  184. view.selection().change_from_model({}, [&](ModelSelection& selection) {
  185. Vector<ModelIndex> selected_indexes_in_source;
  186. Vector<ModelIndex> stale_indexes_in_selection;
  187. selection.for_each_index([&](const ModelIndex& index) {
  188. if (index.parent() == mapping.source_parent) {
  189. stale_indexes_in_selection.append(index);
  190. selected_indexes_in_source.append(source().index(old_source_rows[index.row()], index.column(), mapping.source_parent));
  191. }
  192. });
  193. for (auto& index : stale_indexes_in_selection) {
  194. selection.remove(index);
  195. }
  196. for (auto& index : selected_indexes_in_source) {
  197. for (size_t i = 0; i < mapping.source_rows.size(); ++i) {
  198. if (mapping.source_rows[i] == index.row()) {
  199. auto new_source_index = this->index(i, index.column(), mapping.source_parent);
  200. selection.add(new_source_index);
  201. break;
  202. }
  203. }
  204. }
  205. });
  206. });
  207. }
  208. void SortingProxyModel::sort(int column, SortOrder sort_order)
  209. {
  210. for (auto& it : m_mappings) {
  211. auto& mapping = *it.value;
  212. sort_mapping(mapping, column, sort_order);
  213. }
  214. m_last_key_column = column;
  215. m_last_sort_order = sort_order;
  216. did_update(UpdateFlag::DontInvalidateIndexes);
  217. }
  218. SortingProxyModel::InternalMapIterator SortingProxyModel::build_mapping(const ModelIndex& source_parent)
  219. {
  220. auto it = m_mappings.find(source_parent);
  221. if (it != m_mappings.end())
  222. return it;
  223. auto mapping = make<Mapping>();
  224. mapping->source_parent = source_parent;
  225. int row_count = source().row_count(source_parent);
  226. mapping->source_rows.resize(row_count);
  227. mapping->proxy_rows.resize(row_count);
  228. sort_mapping(*mapping, m_last_key_column, m_last_sort_order);
  229. if (source_parent.is_valid()) {
  230. auto source_grand_parent = source_parent.parent();
  231. build_mapping(source_grand_parent);
  232. }
  233. m_mappings.set(source_parent, move(mapping));
  234. return m_mappings.find(source_parent);
  235. }
  236. bool SortingProxyModel::is_column_sortable(int column_index) const
  237. {
  238. return source().is_column_sortable(column_index);
  239. }
  240. bool SortingProxyModel::is_editable(const ModelIndex& proxy_index) const
  241. {
  242. return source().is_editable(map_to_source(proxy_index));
  243. }
  244. void SortingProxyModel::set_data(const ModelIndex& proxy_index, const Variant& data)
  245. {
  246. source().set_data(map_to_source(proxy_index), data);
  247. }
  248. bool SortingProxyModel::is_searchable() const
  249. {
  250. return source().is_searchable();
  251. }
  252. Vector<ModelIndex, 1> SortingProxyModel::matches(const StringView& searching, unsigned flags, const ModelIndex& proxy_index)
  253. {
  254. auto found_indexes = source().matches(searching, flags, map_to_source(proxy_index));
  255. for (size_t i = 0; i < found_indexes.size(); i++)
  256. found_indexes[i] = map_to_proxy(found_indexes[i]);
  257. return found_indexes;
  258. }
  259. }