FilteringProxyModel.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  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 <LibGUI/FilteringProxyModel.h>
  27. namespace GUI {
  28. ModelIndex FilteringProxyModel::index(int row, int column, const ModelIndex& parent_index) const
  29. {
  30. int parent_row = parent_index.row();
  31. if (!parent_index.is_valid())
  32. parent_row = 0;
  33. return create_index(parent_row + row, column);
  34. }
  35. int FilteringProxyModel::row_count(const ModelIndex&) const
  36. {
  37. return m_matching_indices.size();
  38. }
  39. int FilteringProxyModel::column_count(const ModelIndex& index) const
  40. {
  41. if (!index.is_valid())
  42. return {};
  43. if ((size_t)index.row() > m_matching_indices.size() || index.row() < 0)
  44. return 0;
  45. return m_model.column_count(m_matching_indices[index.row()]);
  46. }
  47. Variant FilteringProxyModel::data(const ModelIndex& index, ModelRole role) const
  48. {
  49. if (!index.is_valid())
  50. return {};
  51. if ((size_t)index.row() > m_matching_indices.size() || index.row() < 0)
  52. return 0;
  53. return m_matching_indices[index.row()].data(role);
  54. }
  55. void FilteringProxyModel::update()
  56. {
  57. m_model.update();
  58. filter();
  59. did_update();
  60. }
  61. void FilteringProxyModel::filter()
  62. {
  63. m_matching_indices.clear();
  64. Function<void(ModelIndex&)> add_matching = [&](ModelIndex& parent_index) {
  65. for (auto i = 0; i < m_model.row_count(parent_index); ++i) {
  66. auto index = m_model.index(i, 0, parent_index);
  67. if (!index.is_valid())
  68. continue;
  69. auto filter_matches = m_model.data_matches(index, m_filter_term);
  70. bool matches = filter_matches == TriState::True;
  71. if (filter_matches == TriState::Unknown) {
  72. auto data = index.data();
  73. if (data.is_string() && data.as_string().contains(m_filter_term))
  74. matches = true;
  75. }
  76. if (matches)
  77. m_matching_indices.append(index);
  78. add_matching(index);
  79. }
  80. };
  81. ModelIndex parent_index;
  82. add_matching(parent_index);
  83. }
  84. void FilteringProxyModel::set_filter_term(const StringView& term)
  85. {
  86. if (m_filter_term == term)
  87. return;
  88. m_filter_term = term;
  89. update();
  90. }
  91. ModelIndex FilteringProxyModel::map(const ModelIndex& index) const
  92. {
  93. if (!index.is_valid())
  94. return {};
  95. auto row = index.row();
  96. if (m_matching_indices.size() > (size_t)row)
  97. return m_matching_indices[row];
  98. return {};
  99. }
  100. }