RemoteObjectGraphModel.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "RemoteObjectGraphModel.h"
  2. #include "RemoteObject.h"
  3. #include "RemoteProcess.h"
  4. #include <AK/JsonObject.h>
  5. #include <AK/JsonValue.h>
  6. #include <LibDraw/PNGLoader.h>
  7. #include <LibGUI/GApplication.h>
  8. #include <stdio.h>
  9. RemoteObjectGraphModel::RemoteObjectGraphModel(RemoteProcess& process)
  10. : m_process(process)
  11. {
  12. m_object_icon.set_bitmap_for_size(16, load_png("/res/icons/16x16/inspector-object.png"));
  13. m_window_icon.set_bitmap_for_size(16, load_png("/res/icons/16x16/window.png"));
  14. }
  15. RemoteObjectGraphModel::~RemoteObjectGraphModel()
  16. {
  17. }
  18. GModelIndex RemoteObjectGraphModel::index(int row, int column, const GModelIndex& parent) const
  19. {
  20. if (!parent.is_valid()) {
  21. if (m_process.roots().is_empty())
  22. return {};
  23. return create_index(row, column, &m_process.roots().at(row));
  24. }
  25. auto& remote_parent = *static_cast<RemoteObject*>(parent.internal_data());
  26. return create_index(row, column, &remote_parent.children.at(row));
  27. }
  28. GModelIndex RemoteObjectGraphModel::parent_index(const GModelIndex& index) const
  29. {
  30. if (!index.is_valid())
  31. return {};
  32. auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
  33. if (!remote_object.parent)
  34. return {};
  35. // NOTE: If the parent has no parent, it's a root, so we have to look among the remote roots.
  36. if (!remote_object.parent->parent) {
  37. for (int row = 0; row < m_process.roots().size(); ++row) {
  38. if (&m_process.roots()[row] == remote_object.parent)
  39. return create_index(row, 0, remote_object.parent);
  40. }
  41. ASSERT_NOT_REACHED();
  42. return {};
  43. }
  44. for (int row = 0; row < remote_object.parent->parent->children.size(); ++row) {
  45. if (&remote_object.parent->parent->children[row] == remote_object.parent)
  46. return create_index(row, 0, remote_object.parent);
  47. }
  48. ASSERT_NOT_REACHED();
  49. return {};
  50. }
  51. int RemoteObjectGraphModel::row_count(const GModelIndex& index) const
  52. {
  53. if (!index.is_valid())
  54. return m_process.roots().size();
  55. auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
  56. return remote_object.children.size();
  57. }
  58. int RemoteObjectGraphModel::column_count(const GModelIndex&) const
  59. {
  60. return 1;
  61. }
  62. GVariant RemoteObjectGraphModel::data(const GModelIndex& index, Role role) const
  63. {
  64. auto* remote_object = static_cast<RemoteObject*>(index.internal_data());
  65. if (role == Role::Icon) {
  66. if (remote_object->class_name == "GWindow")
  67. return m_window_icon;
  68. return m_object_icon;
  69. }
  70. if (role == Role::Display) {
  71. return String::format("%s{%s} (%d)", remote_object->class_name.characters(), remote_object->address.characters(), remote_object->children.size());
  72. }
  73. return {};
  74. }
  75. void RemoteObjectGraphModel::update()
  76. {
  77. did_update();
  78. }