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

ToolLayerImportControl.axaml.cs « Tools « Controls « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f2bfefa80cdc0ac0424776c61fdf5f6fd1dcd62a (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
using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using UVtools.Core.FileFormats;
using UVtools.Core.Objects;
using UVtools.Core.Operations;
using UVtools.WPF.Windows;

namespace UVtools.WPF.Controls.Tools
{
    public class ToolLayerImportControl : ToolControl
    {
        private bool _isAutoSortLayersByFileNameChecked;
        private ValueDescription _selectedFile;
        private Bitmap _previewImage;

        private ListBox FilesListBox;

        public OperationLayerImport Operation => BaseOperation as OperationLayerImport;

        public uint MaximumLayer => App.SlicerFile.LastLayerIndex;

        public string InfoLayerHeightStr => $"({App.SlicerFile.GetHeightFromLayer(Operation.StartLayerIndex)}mm)";

        public bool IsAutoSortLayersByFileNameChecked
        {
            get => _isAutoSortLayersByFileNameChecked;
            set => RaiseAndSetIfChanged(ref _isAutoSortLayersByFileNameChecked, value);
        }

        public string InfoImportResult 
        {
            get
            {
                if (Operation.Files.Count <= 0) return null;
                /*uint modelTotalLayers = (uint) Operation.Files.Count;//Operation.CalculateTotalLayers(App.SlicerFile.LayerCount);
                string textFactor = "grow";
                if (modelTotalLayers < App.SlicerFile.LayerCount)
                {
                    textFactor = "shrink";
                }
                else if (modelTotalLayers == App.SlicerFile.LayerCount)
                {
                    textFactor = "keep";
                }*/

                return
                    $"{Operation.Files.Count} files will be imported into model starting from layer {Operation.StartLayerIndex} {InfoLayerHeightStr}.";
                    //$"Model will {textFactor} from layers {App.SlicerFile.LayerCount} ({App.SlicerFile.TotalHeight}mm) to {modelTotalLayers} ({App.SlicerFile.GetHeightFromLayer(modelTotalLayers, false)}mm)";
            }
            
        }

        public ValueDescription SelectedFile
        {
            get => _selectedFile;
            set
            {
                if(!RaiseAndSetIfChanged(ref _selectedFile, value)) return;
                if (_selectedFile is null)
                {
                    PreviewImage = null;
                    return;
                }
                if (!_selectedFile.ValueAsString.EndsWith(".png", StringComparison.OrdinalIgnoreCase) &&
                    !_selectedFile.ValueAsString.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) &&
                    !_selectedFile.ValueAsString.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase) &&
                    !_selectedFile.ValueAsString.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) &&
                    !_selectedFile.ValueAsString.EndsWith(".gif", StringComparison.OrdinalIgnoreCase)) return;
                PreviewImage = new Bitmap(_selectedFile.ValueAsString);
            }
        }

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


        public ToolLayerImportControl()
        {
            BaseOperation = new OperationLayerImport(SlicerFile);
            if (!ValidateSpawn()) return;
            InitializeComponent();

            FilesListBox = this.Find<ListBox>("FilesListBox");
            FilesListBox.DoubleTapped += (sender, args) =>
            {
                if (FilesListBox.SelectedItem is not ValueDescription file) return;
                App.StartProcess(file.ValueAsString);
            };
            FilesListBox.KeyUp += (sender, e) =>
            {
                switch (e.Key)
                {
                    case Key.Escape:
                        FilesListBox.SelectedItems.Clear();
                        e.Handled = true;
                        break;
                    case Key.Delete:
                        RemoveFiles();
                        e.Handled = true;
                        break;
                    case Key.A:
                        if ((e.KeyModifiers & KeyModifiers.Control) != 0)
                        {
                            FilesListBox.SelectAll();
                            e.Handled = true;
                        }
                        break;
                }
            };
        }

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

        /*public override async Task<bool> ValidateForm()
        {
            UpdateOperation();
            var message = Operation.Validate();
            if (message is null) return true;


            message.Content += "\nDo you want to remove all invalid files from list?";
            if (await ParentWindow.MessageBoxQuestion(message.ToString()) == ButtonResult.Yes)
            {
                ConcurrentBag<StringTag> result = (ConcurrentBag<StringTag>)message.Tag;
                foreach (var file in result)
                {
                    Operation.Files.Remove(file);
                }
            }

            return false;
        }*/

        public override void Callback(ToolWindow.Callbacks callback)
        {
            switch (callback)
            {
                case ToolWindow.Callbacks.Init:
                case ToolWindow.Callbacks.Loaded:
                    RefreshGUI();
                    Operation.Files.CollectionChanged += (sender, args) => RefreshGUI();
                    Operation.PropertyChanged += (sender, args) => RefreshGUI();
                    break;
            }
        }

        public void RefreshGUI()
        {
            RaisePropertyChanged(nameof(InfoLayerHeightStr));
            RaisePropertyChanged(nameof(InfoImportResult));
            ParentWindow.ButtonOkEnabled = Operation.Files.Count > 0;
        }

        public async void AddFiles()
        {
            var filters = Helpers.ToAvaloniaFileFilter(FileFormat.AllFileFiltersAvalonia);
            var orderedFilters = new List<FileDialogFilter> { filters[UserSettings.Instance.General.DefaultOpenFileExtensionIndex] };
            for (int i = 0; i < filters.Count; i++)
            {
                if (i == UserSettings.Instance.General.DefaultOpenFileExtensionIndex) continue;
                orderedFilters.Add(filters[i]);
            }

            var dialog = new OpenFileDialog
            {
                AllowMultiple = true,
                Filters = orderedFilters,
                Directory = UserSettings.Instance.General.DefaultDirectoryOpenFile
            };

            var files = await dialog.ShowAsync(ParentWindow);
            if (files is null || files.Length == 0) return;
            foreach (var filename in files)
            {
                Operation.AddFile(filename);
            }

            if (_isAutoSortLayersByFileNameChecked)
            {
                Operation.Sort();
            }
        }

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

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