Repository navigation
Expand file tree
/
Copy pathListData.cs
More file actions
106 lines (94 loc) · 3.24 KB
/
Copy pathListData.cs
File metadata and controls
106 lines (94 loc) · 3.24 KB
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
// Copyright (C) 2026 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
using Qt.Bridge.Models;
using Qt.DotNet;
namespace ModelsAndViews
{
public class ListData : Model
{
public List<string> Items { get; } =
[
"John",
"Paul",
"George",
"Ringo"
];
public override ModelIndex Parent(ModelIndex index) => ModelIndex.Empty;
public override ModelIndex Index(int row, int column, ModelIndex parent)
{
if (parent?.IsValid == true)
return ModelIndex.Empty;
return new ModelIndex(row, column);
}
public override int RowCount(ModelIndex parent)
{
if (parent?.IsValid == true)
return 0;
return Items.Count;
}
public override int ColumnCount(ModelIndex parent) => 1;
private static Dictionary<int, string> RoleNamesById { get; } = new()
{
{ Roles.DisplayRole, "display" },
{ Roles.EditRole, "edit" }
};
public override Dictionary<int, string> RoleNames() => RoleNamesById;
public override int Flags(ModelIndex index) => base.Flags(index) | ItemFlags.ItemIsEditable;
public override object Data(ModelIndex index, int role)
{
if (index is not { IsValid: true })
return null;
if (index.Row < 0 || index.Row >= Items.Count)
return null;
return role switch
{
Roles.DisplayRole or Roles.EditRole => Items[index.Row],
_ => null
};
}
public override bool SetData(ModelIndex index, object value, int role)
{
if (index is not { IsValid: true })
return false;
if (index.Row < 0 || index.Row >= Items.Count)
return false;
Items[index.Row] = value.ToString();
DataChanged(index, index, [Roles.DisplayRole, Roles.EditRole]);
return true;
}
public override bool InsertRows(int row, int count, ModelIndex parent = null)
{
if (parent?.IsValid == true)
return false;
if (row < 0 || row > Items.Count)
return false;
if (count < 1)
return false;
BeginInsertRows(parent, row, row + count - 1);
try {
var newRows = Enumerable.Range(0, count)
.Select(_ => "(empty)");
Items.InsertRange(row, newRows);
} finally {
EndInsertRows();
}
return true;
}
public override bool RemoveRows(int row, int count, ModelIndex parent = null)
{
if (parent?.IsValid == true)
return false;
if (row < 0 || row >= Items.Count)
return false;
if (count < 1 || row + count > Items.Count)
return false;
BeginRemoveRows(parent, row, row + count - 1);
try {
Items.RemoveRange(row, count);
} finally {
EndRemoveRows();
}
return true;
}
}
}