SortingProxyModel.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #include <stdio.h>
  30. #include <stdlib.h>
  31. namespace GUI {
  32. SortingProxyModel::SortingProxyModel(NonnullRefPtr<Model>&& target)
  33. : m_target(move(target))
  34. , m_key_column(-1)
  35. {
  36. m_target->on_update = [this] {
  37. resort();
  38. };
  39. }
  40. SortingProxyModel::~SortingProxyModel()
  41. {
  42. }
  43. int SortingProxyModel::row_count(const ModelIndex& index) const
  44. {
  45. return target().row_count(index);
  46. }
  47. int SortingProxyModel::column_count(const ModelIndex& index) const
  48. {
  49. return target().column_count(index);
  50. }
  51. ModelIndex SortingProxyModel::map_to_target(const ModelIndex& index) const
  52. {
  53. if (!index.is_valid())
  54. return {};
  55. if (static_cast<size_t>(index.row()) >= m_row_mappings.size() || index.column() >= column_count())
  56. return {};
  57. return target().index(m_row_mappings[index.row()], index.column());
  58. }
  59. String SortingProxyModel::row_name(int index) const
  60. {
  61. return target().row_name(index);
  62. }
  63. String SortingProxyModel::column_name(int index) const
  64. {
  65. return target().column_name(index);
  66. }
  67. Model::ColumnMetadata SortingProxyModel::column_metadata(int index) const
  68. {
  69. return target().column_metadata(index);
  70. }
  71. Variant SortingProxyModel::data(const ModelIndex& index, Role role) const
  72. {
  73. auto target_index = map_to_target(index);
  74. if (!target_index.is_valid()) {
  75. dbg() << "BUG! SortingProxyModel: Unable to convert " << index << " to target";
  76. return {};
  77. }
  78. return target().data(map_to_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()
  98. {
  99. auto old_row_mappings = m_row_mappings;
  100. int row_count = target().row_count();
  101. m_row_mappings.resize(row_count);
  102. for (int i = 0; i < row_count; ++i)
  103. m_row_mappings[i] = i;
  104. if (m_key_column == -1) {
  105. did_update(Model::UpdateFlag::DontInvalidateIndexes);
  106. return;
  107. }
  108. quick_sort(m_row_mappings, [&](auto row1, auto row2) -> bool {
  109. auto data1 = target().data(target().index(row1, m_key_column), Model::Role::Sort);
  110. auto data2 = target().data(target().index(row2, m_key_column), Model::Role::Sort);
  111. if (data1 == data2)
  112. return 0;
  113. bool is_less_than;
  114. if (data1.is_string() && data2.is_string() && !m_sorting_case_sensitive)
  115. is_less_than = data1.as_string().to_lowercase() < data2.as_string().to_lowercase();
  116. else
  117. is_less_than = data1 < data2;
  118. return m_sort_order == SortOrder::Ascending ? is_less_than : !is_less_than;
  119. });
  120. did_update(Model::UpdateFlag::DontInvalidateIndexes);
  121. for_each_view([&](AbstractView& view) {
  122. auto& selection = view.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. bool SortingProxyModel::is_column_sortable(int column_index) const
  139. {
  140. return target().is_column_sortable(column_index);
  141. }
  142. }