summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI/GJsonArrayModel.h
blob: 5a75f620ba5b1871386b23125e4cfa6d57d635d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once

#include <AK/JsonArray.h>
#include <LibGUI/GModel.h>

class GJsonArrayModel final : public GModel {
public:
    struct FieldSpec {
        String json_field_name;
        String column_name;
        TextAlignment text_alignment;
    };

    static NonnullRefPtr<GJsonArrayModel> create(const String& json_path, Vector<FieldSpec>&& fields)
    {
        return adopt(*new GJsonArrayModel(json_path, move(fields)));
    }

    virtual ~GJsonArrayModel() override {}

    virtual int row_count(const GModelIndex& = GModelIndex()) const override { return m_array.size(); }
    virtual int column_count(const GModelIndex& = GModelIndex()) const override { return m_fields.size(); }
    virtual String column_name(int column) const override { return m_fields[column].column_name; }
    virtual ColumnMetadata column_metadata(int) const override;
    virtual GVariant data(const GModelIndex&, Role = Role::Display) const override;
    virtual void update() override;

private:
    GJsonArrayModel(const String& json_path, Vector<FieldSpec>&& fields)
        : m_json_path(json_path)
        , m_fields(move(fields))
    {
    }

    String m_json_path;
    Vector<FieldSpec> m_fields;
    JsonArray m_array;
};