summaryrefslogtreecommitdiff
path: root/Userland/Demos/WidgetGallery/GalleryModels.h
blob: f00992f07f7f3cd0f2a6a6e36ff2610969ffcc92 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright (c) 2021, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/NonnullRefPtr.h>
#include <AK/Vector.h>
#include <LibCore/DirIterator.h>
#include <LibGUI/Model.h>

class MouseCursorModel final : public GUI::Model {
public:
    static NonnullRefPtr<MouseCursorModel> create() { return adopt_ref(*new MouseCursorModel); }
    virtual ~MouseCursorModel() override { }

    enum Column {
        Bitmap,
        Name,
        __Count,
    };

    virtual int row_count(const GUI::ModelIndex&) const override { return m_cursors.size(); }
    virtual int column_count(const GUI::ModelIndex&) const override { return Column::__Count; }
    virtual String column_name(int column_index) const override
    {
        switch (column_index) {
        case Column::Bitmap:
            return {};
        case Column::Name:
            return "Name";
        }
        VERIFY_NOT_REACHED();
    }
    virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
    {
        auto& cursor = m_cursors[index.row()];

        if (role == GUI::ModelRole::Display) {
            switch (index.column()) {
            case Column::Bitmap:
                if (!cursor.bitmap)
                    return {};
                return *cursor.bitmap;
            case Column::Name:
                return cursor.name;
            }
            VERIFY_NOT_REACHED();
        }
        return {};
    }
    virtual void update() override
    {
        m_cursors.clear();

        Core::DirIterator iterator("/res/cursors", Core::DirIterator::Flags::SkipDots);

        while (iterator.has_next()) {
            auto path = iterator.next_full_path();
            if (path.contains("2x"))
                continue;
            Cursor cursor;
            cursor.path = move(path);
            cursor.bitmap = Gfx::Bitmap::load_from_file(cursor.path);
            auto filename_split = cursor.path.split('/');
            cursor.name = filename_split[2];
            m_cursors.append(move(cursor));
        }

        did_update();
    }

private:
    MouseCursorModel() { }

    struct Cursor {
        RefPtr<Gfx::Bitmap> bitmap;
        String path;
        String name;
    };

    Vector<Cursor> m_cursors;
};

class FileIconsModel final : public GUI::Model {
public:
    static NonnullRefPtr<FileIconsModel> create() { return adopt_ref(*new FileIconsModel); }
    virtual ~FileIconsModel() override { }

    enum Column {
        BigIcon,
        LittleIcon,
        Name,
        __Count,
    };

    virtual int row_count(const GUI::ModelIndex&) const override { return m_icon_sets.size(); }
    virtual int column_count(const GUI::ModelIndex&) const override { return Column::__Count; }
    virtual String column_name(int column_index) const override
    {
        switch (column_index) {
        case Column::BigIcon:
            return {};
        case Column::LittleIcon:
            return {};
        case Column::Name:
            return "Name";
        }
        VERIFY_NOT_REACHED();
    }
    virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
    {
        auto& icon_set = m_icon_sets[index.row()];

        if (role == GUI::ModelRole::Display) {
            switch (index.column()) {
            case Column::BigIcon:
                if (!icon_set.big_icon)
                    return {};
                return *icon_set.big_icon;
            case Column::LittleIcon:
                if (!icon_set.little_icon)
                    return {};
                return *icon_set.little_icon;
            case Column::Name:
                return icon_set.name;
            }
            VERIFY_NOT_REACHED();
        }
        return {};
    }
    virtual void update() override
    {
        m_icon_sets.clear();

        Core::DirIterator big_iterator("/res/icons/32x32", Core::DirIterator::Flags::SkipDots);

        while (big_iterator.has_next()) {
            auto path = big_iterator.next_full_path();
            if (!path.contains("filetype-") && !path.contains("app-"))
                continue;
            IconSet icon_set;
            icon_set.big_icon = Gfx::Bitmap::load_from_file(path);
            auto filename_split = path.split('/');
            icon_set.name = filename_split[3];
            m_icon_sets.append(move(icon_set));
        }

        auto big_icons_found = m_icon_sets.size();

        Core::DirIterator little_iterator("/res/icons/16x16", Core::DirIterator::Flags::SkipDots);

        while (little_iterator.has_next()) {
            auto path = little_iterator.next_full_path();
            if (!path.contains("filetype-") && !path.contains("app-"))
                continue;
            IconSet icon_set;
            icon_set.little_icon = Gfx::Bitmap::load_from_file(path);
            auto filename_split = path.split('/');
            icon_set.name = filename_split[3];
            for (size_t i = 0; i < big_icons_found; i++) {
                if (icon_set.name == m_icon_sets[i].name) {
                    m_icon_sets[i].little_icon = icon_set.little_icon;
                    goto next_iteration;
                }
            }
            m_icon_sets.append(move(icon_set));
        next_iteration:
            continue;
        }

        did_update();
    }

private:
    FileIconsModel() { }

    struct IconSet {
        RefPtr<Gfx::Bitmap> big_icon;
        RefPtr<Gfx::Bitmap> little_icon;
        String name;
    };

    Vector<IconSet> m_icon_sets;
};