SortingProxyModel.cpp 8.9 KB

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