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
|
/***************************************************************************************************
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 Qt.DotNet;
using System.Collections;
using UserViewLib;
namespace UserViewQml
{
using static Adapter;
public class UserListModel : QAbstractListModel, IUserList
{
private readonly object CriticalSection = new();
private UserList Users { get; set; } = new();
private IQModelIndex NullIndex { get; } = null;
private IQModelIndex FirstIndex { get; } = null;
public int Count { get; private set; } = 0;
public UserListModel()
{
NullIndex = QModelIndex();
FirstIndex = CreateIndex(0, 0, 0);
}
public void Add(User user, int index = -1)
{
if (user == null)
return;
if (index < 0 || index > Count)
index = Count;
BeginInsertRows(NullIndex, index, index);
lock (CriticalSection) {
Users.Add(user, index);
Count = Users.Count;
}
EndInsertRows();
}
public void RemoveAt(int index)
{
BeginRemoveRows(NullIndex, index, index);
lock (CriticalSection) {
Users.RemoveAt(index);
Count = Users.Count;
}
EndRemoveRows();
EmitDataChanged(FirstIndex, FirstIndex, [(int)ItemDataRole.DisplayRole]);
}
public int BinarySearch(User user, IComparer<User> comparer)
{
lock (CriticalSection) {
return Users.BinarySearch(user, comparer);
}
}
public IEnumerator<User> GetEnumerator()
{
lock (CriticalSection) {
return Users.GetEnumerator();
}
}
IEnumerator IEnumerable.GetEnumerator()
{
lock (CriticalSection) {
return Users.GetEnumerator();
}
}
public enum UserItemRoles
{
None = ItemDataRole.UserRole,
FullName, FirstName, LastName, Email, Thumbnail, Picture
}
public override string RoleNames()
{
return "fullName,firstName,lastName,email,thumbnail,picture";
}
public override IQVariant Data(IQModelIndex index, int role = 0)
{
if (index.Row() >= Count)
return null;
var user = Users.ElementAt(index.Row());
string text = role switch
{
(int)ItemDataRole.DisplayRole or
(int)UserItemRoles.FullName => user.Name.Full,
(int)UserItemRoles.FirstName => user.Name.First,
(int)UserItemRoles.LastName => user.Name.Last,
(int)UserItemRoles.Email => user.Email,
(int)UserItemRoles.Thumbnail => user.Picture.Thumbnail,
(int)UserItemRoles.Picture => user.Picture.Large,
_ => string.Empty
};
if (string.IsNullOrEmpty(text))
return null;
var data = QVariant(text);
return data;
}
public override int RowCount(IQModelIndex parent = null)
{
if (parent?.IsValid() == true)
return 0;
return Count;
}
}
}
|