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
|
/*
* 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.Drawing;
using System.Xml.Serialization;
using Emgu.CV;
using UVtools.Core.FileFormats;
using UVtools.Core.Objects;
namespace UVtools.Core.Operations
{
[Serializable]
public abstract class Operation : BindableBase, IDisposable
{
#region Members
private Rectangle _roi = Rectangle.Empty;
private uint _layerIndexEnd;
private uint _layerIndexStart;
private string _profileName;
private bool _profileIsDefault;
private Enumerations.LayerRangeSelection _layerRangeSelection = Enumerations.LayerRangeSelection.All;
private FileFormat _slicerFile;
public const byte ClassNameLength = 9;
#endregion
#region Properties
[XmlIgnore]
public FileFormat SlicerFile
{
get => _slicerFile;
set
{
if(!RaiseAndSetIfChanged(ref _slicerFile, value)) return;
InitWithSlicerFile();
}
}
[XmlIgnore]
public object Tag { get; set; }
/// <summary>
/// Gets the ID name of this operation, this comes from class name with "Operation" removed
/// </summary>
public string Id => GetType().Name.Remove(0, ClassNameLength);
public virtual Enumerations.LayerRangeSelection StartLayerRangeSelection => Enumerations.LayerRangeSelection.All;
/// <summary>
/// Gets the last used layer range selection, returns none if custom
/// </summary>
public Enumerations.LayerRangeSelection LayerRangeSelection
{
get => _layerRangeSelection;
set => RaiseAndSetIfChanged(ref _layerRangeSelection, value);
}
public virtual string LayerRangeString
{
get
{
if (LayerRangeSelection == Enumerations.LayerRangeSelection.None)
{
return $" [Layers: {LayerIndexStart}-{LayerIndexEnd}]";
}
return $" [Layers: {LayerRangeSelection}]";
}
}
/// <summary>
/// Gets if this operation should set layer range to the actual layer index on layer preview
/// </summary>
public virtual bool PassActualLayerIndex => false;
/// <summary>
/// Gets if this operation can make use of ROI
/// </summary>
public virtual bool CanROI => true;
/// <summary>
/// Gets if this operation can store profiles
/// </summary>
public virtual bool CanHaveProfiles => true;
/// <summary>
/// Gets if this operation supports cancellation
/// </summary>
public virtual bool CanCancel => true;
/// <summary>
/// Gets the title of this operation
/// </summary>
public virtual string Title => Id;
/// <summary>
/// Gets a descriptive text of this operation
/// </summary>
public virtual string Description => Id;
/// <summary>
/// Gets the Ok button text
/// </summary>
public virtual string ButtonOkText => Title;
/// <summary>
/// Gets the confirmation text for the operation
/// </summary>
public virtual string ConfirmationText => $"Are you sure you want to {Id}";
/// <summary>
/// Gets the progress window title
/// </summary>
public virtual string ProgressTitle => "Processing items";
/// <summary>
/// Gets the progress action name
/// </summary>
public virtual string ProgressAction => Id;
public bool HaveAction => !string.IsNullOrEmpty(ProgressAction);
/// <summary>
/// Gets the start layer index where operation will starts in
/// </summary>
public virtual uint LayerIndexStart
{
get => _layerIndexStart;
set => RaiseAndSetIfChanged(ref _layerIndexStart, value);
}
/// <summary>
/// Gets the end layer index where operation will ends in
/// </summary>
public virtual uint LayerIndexEnd
{
get => _layerIndexEnd;
set => RaiseAndSetIfChanged(ref _layerIndexEnd, value);
}
public uint LayerRangeCount => LayerIndexEnd - LayerIndexStart + 1;
/// <summary>
/// Gets the name for this profile
/// </summary>
public string ProfileName
{
get => _profileName;
set => RaiseAndSetIfChanged(ref _profileName, value);
}
public bool ProfileIsDefault
{
get => _profileIsDefault;
set => RaiseAndSetIfChanged(ref _profileIsDefault, value);
}
/// <summary>
/// Gets or sets an ROI to process this operation
/// </summary>
[XmlIgnore]
public Rectangle ROI
{
get => _roi;
set => RaiseAndSetIfChanged(ref _roi, value);
}
public bool HaveROI => !ROI.IsEmpty;
#endregion
#region Constructor
protected Operation() { }
protected Operation(FileFormat slicerFile)
{
_slicerFile = slicerFile;
SelectAllLayers();
InitWithSlicerFile();
}
#endregion
#region Methods
/// <summary>
/// Validates the operation
/// </summary>
/// <returns>null or empty if validates, or else, return a string with error message</returns>
public virtual StringTag Validate(params object[] parameters) => null;
public bool CanValidate(params object[] parameters)
{
var result = Validate(parameters);
return result is null || string.IsNullOrEmpty(result.Content);
}
public void SelectAllLayers()
{
LayerIndexStart = 0;
LayerIndexEnd = SlicerFile.LastLayerIndex;
LayerRangeSelection = Enumerations.LayerRangeSelection.All;
}
public void SelectCurrentLayer(uint layerIndex)
{
LayerIndexStart = LayerIndexEnd = layerIndex;
LayerRangeSelection = Enumerations.LayerRangeSelection.Current;
}
public void SelectBottomLayers()
{
LayerIndexStart = 0;
LayerIndexEnd = SlicerFile.BottomLayerCount - 1u;
LayerRangeSelection = Enumerations.LayerRangeSelection.Bottom;
}
public void SelectNormalLayers()
{
LayerIndexStart = SlicerFile.BottomLayerCount;
LayerIndexEnd = SlicerFile.LastLayerIndex;
LayerRangeSelection = Enumerations.LayerRangeSelection.Normal;
}
public void SelectFirstLayer()
{
LayerIndexStart = LayerIndexEnd = 0;
LayerRangeSelection = Enumerations.LayerRangeSelection.First;
}
public void SelectLastLayer()
{
LayerIndexStart = LayerIndexEnd = SlicerFile.LastLayerIndex;
LayerRangeSelection = Enumerations.LayerRangeSelection.Last;
}
public void SelectLayers(Enumerations.LayerRangeSelection range)
{
switch (range)
{
case Enumerations.LayerRangeSelection.None:
break;
case Enumerations.LayerRangeSelection.All:
SelectAllLayers();
break;
case Enumerations.LayerRangeSelection.Current:
//SelectCurrentLayer();
break;
case Enumerations.LayerRangeSelection.Bottom:
SelectBottomLayers();
break;
case Enumerations.LayerRangeSelection.Normal:
SelectNormalLayers();
break;
case Enumerations.LayerRangeSelection.First:
SelectFirstLayer();
break;
case Enumerations.LayerRangeSelection.Last:
SelectLastLayer();
break;
default:
throw new ArgumentOutOfRangeException();
}
}
/// <summary>
/// Called to init the object when <see cref="SlicerFile"/> changes
/// </summary>
public virtual void InitWithSlicerFile() { }
public Mat GetRoiOrDefault(Mat defaultMat)
{
return HaveROI ? new Mat(defaultMat, ROI) : defaultMat;
}
public virtual Operation Clone()
{
var operation = MemberwiseClone() as Operation;
operation.SlicerFile = _slicerFile;
return operation;
}
public override string ToString()
{
if (!string.IsNullOrEmpty(ProfileName)) return ProfileName;
var result = $"{Title}: {LayerRangeString}";
return result;
}
protected virtual bool ExecuteInternally(OperationProgress progress)
{
throw new NotImplementedException();
}
public bool Execute(OperationProgress progress = null)
{
if (_slicerFile is null) throw new InvalidOperationException($"{Title} can't execute due the lacking of file parent.");
progress ??= new OperationProgress();
progress.Reset(ProgressAction, LayerRangeCount);
var result = ExecuteInternally(progress);
progress.Token.ThrowIfCancellationRequested();
return result;
}
public virtual bool Execute(Mat mat, params object[] arguments)
{
throw new NotImplementedException();
}
public virtual void Dispose() { }
#endregion
}
}
|