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-10 05:29:46 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/JsonArray.h>
|
2019-08-10 10:06:29 -03:00
|
|
|
#include <AK/JsonObject.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Model.h>
|
2019-08-10 05:29:46 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
|
|
class JsonArrayModel final : public Model {
|
2019-08-10 05:29:46 -03:00
|
|
|
public:
|
|
|
|
|
struct FieldSpec {
|
2020-02-06 07:56:38 -03:00
|
|
|
FieldSpec(const String& a_column_name, Gfx::TextAlignment a_text_alignment, Function<Variant(const JsonObject&)>&& a_massage_for_display, Function<Variant(const JsonObject&)>&& a_massage_for_sort = {}, Function<Variant(const JsonObject&)>&& a_massage_for_custom = {})
|
2019-08-10 10:06:29 -03:00
|
|
|
: column_name(a_column_name)
|
|
|
|
|
, text_alignment(a_text_alignment)
|
|
|
|
|
, massage_for_display(move(a_massage_for_display))
|
2019-08-12 06:54:18 -03:00
|
|
|
, massage_for_sort(move(a_massage_for_sort))
|
2019-08-14 15:30:18 -03:00
|
|
|
, massage_for_custom(move(a_massage_for_custom))
|
2019-08-10 10:06:29 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 07:56:38 -03:00
|
|
|
FieldSpec(const String& a_json_field_name, const String& a_column_name, Gfx::TextAlignment a_text_alignment)
|
2019-08-10 06:06:29 -03:00
|
|
|
: json_field_name(a_json_field_name)
|
|
|
|
|
, column_name(a_column_name)
|
|
|
|
|
, text_alignment(a_text_alignment)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 05:29:46 -03:00
|
|
|
String json_field_name;
|
|
|
|
|
String column_name;
|
2020-02-06 07:56:38 -03:00
|
|
|
Gfx::TextAlignment text_alignment;
|
2020-02-02 11:07:41 -03:00
|
|
|
Function<Variant(const JsonObject&)> massage_for_display;
|
|
|
|
|
Function<Variant(const JsonObject&)> massage_for_sort;
|
|
|
|
|
Function<Variant(const JsonObject&)> massage_for_custom;
|
2019-08-10 05:29:46 -03:00
|
|
|
};
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
static NonnullRefPtr<JsonArrayModel> create(const String& json_path, Vector<FieldSpec>&& fields)
|
2019-08-10 05:29:46 -03:00
|
|
|
{
|
2021-04-23 11:46:57 -03:00
|
|
|
return adopt_ref(*new JsonArrayModel(json_path, move(fields)));
|
2019-08-10 05:29:46 -03:00
|
|
|
}
|
|
|
|
|
|
2020-09-20 16:01:08 -03:00
|
|
|
virtual ~JsonArrayModel() override { }
|
2019-08-10 05:29:46 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
virtual int row_count(const ModelIndex& = ModelIndex()) const override { return m_array.size(); }
|
|
|
|
|
virtual int column_count(const ModelIndex& = ModelIndex()) const override { return m_fields.size(); }
|
2019-08-10 05:29:46 -03:00
|
|
|
virtual String column_name(int column) const override { return m_fields[column].column_name; }
|
2020-08-16 11:00:07 -03:00
|
|
|
virtual Variant data(const ModelIndex&, ModelRole = ModelRole::Display) const override;
|
2021-05-25 11:13:19 -03:00
|
|
|
virtual void invalidate() override;
|
2019-08-10 05:29:46 -03:00
|
|
|
|
2019-08-10 06:06:29 -03:00
|
|
|
const String& json_path() const { return m_json_path; }
|
|
|
|
|
void set_json_path(const String& json_path);
|
|
|
|
|
|
2020-03-26 16:01:23 -03:00
|
|
|
bool add(const Vector<JsonValue>&& fields);
|
2021-03-25 08:30:42 -03:00
|
|
|
bool set(int row, Vector<JsonValue>&& fields);
|
2020-03-26 16:01:23 -03:00
|
|
|
bool remove(int row);
|
|
|
|
|
bool store();
|
|
|
|
|
|
2019-08-10 05:29:46 -03:00
|
|
|
private:
|
2020-02-02 11:07:41 -03:00
|
|
|
JsonArrayModel(const String& json_path, Vector<FieldSpec>&& fields)
|
2019-08-10 05:29:46 -03:00
|
|
|
: m_json_path(json_path)
|
|
|
|
|
, m_fields(move(fields))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String m_json_path;
|
|
|
|
|
Vector<FieldSpec> m_fields;
|
|
|
|
|
JsonArray m_array;
|
|
|
|
|
};
|
2020-02-02 11:07:41 -03:00
|
|
|
|
|
|
|
|
}
|