SortingProxyModel.cpp 8.7 KB

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