RemoteObjectGraphModel.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "RemoteObjectGraphModel.h"
  8. #include "RemoteObject.h"
  9. #include "RemoteProcess.h"
  10. #include <AK/JsonValue.h>
  11. #include <LibGUI/Application.h>
  12. #include <stdio.h>
  13. namespace Inspector {
  14. RemoteObjectGraphModel::RemoteObjectGraphModel(RemoteProcess& process)
  15. : m_process(process)
  16. {
  17. m_object_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"sv).release_value_but_fixme_should_propagate_errors());
  18. m_window_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png"sv).release_value_but_fixme_should_propagate_errors());
  19. m_layout_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/layout.png"sv).release_value_but_fixme_should_propagate_errors());
  20. m_timer_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/timer.png"sv).release_value_but_fixme_should_propagate_errors());
  21. }
  22. GUI::ModelIndex RemoteObjectGraphModel::index(int row, int column, const GUI::ModelIndex& parent) const
  23. {
  24. if (!parent.is_valid()) {
  25. if (m_process.roots().is_empty())
  26. return {};
  27. return create_index(row, column, &m_process.roots().at(row));
  28. }
  29. auto& remote_parent = *static_cast<RemoteObject*>(parent.internal_data());
  30. return create_index(row, column, &remote_parent.children.at(row));
  31. }
  32. GUI::ModelIndex RemoteObjectGraphModel::parent_index(const GUI::ModelIndex& index) const
  33. {
  34. if (!index.is_valid())
  35. return {};
  36. auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
  37. if (!remote_object.parent)
  38. return {};
  39. // NOTE: If the parent has no parent, it's a root, so we have to look among the remote roots.
  40. if (!remote_object.parent->parent) {
  41. for (size_t row = 0; row < m_process.roots().size(); ++row) {
  42. if (m_process.roots()[row] == remote_object.parent)
  43. return create_index(row, 0, remote_object.parent);
  44. }
  45. VERIFY_NOT_REACHED();
  46. return {};
  47. }
  48. for (size_t row = 0; row < remote_object.parent->parent->children.size(); ++row) {
  49. if (remote_object.parent->parent->children[row] == remote_object.parent)
  50. return create_index(row, 0, remote_object.parent);
  51. }
  52. VERIFY_NOT_REACHED();
  53. return {};
  54. }
  55. int RemoteObjectGraphModel::row_count(const GUI::ModelIndex& index) const
  56. {
  57. if (!index.is_valid())
  58. return m_process.roots().size();
  59. auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
  60. return remote_object.children.size();
  61. }
  62. int RemoteObjectGraphModel::column_count(const GUI::ModelIndex&) const
  63. {
  64. return 1;
  65. }
  66. GUI::Variant RemoteObjectGraphModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  67. {
  68. auto* remote_object = static_cast<RemoteObject*>(index.internal_data());
  69. if (role == GUI::ModelRole::Icon) {
  70. if (remote_object->class_name == "Window")
  71. return m_window_icon;
  72. if (remote_object->class_name == "Timer")
  73. return m_timer_icon;
  74. if (remote_object->class_name.ends_with("Layout"sv))
  75. return m_layout_icon;
  76. return m_object_icon;
  77. }
  78. if (role == GUI::ModelRole::Display)
  79. return DeprecatedString::formatted("{}({:p})", remote_object->class_name, remote_object->address);
  80. return {};
  81. }
  82. }