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

OperationProfiles.cs « Structures « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e097b67a0af18e1ccf13194d01210a91b172b56b (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
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
/*
 *                     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.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using UVtools.Core.Extensions;
using UVtools.Core.Operations;

namespace UVtools.WPF.Structures;

[Serializable]
public class OperationProfiles //: IList<Operation>
{
    #region Properties
    /// <summary>
    /// Default filepath for store <see cref="UserSettings"/>
    /// </summary>
    private static string FilePath => Path.Combine(UserSettings.SettingsFolder, "operation_profiles.xml");

    [XmlElement(typeof(OperationRepairLayers))]

    [XmlElement(typeof(OperationResize))]
    [XmlElement(typeof(OperationFlip))]
    [XmlElement(typeof(OperationRotate))]
    [XmlElement(typeof(OperationSolidify))]
    [XmlElement(typeof(OperationMorph))]
    [XmlElement(typeof(OperationRaftRelief))]
    [XmlElement(typeof(OperationRedrawModel))]
    [XmlElement(typeof(OperationThreshold))]
    [XmlElement(typeof(OperationLayerArithmetic))]
    [XmlElement(typeof(OperationPixelArithmetic))]
    [XmlElement(typeof(OperationPixelDimming))]
    [XmlElement(typeof(OperationInfill))]
    [XmlElement(typeof(OperationBlur))]
    [XmlElement(typeof(OperationFadeExposureTime))]
    [XmlElement(typeof(OperationDoubleExposure))]
    [XmlElement(typeof(OperationDynamicLayerHeight))]
    [XmlElement(typeof(OperationDynamicLifts))]
    [XmlElement(typeof(OperationRaiseOnPrintFinish))]
    [XmlElement(typeof(OperationChangeResolution))]
    [XmlElement(typeof(OperationTimelapse))]
    [XmlElement(typeof(OperationLithophane))]
    [XmlElement(typeof(OperationPCBExposure))]
    [XmlElement(typeof(OperationScripting))]
        
    [XmlElement(typeof(OperationLayerExportGif))]
    [XmlElement(typeof(OperationLayerExportHtml))]

    [XmlElement(typeof(OperationCalibrateExposureFinder))]
    [XmlElement(typeof(OperationCalibrateElephantFoot))]
    [XmlElement(typeof(OperationCalibrateXYZAccuracy))]
    [XmlElement(typeof(OperationCalibrateLiftHeight))]
    [XmlElement(typeof(OperationCalibrateBloomingEffect))]
    [XmlElement(typeof(OperationCalibrateTolerance))]
    [XmlElement(typeof(OperationCalibrateGrayscale))]
    [XmlElement(typeof(OperationCalibrateStressTower))]
    public List<Operation> Operations { get; internal set; } = new();

    [XmlIgnore]
    public static List<Operation> Profiles
    {
        get => Instance.Operations;
        internal set => Instance.Operations = value;
    }

    #endregion

    #region Singleton

    private static Lazy<OperationProfiles> _instanceHolder =
        new(() => new OperationProfiles());

    /// <summary>
    /// Instance of <see cref="UserSettings"/> (singleton)
    /// </summary>
    public static OperationProfiles Instance => _instanceHolder.Value;

    //public static List<Operation> Operations => _instance.Operations;
    #endregion

    #region Constructor

    private OperationProfiles()
    { }

    #endregion

    #region Indexers

    public Operation this[uint index]
    {
        get => Operations[(int) index];
        set => Operations[(int) index] = value;
    }

    public Operation this[int index]
    {
        get => Operations[index];
        set => Operations[index] = value;
    }

    #endregion

    #region Enumerable
        
    public IEnumerator<Operation> GetEnumerator()
    {
        return Operations.GetEnumerator();
    }

    /*IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }*/
    #endregion

    #region List Implementation
    public void Add(Operation item)
    {
        Operations.Add(item);
    }

    public int IndexOf(Operation item)
    {
        return Operations.IndexOf(item);
    }

    public void Insert(int index, Operation item)
    {
        Operations.Insert(index, item);
    }

    public bool Remove(Operation item)
    {
        return Operations.Remove(item);
    }

    public void RemoveAt(int index)
    {
        Operations.RemoveAt(index);
    }

    public void Clear()
    {
        Operations.Clear();
    }

    public bool Contains(Operation item)
    {
        return Operations.Contains(item);
    }

    public void CopyTo(Operation[] array, int arrayIndex)
    {
        Operations.CopyTo(array, arrayIndex);
    }

    public int Count => Operations.Count;
    public bool IsReadOnly => false;
    #endregion

    #region Static Methods
    /// <summary>
    /// Clear all profiles
    /// </summary>
    /// <param name="save">True to save settings on file, otherwise false</param>
    public static void ClearProfiles(bool save = true)
    {
        Instance.Clear();
        if (save) Save();
    }

    /// <summary>
    /// Clear all profiles given a type
    /// </summary>
    /// <param name="type">Type profiles to clear</param>
    /// <param name="save">True to save settings on file, otherwise false</param>
    public static void ClearProfiles(Type type, bool save = true)
    {
        Profiles.RemoveAll(operation => operation.GetType() == type);
        if (save) Save();
    }

    /// <summary>
    /// Load settings from file
    /// </summary>
    public static void Load()
    {
        if (!File.Exists(FilePath))
        {
            return;
        }

        try
        {
            var instance = XmlExtensions.DeserializeFromFile<OperationProfiles>(FilePath);
            _instanceHolder = new Lazy<OperationProfiles>(() => instance);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
            ClearProfiles();
        }
    }

    /// <summary>
    /// Save settings to file
    /// </summary>
    public static void Save()
    {
        try
        {
            XmlExtensions.SerializeToFile(Instance, FilePath, XmlExtensions.SettingsIndent);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
        }
    }

    public static Operation FindByName(Operation baseOperation, string profileName)
    {
        return Profiles.Find(operation =>
            operation.GetType() == baseOperation.GetType() &&
            (
                !string.IsNullOrWhiteSpace(profileName) && operation.ProfileName == profileName
                || operation.Equals(baseOperation)
            ));
    }


    /// <summary>
    /// Adds a profile
    /// </summary>
    /// <param name="operation"></param>
    /// <param name="save"></param>
    public static void AddProfile(Operation operation, bool save = true)
    {
        Profiles.Insert(0, operation);
        if(save) Save();
    }

    /// <summary>
    /// Removes a profile
    /// </summary>
    /// <param name="operation"></param>
    /// <param name="save"></param>
    public static void RemoveProfile(Operation operation, bool save = true)
    {
        Instance.Remove(operation);
        if (save) Save();
    }

    /// <summary>
    /// Get all profiles within a type
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public static List<Operation> GetOperations(Type type) 
        => Profiles.Where(operation => operation.GetType() == type).ToList();

    /// <summary>
    /// Get all profiles within a type
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static List<T> GetOperations<T>()
    {
        var result = new List<T>();
        foreach (var operation in Instance)
        {
            if (operation is T operationCast)
            {
                result.Add(operationCast);
            }
        }

        return result;
    }

    #endregion
}