ModelIndex.cpp 959 B

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