GSortingProxyModel.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <LibGUI/GSortingProxyModel.h>
  2. #include <AK/QuickSort.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. GSortingProxyModel::GSortingProxyModel(Retained<GModel>&& target)
  6. : m_target(move(target))
  7. , m_key_column(-1)
  8. {
  9. m_target->on_model_update = [this] (GModel&) {
  10. resort();
  11. };
  12. }
  13. GSortingProxyModel::~GSortingProxyModel()
  14. {
  15. }
  16. int GSortingProxyModel::row_count(const GModelIndex& index) const
  17. {
  18. return target().row_count(index);
  19. }
  20. int GSortingProxyModel::column_count(const GModelIndex& index) const
  21. {
  22. return target().column_count(index);
  23. }
  24. GModelIndex GSortingProxyModel::map_to_target(const GModelIndex& index) const
  25. {
  26. if (!index.is_valid())
  27. return { };
  28. if (index.row() >= row_count() || index.column() >= column_count())
  29. return { };
  30. return { m_row_mappings[index.row()], index.column() };
  31. }
  32. String GSortingProxyModel::row_name(int index) const
  33. {
  34. return target().row_name(index);
  35. }
  36. String GSortingProxyModel::column_name(int index) const
  37. {
  38. return target().column_name(index);
  39. }
  40. GModel::ColumnMetadata GSortingProxyModel::column_metadata(int index) const
  41. {
  42. return target().column_metadata(index);
  43. }
  44. GVariant GSortingProxyModel::data(const GModelIndex& index, Role role) const
  45. {
  46. return target().data(map_to_target(index), role);
  47. }
  48. void GSortingProxyModel::activate(const GModelIndex& index)
  49. {
  50. target().activate(map_to_target(index));
  51. }
  52. void GSortingProxyModel::update()
  53. {
  54. target().update();
  55. }
  56. void GSortingProxyModel::set_key_column_and_sort_order(int column, GSortOrder sort_order)
  57. {
  58. if (column == m_key_column && sort_order == m_sort_order)
  59. return;
  60. ASSERT(column >= 0 && column < column_count());
  61. m_key_column = column;
  62. m_sort_order = sort_order;
  63. resort();
  64. }
  65. void GSortingProxyModel::resort()
  66. {
  67. int previously_selected_target_row = map_to_target(selected_index()).row();
  68. int row_count = target().row_count();
  69. m_row_mappings.resize(row_count);
  70. for (int i = 0; i < row_count; ++i)
  71. m_row_mappings[i] = i;
  72. if (m_key_column == -1)
  73. return;
  74. quick_sort(m_row_mappings.begin(), m_row_mappings.end(), [&] (auto row1, auto row2) -> bool {
  75. auto data1 = target().data({ row1, m_key_column }, GModel::Role::Sort);
  76. auto data2 = target().data({ row2, m_key_column }, GModel::Role::Sort);
  77. if (data1 == data2)
  78. return 0;
  79. bool is_less_than = data1 < data2;
  80. return m_sort_order == GSortOrder::Ascending ? is_less_than : !is_less_than;
  81. });
  82. if (previously_selected_target_row != -1) {
  83. // Preserve selection.
  84. for (int i = 0; i < row_count; ++i) {
  85. if (m_row_mappings[i] == previously_selected_target_row) {
  86. set_selected_index({ i, 0 });
  87. break;
  88. }
  89. }
  90. }
  91. did_update();
  92. }