PersistentModelIndex.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/PersistentModelIndex.h>
  7. namespace GUI {
  8. PersistentModelIndex::PersistentModelIndex(ModelIndex const& index)
  9. {
  10. if (!index.is_valid())
  11. return;
  12. auto* model = const_cast<Model*>(index.model());
  13. m_handle = model->register_persistent_index({}, index);
  14. }
  15. int PersistentModelIndex::row() const
  16. {
  17. if (!has_valid_handle())
  18. return -1;
  19. return m_handle->m_index.row();
  20. }
  21. int PersistentModelIndex::column() const
  22. {
  23. if (!has_valid_handle())
  24. return -1;
  25. return m_handle->m_index.column();
  26. }
  27. PersistentModelIndex PersistentModelIndex::parent() const
  28. {
  29. if (!has_valid_handle())
  30. return {};
  31. return { m_handle->m_index.parent() };
  32. }
  33. PersistentModelIndex PersistentModelIndex::sibling_at_column(int column) const
  34. {
  35. if (!has_valid_handle())
  36. return {};
  37. return { m_handle->m_index.sibling_at_column(column) };
  38. }
  39. Variant PersistentModelIndex::data(ModelRole role) const
  40. {
  41. if (!has_valid_handle())
  42. return {};
  43. return { m_handle->m_index.data(role) };
  44. }
  45. PersistentModelIndex::operator ModelIndex() const
  46. {
  47. if (!has_valid_handle())
  48. return {};
  49. else
  50. return m_handle->m_index;
  51. }
  52. bool PersistentModelIndex::operator==(PersistentModelIndex const& other) const
  53. {
  54. bool is_this_valid = has_valid_handle();
  55. bool is_other_valid = other.has_valid_handle();
  56. if (!is_this_valid && !is_other_valid)
  57. return true;
  58. if (is_this_valid != is_other_valid)
  59. return false;
  60. return m_handle->m_index == other.m_handle->m_index;
  61. }
  62. bool PersistentModelIndex::operator!=(PersistentModelIndex const& other) const
  63. {
  64. return !(*this == other);
  65. }
  66. bool PersistentModelIndex::operator==(ModelIndex const& other) const
  67. {
  68. if (!has_valid_handle()) {
  69. return !other.is_valid();
  70. }
  71. return m_handle->m_index == other;
  72. }
  73. bool PersistentModelIndex::operator!=(ModelIndex const& other) const
  74. {
  75. return !(*this == other);
  76. }
  77. }