SortingProxyModel.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <AK/TemporaryChange.h>
  28. #include <LibGUI/AbstractView.h>
  29. #include <LibGUI/SortingProxyModel.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. namespace GUI {
  33. SortingProxyModel::SortingProxyModel(NonnullRefPtr<Model> source)
  34. : m_source(move(source))
  35. , m_key_column(-1)
  36. {
  37. // Since the source model already called Model::did_update we can't
  38. // assume we will get another call. So, we need to register for further
  39. // updates and just call resort() right away, otherwise requests
  40. // to this model won't work because there are no indices to map
  41. m_source->register_client(*this);
  42. resort();
  43. }
  44. SortingProxyModel::~SortingProxyModel()
  45. {
  46. m_source->unregister_client(*this);
  47. }
  48. void SortingProxyModel::model_did_update(unsigned flags)
  49. {
  50. resort(flags);
  51. }
  52. int SortingProxyModel::row_count(const ModelIndex& index) const
  53. {
  54. auto source_index = map_to_source(index);
  55. return source().row_count(source_index);
  56. }
  57. int SortingProxyModel::column_count(const ModelIndex& index) const
  58. {
  59. auto source_index = map_to_source(index);
  60. return source().column_count(source_index);
  61. }
  62. ModelIndex SortingProxyModel::map_to_source(const ModelIndex& index) const
  63. {
  64. if (!index.is_valid())
  65. return {};
  66. if (static_cast<size_t>(index.row()) >= m_row_mappings.size() || index.column() >= column_count())
  67. return {};
  68. return source().index(m_row_mappings[index.row()], index.column());
  69. }
  70. String SortingProxyModel::column_name(int column) const
  71. {
  72. return source().column_name(column);
  73. }
  74. Variant SortingProxyModel::data(const ModelIndex& index, Role role) const
  75. {
  76. auto source_index = map_to_source(index);
  77. ASSERT(source_index.is_valid());
  78. return source().data(source_index, role);
  79. }
  80. void SortingProxyModel::update()
  81. {
  82. source().update();
  83. }
  84. StringView SortingProxyModel::drag_data_type() const
  85. {
  86. return source().drag_data_type();
  87. }
  88. void SortingProxyModel::set_key_column_and_sort_order(int column, SortOrder sort_order)
  89. {
  90. if (column == m_key_column && sort_order == m_sort_order)
  91. return;
  92. ASSERT(column >= 0 && column < column_count());
  93. m_key_column = column;
  94. m_sort_order = sort_order;
  95. resort();
  96. }
  97. bool SortingProxyModel::less_than(const ModelIndex& index1, const ModelIndex& index2) const
  98. {
  99. auto data1 = data(index1, Role::Sort);
  100. auto data2 = data(index2, Role::Sort);
  101. if (data1.is_string() && data2.is_string())
  102. return data1.as_string().to_lowercase() < data2.as_string().to_lowercase();
  103. return data1 < data2;
  104. }
  105. void SortingProxyModel::resort(unsigned flags)
  106. {
  107. auto old_row_mappings = m_row_mappings;
  108. int row_count = source().row_count();
  109. m_row_mappings.resize(row_count);
  110. for (int i = 0; i < row_count; ++i)
  111. m_row_mappings[i] = i;
  112. if (m_key_column == -1) {
  113. did_update(flags);
  114. return;
  115. }
  116. quick_sort(m_row_mappings, [&](auto row1, auto row2) -> bool {
  117. bool is_less_than = less_than(source().index(row1, m_key_column), source().index(row2, m_key_column));
  118. return m_sort_order == SortOrder::Ascending ? is_less_than : !is_less_than;
  119. });
  120. for_each_view([&](AbstractView& view) {
  121. view.selection().change_from_model({}, [&](ModelSelection& selection) {
  122. Vector<ModelIndex> selected_indexes_in_source;
  123. selection.for_each_index([&](const ModelIndex& index) {
  124. selected_indexes_in_source.append(source().index(old_row_mappings[index.row()], index.column()));
  125. });
  126. selection.clear();
  127. for (auto& index : selected_indexes_in_source) {
  128. for (size_t i = 0; i < m_row_mappings.size(); ++i) {
  129. if (m_row_mappings[i] == index.row()) {
  130. selection.add(this->index(i, index.column()));
  131. continue;
  132. }
  133. }
  134. }
  135. });
  136. });
  137. did_update(flags);
  138. }
  139. bool SortingProxyModel::is_column_sortable(int column_index) const
  140. {
  141. return source().is_column_sortable(column_index);
  142. }
  143. }