mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 14:48:14 +00:00
GJsonArrayModel: Support fields that aren't tied to a single JSON value
Change the custom data massaging callback to take a const JsonObject&. This will allow binding together data from multiple fields into one output in the model. :^)
This commit is contained in:
parent
a4548a150f
commit
afd25679bc
3 changed files with 13 additions and 6 deletions
|
@ -16,8 +16,8 @@ ProcessFileDescriptorMapWidget::ProcessFileDescriptorMapWidget(GWidget* parent)
|
||||||
pid_fds_fields.empend("class", "Class", TextAlignment::CenterLeft);
|
pid_fds_fields.empend("class", "Class", TextAlignment::CenterLeft);
|
||||||
pid_fds_fields.empend("offset", "Offset", TextAlignment::CenterRight);
|
pid_fds_fields.empend("offset", "Offset", TextAlignment::CenterRight);
|
||||||
pid_fds_fields.empend("absolute_path", "Path", TextAlignment::CenterLeft);
|
pid_fds_fields.empend("absolute_path", "Path", TextAlignment::CenterLeft);
|
||||||
pid_fds_fields.empend("seekable", "Access", TextAlignment::CenterLeft, [](auto& data) {
|
pid_fds_fields.empend("Access", TextAlignment::CenterLeft, [](auto& object) {
|
||||||
return data.to_bool() ? "Seekable" : "Sequential";
|
return object.get("seekable").to_bool() ? "Seekable" : "Sequential";
|
||||||
});
|
});
|
||||||
|
|
||||||
m_table_view->set_model(GJsonArrayModel::create({}, move(pid_fds_fields)));
|
m_table_view->set_model(GJsonArrayModel::create({}, move(pid_fds_fields)));
|
||||||
|
|
|
@ -33,7 +33,7 @@ GVariant GJsonArrayModel::data(const GModelIndex& index, Role role) const
|
||||||
auto& json_field_name = field_spec.json_field_name;
|
auto& json_field_name = field_spec.json_field_name;
|
||||||
auto data = object.get(json_field_name);
|
auto data = object.get(json_field_name);
|
||||||
if (field_spec.massage_for_display)
|
if (field_spec.massage_for_display)
|
||||||
return field_spec.massage_for_display(data);
|
return field_spec.massage_for_display(object);
|
||||||
if (data.is_int())
|
if (data.is_int())
|
||||||
return data.as_int();
|
return data.as_int();
|
||||||
if (data.is_uint())
|
if (data.is_uint())
|
||||||
|
|
|
@ -1,23 +1,30 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/JsonArray.h>
|
#include <AK/JsonArray.h>
|
||||||
|
#include <AK/JsonObject.h>
|
||||||
#include <LibGUI/GModel.h>
|
#include <LibGUI/GModel.h>
|
||||||
|
|
||||||
class GJsonArrayModel final : public GModel {
|
class GJsonArrayModel final : public GModel {
|
||||||
public:
|
public:
|
||||||
struct FieldSpec {
|
struct FieldSpec {
|
||||||
FieldSpec(const String& a_json_field_name, const String& a_column_name, TextAlignment a_text_alignment, Function<GVariant(const GVariant&)>&& a_massage_for_display = {})
|
FieldSpec(const String& a_column_name, TextAlignment a_text_alignment, Function<GVariant(const JsonObject&)>&& a_massage_for_display = {})
|
||||||
|
: column_name(a_column_name)
|
||||||
|
, text_alignment(a_text_alignment)
|
||||||
|
, massage_for_display(move(a_massage_for_display))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
FieldSpec(const String& a_json_field_name, const String& a_column_name, TextAlignment a_text_alignment)
|
||||||
: json_field_name(a_json_field_name)
|
: json_field_name(a_json_field_name)
|
||||||
, column_name(a_column_name)
|
, column_name(a_column_name)
|
||||||
, text_alignment(a_text_alignment)
|
, text_alignment(a_text_alignment)
|
||||||
, massage_for_display(move(a_massage_for_display))
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
String json_field_name;
|
String json_field_name;
|
||||||
String column_name;
|
String column_name;
|
||||||
TextAlignment text_alignment;
|
TextAlignment text_alignment;
|
||||||
Function<GVariant(const GVariant&)> massage_for_display;
|
Function<GVariant(const JsonObject&)> massage_for_display;
|
||||||
};
|
};
|
||||||
|
|
||||||
static NonnullRefPtr<GJsonArrayModel> create(const String& json_path, Vector<FieldSpec>&& fields)
|
static NonnullRefPtr<GJsonArrayModel> create(const String& json_path, Vector<FieldSpec>&& fields)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue