PersistentModelIndex.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/RefCounted.h>
  9. #include <AK/WeakPtr.h>
  10. #include <LibGUI/Model.h>
  11. #include <LibGUI/ModelIndex.h>
  12. namespace GUI {
  13. /// A PersistentHandle is an internal data structure used to keep track of the
  14. /// target of multiple PersistentModelIndex instances.
  15. class PersistentHandle : public Weakable<PersistentHandle> {
  16. friend Model;
  17. friend PersistentModelIndex;
  18. friend AK::Traits<GUI::PersistentModelIndex>;
  19. PersistentHandle(ModelIndex const& index)
  20. : m_index(index)
  21. {
  22. }
  23. ModelIndex m_index;
  24. };
  25. class PersistentModelIndex {
  26. public:
  27. PersistentModelIndex() = default;
  28. PersistentModelIndex(ModelIndex const&);
  29. PersistentModelIndex(PersistentModelIndex const&) = default;
  30. PersistentModelIndex(PersistentModelIndex&&) = default;
  31. PersistentModelIndex& operator=(PersistentModelIndex const&) = default;
  32. PersistentModelIndex& operator=(PersistentModelIndex&&) = default;
  33. bool is_valid() const { return has_valid_handle() && m_handle->m_index.is_valid(); }
  34. bool has_valid_handle() const { return !m_handle.is_null(); }
  35. int row() const;
  36. int column() const;
  37. PersistentModelIndex parent() const;
  38. PersistentModelIndex sibling_at_column(int column) const;
  39. Variant data(ModelRole = ModelRole::Display) const;
  40. void* internal_data() const
  41. {
  42. if (has_valid_handle())
  43. return m_handle->m_index.internal_data();
  44. else
  45. return nullptr;
  46. }
  47. operator ModelIndex() const;
  48. bool operator==(PersistentModelIndex const&) const;
  49. bool operator!=(PersistentModelIndex const&) const;
  50. bool operator==(ModelIndex const&) const;
  51. bool operator!=(ModelIndex const&) const;
  52. private:
  53. friend AK::Traits<GUI::PersistentModelIndex>;
  54. WeakPtr<PersistentHandle> m_handle;
  55. };
  56. }
  57. namespace AK {
  58. template<>
  59. struct Formatter<GUI::PersistentModelIndex> : Formatter<FormatString> {
  60. ErrorOr<void> format(FormatBuilder& builder, GUI::PersistentModelIndex const& value)
  61. {
  62. return Formatter<FormatString>::format(builder, "PersistentModelIndex({},{},{})", value.row(), value.column(), value.internal_data());
  63. }
  64. };
  65. template<>
  66. struct Traits<GUI::PersistentModelIndex> : public GenericTraits<GUI::PersistentModelIndex> {
  67. static unsigned hash(const GUI::PersistentModelIndex& index)
  68. {
  69. if (index.has_valid_handle())
  70. return Traits<GUI::ModelIndex>::hash(index.m_handle->m_index);
  71. else
  72. return 0;
  73. }
  74. };
  75. }