mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:17:44 +00:00
GJsonArrayModel: Add an optional "massage_for_display" fieldspec hook
This allows to you install a custom callback that can do anything with Role::Display data before it's returned by GJsonArrayModel::data().
This commit is contained in:
parent
0f0a528323
commit
078ce97c41
3 changed files with 46 additions and 24 deletions
|
@ -26,11 +26,14 @@ GModel::ColumnMetadata GJsonArrayModel::column_metadata(int column) const
|
|||
|
||||
GVariant GJsonArrayModel::data(const GModelIndex& index, Role role) const
|
||||
{
|
||||
auto& field_spec = m_fields[index.column()];
|
||||
auto& object = m_array.at(index.row()).as_object();
|
||||
|
||||
if (role == GModel::Role::Display) {
|
||||
auto& json_field_name = m_fields[index.column()].json_field_name;
|
||||
auto& json_field_name = field_spec.json_field_name;
|
||||
auto data = object.get(json_field_name);
|
||||
if (field_spec.massage_for_display)
|
||||
return field_spec.massage_for_display(data);
|
||||
if (data.is_int())
|
||||
return data.as_int();
|
||||
if (data.is_uint())
|
||||
|
@ -39,3 +42,12 @@ GVariant GJsonArrayModel::data(const GModelIndex& index, Role role) const
|
|||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void GJsonArrayModel::set_json_path(const String& json_path)
|
||||
{
|
||||
if (m_json_path == json_path)
|
||||
return;
|
||||
|
||||
m_json_path = json_path;
|
||||
update();
|
||||
}
|
||||
|
|
|
@ -6,9 +6,18 @@
|
|||
class GJsonArrayModel final : public GModel {
|
||||
public:
|
||||
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 = {})
|
||||
: json_field_name(a_json_field_name)
|
||||
, column_name(a_column_name)
|
||||
, text_alignment(a_text_alignment)
|
||||
, massage_for_display(move(a_massage_for_display))
|
||||
{
|
||||
}
|
||||
|
||||
String json_field_name;
|
||||
String column_name;
|
||||
TextAlignment text_alignment;
|
||||
Function<GVariant(const GVariant&)> massage_for_display;
|
||||
};
|
||||
|
||||
static NonnullRefPtr<GJsonArrayModel> create(const String& json_path, Vector<FieldSpec>&& fields)
|
||||
|
@ -25,6 +34,9 @@ public:
|
|||
virtual GVariant data(const GModelIndex&, Role = Role::Display) const override;
|
||||
virtual void update() override;
|
||||
|
||||
const String& json_path() const { return m_json_path; }
|
||||
void set_json_path(const String& json_path);
|
||||
|
||||
private:
|
||||
GJsonArrayModel(const String& json_path, Vector<FieldSpec>&& fields)
|
||||
: m_json_path(json_path)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue