aboutsummaryrefslogtreecommitdiffstats
path: root/examples/Primes/PrimeFactory.cs
blob: 76da5ce04ea7f2282054502bf8e45ddcadd4c840 (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
/***************************************************************************************************
 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 PrimeCreateEventArgs : EventArgs
    {
        public List<Prime> Primes { get; set; }
    }

    public class PrimeFactory
    {
        public int GetNthPrime(int n)
        {
            return Prime.NthPrime(n);
        }

        public Prime[] GetNPrimes(int n)
        {
            var primes = new List<Prime>();
            for (int i = 0; i < n; i++) {
                primes.Add(new Prime() { N = i + 1 });
            }
            return primes.ToArray();
        }

        public int[] GetNPrimeValues(int n)
        {
            var primes = new List<int>();
            for (int i = 0; i < n; i++) {
                primes.Add(GetNthPrime(i + 1));
            }
            return primes.ToArray();
        }

        public event EventHandler<PrimeCreateEventArgs> PrimesCreated;

        public void CreateNPrimes(int n)
        {
            var primes = GetNPrimes(n);
            PrimesCreated?.Invoke(this, new() { Primes = primes.ToList() });
        }
    }
}