Serenity Operating System
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
8#include "RemoteObjectGraphModel.h"
9#include "RemoteObject.h"
10#include "RemoteProcess.h"
11#include <AK/JsonValue.h>
12#include <LibGUI/Application.h>
13#include <stdio.h>
14
15namespace Inspector {
16
17RemoteObjectGraphModel::RemoteObjectGraphModel(RemoteProcess& process)
18 : m_process(process)
19{
20 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());
21 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());
22 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());
23 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());
24}
25
26GUI::ModelIndex RemoteObjectGraphModel::index(int row, int column, const GUI::ModelIndex& parent) const
27{
28 if (!parent.is_valid()) {
29 if (m_process.roots().is_empty())
30 return {};
31 return create_index(row, column, &m_process.roots().at(row));
32 }
33 auto& remote_parent = *static_cast<RemoteObject*>(parent.internal_data());
34 return create_index(row, column, &remote_parent.children.at(row));
35}
36
37GUI::ModelIndex RemoteObjectGraphModel::parent_index(const GUI::ModelIndex& index) const
38{
39 if (!index.is_valid())
40 return {};
41 auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
42 if (!remote_object.parent)
43 return {};
44
45 // NOTE: If the parent has no parent, it's a root, so we have to look among the remote roots.
46 if (!remote_object.parent->parent) {
47 for (size_t row = 0; row < m_process.roots().size(); ++row) {
48 if (m_process.roots()[row] == remote_object.parent)
49 return create_index(row, 0, remote_object.parent);
50 }
51 VERIFY_NOT_REACHED();
52 return {};
53 }
54
55 for (size_t row = 0; row < remote_object.parent->parent->children.size(); ++row) {
56 if (remote_object.parent->parent->children[row] == remote_object.parent)
57 return create_index(row, 0, remote_object.parent);
58 }
59
60 VERIFY_NOT_REACHED();
61 return {};
62}
63
64int RemoteObjectGraphModel::row_count(const GUI::ModelIndex& index) const
65{
66 if (!index.is_valid())
67 return m_process.roots().size();
68 auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
69 return remote_object.children.size();
70}
71
72int RemoteObjectGraphModel::column_count(const GUI::ModelIndex&) const
73{
74 return 1;
75}
76
77GUI::Variant RemoteObjectGraphModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
78{
79 auto* remote_object = static_cast<RemoteObject*>(index.internal_data());
80 if (role == GUI::ModelRole::Icon) {
81 if (remote_object->class_name == "Window")
82 return m_window_icon;
83 if (remote_object->class_name == "Timer")
84 return m_timer_icon;
85 if (remote_object->class_name.ends_with("Layout"sv))
86 return m_layout_icon;
87 return m_object_icon;
88 }
89 if (role == GUI::ModelRole::Display)
90 return DeprecatedString::formatted("{}({:p})", remote_object->class_name, remote_object->address);
91
92 return {};
93}
94
95}