aboutsummaryrefslogtreecommitdiffstats
path: root/examples/Primes/Primes.cs
blob: 3011344e10c6bdad7ac28ccd8a5b16f9553c7bc4 (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
/***************************************************************************************************
 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 System.ComponentModel;
using Qt.DotNet;
using Qt.DotNet.Utils;
using Qt.Quick;

namespace PrimesApp
{
    public class Primes : ListModel<Prime>, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private List<Prime> Items { get; } = new();

        public int Count
        {
            get => Items.Count;
            set
            {
                if (value == Items.Count)
                    return;
                Items.Clear();
                Items.AddRange(Enumerable.Range(0, value)
                    .Select(i => new Prime() { N = i + 1 }));
                PropertyChanged?.Invoke(this, new(nameof(Count)));
            }
        }

        public override int ItemCount() => Count;

        public override Prime Data(int idx)
        {
            if (idx < 0 || idx >= Count)
                return null;
            return Items[idx];
        }
    }
}