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

CalibrateExposureFinderControl.axaml.cs « Calibrators « Controls « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 52aaf61a6baa78e707fe14b11b0ee9e6410f81ee (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
using System;
using System.Linq;
using System.Timers;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using MessageBox.Avalonia.Enums;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
using UVtools.Core.Objects;
using UVtools.Core.Operations;
using UVtools.WPF.Controls.Tools;
using UVtools.WPF.Extensions;
using UVtools.WPF.Windows;

namespace UVtools.WPF.Controls.Calibrators
{
    public class CalibrateExposureFinderControl : ToolControl
    {
        public OperationCalibrateExposureFinder Operation => BaseOperation as OperationCalibrateExposureFinder;

        private Timer _timer;

        private Bitmap _previewImage;
        private DataGrid _exposureTable;

        public Bitmap PreviewImage
        {
            get => _previewImage;
            set => RaiseAndSetIfChanged(ref _previewImage, value);
        }

        public bool CanSupportPerLayerSettings => SlicerFile.HavePrintParameterPerLayerModifier(FileFormat.PrintParameterModifier.ExposureSeconds);

        public CalibrateExposureFinderControl()
        {
            InitializeComponent();
            _exposureTable = this.FindControl<DataGrid>("ExposureTable");
            BaseOperation = new OperationCalibrateExposureFinder(SlicerFile);

            _timer = new Timer(100)
            {
                AutoReset = false
            };
            _timer.Elapsed += (sender, e) => Dispatcher.UIThread.InvokeAsync(UpdatePreview);
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }

        public override void Callback(ToolWindow.Callbacks callback)
        {
            if (App.SlicerFile is null) return;
            switch (callback)
            {
                case ToolWindow.Callbacks.Init:
                case ToolWindow.Callbacks.Loaded:
                    Operation.PropertyChanged += (sender, e) =>
                    {
                        _timer.Stop();
                        _timer.Start();
                    };
                    _timer.Stop();
                    _timer.Start();
                    break;
            }
        }

        public void UpdatePreview()
        {
            var layers = Operation.GetLayers(true);
            _previewImage?.Dispose();
            if (layers is not null)
            {
                PreviewImage = layers[^1].ToBitmap();
                foreach (var layer in layers)
                {
                    layer.Dispose();
                }
            }
        }

        public void BrightnessExposureGenAdd()
        {
            var values = Operation.MultipleBrightnessValuesArray.ToList();
            // normal exposure - 255
            //     wanted      -  x
            byte brightness = (byte)Math.Round(Operation.MultipleBrightnessGenExposureTime * byte.MaxValue / Operation.NormalExposure).Clamp(1, byte.MaxValue);
            if (values.Contains(brightness)) return;
            values.Add(brightness);
            values.Sort((b, b1) => b1.CompareTo(b));
            Operation.MultipleBrightnessValues = string.Join(", ", values);
        }

        public async void GenerateExposureTable()
        {
            if (Operation.ExposureTable.Count > 0)
            {
                if (await ParentWindow.MessageBoxQuestion(
                    "This automatic exposure table generation will clear the current table data!\n" +
                    "Do you want to continue?"
                    ) != ButtonResult.Yes) return;
            }

            Operation.GenerateExposure();
        }

        public async void ExposureTableAddManual()
        {
            var exposure = Operation.ExposureManualEntry;
            if (!exposure.IsValid)
            {
                await ParentWindow.MessageBoxError(
                    $"Layer height and exposures must be higher than zero (0).\n{exposure}");
                return;
            }

            if (Operation.ExposureTable.Contains(exposure))
            {
                await ParentWindow.MessageBoxError(
                    $"The configured layer height and exposure data already exists on the table.\n{exposure}");
                return;
            }

            Operation.ExposureTable.Add(exposure);
            Operation.SanitizeExposureTable();
        }

        public async void ExposureTableClearEntries()
        {
            if (Operation.ExposureTable.Count <= 0) return;
            if (await ParentWindow.MessageBoxQuestion(
                $"Are you sure you want to all the {Operation.ExposureTable.Count} entries?"
            ) != ButtonResult.Yes) return;

            Operation.ExposureTable.Clear();
        }

        public async void ExposureTableRemoveSelectedEntries()
        {
            if (_exposureTable.SelectedItems.Count <= 0) return;
            if (await ParentWindow.MessageBoxQuestion(
                $"Are you sure you want to remove the {_exposureTable.SelectedItems.Count} selected entries?"
            ) != ButtonResult.Yes) return;

            Operation.ExposureTable.RemoveRange(_exposureTable.SelectedItems.Cast<ExposureItem>());
        }
    }
}