summaryrefslogtreecommitdiff
path: root/LibGUI/GTableModel.h
blob: e0b43b904a6d4aed0a007488742c9cb687e204bb (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
#pragma once

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

class GTableView;

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 GTableModel {
public:
    struct ColumnMetadata {
        int preferred_width { 0 };
        TextAlignment text_alignment { TextAlignment::CenterLeft };
    };

    virtual ~GTableModel();

    virtual int row_count() const = 0;
    virtual int column_count() 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&) const = 0;
    virtual void update() = 0;
    virtual void activate(const GModelIndex&) { }

    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& index) { m_selected_index = index; }
    GModelIndex selected_index() const { return m_selected_index; }

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

protected:
    GTableModel();

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

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