RemoteObjectGraphModel.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "RemoteObjectGraphModel.h"
  27. #include "RemoteObject.h"
  28. #include "RemoteProcess.h"
  29. #include <AK/JsonObject.h>
  30. #include <AK/JsonValue.h>
  31. #include <LibDraw/PNGLoader.h>
  32. #include <LibGUI/GApplication.h>
  33. #include <stdio.h>
  34. RemoteObjectGraphModel::RemoteObjectGraphModel(RemoteProcess& process)
  35. : m_process(process)
  36. {
  37. m_object_icon.set_bitmap_for_size(16, Gfx::load_png("/res/icons/16x16/inspector-object.png"));
  38. m_window_icon.set_bitmap_for_size(16, Gfx::load_png("/res/icons/16x16/window.png"));
  39. }
  40. RemoteObjectGraphModel::~RemoteObjectGraphModel()
  41. {
  42. }
  43. GUI::ModelIndex RemoteObjectGraphModel::index(int row, int column, const GUI::ModelIndex& parent) const
  44. {
  45. if (!parent.is_valid()) {
  46. if (m_process.roots().is_empty())
  47. return {};
  48. return create_index(row, column, &m_process.roots().at(row));
  49. }
  50. auto& remote_parent = *static_cast<RemoteObject*>(parent.internal_data());
  51. return create_index(row, column, &remote_parent.children.at(row));
  52. }
  53. GUI::ModelIndex RemoteObjectGraphModel::parent_index(const GUI::ModelIndex& index) const
  54. {
  55. if (!index.is_valid())
  56. return {};
  57. auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
  58. if (!remote_object.parent)
  59. return {};
  60. // NOTE: If the parent has no parent, it's a root, so we have to look among the remote roots.
  61. if (!remote_object.parent->parent) {
  62. for (int row = 0; row < m_process.roots().size(); ++row) {
  63. if (&m_process.roots()[row] == remote_object.parent)
  64. return create_index(row, 0, remote_object.parent);
  65. }
  66. ASSERT_NOT_REACHED();
  67. return {};
  68. }
  69. for (int row = 0; row < remote_object.parent->parent->children.size(); ++row) {
  70. if (&remote_object.parent->parent->children[row] == remote_object.parent)
  71. return create_index(row, 0, remote_object.parent);
  72. }
  73. ASSERT_NOT_REACHED();
  74. return {};
  75. }
  76. int RemoteObjectGraphModel::row_count(const GUI::ModelIndex& index) const
  77. {
  78. if (!index.is_valid())
  79. return m_process.roots().size();
  80. auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
  81. return remote_object.children.size();
  82. }
  83. int RemoteObjectGraphModel::column_count(const GUI::ModelIndex&) const
  84. {
  85. return 1;
  86. }
  87. GUI::Variant RemoteObjectGraphModel::data(const GUI::ModelIndex& index, Role role) const
  88. {
  89. auto* remote_object = static_cast<RemoteObject*>(index.internal_data());
  90. if (role == Role::Icon) {
  91. if (remote_object->class_name == "GWindow")
  92. return m_window_icon;
  93. return m_object_icon;
  94. }
  95. if (role == Role::Display) {
  96. return String::format("%s{%s} (%d)", remote_object->class_name.characters(), remote_object->address.characters(), remote_object->children.size());
  97. }
  98. return {};
  99. }
  100. void RemoteObjectGraphModel::update()
  101. {
  102. did_update();
  103. }