RemoteObjectGraphModel.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <LibGUI/Application.h>
  32. #include <stdio.h>
  33. RemoteObjectGraphModel::RemoteObjectGraphModel(RemoteProcess& process)
  34. : m_process(process)
  35. {
  36. m_object_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
  37. m_window_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png"));
  38. }
  39. RemoteObjectGraphModel::~RemoteObjectGraphModel()
  40. {
  41. }
  42. GUI::ModelIndex RemoteObjectGraphModel::index(int row, int column, const GUI::ModelIndex& parent) const
  43. {
  44. if (!parent.is_valid()) {
  45. if (m_process.roots().is_empty())
  46. return {};
  47. return create_index(row, column, &m_process.roots().at(row));
  48. }
  49. auto& remote_parent = *static_cast<RemoteObject*>(parent.internal_data());
  50. return create_index(row, column, &remote_parent.children.at(row));
  51. }
  52. GUI::ModelIndex RemoteObjectGraphModel::parent_index(const GUI::ModelIndex& index) const
  53. {
  54. if (!index.is_valid())
  55. return {};
  56. auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
  57. if (!remote_object.parent)
  58. return {};
  59. // NOTE: If the parent has no parent, it's a root, so we have to look among the remote roots.
  60. if (!remote_object.parent->parent) {
  61. for (size_t row = 0; row < m_process.roots().size(); ++row) {
  62. if (&m_process.roots()[row] == remote_object.parent)
  63. return create_index(row, 0, remote_object.parent);
  64. }
  65. ASSERT_NOT_REACHED();
  66. return {};
  67. }
  68. for (size_t row = 0; row < remote_object.parent->parent->children.size(); ++row) {
  69. if (&remote_object.parent->parent->children[row] == remote_object.parent)
  70. return create_index(row, 0, remote_object.parent);
  71. }
  72. ASSERT_NOT_REACHED();
  73. return {};
  74. }
  75. int RemoteObjectGraphModel::row_count(const GUI::ModelIndex& index) const
  76. {
  77. if (!index.is_valid())
  78. return m_process.roots().size();
  79. auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
  80. return remote_object.children.size();
  81. }
  82. int RemoteObjectGraphModel::column_count(const GUI::ModelIndex&) const
  83. {
  84. return 1;
  85. }
  86. GUI::Variant RemoteObjectGraphModel::data(const GUI::ModelIndex& index, Role role) const
  87. {
  88. auto* remote_object = static_cast<RemoteObject*>(index.internal_data());
  89. if (role == Role::Icon) {
  90. if (remote_object->class_name == "GWindow")
  91. return m_window_icon;
  92. return m_object_icon;
  93. }
  94. if (role == Role::Display) {
  95. return String::format("%s{%s} (%d)", remote_object->class_name.characters(), remote_object->address.characters(), remote_object->children.size());
  96. }
  97. return {};
  98. }
  99. void RemoteObjectGraphModel::update()
  100. {
  101. did_update();
  102. }