2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-08-18 05:19:13 -03:00
|
|
|
#include "RemoteObjectGraphModel.h"
|
2019-08-19 15:29:52 -03:00
|
|
|
#include "RemoteObject.h"
|
|
|
|
|
#include "RemoteProcess.h"
|
2019-08-18 05:19:13 -03:00
|
|
|
#include <AK/JsonValue.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Application.h>
|
2019-08-18 05:19:13 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2020-09-25 16:03:29 -03:00
|
|
|
namespace Inspector {
|
|
|
|
|
|
2019-08-19 15:29:52 -03:00
|
|
|
RemoteObjectGraphModel::RemoteObjectGraphModel(RemoteProcess& process)
|
|
|
|
|
: m_process(process)
|
2019-08-18 05:19:13 -03:00
|
|
|
{
|
2021-07-21 13:02:15 -03:00
|
|
|
m_object_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspector-object.png"));
|
|
|
|
|
m_window_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/window.png"));
|
|
|
|
|
m_layout_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/layout.png"));
|
|
|
|
|
m_timer_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/timer.png"));
|
2019-08-18 05:19:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RemoteObjectGraphModel::~RemoteObjectGraphModel()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
GUI::ModelIndex RemoteObjectGraphModel::index(int row, int column, const GUI::ModelIndex& parent) const
|
2019-08-18 05:19:13 -03:00
|
|
|
{
|
|
|
|
|
if (!parent.is_valid()) {
|
2019-08-19 15:29:52 -03:00
|
|
|
if (m_process.roots().is_empty())
|
2019-08-18 05:19:13 -03:00
|
|
|
return {};
|
2019-08-19 15:29:52 -03:00
|
|
|
return create_index(row, column, &m_process.roots().at(row));
|
2019-08-18 05:19:13 -03:00
|
|
|
}
|
|
|
|
|
auto& remote_parent = *static_cast<RemoteObject*>(parent.internal_data());
|
2019-08-19 15:29:52 -03:00
|
|
|
return create_index(row, column, &remote_parent.children.at(row));
|
2019-08-18 05:19:13 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
GUI::ModelIndex RemoteObjectGraphModel::parent_index(const GUI::ModelIndex& index) const
|
2019-08-18 06:12:43 -03:00
|
|
|
{
|
|
|
|
|
if (!index.is_valid())
|
|
|
|
|
return {};
|
|
|
|
|
auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
|
|
|
|
|
if (!remote_object.parent)
|
|
|
|
|
return {};
|
2019-09-23 15:29:03 -03:00
|
|
|
|
|
|
|
|
// NOTE: If the parent has no parent, it's a root, so we have to look among the remote roots.
|
|
|
|
|
if (!remote_object.parent->parent) {
|
2020-02-25 10:49:47 -03:00
|
|
|
for (size_t row = 0; row < m_process.roots().size(); ++row) {
|
2019-09-23 15:29:03 -03:00
|
|
|
if (&m_process.roots()[row] == remote_object.parent)
|
|
|
|
|
return create_index(row, 0, remote_object.parent);
|
|
|
|
|
}
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-09-23 15:29:03 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 10:49:47 -03:00
|
|
|
for (size_t row = 0; row < remote_object.parent->parent->children.size(); ++row) {
|
2019-09-23 15:29:03 -03:00
|
|
|
if (&remote_object.parent->parent->children[row] == remote_object.parent)
|
2019-08-18 06:12:43 -03:00
|
|
|
return create_index(row, 0, remote_object.parent);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-08-18 06:12:43 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
int RemoteObjectGraphModel::row_count(const GUI::ModelIndex& index) const
|
2019-08-18 05:19:13 -03:00
|
|
|
{
|
|
|
|
|
if (!index.is_valid())
|
2019-08-19 15:29:52 -03:00
|
|
|
return m_process.roots().size();
|
2019-08-18 05:19:13 -03:00
|
|
|
auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
|
|
|
|
|
return remote_object.children.size();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
int RemoteObjectGraphModel::column_count(const GUI::ModelIndex&) const
|
2019-08-18 05:19:13 -03:00
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 11:00:07 -03:00
|
|
|
GUI::Variant RemoteObjectGraphModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
|
2019-08-18 05:19:13 -03:00
|
|
|
{
|
|
|
|
|
auto* remote_object = static_cast<RemoteObject*>(index.internal_data());
|
2020-08-16 11:00:07 -03:00
|
|
|
if (role == GUI::ModelRole::Icon) {
|
2020-03-05 05:24:10 -03:00
|
|
|
if (remote_object->class_name == "Window")
|
2019-08-18 14:54:35 -03:00
|
|
|
return m_window_icon;
|
2020-03-05 07:27:03 -03:00
|
|
|
if (remote_object->class_name == "Timer")
|
|
|
|
|
return m_timer_icon;
|
2020-03-05 07:13:44 -03:00
|
|
|
if (remote_object->class_name.ends_with("Layout"))
|
|
|
|
|
return m_layout_icon;
|
2019-08-18 05:19:13 -03:00
|
|
|
return m_object_icon;
|
|
|
|
|
}
|
2020-10-13 13:34:27 -03:00
|
|
|
if (role == GUI::ModelRole::Display)
|
|
|
|
|
return String::formatted("{}({:p})", remote_object->class_name, remote_object->address);
|
|
|
|
|
|
2019-08-18 05:19:13 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-25 16:03:29 -03:00
|
|
|
}
|