SortingProxyModel.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. int SortingProxyModel::row_count(const ModelIndex& proxy_index) const
  59. {
  60. return source().row_count(map_to_source(proxy_index));
  61. }
  62. int SortingProxyModel::column_count(const ModelIndex& proxy_index) const
  63. {
  64. return source().column_count(map_to_source(proxy_index));
  65. }
  66. ModelIndex SortingProxyModel::map_to_source(const ModelIndex& proxy_index) const
  67. {
  68. if (!proxy_index.is_valid())
  69. return {};
  70. ASSERT(proxy_index.model() == this);
  71. ASSERT(proxy_index.internal_data());
  72. auto& index_mapping = *static_cast<Mapping*>(proxy_index.internal_data());
  73. auto it = m_mappings.find(index_mapping.source_parent);
  74. ASSERT(it != m_mappings.end());
  75. auto& mapping = *it->value;
  76. if (static_cast<size_t>(proxy_index.row()) >= mapping.source_rows.size() || proxy_index.column() >= column_count())
  77. return {};
  78. int source_row = mapping.source_rows[proxy_index.row()];
  79. int source_column = proxy_index.column();
  80. return source().index(source_row, source_column, it->key);
  81. }
  82. ModelIndex SortingProxyModel::map_to_proxy(const ModelIndex& source_index) const
  83. {
  84. if (!source_index.is_valid())
  85. return {};
  86. ASSERT(source_index.model() == m_source);
  87. auto source_parent = source_index.parent();
  88. auto it = const_cast<SortingProxyModel*>(this)->build_mapping(source_parent);
  89. auto& mapping = *it->value;
  90. if (source_index.row() >= static_cast<int>(mapping.proxy_rows.size()) || source_index.column() >= column_count())
  91. return {};
  92. int proxy_row = mapping.proxy_rows[source_index.row()];
  93. int proxy_column = source_index.column();
  94. if (proxy_row < 0 || proxy_column < 0)
  95. return {};
  96. return create_index(proxy_row, proxy_column, &mapping);
  97. }
  98. String SortingProxyModel::column_name(int column) const
  99. {
  100. return source().column_name(column);
  101. }
  102. Variant SortingProxyModel::data(const ModelIndex& proxy_index, ModelRole role) const
  103. {
  104. return source().data(map_to_source(proxy_index), role);
  105. }
  106. void SortingProxyModel::update()
  107. {
  108. source().update();
  109. }
  110. StringView SortingProxyModel::drag_data_type() const
  111. {
  112. return source().drag_data_type();
  113. }
  114. bool SortingProxyModel::less_than(const ModelIndex& index1, const ModelIndex& index2) const
  115. {
  116. auto data1 = index1.data(m_sort_role);
  117. auto data2 = index2.data(m_sort_role);
  118. if (data1.is_string() && data2.is_string())
  119. return data1.as_string().to_lowercase() < data2.as_string().to_lowercase();
  120. return data1 < data2;
  121. }
  122. ModelIndex SortingProxyModel::index(int row, int column, const ModelIndex& parent) const
  123. {
  124. if (row < 0 || column < 0)
  125. return {};
  126. auto source_parent = map_to_source(parent);
  127. const_cast<SortingProxyModel*>(this)->build_mapping(source_parent);
  128. auto it = m_mappings.find(source_parent);
  129. ASSERT(it != m_mappings.end());
  130. auto& mapping = *it->value;
  131. if (row >= static_cast<int>(mapping.source_rows.size()) || column >= column_count())
  132. return {};
  133. return create_index(row, column, &mapping);
  134. }
  135. ModelIndex SortingProxyModel::parent_index(const ModelIndex& proxy_index) const
  136. {
  137. if (!proxy_index.is_valid())
  138. return {};
  139. ASSERT(proxy_index.model() == this);
  140. ASSERT(proxy_index.internal_data());
  141. auto& index_mapping = *static_cast<Mapping*>(proxy_index.internal_data());
  142. auto it = m_mappings.find(index_mapping.source_parent);
  143. ASSERT(it != m_mappings.end());
  144. return map_to_proxy(it->value->source_parent);
  145. }
  146. void SortingProxyModel::sort_mapping(Mapping& mapping, int column, SortOrder sort_order)
  147. {
  148. if (column == -1) {
  149. int row_count = source().row_count(mapping.source_parent);
  150. for (int i = 0; i < row_count; ++i) {
  151. mapping.source_rows[i] = i;
  152. mapping.proxy_rows[i] = i;
  153. }
  154. return;
  155. }
  156. auto old_source_rows = mapping.source_rows;
  157. int row_count = source().row_count(mapping.source_parent);
  158. for (int i = 0; i < row_count; ++i)
  159. mapping.source_rows[i] = i;
  160. quick_sort(mapping.source_rows, [&](auto row1, auto row2) -> bool {
  161. bool is_less_than = less_than(source().index(row1, column, mapping.source_parent), source().index(row2, column, mapping.source_parent));
  162. return sort_order == SortOrder::Ascending ? is_less_than : !is_less_than;
  163. });
  164. for (int i = 0; i < row_count; ++i)
  165. mapping.proxy_rows[mapping.source_rows[i]] = i;
  166. // FIXME: I really feel like this should be done at the view layer somehow.
  167. for_each_view([&](AbstractView& view) {
  168. // Update the view's cursor.
  169. auto cursor = view.cursor_index();
  170. if (cursor.is_valid() && cursor.parent() == mapping.source_parent) {
  171. for (size_t i = 0; i < mapping.source_rows.size(); ++i) {
  172. if (mapping.source_rows[i] == view.cursor_index().row()) {
  173. auto new_source_index = this->index(i, view.cursor_index().column(), mapping.source_parent);
  174. view.set_cursor(new_source_index, AbstractView::SelectionUpdate::None, false);
  175. break;
  176. }
  177. }
  178. }
  179. // Update the view's selection.
  180. view.selection().change_from_model({}, [&](ModelSelection& selection) {
  181. Vector<ModelIndex> selected_indexes_in_source;
  182. Vector<ModelIndex> stale_indexes_in_selection;
  183. selection.for_each_index([&](const ModelIndex& index) {
  184. if (index.parent() == mapping.source_parent) {
  185. stale_indexes_in_selection.append(index);
  186. selected_indexes_in_source.append(source().index(old_source_rows[index.row()], index.column(), mapping.source_parent));
  187. }
  188. });
  189. for (auto& index : stale_indexes_in_selection) {
  190. selection.remove(index);
  191. }
  192. for (auto& index : selected_indexes_in_source) {
  193. for (size_t i = 0; i < mapping.source_rows.size(); ++i) {
  194. if (mapping.source_rows[i] == index.row()) {
  195. auto new_source_index = this->index(i, index.column(), mapping.source_parent);
  196. selection.add(new_source_index);
  197. break;
  198. }
  199. }
  200. }
  201. });
  202. });
  203. }
  204. void SortingProxyModel::sort(int column, SortOrder sort_order)
  205. {
  206. for (auto& it : m_mappings) {
  207. auto& mapping = *it.value;
  208. sort_mapping(mapping, column, sort_order);
  209. }
  210. m_last_key_column = column;
  211. m_last_sort_order = sort_order;
  212. did_update(UpdateFlag::DontInvalidateIndexes);
  213. }
  214. SortingProxyModel::InternalMapIterator SortingProxyModel::build_mapping(const ModelIndex& source_parent)
  215. {
  216. auto it = m_mappings.find(source_parent);
  217. if (it != m_mappings.end())
  218. return it;
  219. auto mapping = make<Mapping>();
  220. mapping->source_parent = source_parent;
  221. int row_count = source().row_count(source_parent);
  222. mapping->source_rows.resize(row_count);
  223. mapping->proxy_rows.resize(row_count);
  224. sort_mapping(*mapping, m_last_key_column, m_last_sort_order);
  225. if (source_parent.is_valid()) {
  226. auto source_grand_parent = source_parent.parent();
  227. build_mapping(source_grand_parent);
  228. }
  229. m_mappings.set(source_parent, move(mapping));
  230. return m_mappings.find(source_parent);
  231. }
  232. bool SortingProxyModel::is_column_sortable(int column_index) const
  233. {
  234. return source().is_column_sortable(column_index);
  235. }
  236. }