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

ToolPCBExposureControl.axaml.cs « Tools « Controls « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ceb8d256f6eaf17a91a00c0cec7faadef6408b05 (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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Timers;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using UVtools.Core.Extensions;
using UVtools.Core.Objects;
using UVtools.Core.Operations;
using UVtools.WPF.Extensions;
using UVtools.WPF.Windows;
using Bitmap = Avalonia.Media.Imaging.Bitmap;

namespace UVtools.WPF.Controls.Tools
{
    public partial class ToolPCBExposureControl : ToolControl
    {

        public OperationPCBExposure Operation => BaseOperation as OperationPCBExposure;

        private readonly Timer _timer;
        private ListBox FilesListBox;

        private Bitmap _previewImage;
        private ValueDescription _selectedFile;

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

        public ValueDescription SelectedFile
        {
            get => _selectedFile;
            set
            {
                if (!RaiseAndSetIfChanged(ref _selectedFile, value)) return;
                UpdatePreview();
            }
        }

        public ToolPCBExposureControl()
        {
            BaseOperation = new OperationPCBExposure(SlicerFile);
            if (!ValidateSpawn()) return;
            InitializeComponent();

            FilesListBox = this.Find<ListBox>("FilesListBox");

            _timer = new Timer(20)
            {
                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();
                    if(ParentWindow is not null) ParentWindow.ButtonOkEnabled = Operation.FileCount > 0;
                    Operation.Files.CollectionChanged += (sender, e) => ParentWindow.ButtonOkEnabled = Operation.FileCount > 0;
                    break;
            }
        }

        public void UpdatePreview()
        {
            try
            {
                PreviewImage = null;
                if (_selectedFile is null)
                {
                    return;
                }
                
                if (!OperationPCBExposure.ValidExtensions.Any(extension => _selectedFile.ValueAsString.EndsWith($".{extension}", StringComparison.InvariantCultureIgnoreCase)) || !File.Exists(_selectedFile.ValueAsString)) return;
                using var mat = Operation.GetMat(_selectedFile.ValueAsString);
                using var matCropped = mat.CropByBounds(20);
                _previewImage?.Dispose();
                PreviewImage = matCropped.ToBitmap();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
            
        }

        public async void AddFiles()
        {
            var dialog = new OpenFileDialog
            {
                AllowMultiple = true,
                Filters = new List<FileDialogFilter>
                {
                    new()
                    {
                        Name = "Gerber files",
                        Extensions = new List<string>(OperationPCBExposure.ValidExtensions)
                    },
                },
            };

            var files = await dialog.ShowAsync(ParentWindow);
            if (files is null || files.Length == 0) return;

            Operation.AddFiles(files);
        }

        public async void AddFilesFromZip()
        {
            var dialog = new OpenFileDialog
            {
                AllowMultiple = false,
                Filters = Helpers.ZipFileFilter
            };

            var files = await dialog.ShowAsync(ParentWindow);
            if (files is null || files.Length == 0) return;

            Operation.AddFilesFromZip(files[0]);
        }

        public void RemoveFiles()
        {
            Operation.Files.RemoveRange(FilesListBox.SelectedItems.OfType<ValueDescription>());
        }

        public void ClearFiles()
        {
            Operation.Files.Clear();
            PreviewImage = null;
        }
    }
}