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

OperationProgress.cs « Operations « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0da2586f1006416ed91677fce7d0b8576d67f63f (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
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
/*
 *                     GNU AFFERO GENERAL PUBLIC LICENSE
 *                       Version 3, 19 November 2007
 *  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 *  Everyone is permitted to copy and distribute verbatim copies
 *  of this license document, but changing it is not allowed.
 */
using System;
using System.Diagnostics;
using System.Threading;
using UVtools.Core.Extensions;
using UVtools.Core.Objects;

namespace UVtools.Core.Operations
{
    public sealed class OperationProgress : BindableBase
    {
        public const string StatusDecodeThumbnails = "Decoded Thumbnails";
        public const string StatusGatherLayers = "Gathered Layers";
        public const string StatusDecodeLayers = "Decoded Layers";
        public const string StatusEncodeLayers = "Encoded 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 = "Layers processed (Islands/Overhangs)";
        public const string StatusResinTrapsOptimized = "Layers optimized (Resin traps)";
        public const string StatusResinTraps = "Layers processed (Resin traps)";
        public const string StatusRepairLayers = "Repaired Layers";

        public readonly object Mutex = new();

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

        private bool _canCancel = true;
        private string _title = "Operation";
        private string _itemName = "Initializing";
        private uint _processedItems;
        private uint _itemCount;
        

        public 
            OperationProgress()
        {
        }

        public OperationProgress(string name, uint value = 0)
        {
            Reset(name, value);
        }

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

        public Stopwatch StopWatch { get; } = new ();

        /// <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 => RaiseAndSetIfChanged(ref _canCancel, value);
        }

        /// <summary>
        /// Gets or sets the item name for the operation
        /// </summary>
        public string Title
        {
            get => _title;
            set => RaiseAndSetIfChanged(ref _title, value);
        }

        public string ElapsedTimeStr => $"{StopWatch.Elapsed.Minutes}m {StopWatch.Elapsed.Seconds}s";
        //{StopWatch.Elapsed.Milliseconds} ms

        /// <summary>
        /// Gets or sets the item name for the operation
        /// </summary>
        public string ItemName
        {
            get => _itemName;
            set => RaiseAndSetIfChanged(ref _itemName, value);
        }

        /// <summary>
        /// Gets or sets the number of processed items
        /// </summary>
        public uint ProcessedItems
        {
            get => _processedItems;
            set
            {
                //_processedItems = value;
                if(!RaiseAndSetIfChanged(ref _processedItems, value)) return;
                RaisePropertyChanged(nameof(ProgressPercent));
                RaisePropertyChanged(nameof(Description));
            }
        }

        /// <summary>
        /// Gets or sets the total of item count on this operation
        /// </summary>
        public uint ItemCount
        {
            get => _itemCount;
            set
            {
                RaiseAndSetIfChanged(ref _itemCount, value);
                RaisePropertyChanged(nameof(IsIndeterminate));
                RaisePropertyChanged(nameof(ProgressPercent));
                RaisePropertyChanged(nameof(Description));
            }
        }

        /// <summary>
        /// Gets or sets an tag
        /// </summary>
        public object Tag { get; set; }

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

        public int ProgressStep => (int)ProgressPercent;

        public string Description => ToString();

        public bool IsIndeterminate => _itemCount == 0;

        /// <summary>
        /// Gets the progress from 0 to 100%
        /// </summary>
        public double ProgressPercent => _itemCount == 0 ? 0 : Math.Round(_processedItems * 100.0 / _itemCount, 2).Clamp(0, 100);

        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}" :
$"{_processedItems.ToString().PadLeft(_itemCount.ToString().Length, '0')}/{_itemCount} {_itemName} | {ProgressPercent:F2}%";
        }

        public void TriggerRefresh()
        {
            RaisePropertyChanged(nameof(ElapsedTimeStr));
            RaisePropertyChanged(nameof(CanCancel));
            //OnPropertyChanged(nameof(ProgressPercent));
            //OnPropertyChanged(nameof(Description));
        }

        public void LockAndIncrement()
        {
            /*lock (Mutex)
            {
                ProcessedItems++;
            }*/
            Interlocked.Increment(ref _processedItems);
            RaisePropertyChanged(nameof(ProcessedItems));
            RaisePropertyChanged(nameof(ProgressPercent));
            RaisePropertyChanged(nameof(Description));
        }
    }
}