ModelIndex.cpp 936 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Model.h>
  7. #include <LibGUI/Variant.h>
  8. namespace GUI {
  9. Variant ModelIndex::data(ModelRole role) const
  10. {
  11. if (!is_valid())
  12. return {};
  13. VERIFY(model());
  14. return model()->data(*this, role);
  15. }
  16. bool ModelIndex::is_parent_of(ModelIndex const& child) const
  17. {
  18. auto current_index = child.parent();
  19. while (current_index.is_valid()) {
  20. if (current_index == *this)
  21. return true;
  22. current_index = current_index.parent();
  23. }
  24. return false;
  25. }
  26. ModelIndex ModelIndex::sibling(int row, int column) const
  27. {
  28. if (!is_valid())
  29. return {};
  30. VERIFY(model());
  31. return model()->index(row, column, parent());
  32. }
  33. ModelIndex ModelIndex::sibling_at_column(int column) const
  34. {
  35. if (!is_valid())
  36. return {};
  37. return sibling(row(), column);
  38. }
  39. }