Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OperationProgress.cs « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8b6559d5301da2a33c1e34b45cce2e9a042cd02c (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
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
using System;
using System.Threading;

namespace UVtools.Core
{
    public sealed class OperationProgress
    {
        public const string StatusDecodeThumbnails = "Decoding Thumbnails";
        public const string StatusGatherLayers = "Gathering Layers";
        public const string StatusDecodeLayers = "Decoding Layers";
        public const string StatusEncodeLayers = "Encoding Layers";
        public const string StatusWritingFile = "Writing File";
        public const string StatusEncodeGcode = "Encoding GCode";

        public const string StatusOptimizingBounds = "Gathering Bounds";
        public const string StatusCalculatingBounds = "Calculating Bounds";

        public const string StatusExtracting = "Extracting";

        public const string StatusIslands = "Islands";
        public const string StatusResinTraps = "Resin traps";
        public const string StatusRepairLayers = "Repair Layers";

        public object Mutex = new object();

        public CancellationTokenSource TokenSource { get; } = new CancellationTokenSource();
        public CancellationToken Token => TokenSource.Token;

        private bool _canCancel = true;

        public OperationProgress()
        {
        }

        public OperationProgress(bool canCancel)
        {
            _canCancel = canCancel;
        }

        /// <summary>
        /// Gets or sets if operation can be cancelled
        /// </summary>
        public bool CanCancel
        {
            get
            {
                if (!_canCancel) return _canCancel;
                return !Token.IsCancellationRequested && Token.CanBeCanceled && _canCancel;
            }
            set => _canCancel = value;
        }

        /// <summary>
        /// Gets or sets the item name for the operation
        /// </summary>
        public string ItemName { get; set; } = StatusDecodeLayers;

        /// <summary>
        /// Gets or sets the number of processed items
        /// </summary>
        public uint ProcessedItems { get; set; }

        /// <summary>
        /// Gets or sets the total of item count on this operation
        /// </summary>
        public uint ItemCount { get; set; }

        /// <summary>
        /// Gets the remaining items to be processed
        /// </summary>
        public uint RemainingItems => ItemCount - ProcessedItems;

        public int ProgressStep => (int) Math.Min(ProcessedItems * 100 / ItemCount, 100);

        /// <summary>
        /// Gets the progress from 0 to 100%
        /// </summary>
        public double ProgressPercent => Math.Round(ProcessedItems * 100.0 / ItemCount, 2);

        public static OperationProgress operator +(OperationProgress progress, uint value)
        {
            progress.ProcessedItems += value;
            return progress;
        }

        public static OperationProgress operator ++(OperationProgress progress)
        {
            progress.ProcessedItems++;
            return progress;
        }

        public static OperationProgress operator --(OperationProgress progress)
        {
            progress.ProcessedItems--;
            return progress;
        }

        public void Reset(string name = "", uint itemCount = 0, uint items = 0)
        {
            ItemName = name;
            ItemCount = itemCount;
            ProcessedItems = items;
        }


        public override string ToString()
        {
            return ItemCount == 0 ?
                $"{ProcessedItems} / ? {ItemName}" :
                $"{RemainingItems} / {ItemCount} / {ProcessedItems} | {ItemName} | {ProgressPercent:0.00}%";
        }
    }
}