RemoteObjectGraphModel.cpp 3.2 KB

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