GModelSelection.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma once
  2. #include <AK/HashTable.h>
  3. #include <LibGUI/GModelIndex.h>
  4. class GAbstractView;
  5. class GModelSelection {
  6. public:
  7. GModelSelection(GAbstractView& view)
  8. : m_view(view)
  9. {
  10. }
  11. int size() const { return m_indexes.size(); }
  12. bool is_empty() const { return m_indexes.is_empty(); }
  13. bool contains(const GModelIndex& index) const { return m_indexes.contains(index); }
  14. bool contains_row(int row) const
  15. {
  16. for (auto& index : m_indexes) {
  17. if (index.row() == row)
  18. return true;
  19. }
  20. return false;
  21. }
  22. void set(const GModelIndex&);
  23. void add(const GModelIndex&);
  24. void toggle(const GModelIndex&);
  25. bool remove(const GModelIndex&);
  26. void clear();
  27. template<typename Callback>
  28. void for_each_index(Callback callback)
  29. {
  30. for (auto& index : indexes())
  31. callback(index);
  32. }
  33. template<typename Callback>
  34. void for_each_index(Callback callback) const
  35. {
  36. for (auto& index : indexes())
  37. callback(index);
  38. }
  39. Vector<GModelIndex> indexes() const
  40. {
  41. Vector<GModelIndex> selected_indexes;
  42. for (auto& index : m_indexes)
  43. selected_indexes.append(index);
  44. return selected_indexes;
  45. }
  46. // FIXME: This doesn't guarantee that what you get is the lowest or "first" index selected..
  47. GModelIndex first() const
  48. {
  49. if (m_indexes.is_empty())
  50. return {};
  51. return *m_indexes.begin();
  52. }
  53. private:
  54. GAbstractView& m_view;
  55. HashTable<GModelIndex> m_indexes;
  56. };