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

MainWindow.Issues.cs « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 380525c08e4079af4ba5bf3a748a1b3498c0a8b3 (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
/*
 *                     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.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
using Emgu.CV;
using Emgu.CV.Util;
using MessageBox.Avalonia.Enums;
using UVtools.Core; 
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
using UVtools.Core.Layers;
using UVtools.Core.Objects;
using UVtools.Core.Operations;
using UVtools.WPF.Extensions;
using Brushes = Avalonia.Media.Brushes;

namespace UVtools.WPF;

public partial class MainWindow
{
    #region Members
    private bool _firstTimeOnIssues = true;
    public DataGrid IssuesGrid;

    private int _issueSelectedIndex = -1;

    public IEnumerable IssuesGridItems 
    {
        get
        {
            if (!IsFileLoaded || DataContext is null) return null;
            if (Settings.Issues.DataGridGroupByType || Settings.Issues.DataGridGroupByLayerIndex)
            {
                var groupView = new DataGridCollectionView(SlicerFile.IssueManager);
                if (Settings.Issues.DataGridGroupByType) groupView.GroupDescriptions.Add(new DataGridPathGroupDescription("Type"));
                if (Settings.Issues.DataGridGroupByLayerIndex) groupView.GroupDescriptions.Add(new DataGridPathGroupDescription("StartLayerIndex"));

                return groupView;
            }

            return SlicerFile.IssueManager;
        }
    }
    #endregion

    #region Properties

    private uint _resinTrapDetectionStartLayer;

    public bool IssueCanGoPrevious => IsFileLoaded && SlicerFile.IssueManager.Count > 0 && _issueSelectedIndex > 0;
    public bool IssueCanGoNext => IsFileLoaded && SlicerFile.IssueManager.Count > 0 && _issueSelectedIndex < SlicerFile.IssueManager.Count - 1;

    public uint ResinTrapDetectionStartLayer
    {
        get => _resinTrapDetectionStartLayer;
        set => RaiseAndSetIfChanged(ref _resinTrapDetectionStartLayer, value);
    }

    public bool SuppressIssueGridSelectionEvent { get; set; }

    #endregion

    #region Methods

    public void InitIssues()
    {
        IssuesGrid = this.FindControl<DataGrid>("IssuesGrid");
        IssuesGrid.CellPointerPressed += IssuesGridOnCellPointerPressed;
        IssuesGrid.SelectionChanged += IssuesGridOnSelectionChanged;
        IssuesGrid.KeyUp += IssuesGridOnKeyUp;
    }

    public void IssueGoPrevious()
    {
        if (!IssueCanGoPrevious) return;
        IssueSelectedIndex--;
    }

    public void IssueGoNext()
    {
        if (!IssueCanGoNext) return;
        IssueSelectedIndex++;
    }

    /*public List<IssueOfContours> GetOverlappingIssues(IssueOfContours targetIssue, int indexOffset)
    {
        var retValue = new List<IssueOfContours>();

        int targetLayerIndex = (int)targetIssue.LayerIndex + indexOffset;
        if (targetLayerIndex > SlicerFile.LayerCount - 1 || targetLayerIndex < 0) return retValue;

        foreach (IssueOfContours candidate in SlicerFile.IssueManager.GetIssuesBy(MainIssue.IssueType.SuctionCup, (uint)targetLayerIndex))
        {
            using var vec1 = new VectorOfVectorOfPoint(targetIssue.Contours);
            using var vec2 = new VectorOfVectorOfPoint(candidate.Contours);
            if (!EmguContours.ContoursIntersect(vec1, vec2)) continue;
            retValue.Add(candidate);
            break;
        }

        return retValue;
    }*/

    public async void RemoveRepairIssues(IEnumerable<MainIssue> issues, bool promptConfirmation = true, bool suctionCupDrill = true)
    {
        if (!issues.Any()) return;

        uint emptyLayers = 0;
        uint islands = 0;
        uint resinTraps = 0;
        uint suctionCups = 0;
        foreach (MainIssue mainIssue in issues)
        {
            switch (mainIssue.Type)
            {
                case MainIssue.IssueType.Island:
                    islands++;
                    break;
                case MainIssue.IssueType.Overhang:
                    // No action
                    break;
                case MainIssue.IssueType.ResinTrap:
                    resinTraps++;
                    break;
                case MainIssue.IssueType.SuctionCup:
                    suctionCups++;
                    break;
                case MainIssue.IssueType.TouchingBound:
                    // No action
                    break;
                case MainIssue.IssueType.PrintHeight:
                    // No action
                    break;
                case MainIssue.IssueType.EmptyLayer:
                    emptyLayers++;
                    break;
                case MainIssue.IssueType.Debug:
                    // No action
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        if (emptyLayers == 0 && islands == 0 && resinTraps == 0 && suctionCups == 0) return;

        if (promptConfirmation && await this.MessageBoxQuestion(
                $"Are you sure you want to remove and/or repair all selected {issues.Count()} issues?\n\n" +
                "If any, this option will:\n" +
                (emptyLayers > 0 ? $"- Remove {emptyLayers} empty layer(s)\n" : string.Empty) +
                (islands > 0 ? $"- Remove {emptyLayers} island(s)\n" : string.Empty) +
                (resinTraps > 0 ? $"- Fill/solidify {resinTraps} resin trap(s)\n" : string.Empty) +
                (suctionCups > 0 ? $"- Drill {suctionCups} suction cup(s) at it's center\n" : string.Empty) +
                "\nWarning: Removing an island can cause other issues to appear if there is material present in the layers above it.\n" +
                "Always check previous and next layers before performing an island removal.", $"Remove {issues.Count()} Issues?") != ButtonResult.Yes) return;

        var processParallelIssues = new Dictionary<uint, List<Issue>>();
        var processSuctionCups = new List<MainIssue>();
        var layersToRemove = new List<uint>();


        foreach (MainIssue mainIssue in issues)
        {
            switch (mainIssue.Type)
            {
                case MainIssue.IssueType.Island:
                case MainIssue.IssueType.ResinTrap:
                    foreach (var issue in mainIssue)
                    {
                        // Islands and resin traps
                        if (!processParallelIssues.TryGetValue(issue.LayerIndex, out var issueList))
                        {
                            issueList = new List<Issue>();
                            processParallelIssues.Add(issue.LayerIndex, issueList);
                        }

                        issueList.Add(issue);

                    }
                    continue;
                case MainIssue.IssueType.SuctionCup:
                    if (!suctionCupDrill)
                    {
                        foreach (var issue in mainIssue)
                        {
                            // Islands and resin traps
                            if (!processParallelIssues.TryGetValue(issue.LayerIndex, out var issueList))
                            {
                                issueList = new List<Issue>();
                                processParallelIssues.Add(issue.LayerIndex, issueList);
                            }

                            issueList.Add(issue);

                        }
                        continue;
                    }
                    if(mainIssue.StartLayerIndex == 0) continue;
                    processSuctionCups.Add(mainIssue);
                    continue;
                case MainIssue.IssueType.EmptyLayer:
                    layersToRemove.AddRange(mainIssue.Select(issue => issue.LayerIndex));
                    continue;
            }
        }


        var totalIssues = processParallelIssues.Count + processSuctionCups.Count + layersToRemove.Count;
        if (totalIssues == 0) return;

        var issueRemoveList = new List<MainIssue>();

        IsGUIEnabled = false;
        ShowProgressWindow("Removing selected issues", false);

        Clipboard.Snapshot();

        var task = await Task.Factory.StartNew(() =>
        {
            Progress.Reset("Removing selected issues", (uint)processParallelIssues.Count);
            try
            {
                Parallel.ForEach(processParallelIssues, CoreSettings.GetParallelOptions(Progress), layerIssues =>
                {
                    using (var image = SlicerFile[layerIssues.Key].LayerMat)
                    {
                        var bytes = image.GetDataByteSpan();

                        bool edited = false;
                        foreach (var issue in layerIssues.Value)
                        {
                            if (issue.Type == MainIssue.IssueType.Island)
                            {
                                var issueOfPoints = (IssueOfPoints)issue;
                                foreach (var pixel in issueOfPoints.Points)
                                {
                                    bytes[image.GetPixelPos(pixel.X, pixel.Y)] = 0;
                                }

                                edited = true;
                            }
                            else if (issue.Type == MainIssue.IssueType.ResinTrap || (issue.Type == MainIssue.IssueType.SuctionCup && !suctionCupDrill))
                            {
                                var issueOfContours = (IssueOfContours)issue;
                                using var contours = new VectorOfVectorOfPoint(issueOfContours.Contours);
                                CvInvoke.DrawContours(image, contours, -1, EmguExtensions.WhiteColor, -1);
                                if (Settings.LayerRepair.ResinTrapsOverlapBy > 0)
                                {
                                    CvInvoke.DrawContours(image, contours, -1, EmguExtensions.WhiteColor, Settings.LayerRepair.ResinTrapsOverlapBy * 2 + 1);
                                }
                                edited = true;
                            }
                        }

                        if (edited)
                        {
                            SlicerFile[layerIssues.Key].LayerMat = image;
                        }
                    }

                    Progress.LockAndIncrement();
                });

                if (layersToRemove.Count > 0)
                {
                    OperationLayerRemove.RemoveLayers(SlicerFile, layersToRemove);
                }

                if(suctionCupDrill) issueRemoveList.AddRange(SlicerFile.IssueManager.DrillSuctionCupsForIssues(processSuctionCups, UserSettings.Instance.LayerRepair.SuctionCupsVentHole, Progress));

            }
            catch (Exception ex)
            {
                Dispatcher.UIThread.InvokeAsync(async () =>
                    await this.MessageBoxError(ex.ToString(), "Removal/repair failed"));

                return false;
            }

            return true;
        });

        IsGUIEnabled = true;

        if (!task)
        {
            Clipboard.RestoreSnapshot();
            return;
        }

        var whiteListLayers = new List<uint>();

        // Update GUI
            
        foreach (MainIssue issue in issues)
        {
            if (issue.IsSuctionCup && !suctionCupDrill)
            {
                issueRemoveList.Add(issue);
                continue;
            }

            if (issue.Type 
                is not MainIssue.IssueType.Island
                and not MainIssue.IssueType.ResinTrap
                and not MainIssue.IssueType.EmptyLayer) continue;


            issueRemoveList.Add(issue);


            if (issue.IsIsland)
            {
                var nextLayer = issue.StartLayerIndex + 1;
                if (nextLayer >= SlicerFile.LayerCount) continue;
                if (whiteListLayers.Contains(nextLayer)) continue;
                whiteListLayers.Add(nextLayer);
            }
                
            //Issues.Remove(issue);

        }

        if (issueRemoveList.Count == 0) return;

        Clipboard.Clip($"Manually removed {issueRemoveList.Count} issues");
            
        IssuesGrid.SelectedIndex = -1;
        SlicerFile.IssueManager.RemoveRange(issueRemoveList);

        if (layersToRemove.Count > 0)
        {
            ResetDataContext();
        }

        if (Settings.PixelEditor.PartialUpdateIslandsOnEditing)
        {
            await UpdateIslandsOverhangs(whiteListLayers);
        }

        ShowLayer(); // It will call latter so its a extra call
        CanSave = true;
    }

    public void OnClickIssueRemove()
    {
        RemoveRepairIssues(IssuesGrid.SelectedItems.OfType<MainIssue>(), true);
    }

    public void SelectedIssuesIslandRemove()
    {
        if (IssuesGrid.SelectedItem is null) return;
        RemoveRepairIssues(IssuesGrid.SelectedItems.OfType<MainIssue>().Where(mainIssue => mainIssue.IsIsland), false);
    }

    public void SelectedIssuesResinTrapSolidify()
    {
        if (IssuesGrid.SelectedItem is null) return;
        RemoveRepairIssues(IssuesGrid.SelectedItems.OfType<MainIssue>().Where(mainIssue => mainIssue.IsResinTrap), false);
    }

    public void SelectedIssuesSuctionCupDrill()
    {
        if (IssuesGrid.SelectedItem is null) return;
        RemoveRepairIssues(IssuesGrid.SelectedItems.OfType<MainIssue>().Where(mainIssue => mainIssue.IsSuctionCup), false);
    }

    public void SelectedIssuesSuctionCupSolidify()
    {
        if (IssuesGrid.SelectedItem is null) return;
        RemoveRepairIssues(IssuesGrid.SelectedItems.OfType<MainIssue>().Where(mainIssue => mainIssue.IsSuctionCup), false, false);
    }

    public void SelectedIssuesEmptyLayerRemove()
    {
        if (IssuesGrid.SelectedItem is null) return;
        RemoveRepairIssues(IssuesGrid.SelectedItems.OfType<MainIssue>().Where(mainIssue => mainIssue.IsEmptyLayer), false);
    }

    public async void OnClickIssueIgnore()
    {
        if ((_globalModifiers & KeyModifiers.Alt) != 0)
        {
            if(SlicerFile.IssueManager.IgnoredIssues.Count == 0) return;
            if (await this.MessageBoxQuestion(
                    $"Are you sure you want to re-enable {SlicerFile.IssueManager.IgnoredIssues.Count} ignored issues?\n" +
                    "A full re-detect will be required to get the ignored issues.\n", $"Re-enable {SlicerFile.IssueManager.IgnoredIssues.Count} Issues?") !=
                ButtonResult.Yes) return;

            SlicerFile.IssueManager.IgnoredIssues.Clear();

            return;
        }

        if (IssuesGrid.SelectedItems.Count == 0) return;

        if (await this.MessageBoxQuestion(
                $"Are you sure you want to hide and ignore all selected {IssuesGrid.SelectedItems.Count} issues?\n" +
                "The ignored issues won't be re-detected.\n", $"Ignore {IssuesGrid.SelectedItems.Count} Issues?") !=
            ButtonResult.Yes) return;

        var list = IssuesGrid.SelectedItems.Cast<MainIssue>().ToArray();
        SlicerFile.IssueManager.IgnoredIssues.AddRange(list);
        IssuesGrid.SelectedItems.Clear();
        SlicerFile.IssueManager.RemoveRange(list);
        ShowLayer();
    }

    private async Task UpdateIslandsOverhangs(List<uint> whiteListLayers)
    {
        if (whiteListLayers.Count == 0) return;
        var islandConfig = GetIslandDetectionConfiguration();
        var overhangConfig = GetOverhangDetectionConfiguration();
        var resinTrapConfig = new ResinTrapDetectionConfiguration(false);
        var touchingBoundConfig = new TouchingBoundDetectionConfiguration(false);
        var printHeightConfig = new PrintHeightDetectionConfiguration(false);
        islandConfig.Enabled = true;
        islandConfig.WhiteListLayers = whiteListLayers;
        overhangConfig.Enabled = true;
        overhangConfig.WhiteListLayers = whiteListLayers;


        IsGUIEnabled = false;
        ShowProgressWindow("Updating Issues");


        var issueList = SlicerFile.IssueManager.ToList();
        issueList.RemoveAll(issue =>
            islandConfig.WhiteListLayers.Contains(issue.StartLayerIndex) && issue.Type is MainIssue.IssueType.Island or MainIssue.IssueType.Overhang);
        /*foreach (var layerIndex in islandConfig.WhiteListLayers)
        {
            issueList.RemoveAll(issue =>
                issue.LayerIndex == layerIndex && (issue.Type == LayerIssue.IssueType.Island ||
                                                   issue.Type == LayerIssue.IssueType.Overhang));
            
        }*/

        var resultIssues = await Task.Factory.StartNew(() =>
        {
            try
            {
                var issues = SlicerFile.IssueManager.DetectIssues(islandConfig, overhangConfig, resinTrapConfig,
                    touchingBoundConfig, printHeightConfig, false, Progress);

                issues.RemoveAll(issue => issue.Type is not MainIssue.IssueType.Island and not MainIssue.IssueType.Overhang); // Remove all non islands and overhangs
                return issues;
            }

            catch (OperationCanceledException)
            {

            }
            catch (Exception ex)
            {
                Dispatcher.UIThread.InvokeAsync(async () =>
                    await this.MessageBoxError(ex.ToString(), "Error while trying to compute issues"));
            }

            return null;
        });

        IsGUIEnabled = true;

        if (resultIssues is not null && resultIssues.Count > 0) issueList.AddRange(resultIssues);

        switch (Settings.Issues.DataGridOrderBy)
        {
            case IssuesOrderBy.TypeAscLayerAscAreaDesc:
                issueList = issueList.OrderBy(issue => issue.Type)
                    .ThenBy(issue => issue.StartLayerIndex)
                    .ThenByDescending(issue => issue.Area).ToList();
                break;
            case IssuesOrderBy.TypeAscAreaDescLayerAsc:
                issueList = issueList.OrderBy(issue => issue.Type)
                    .ThenByDescending(issue => issue.Area)
                    .ThenBy(issue => issue.StartLayerIndex).ToList();
                break;
            case IssuesOrderBy.AreaDescLayerIndexAscTypeAsc:
                issueList = issueList.OrderByDescending(issue => issue.Area)
                    .ThenBy(issue => issue.StartLayerIndex)
                    .ThenBy(issue => issue.Type).ToList();
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(Settings.Issues.DataGridOrderBy));
        }
        
            
        SlicerFile.IssueManager.ReplaceCollection(issueList);
    }

    public int IssueSelectedIndex
    {
        get => _issueSelectedIndex;
        set
        {
            if (!RaiseAndSetIfChanged(ref _issueSelectedIndex, value)) return;
            if(_issueSelectedIndex > 0) IssuesGrid.ScrollIntoView(SlicerFile.IssueManager.FirstOrDefault(issue => ReferenceEquals(issue, IssuesGrid.SelectedItem)), null);
            RaisePropertyChanged(nameof(IssueSelectedIndexStr));
            RaisePropertyChanged(nameof(IssueCanGoPrevious));
            RaisePropertyChanged(nameof(IssueCanGoNext));
        }
    }

    public string IssueSelectedIndexStr => IsFileLoaded 
        ? (_issueSelectedIndex + 1).ToString().PadLeft(SlicerFile.IssueManager.Count.ToString().Length, '0')
        : "0";

    private void IssuesGridOnSelectionChanged(object? sender, SelectionChangedEventArgs e)
    {
        if (DataContext is null || SuppressIssueGridSelectionEvent) return;

        if (IssuesGrid.SelectedItem is not MainIssue mainIssue)
        {
            ShowLayer();
            return;
        }

        var issue = mainIssue.FirstOrDefault();
        ZoomToIssue(issue, true);
    }

        
    private void IssuesGridOnCellPointerPressed(object? sender, DataGridCellPointerPressedEventArgs e)
    {
        if (e.PointerPressedEventArgs.ClickCount == 2) return;
        if (IssuesGrid.SelectedItem is not MainIssue) return;
        // Double clicking an issue will center and zoom into the 
        // selected issue. Left click on an issue will zoom to fit.
            
        var pointer = e.PointerPressedEventArgs.GetCurrentPoint(IssuesGrid);

        if (pointer.Properties.IsRightButtonPressed)
        {
            ZoomToFit();
            return;
        }

        //ForceUpdateActualLayer(issue.LayerIndex);

    }

    private void IssuesGridOnKeyUp(object? sender, KeyEventArgs e)
    {
        switch (e.Key)
        {
            case Key.Escape:
                IssuesGrid.SelectedItems.Clear();
                break;
            case Key.Multiply:
                var selectedItems = IssuesGrid.SelectedItems.OfType<MainIssue>().ToList();
                IssuesGrid.SelectedItems.Clear();
                foreach (var item in SlicerFile.IssueManager)
                {
                    if (!selectedItems.Contains(item))
                        IssuesGrid.SelectedItems.Add(item);
                }
                    

                break;
            case Key.Delete:
                OnClickIssueRemove();
                break;
        }
    }

    public async void OnClickRepairIssues()
    {
        await ShowRunOperation(typeof(OperationRepairLayers));
    }

    public async void OnClickExportIssues()
    {
        if (!SlicerFile.IssueManager.HaveIssues) return;
        var dialog = new SaveFileDialog
        {
            DefaultExtension = ".uvtissues",
            Filters = new List<FileDialogFilter>
            {
                new()
                {
                    Name = "UVtools issues files",
                    Extensions = new List<string> {"uvtissues"}
                }
            },
            InitialFileName = SlicerFile.FilenameNoExt,
            Directory = SlicerFile.DirectoryPath
        };

        var path = await dialog.ShowAsync(this);
        if (string.IsNullOrWhiteSpace(path)) return;
        
        IsGUIEnabled = false;
        try
        {
            var exportIssues = new SerializableIssuesDocument(SlicerFile);
            exportIssues.SerializeAsync(path);
        }
        catch (Exception e)
        {
            await this.MessageBoxError(e.ToString());
            Debug.WriteLine(e);
            if(File.Exists(path)) File.Delete(path);
        }

        IsGUIEnabled = true;
    }

    public async Task OnClickDetectIssues()
    {
        if (!IsFileLoaded) return;
        if (SlicerFile.DecodeType == FileFormat.FileDecodeType.Partial)
        {
            await this.MessageBoxError("The file was open in partial mode and the detect issues is unable to run in this mode.\n" +
                                       "Please reload the file in full mode in order to use detect issues.", "Unable to run in partial mode");
            return;

        }
        await ComputeIssues(
            GetIslandDetectionConfiguration(),
            GetOverhangDetectionConfiguration(),
            GetResinTrapDetectionConfiguration(),
            GetTouchingBoundsDetectionConfiguration(),
            GetPrintHeightDetectionConfiguration(),
            Settings.Issues.ComputeEmptyLayers);
    }

    private async Task ComputeIssues(IslandDetectionConfiguration islandConfig = null,
        OverhangDetectionConfiguration overhangConfig = null,
        ResinTrapDetectionConfiguration resinTrapConfig = null,
        TouchingBoundDetectionConfiguration touchingBoundConfig = null,
        PrintHeightDetectionConfiguration printHeightConfig = null,
        bool emptyLayersConfig = true)
    {

        SlicerFile.IssueManager.Clear();
        IsGUIEnabled = false;
        ShowProgressWindow("Computing Issues");

        var resultIssues = await Task.Factory.StartNew(() =>
        {
            try
            {
                var issues = SlicerFile.IssueManager.DetectIssues(islandConfig, overhangConfig, resinTrapConfig, touchingBoundConfig,
                    printHeightConfig, emptyLayersConfig, Progress);

                switch (Settings.Issues.DataGridOrderBy)
                {
                    case IssuesOrderBy.TypeAscLayerAscAreaDesc:
                        // This order is already made on the detection
                        break;
                    case IssuesOrderBy.TypeAscAreaDescLayerAsc:
                        issues = issues.OrderBy(issue => issue.Type)
                            .ThenByDescending(issue => issue.Area)
                            .ThenBy(issue => issue.StartLayerIndex).ToList();
                        break;
                    case IssuesOrderBy.AreaDescLayerIndexAscTypeAsc:
                        issues = issues.OrderByDescending(issue => issue.Area)
                            .ThenBy(issue => issue.StartLayerIndex)
                            .ThenBy(issue => issue.Type).ToList();
                        break;
                    default:
                        throw new ArgumentOutOfRangeException(nameof(Settings.Issues.DataGridOrderBy));
                }

                return issues;
            }
            catch (OperationCanceledException)
            {

            }
            catch (Exception ex)
            {
                Dispatcher.UIThread.InvokeAsync(async () =>
                    await this.MessageBoxError(ex.ToString(), "Error while trying compute issues"));
            }

            return null;
        });

        IsGUIEnabled = true;
            
        if (resultIssues is null)
        {
            return;
        }
        SlicerFile.IssueManager.AddRange(resultIssues);
            
        ShowLayer();

        RaisePropertyChanged(nameof(IssueSelectedIndexStr));
        RaisePropertyChanged(nameof(IssueCanGoPrevious));
        RaisePropertyChanged(nameof(IssueCanGoNext));

    }

    /*public Dictionary<uint, uint> GetIssuesCountPerLayer()
    {
        if (SlicerFile.IssueManager.Count == 0) return null;
        Dictionary<uint, uint> layerIndexIssueCount = new();
        foreach (var issue in Issues)
        {
            if (!layerIndexIssueCount.ContainsKey(issue.StartLayerIndex))
            {
                layerIndexIssueCount.Add(issue.LayerIndex, 1);
            }
            else
            {
                layerIndexIssueCount[issue.LayerIndex]++;
            }
        }

        return layerIndexIssueCount;
    }*/

    public Dictionary<MainIssue.IssueType, ISolidColorBrush> GetIssueColors(bool highlightColors = false)
    {
        return new Dictionary<MainIssue.IssueType, ISolidColorBrush>
        {
            {MainIssue.IssueType.Island,     highlightColors ? Settings.LayerPreview.IslandHighlightBrush : Settings.LayerPreview.IslandBrush},
            {MainIssue.IssueType.Overhang,   highlightColors ? Settings.LayerPreview.OverhangHighlightBrush : Settings.LayerPreview.OverhangBrush},
            {MainIssue.IssueType.ResinTrap,  highlightColors ? Settings.LayerPreview.ResinTrapHighlightBrush : Settings.LayerPreview.ResinTrapBrush},
            {MainIssue.IssueType.SuctionCup, highlightColors ? Settings.LayerPreview.SuctionCupHighlightBrush : Settings.LayerPreview.SuctionCupBrush},
            {MainIssue.IssueType.TouchingBound, Settings.LayerPreview.TouchingBoundsBrush},
            {MainIssue.IssueType.EmptyLayer, Brushes.Red},
            {MainIssue.IssueType.PrintHeight, Brushes.Red},
            {MainIssue.IssueType.Debug, new ImmutableSolidColorBrush(new Color(255, 15, 112, 16))},
        };
    }

    private void UpdateLayerTrackerHighlightIssues()
    {
        _issuesSliderCanvas.Children.Clear();
        if (!IsFileLoaded || SlicerFile.IssueManager.Count == 0) return;

        var tickFrequencySize = _issuesSliderCanvas.Bounds.Height * LayerSlider.TickFrequency / LayerSlider.Maximum;
        var stroke = (int)Math.Ceiling(tickFrequencySize);

        var colorDictionary = GetIssueColors(true);


        var issues = SlicerFile.IssueManager.GetIssues().OrderBy(issue => issue.Parent.Type).DistinctBy(mainIssue => mainIssue.LayerIndex);

        foreach (var issue in issues)
        {
            var color = Brushes.Red;

            if (Settings.LayerPreview.UseIssueColorOnTracker) colorDictionary.TryGetValue(issue.Parent.Type, out color);

            var yPos = tickFrequencySize * issue.LayerIndex;
            if (issue.LayerIndex == 0 && stroke > 3)
            {
                yPos += tickFrequencySize / 2;
            }
            else if (issue.LayerIndex == SlicerFile.LastLayerIndex && stroke > 3)
            {
                yPos -= tickFrequencySize / 2;
            }
            var line = new Line { StrokeThickness = stroke, Stroke = color, EndPoint = new Avalonia.Point(_issuesSliderCanvas.Width, 0) };
            _issuesSliderCanvas.Children.Add(line);
            Canvas.SetBottom(line, yPos);
        }

        /*for (int layerIndex = 0; layerIndex < SlicerFile.LayerCount; layerIndex++)
        {
            var color = Brushes.Red;

            if(Settings.LayerPreview.UseIssueColorOnTracker) colorDictionary.TryGetValue(issue.Type, out color);

            var yPos = tickFrequencySize * layerIndex;
            if (layerIndex == 0 && stroke > 3)
            {
                yPos += tickFrequencySize / 2;
            }
            else if (layerIndex == SlicerFile.LastLayerIndex && stroke > 3)
            {
                yPos -= tickFrequencySize / 2;
            }
            var line = new Line { StrokeThickness = stroke, Stroke = color, EndPoint = new Avalonia.Point(_issuesSliderCanvas.Width, 0) };
            _issuesSliderCanvas.Children.Add(line);
            Canvas.SetBottom(line, yPos);
        }*/
            
        /*var issuesCountPerLayer = GetIssuesCountPerLayer();
        if (issuesCountPerLayer is null)
        {
            return;
        }

        //var tickFrequencySize = LayerSlider.Track.Bounds.Height * LayerSlider.TickFrequency / (LayerSlider.Maximum - LayerSlider.Minimum);
        var tickFrequencySize = _issuesSliderCanvas.Bounds.Height * LayerSlider.TickFrequency / (LayerSlider.Maximum - LayerSlider.Minimum);
        var stroke = (int)Math.Ceiling(tickFrequencySize);
        foreach (var value in issuesCountPerLayer)
        {
            var yPos = tickFrequencySize * value.Key;
            if (value.Key == 0 && stroke > 3)
            {
                yPos += tickFrequencySize / 2;
            }
            else if(value.Key == SlicerFile.LastLayerIndex && stroke > 3)
            {
                yPos -= tickFrequencySize / 2;
            }
            var line = new Line{StrokeThickness = stroke, Stroke = Brushes.Red, EndPoint = new Avalonia.Point(_issuesSliderCanvas.Width, 0)};
            _issuesSliderCanvas.Children.Add(line);
            Canvas.SetBottom(line, yPos);
        }*/
    }

    public void IssuesClear(bool clearIgnored = true)
    {
        if (!IsFileLoaded) return;
        SlicerFile.IssueManager.Clear();
        if(clearIgnored) SlicerFile.IssueManager.IgnoredIssues.Clear();
    }

    public void SetResinTrapDetectionStartLayer(char which)
    {
        switch (which)
        {
            case 'N':
                ResinTrapDetectionStartLayer = SlicerFile.FirstNormalLayer.Index;
                break;
            case 'C':
                ResinTrapDetectionStartLayer = ActualLayer;
                break;
        }
    }



    public IslandDetectionConfiguration GetIslandDetectionConfiguration(bool enable)
    {
        return new()
        {
            Enabled = enable,
            EnhancedDetection = Settings.Issues.IslandEnhancedDetection,
            AllowDiagonalBonds = Settings.Issues.IslandAllowDiagonalBonds,
            BinaryThreshold = Settings.Issues.IslandBinaryThreshold,
            RequiredAreaToProcessCheck = Settings.Issues.IslandRequiredAreaToProcessCheck,
            RequiredPixelBrightnessToProcessCheck = Settings.Issues.IslandRequiredPixelBrightnessToProcessCheck,
            RequiredPixelsToSupportMultiplier = Settings.Issues.IslandRequiredPixelsToSupportMultiplier,
            RequiredPixelsToSupport = Settings.Issues.IslandRequiredPixelsToSupport,
            RequiredPixelBrightnessToSupport = Settings.Issues.IslandRequiredPixelBrightnessToSupport
        };
    }
    public IslandDetectionConfiguration GetIslandDetectionConfiguration() => GetIslandDetectionConfiguration(Settings.Issues.ComputeIslands);

    public OverhangDetectionConfiguration GetOverhangDetectionConfiguration(bool enable)
    {
        return new()
        {
            Enabled = enable,
            IndependentFromIslands = Settings.Issues.OverhangIndependentFromIslands,
            ErodeIterations = Settings.Issues.OverhangErodeIterations,
        };
    }
    public OverhangDetectionConfiguration GetOverhangDetectionConfiguration() => GetOverhangDetectionConfiguration(Settings.Issues.ComputeOverhangs);


    public ResinTrapDetectionConfiguration GetResinTrapDetectionConfiguration(bool enable)
    {
        return new()
        {
            Enabled = enable,
            StartLayerIndex = _resinTrapDetectionStartLayer,
            BinaryThreshold = Settings.Issues.ResinTrapBinaryThreshold,
            RequiredAreaToProcessCheck = Settings.Issues.ResinTrapRequiredAreaToProcessCheck,
            RequiredBlackPixelsToDrain = Settings.Issues.ResinTrapRequiredBlackPixelsToDrain,
            MaximumPixelBrightnessToDrain = Settings.Issues.ResinTrapMaximumPixelBrightnessToDrain,
            DetectSuctionCups = Settings.Issues.ComputeSuctionCups,
            RequiredAreaToConsiderSuctionCup = Settings.Issues.SuctionCupRequiredAreaToConsider,
            RequiredHeightToConsiderSuctionCup = Settings.Issues.SuctionCupRequiredHeightToConsider
        };
    }
    public ResinTrapDetectionConfiguration GetResinTrapDetectionConfiguration() => GetResinTrapDetectionConfiguration(Settings.Issues.ComputeResinTraps);

    public TouchingBoundDetectionConfiguration GetTouchingBoundsDetectionConfiguration(bool enable)
    {
        return new()
        {
            Enabled = enable,
            MinimumPixelBrightness = UserSettings.Instance.Issues.TouchingBoundMinimumPixelBrightness,
            MarginLeft = UserSettings.Instance.Issues.TouchingBoundMarginLeft,
            MarginTop = UserSettings.Instance.Issues.TouchingBoundMarginTop,
            MarginRight = UserSettings.Instance.Issues.TouchingBoundMarginRight,
            MarginBottom = UserSettings.Instance.Issues.TouchingBoundMarginBottom,
        };
    }
    public TouchingBoundDetectionConfiguration GetTouchingBoundsDetectionConfiguration() => GetTouchingBoundsDetectionConfiguration(Settings.Issues.ComputeTouchingBounds);


    public PrintHeightDetectionConfiguration GetPrintHeightDetectionConfiguration(bool enable)
    {
        return new ()
        {
            Enabled = enable,
            Offset = (float) Settings.Issues.PrintHeightOffset
        };
    }
    public PrintHeightDetectionConfiguration GetPrintHeightDetectionConfiguration() => GetPrintHeightDetectionConfiguration(Settings.Issues.ComputePrintHeight);


    #endregion
}