GSortingProxyModel.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/GAbstractView.h>
  28. #include <LibGUI/GSortingProxyModel.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. GSortingProxyModel::GSortingProxyModel(NonnullRefPtr<GModel>&& target)
  32. : m_target(move(target))
  33. , m_key_column(-1)
  34. {
  35. m_target->on_update = [this] {
  36. resort();
  37. };
  38. }
  39. GSortingProxyModel::~GSortingProxyModel()
  40. {
  41. }
  42. int GSortingProxyModel::row_count(const GModelIndex& index) const
  43. {
  44. return target().row_count(index);
  45. }
  46. int GSortingProxyModel::column_count(const GModelIndex& index) const
  47. {
  48. return target().column_count(index);
  49. }
  50. GModelIndex GSortingProxyModel::map_to_target(const GModelIndex& index) const
  51. {
  52. if (!index.is_valid())
  53. return {};
  54. if (index.row() >= m_row_mappings.size() || index.column() >= column_count())
  55. return {};
  56. return target().index(m_row_mappings[index.row()], index.column());
  57. }
  58. String GSortingProxyModel::row_name(int index) const
  59. {
  60. return target().row_name(index);
  61. }
  62. String GSortingProxyModel::column_name(int index) const
  63. {
  64. return target().column_name(index);
  65. }
  66. GModel::ColumnMetadata GSortingProxyModel::column_metadata(int index) const
  67. {
  68. return target().column_metadata(index);
  69. }
  70. GVariant GSortingProxyModel::data(const GModelIndex& index, Role role) const
  71. {
  72. return target().data(map_to_target(index), role);
  73. }
  74. void GSortingProxyModel::update()
  75. {
  76. target().update();
  77. }
  78. StringView GSortingProxyModel::drag_data_type() const
  79. {
  80. return target().drag_data_type();
  81. }
  82. void GSortingProxyModel::set_key_column_and_sort_order(int column, GSortOrder sort_order)
  83. {
  84. if (column == m_key_column && sort_order == m_sort_order)
  85. return;
  86. ASSERT(column >= 0 && column < column_count());
  87. m_key_column = column;
  88. m_sort_order = sort_order;
  89. resort();
  90. }
  91. void GSortingProxyModel::resort()
  92. {
  93. auto old_row_mappings = m_row_mappings;
  94. int row_count = target().row_count();
  95. m_row_mappings.resize(row_count);
  96. for (int i = 0; i < row_count; ++i)
  97. m_row_mappings[i] = i;
  98. if (m_key_column == -1) {
  99. did_update();
  100. return;
  101. }
  102. quick_sort(m_row_mappings.begin(), m_row_mappings.end(), [&](auto row1, auto row2) -> bool {
  103. auto data1 = target().data(target().index(row1, m_key_column), GModel::Role::Sort);
  104. auto data2 = target().data(target().index(row2, m_key_column), GModel::Role::Sort);
  105. if (data1 == data2)
  106. return 0;
  107. bool is_less_than;
  108. if (data1.is_string() && data2.is_string() && !m_sorting_case_sensitive)
  109. is_less_than = data1.as_string().to_lowercase() < data2.as_string().to_lowercase();
  110. else
  111. is_less_than = data1 < data2;
  112. return m_sort_order == GSortOrder::Ascending ? is_less_than : !is_less_than;
  113. });
  114. did_update();
  115. for_each_view([&](GAbstractView& view) {
  116. auto& selection = view.selection();
  117. Vector<GModelIndex> selected_indexes_in_target;
  118. selection.for_each_index([&](const GModelIndex& index) {
  119. selected_indexes_in_target.append(target().index(old_row_mappings[index.row()], index.column()));
  120. });
  121. selection.clear();
  122. for (auto& index : selected_indexes_in_target) {
  123. for (int i = 0; i < m_row_mappings.size(); ++i) {
  124. if (m_row_mappings[i] == index.row()) {
  125. selection.add(this->index(i, index.column()));
  126. continue;
  127. }
  128. }
  129. }
  130. });
  131. }