SortingProxyModel.cpp 8.7 KB

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