summaryrefslogtreecommitdiff
path: root/LibGUI/GModel.h
blob: 0aba8900cd6ac548073b41f687ef843139b55dac (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#pragma once

#include <AK/AKString.h>
#include <AK/Badge.h>
#include <AK/Function.h>
#include <AK/HashTable.h>
#include <AK/Retainable.h>
#include <LibGUI/GModelIndex.h>
#include <LibGUI/GVariant.h>
#include <SharedGraphics/TextAlignment.h>

class Font;
class GAbstractView;

enum class GSortOrder
{
    None,
    Ascending,
    Descending
};

class GModelNotification {
public:
    enum Type
    {
        Invalid = 0,
        ModelUpdated,
    };

    explicit GModelNotification(Type type, const GModelIndex& index = GModelIndex())
        : m_type(type)
        , m_index(index)
    {
    }

    Type type() const { return m_type; }
    GModelIndex index() const { return m_index; }

private:
    Type m_type { Invalid };
    GModelIndex m_index;
};

class GModel : public Retainable<GModel> {
public:
    struct ColumnMetadata {
        int preferred_width { 0 };
        TextAlignment text_alignment { TextAlignment::CenterLeft };
        const Font* font { nullptr };
    };

    enum class Role
    {
        Display,
        Sort,
        Custom,
        ForegroundColor,
        BackgroundColor,
        Icon
    };

    virtual ~GModel();

    virtual int row_count(const GModelIndex& = GModelIndex()) const = 0;
    virtual int column_count(const GModelIndex& = GModelIndex()) const = 0;
    virtual String row_name(int) const { return {}; }
    virtual String column_name(int) const { return {}; }
    virtual ColumnMetadata column_metadata(int) const { return {}; }
    virtual GVariant data(const GModelIndex&, Role = Role::Display) const = 0;
    virtual void update() = 0;
    virtual GModelIndex parent_index(const GModelIndex&) const { return {}; }
    virtual GModelIndex index(int row, int column = 0, const GModelIndex& = GModelIndex()) const { return create_index(row, column); }
    virtual GModelIndex sibling(int row, int column, const GModelIndex& parent) const;
    virtual bool is_editable(const GModelIndex&) const { return false; }
    virtual void set_data(const GModelIndex&, const GVariant&) {}

    bool is_valid(const GModelIndex& index) const
    {
        return index.row() >= 0 && index.row() < row_count() && index.column() >= 0 && index.column() < column_count();
    }

    void set_selected_index(const GModelIndex&);
    GModelIndex selected_index() const { return m_selected_index; }

    virtual int key_column() const { return -1; }
    virtual GSortOrder sort_order() const { return GSortOrder::None; }
    virtual void set_key_column_and_sort_order(int, GSortOrder) {}

    void register_view(Badge<GAbstractView>, GAbstractView&);
    void unregister_view(Badge<GAbstractView>, GAbstractView&);

    Function<void(GModel&)> on_model_update;
    Function<void(const GModelIndex&)> on_selection_changed;

protected:
    GModel();

    void for_each_view(Function<void(GAbstractView&)>);
    void did_update();

    GModelIndex create_index(int row, int column, void* data = nullptr) const;

private:
    HashTable<GAbstractView*> m_views;
    GModelIndex m_selected_index;
};

inline GModelIndex GModelIndex::parent() const
{
    return m_model ? m_model->parent_index(*this) : GModelIndex();
}