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/TemporaryChange.h>
  27. #include <AK/QuickSort.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>&& target)
  34. : m_target(move(target))
  35. , m_key_column(-1)
  36. {
  37. // Since the target 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_target->register_client(*this);
  42. resort();
  43. }
  44. SortingProxyModel::~SortingProxyModel()
  45. {
  46. m_target->unregister_client(*this);
  47. }
  48. void SortingProxyModel::on_model_update(unsigned flags)
  49. {
  50. resort(flags);
  51. }
  52. int SortingProxyModel::row_count(const ModelIndex& index) const
  53. {
  54. auto target_index = map_to_target(index);
  55. return target().row_count(target_index);
  56. }
  57. int SortingProxyModel::column_count(const ModelIndex& index) const
  58. {
  59. auto target_index = map_to_target(index);
  60. return target().column_count(target_index);
  61. }
  62. ModelIndex SortingProxyModel::map_to_target(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 target().index(m_row_mappings[index.row()], index.column());
  69. }
  70. String SortingProxyModel::column_name(int column) const
  71. {
  72. return target().column_name(column);
  73. }
  74. Variant SortingProxyModel::data(const ModelIndex& index, Role role) const
  75. {
  76. auto target_index = map_to_target(index);
  77. ASSERT(target_index.is_valid());
  78. return target().data(target_index, role);
  79. }
  80. void SortingProxyModel::update()
  81. {
  82. target().update();
  83. }
  84. StringView SortingProxyModel::drag_data_type() const
  85. {
  86. return target().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. void SortingProxyModel::resort(unsigned flags)
  98. {
  99. TemporaryChange change(m_sorting, true);
  100. auto old_row_mappings = m_row_mappings;
  101. int row_count = target().row_count();
  102. m_row_mappings.resize(row_count);
  103. for (int i = 0; i < row_count; ++i)
  104. m_row_mappings[i] = i;
  105. if (m_key_column == -1) {
  106. did_update(flags);
  107. return;
  108. }
  109. quick_sort(m_row_mappings, [&](auto row1, auto row2) -> bool {
  110. auto data1 = target().data(target().index(row1, m_key_column), m_sort_role);
  111. auto data2 = target().data(target().index(row2, m_key_column), m_sort_role);
  112. if (data1 == data2)
  113. return 0;
  114. bool is_less_than;
  115. if (data1.is_string() && data2.is_string() && !m_sorting_case_sensitive)
  116. is_less_than = data1.as_string().to_lowercase() < data2.as_string().to_lowercase();
  117. else
  118. is_less_than = data1 < data2;
  119. return m_sort_order == SortOrder::Ascending ? is_less_than : !is_less_than;
  120. });
  121. for_each_view([&](AbstractView& view) {
  122. view.selection().change_from_model({}, [&](ModelSelection& selection) {
  123. Vector<ModelIndex> selected_indexes_in_target;
  124. selection.for_each_index([&](const ModelIndex& index) {
  125. selected_indexes_in_target.append(target().index(old_row_mappings[index.row()], index.column()));
  126. });
  127. selection.clear();
  128. for (auto& index : selected_indexes_in_target) {
  129. for (size_t i = 0; i < m_row_mappings.size(); ++i) {
  130. if (m_row_mappings[i] == index.row()) {
  131. selection.add(this->index(i, index.column()));
  132. continue;
  133. }
  134. }
  135. }
  136. });
  137. });
  138. did_update(flags);
  139. }
  140. bool SortingProxyModel::is_column_sortable(int column_index) const
  141. {
  142. return target().is_column_sortable(column_index);
  143. }
  144. }