aboutsummaryrefslogtreecommitdiffstats
path: root/examples/ColorPalette/PaginatedResource.Model.cs
blob: 68acc4dea220cf01bff0e041c292424c614f70d8 (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
/***************************************************************************************************
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using Refit;
using Qt.DotNet;
using Qt.DotNet.Text;

namespace ColorPalette
{
    public sealed partial class PaginatedResource
    {
        public Model Data { get; set; }

        public PaginatedResource()
        {
            Data = new(this);
        }

        public class Model : ListModel
        {
            private Dictionary<int, string> RoleMap { get; set; }
            private PaginatedResource ResourcePage { get; set; }

            private const string ResourceRole = "resource";

            internal Model(PaginatedResource resPage)
            {
                ResourcePage = resPage;
                RoleMap = ResourceRoles
                    .Select((r, i) => new
                    {
                        Role = r.ConvertCase(CaseStyle.Pascal, CaseStyle.Camel),
                        Index = Roles.UserRole + 1 + i
                    })
                    .Prepend(new { Role = ResourceRole, Index = Roles.UserRole })
                    .ToDictionary(x => x.Index, x => x.Role);
            }

            public object this[int index]
            {
                get
                {
                    if (index < 0 || index >= ResourcePage.Resources.Count)
                        return null;
                    return ResourcePage.Resources[index];
                }
            }

            public object this[int index, string role]
            {
                get
                {
                    if (this[index] is not ResourceData resource)
                        return null;
                    if (role == ResourceRole)
                        return resource;
                    return resource[role.ConvertCase(CaseStyle.Camel, CaseStyle.Pascal)];
                }
            }

            public object this[int index, int role]
            {
                get
                {
                    if (index < 0 || index >= ResourcePage.Resources.Count)
                        return null;
                    if (!RoleMap.TryGetValue(role, out var roleName))
                        return null;
                    return this[index, roleName];
                }
            }

            public int Find(string role, object value)
            {
                for (int i = 0; i < ResourcePage.Resources.Count; ++i) {
                    if (this[i, role].Equals(value))
                        return i;
                }
                return -1;
            }

            public override Dictionary<int, string> RoleNames()
            {
                return RoleMap;
            }

            public override object Data(ModelIndex index, int role)
            {
                return this[index.Row, role];
            }

            public override int RowCount(ModelIndex parent)
            {
                return ResourcePage.Resources.Count;
            }
        }
    }
}