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

MainWindow.Information.cs « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b0bb1a6d64a84a71808d107470f514812c70c7f (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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 *                     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;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using MessageBox.Avalonia.Enums;
using UVtools.Core.FileFormats;
using UVtools.Core.Objects;
using UVtools.WPF.Extensions;
using UVtools.WPF.Structures;
using Bitmap = Avalonia.Media.Imaging.Bitmap;
using Helpers = UVtools.WPF.Controls.Helpers;

namespace UVtools.WPF
{
    public partial class MainWindow
    {
        public ObservableCollection<SlicerProperty> SlicerProperties { get; } = new ObservableCollection<SlicerProperty>();
        public DataGrid PropertiesGrid;
        public DataGrid CurrentLayerGrid;

        private uint _visibleThumbnailIndex;
        private Bitmap _visibleThumbnailImage;
        private ObservableCollection<StringTag> _currentLayerProperties = new ObservableCollection<StringTag>();

        public ObservableCollection<StringTag> CurrentLayerProperties
        {
            get => _currentLayerProperties;
            set => RaiseAndSetIfChanged(ref _currentLayerProperties, value);
        }

        public void InitInformation()
        {
            PropertiesGrid = this.Find<DataGrid>(nameof(PropertiesGrid));
            CurrentLayerGrid = this.Find<DataGrid>(nameof(CurrentLayerGrid));
            PropertiesGrid.KeyUp += GridOnKeyUp;
            CurrentLayerGrid.KeyUp += GridOnKeyUp;
            /*CurrentLayerGrid.BeginningEdit += (sender, e) =>
            {
                if (e.Row.DataContext is StringTag stringTag)
                {
                    if (e.Column.DisplayIndex == 0
                        || e.Row.DataContext.ToString() != nameof(LayerCache.Layer.ExposureTime)
                        && e.Row.DataContext.ToString() != nameof(LayerCache.Layer.LightPWM)
                    )
                    {
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            };
            CurrentLayerGrid.RowEditEnding += (sender, e) =>
            {
                if (e.EditAction == DataGridEditAction.Cancel) return;
                if (!(e.Row.DataContext is StringTag stringTag)) return;
                if (float.TryParse(stringTag.TagString, out var result)) return;
                e.Cancel = true;
            };
            CurrentLayerGrid.RowEditEnded += (sender, e) =>
            {
                if (e.EditAction == DataGridEditAction.Cancel) return;
                if (!(e.Row.DataContext is StringTag stringTag)) return;
                switch (stringTag.Content)
                {
                    //case nameof(LayerCache.)
                }
            };*/

        }

        private void GridOnKeyUp(object? sender, KeyEventArgs e)
        {
            if (sender is DataGrid dataGrid)
            {
                switch (e.Key)
                {
                    case Key.Escape:
                        dataGrid.SelectedItems.Clear();
                        break;
                    case Key.Multiply:
                        foreach (var item in dataGrid.Items)
                        {
                            if (dataGrid.SelectedItems.Contains(item))
                                dataGrid.SelectedItems.Remove(item);
                            else
                                dataGrid.SelectedItems.Add(item);
                        }

                        break;
                }
            }
        }

        #region Thumbnails
        public uint VisibleThumbnailIndex
        {
            get => _visibleThumbnailIndex;
            set
            {
                if (value == 0)
                {
                    RaiseAndSetIfChanged(ref _visibleThumbnailIndex, value);
                    RaisePropertyChanged(nameof(ThumbnailCanGoPrevious));
                    RaisePropertyChanged(nameof(ThumbnailCanGoNext));
                    VisibleThumbnailImage = null;
                    return;
                }

                if (!IsFileLoaded) return;
                var index = value - 1;
                if (index >= SlicerFile.CreatedThumbnailsCount) return;
                if (SlicerFile.Thumbnails[index] is null) return;
                if (!RaiseAndSetIfChanged(ref _visibleThumbnailIndex, value)) return;

                VisibleThumbnailImage = SlicerFile.Thumbnails[index].ToBitmap();
                RaisePropertyChanged(nameof(ThumbnailCanGoPrevious));
                RaisePropertyChanged(nameof(ThumbnailCanGoNext));
            }
        }

        public bool ThumbnailCanGoPrevious => SlicerFile is { } && _visibleThumbnailIndex > 1;
        public bool ThumbnailCanGoNext => SlicerFile is { } && _visibleThumbnailIndex < SlicerFile.CreatedThumbnailsCount;

        public void ThumbnailGoPrevious()
        {
            if (!ThumbnailCanGoPrevious) return;
            VisibleThumbnailIndex--;
        }

        public void ThumbnailGoNext()
        {
            if (!ThumbnailCanGoNext) return;
            VisibleThumbnailIndex++;
        }

        public Bitmap VisibleThumbnailImage
        {
            get => _visibleThumbnailImage;
            set
            {
                RaiseAndSetIfChanged(ref _visibleThumbnailImage, value);
                RaisePropertyChanged(nameof(VisibleThumbnailResolution));
            }
        }

        public string VisibleThumbnailResolution => _visibleThumbnailImage is null ? null : $"{{Width: {_visibleThumbnailImage.Size.Width}, Height: {_visibleThumbnailImage.Size.Height}}}";

        public async void OnClickThumbnailSave()
        {
            if (SlicerFile is null) return;
            if (ReferenceEquals(SlicerFile.Thumbnails[_visibleThumbnailIndex - 1], null))
            {
                return; // This should never happen!
            }
            var dialog = new SaveFileDialog
            {
                Filters = Helpers.PngFileFilter,
                Directory = Path.GetDirectoryName(SlicerFile.FileFullPath),
                InitialFileName = $"{Path.GetFileNameWithoutExtension(SlicerFile.FileFullPath)}_thumbnail{_visibleThumbnailIndex}.png"
            };

            var filepath = await dialog.ShowAsync(this);

            if (!string.IsNullOrEmpty(filepath))
            {
                SlicerFile.Thumbnails[_visibleThumbnailIndex - 1].Save(filepath);
            }
        }

        public async void OnClickThumbnailImport()
        {
            if (_visibleThumbnailIndex <= 0) return;
            if (ReferenceEquals(SlicerFile.Thumbnails[_visibleThumbnailIndex - 1], null))
            {
                return; // This should never happen!
            }

            var dialog = new OpenFileDialog
            {
                Filters = Helpers.ImagesFileFilter,
                AllowMultiple = false
            };

            var filepath = await dialog.ShowAsync(this);

            if (filepath is null || filepath.Length <= 0) return;
            int i = (int)(_visibleThumbnailIndex - 1);
            SlicerFile.SetThumbnail(i, filepath[0]);
            VisibleThumbnailImage = SlicerFile.Thumbnails[i].ToBitmap();
        }
        #endregion

        #region Slicer Properties

        public async void OnClickPropertiesSaveFile()
        {
            if (SlicerFile?.Configs is null) return;

            var dialog = new SaveFileDialog
            {
                Filters = Helpers.IniFileFilter,
                Directory = Path.GetDirectoryName(SlicerFile.FileFullPath),
                InitialFileName = $"{Path.GetFileNameWithoutExtension(SlicerFile.FileFullPath)}_properties.ini"
            };

            var file = await dialog.ShowAsync(this);

            if (string.IsNullOrEmpty(file)) return;

            try
            {
                using (TextWriter tw = new StreamWriter(file))
                {
                    foreach (var config in SlicerFile.Configs)
                    {
                        var type = config.GetType();
                        tw.WriteLine($"[{type.Name}]");
                        foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
                        {
                            if (property.Name.Equals("Item")) continue;
                            var value = property.GetValue(config);
                            switch (value)
                            {
                                case null:
                                    continue;
                                case IList list:
                                    tw.WriteLine($"{property.Name} = {list.Count}");
                                    break;
                                default:
                                    tw.WriteLine($"{property.Name} = {value}");
                                    break;
                            }
                        }
                        tw.WriteLine();
                    }
                    tw.Close();
                }
            }
            catch (Exception e)
            {
                await this.MessageBoxError(e.ToString(), "Error occur while save properties");
                return;
            }

            var result = await this.MessageBoxQuestion(
                "Properties save was successful. Do you want open the file in the default editor?",
                "Properties save complete");
            if (result != ButtonResult.Yes) return;

            App.StartProcess(file);
        }

        public void OnClickPropertiesSaveClipboard()
        {
            if (SlicerFile?.Configs is null) return;
            var sb = new StringBuilder();
            foreach (var config in SlicerFile.Configs)
            {
                var type = config.GetType();
                sb.AppendLine($"[{type.Name}]");
                foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
                {
                    if (property.Name.Equals("Item")) continue;
                    var value = property.GetValue(config);
                    switch (value)
                    {
                        case null:
                            continue;
                        case IList list:
                            sb.AppendLine($"{property.Name} = {list.Count}");
                            break;
                        default:
                            sb.AppendLine($"{property.Name} = {value}");
                            break;
                    }
                }

                sb.AppendLine();
            }

            Application.Current.Clipboard.SetTextAsync(sb.ToString());
        }

        public void RefreshProperties()
        {
            SlicerProperties.Clear();
            if (SlicerFile.Configs is null) return;
            foreach (var config in SlicerFile.Configs)
            {
                var type = config.GetType();
                foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
                {
                    if (property.Name.Equals("Item")) continue;
                    var value = property.GetValue(config);
                    switch (value)
                    {
                        case null:
                            continue;
                        case IList list:
                            SlicerProperties.Add(new SlicerProperty(property.Name, list.Count.ToString(),
                                config.GetType().Name));
                            break;
                        default:
                            SlicerProperties.Add(
                                new SlicerProperty(property.Name, value.ToString(), config.GetType().Name));
                            break;
                    }
                }
            }
        }
        #endregion

        #region Current Layer

        public void RefreshCurrentLayerData()
        {
            var layer = LayerCache.Layer;
            CurrentLayerProperties.Clear();
            CurrentLayerProperties.Add(new StringTag(nameof(layer.Index), $"{layer.Index}"));
            CurrentLayerProperties.Add(new StringTag(nameof(layer.LayerHeight), $"{layer.LayerHeight:F2}mm"));
            //CurrentLayerProperties.Add(new KeyValuePair<string, string>(nameof(layer.Filename), layer.Filename));
            CurrentLayerProperties.Add(new StringTag(nameof(layer.PositionZ), $"{layer.PositionZ:F2}mm"));
            CurrentLayerProperties.Add(new StringTag(nameof(layer.IsBottomLayer), layer.IsBottomLayer.ToString()));
            CurrentLayerProperties.Add(new StringTag(nameof(layer.IsModified), layer.IsModified.ToString()));
            //CurrentLayerProperties.Add(new StringTag(nameof(layer.BoundingRectangle), layer.BoundingRectangle.ToString()));
            //CurrentLayerProperties.Add(new StringTag(nameof(layer.NonZeroPixelCount), layer.NonZeroPixelCount.ToString()));
            CurrentLayerProperties.Add(new StringTag(nameof(layer.ExposureTime), $"{layer.ExposureTime:F2}s"));

            if (ReferenceEquals(SlicerFile.PrintParameterPerLayerModifiers, null)) return;

            if (SlicerFile.PrintParameterPerLayerModifiers.Contains(FileFormat.PrintParameterModifier.LiftHeight))
                CurrentLayerProperties.Add(new StringTag(nameof(layer.LiftHeight),
                    $"{layer.LiftHeight.ToString(CultureInfo.InvariantCulture)}mm @ {layer.LiftSpeed.ToString(CultureInfo.InvariantCulture)}mm/min"));
            //CurrentLayerProperties.Adnew StringTagg>(nameof(layer.LiftSpeed), $"{layer.LiftSpeed.ToString(CultureInfo.InvariantCulture)}mm/min"));
            if (SlicerFile.PrintParameterPerLayerModifiers.Contains(FileFormat.PrintParameterModifier.RetractSpeed))
                CurrentLayerProperties.Add(new StringTag(nameof(layer.RetractSpeed),
                    $"{layer.RetractSpeed.ToString(CultureInfo.InvariantCulture)}mm/min"));
            if (SlicerFile.PrintParameterPerLayerModifiers.Contains(FileFormat.PrintParameterModifier.LightOffDelay))
                CurrentLayerProperties.Add(new StringTag(nameof(layer.LightOffDelay),
                    $"{layer.LightOffDelay.ToString(CultureInfo.InvariantCulture)}s"));
            if (SlicerFile.PrintParameterPerLayerModifiers.Contains(FileFormat.PrintParameterModifier.LightPWM))
                CurrentLayerProperties.Add(new StringTag(nameof(layer.LightPWM), layer.LightPWM.ToString()));
        }
        #endregion
    }
}