Repository navigation
Expand file tree
/
Copy pathListModel.cs
More file actions
407 lines (368 loc) · 16.3 KB
/
Copy pathListModel.cs
File metadata and controls
407 lines (368 loc) · 16.3 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright (C) 2026 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only
using System.Reflection;
using Qt.DotNet;
namespace Qt.Bridge.Models
{
using Qt.Quick;
/// <summary>
/// Represents a flat one-dimensional model with no parent-child hierarchy.
/// </summary>
/// <remarks>
/// <para>
/// Use <see cref="ListModel"/> or <see cref="ListModel{T}"/> when your data is naturally
/// arranged as a simple sequence of items, such as a list of names, products, or messages. This
/// base class fixes the hierarchical parts of <see cref="Model"/> so subclasses can focus on
/// item count, item data, and list-specific change notifications.
/// </para>
/// <para>
/// In practical terms, a list model has exactly one column at the root level and no child
/// items. Valid parent indexes are treated as having zero rows and zero columns. If your data
/// is arranged as rows and columns, prefer <see cref="TableModel"/> instead.
/// </para>
/// <para>
/// Most applications should derive from <see cref="ListModel{T}"/> rather than directly from
/// <see cref="ListModel"/>, because the generic form automatically maps item values or public
/// item properties to named roles.
/// </para>
/// </remarks>
public abstract class ListModel : Model
{
[Qt.Ignore]
public sealed override ModelIndex Index(int row, int column, ModelIndex parent)
=> ModelIndex.Empty;
[Qt.Ignore]
public sealed override ModelIndex Sibling(int row, int column, ModelIndex index)
=> ModelIndex.Empty;
[Qt.Ignore]
public sealed override int ColumnCount(ModelIndex parent)
=> parent?.IsValid == true ? 0 : 1;
[Qt.Ignore]
public sealed override ModelIndex Parent(ModelIndex index) => ModelIndex.Empty;
[Qt.Ignore]
public sealed override bool HasChildren(ModelIndex parent)
=> parent?.IsValid != true && RowCount(parent) > 0;
}
/// <summary>
/// Provides a strongly typed base class for implementing a flat list model.
/// </summary>
/// <typeparam name="T">
/// The item type exposed by the model. This can be a simple value such as <see cref="string"/>
/// or a custom type with multiple public properties.
/// </typeparam>
/// <remarks>
/// <para>
/// Derive from <see cref="ListModel{T}"/> when each row in the list can be represented as a
/// value of type <typeparamref name="T"/>. Implement <see cref="ItemCount"/> to report the
/// number of items and <see cref="Data(int)"/> to return the item at a given row.
/// </para>
/// <para>
/// If <typeparamref name="T"/> is a simple convertible type such as <see cref="string"/> or
/// <see cref="int"/>, the model automatically exposes built-in <c>display</c>, <c>edit</c>,
/// and <c>item</c> roles. If <typeparamref name="T"/> is a custom type, the model can derive
/// values from <see cref="IDisplayable"/>, <see cref="IEditable"/>, and
/// <see cref="IModelItem"/>, and it also exposes each public instance property as an
/// additional named role.
/// </para>
/// <para>
/// The generic list model follows the same item-type conventions as <see cref="TableModel{T}"/>
/// while keeping a one-dimensional shape. If you need fully custom role names or custom role
/// lookup logic, derive from <see cref="ListModel"/> instead and override
/// <see cref="Model.RoleNames"/> and <see cref="Model.Data(ModelIndex, int)"/> yourself.
/// </para>
/// <![CDATA[
/// ```csharp
/// public class NameList : ListModel<string>
/// {
/// private List<string> Names { get; } = ["Ada", "Linus", "Grace"];
///
/// public override int ItemCount() => Names.Count;
///
/// public override string Data(int index)
/// {
/// if (index < 0 || index >= Names.Count)
/// return null;
/// return Names[index];
/// }
/// }
/// ```
/// ]]>
/// </remarks>
public abstract class ListModel<T> : ListModel
{
private const int ItemRole = Roles.UserRole;
/// <summary>
/// Returns the number of items in the list.
/// </summary>
public abstract int ItemCount();
/// <summary>
/// Returns the item at the specified row.
/// </summary>
/// <remarks>
/// Implement this as a simple row lookup against your backing collection. The generic base
/// class validates the incoming <see cref="ModelIndex"/> before calling this method. Return
/// <see langword="null"/> for invalid rows when <typeparamref name="T"/> is a reference
/// type.
/// </remarks>
public abstract T Data(int index);
/// <summary>
/// Updates the item at the specified row.
/// </summary>
/// <returns>
/// <see langword="true"/> if the item was updated; otherwise, <see langword="false"/>.
/// </returns>
/// <remarks>
/// Override this when the list should support replacing whole items, such as for editable
/// simple value types or when exposing the <c>item</c> role for custom types.
/// </remarks>
protected virtual bool SetData(int index, T value) => false;
/// <summary>
/// Clears the value associated with the specified row.
/// </summary>
/// <remarks>
/// Override this when your list supports a dedicated clear operation distinct from setting
/// a normal value. The default implementation returns <see langword="false"/>.
/// </remarks>
protected virtual bool ClearItemData(int index) => false;
/// <summary>
/// Gets whether the model should reject all edit operations.
/// </summary>
/// <remarks>
/// When this property returns <see langword="true"/>, the model does not expose the
/// built-in edit role and <see cref="SetData(ModelIndex, object, int)"/> always returns
/// <see langword="false"/>.
/// </remarks>
protected virtual bool IsReadOnly => false;
/// <summary>
/// Gets whether the whole item should be exposed through the <c>item</c> role.
/// </summary>
/// <remarks>
/// The default implementation returns <see langword="true"/> for backward compatibility
/// with existing list-model usage patterns.
/// </remarks>
protected virtual bool HasItemRole => true;
private readonly Type itemType = typeof(T);
private bool ItemTypeIsConvertible => ValueConverter.IsConvertible(itemType);
private bool ItemTypeIsModelItem => itemType.IsAssignableTo(typeof(IModelItem));
private bool ItemTypeIsDisplayable
=> ItemTypeIsConvertible || itemType.IsAssignableTo(typeof(IDisplayable));
private bool ItemTypeIsEditable
=> ItemTypeIsConvertible || itemType.IsAssignableTo(typeof(IEditable));
private Dictionary<int, PropertyInfo> roleMap;
private Dictionary<int, string> roleNames;
/// <summary>
/// Gets the role-to-property map inferred from <typeparamref name="T"/>.
/// </summary>
/// <remarks>
/// For custom item types, each public instance property is assigned the next available role
/// id after the optional <c>item</c> role and exposed under its QML-style property name.
/// </remarks>
protected Dictionary<int, PropertyInfo> RoleMap
{
get
{
if (roleMap != null)
return roleMap;
roleMap = new Dictionary<int, PropertyInfo>();
if (ItemTypeIsConvertible)
return roleMap;
var props = itemType.GetProperties();
for (int i = 0; i < props.Length; ++i)
roleMap[ItemRole + i + 1] = props[i];
return roleMap;
}
}
/// <summary>
/// Returns the role names inferred from <typeparamref name="T"/>.
/// </summary>
/// <remarks>
/// Depending on the item type, this can include the built-in <c>display</c> and
/// <c>edit</c> roles, the optional <c>item</c> role for the whole list item, and
/// property-based roles generated from public instance properties.
/// </remarks>
public sealed override Dictionary<int, string> RoleNames()
{
if (roleNames != null)
return roleNames.Any() ? roleNames : null;
roleNames = new Dictionary<int, string>();
if (ItemTypeIsDisplayable)
roleNames[Roles.DisplayRole] = "display";
if (ItemTypeIsEditable && !IsReadOnly)
roleNames[Roles.EditRole] = "edit";
if (HasItemRole)
roleNames[ItemRole] = "item";
foreach (var entry in RoleMap)
roleNames[entry.Key] = entry.Value.Name.ToQmlPropertyName();
return roleNames.Any() ? roleNames : null;
}
/// <summary>
/// Returns the value for the specified row and role.
/// </summary>
/// <remarks>
/// For simple value types, the built-in display and edit roles return the item directly.
/// For custom item types, the base class uses <see cref="IDisplayable"/>,
/// <see cref="IEditable"/>, the optional <c>item</c> role, and public instance properties
/// to resolve role values.
/// </remarks>
public sealed override object Data(ModelIndex index, int role)
{
if (index is not { IsValid: true } || index.Row < 0)
return null;
if (Data(index.Row) is not { } data)
return null;
switch (role) {
case Roles.DisplayRole when ItemTypeIsConvertible:
return data;
case Roles.DisplayRole when data is IDisplayable displayableItem:
return displayableItem.DisplayValue;
case Roles.DisplayRole:
return null;
case Roles.EditRole when ItemTypeIsConvertible:
return data;
case Roles.EditRole when data is IEditable { IsEditable: true } editableItem:
return editableItem.EditValue;
case Roles.EditRole:
return null;
case ItemRole when HasItemRole:
return data;
}
if (!RoleMap.TryGetValue(role, out var property) || property is not { CanRead: true })
return null;
return property.GetValue(data);
}
/// <summary>
/// Returns the capability flags for the specified item.
/// </summary>
/// <remarks>
/// The base implementation derives enabled/selectable state from <see cref="IModelItem"/>
/// when available and derives editability from <see cref="IsReadOnly"/>,
/// <typeparamref name="T"/>, and <see cref="IEditable"/>.
/// </remarks>
public sealed override int Flags(ModelIndex index)
{
if (index is not { IsValid: true } || Data(index.Row) is not { } item)
return ItemFlags.NoItemFlags;
var flags = ItemFlags.NoItemFlags;
if (ItemTypeIsModelItem) {
if (item is IModelItem { IsEnabled: true })
flags |= ItemFlags.ItemIsEnabled;
if (item is IModelItem { IsSelectable: true })
flags |= ItemFlags.ItemIsSelectable;
} else {
flags = base.Flags(index);
}
if (!IsReadOnly && (ItemTypeIsConvertible || item is IEditable { IsEditable: true }))
flags |= ItemFlags.ItemIsEditable;
return flags;
}
/// <summary>
/// Returns the number of rows for the specified parent index.
/// </summary>
/// <remarks>
/// For the root level, this returns <see cref="ItemCount"/>. For valid parent indexes, it
/// returns <c>0</c> because <see cref="ListModel{T}"/> is always a flat list.
/// </remarks>
public sealed override int RowCount(ModelIndex parent)
{
return parent?.IsValid == true ? 0 : ItemCount();
}
/// <summary>
/// Updates the value for the specified row and role.
/// </summary>
/// <remarks>
/// For editable simple value types, this updates the item through <see cref="SetData(int, T)"/>.
/// For custom item types, it updates either <see cref="IEditable.EditValue"/>, the full
/// item via the <c>item</c> role, or a writable public property. Successful updates
/// automatically notify connected views.
/// </remarks>
public sealed override bool SetData(ModelIndex index, object value, int role)
{
if (IsReadOnly)
return false;
if (index is not { IsValid: true } || index.Row < 0 || Data(index.Row) is not { } item)
return false;
switch (role) {
case Roles.EditRole when ItemTypeIsConvertible: {
if (!SetData(index.Row, ValueConverter.ToValue<T>(value)))
return false;
DataChanged(index, index);
return true;
}
case Roles.EditRole when item is IEditable { IsEditable: true } editableItem:
editableItem.EditValue = value;
DataChanged(index, index);
return true;
case Roles.EditRole:
return false;
case ItemRole when HasItemRole && value is T newItem: {
if (!SetData(index.Row, newItem))
return false;
DataChanged(index, index);
return true;
}
}
if (!RoleMap.TryGetValue(role, out var prop) || prop is not { CanWrite: true })
return false;
try {
prop.SetValue(item, value);
DataChanged(index, index);
return true;
} catch (Exception) {
return false;
}
}
/// <summary>
/// Clears the value associated with the specified row.
/// </summary>
/// <remarks>
/// This forwards a valid model index to <see cref="ClearItemData(int)"/> and rejects
/// invalid indexes.
/// </remarks>
public sealed override bool ClearItemData(ModelIndex index)
{
if (index is not { IsValid: true } || index.Row < 0)
return false;
return ClearItemData(index.Row);
}
/// <summary>
/// Notifies that a single list item changed in place.
/// </summary>
protected void DataChanged(int index)
{
var idx = new ModelIndex(index, 0);
DataChanged(idx, idx);
}
/// <summary>
/// Begins an item insertion notification for the specified inclusive item range.
/// </summary>
/// <remarks>
/// Call this immediately before inserting items into the backing collection. Pair it with
/// <see cref="EndInsertItems"/> in a <c>try</c>/<c>finally</c> block.
/// </remarks>
protected void BeginInsertItems(int first, int last)
{
BeginInsertRows(ModelIndex.Empty, first, last);
}
/// <summary> Ends the current item insertion notification sequence. </summary>
protected void EndInsertItems()
{
EndInsertRows();
}
/// <summary>
/// Begins an item removal notification for the specified inclusive item range.
/// </summary>
/// <remarks>
/// Call this immediately before removing items from the backing collection. Pair it with
/// <see cref="EndRemoveItems"/> in a <c>try</c>/<c>finally</c> block.
/// </remarks>
protected void BeginRemoveItems(int first, int last)
{
BeginRemoveRows(ModelIndex.Empty, first, last);
}
/// <summary> Ends the current item removal notification sequence. </summary>
protected void EndRemoveItems()
{
EndRemoveRows();
}
}
}