alielie
2025-10-13 e3868d4d61ded0e76fc8fe3fdacdd9e6d9113cf1
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
using GongSolutions.Wpf.DragDrop;
using HMI.UI;
using NationalInstruments.TestStand.Interop.API;
using Plugin.SeqEditor.Entity;
using System.Windows;
 
namespace Plugin.SeqEditor.ViewModel
{
    public class StepDropHandler : DefaultDropHandler
    {
        private SeqEditorViewModel model;
        public StepDropHandler(SeqEditorViewModel model)
        {
            this.model = model;
        }
 
        public bool CanConvert(IDropInfo dropInfo)
        {
            if (!(dropInfo.TargetItem is TreeItemData targetItem))
            {
                return false;
            }
 
            if (!(targetItem.Data is StepUI target))
            {
                return false;
            }
 
            if (!(dropInfo.Data is TreeItemData sourceItem))
            {
                return false;
            }
 
            if (!(sourceItem.Data is StepUI source))
            {
                return false;
            }
 
            if (targetItem.ParentItem != sourceItem.ParentItem)
            {
                return false;
            }
 
            if (source == target)
            {
                return false;
            }
 
            if (source.sequence != target.sequence)
            {
                return false;
            }
 
            return true;
        }
 
        public override void DragOver(IDropInfo dropInfo)
        {
            if (CanConvert(dropInfo))
            {
                dropInfo.Effects = DragDropEffects.Move;
                dropInfo.DropTargetHintState = DropHintState.Active;
            }
            else
            {
                dropInfo.DropTargetHintState = DropHintState.None;
            }
        }
 
        public override void Drop(IDropInfo dropInfo)
        {
            if (!CanConvert(dropInfo))
            {
                return;
            }
            var sourceItem = (TreeItemData)dropInfo.Data;
            var targetItem = (TreeItemData)dropInfo.TargetItem;
 
            var source = sourceItem.Data as StepUI;
            var target = targetItem.Data as StepUI;
            int sourceIndex = source.IndexInSequence;
            int targetIndex = target.IndexInSequence;
 
            TreeItemDataCollection collection = model.TreeCollection;
 
            List<TreeItemData> datasource;
 
            if (sourceItem.ParentItem == null)
            {
                datasource = collection.RootItems;
            }
            else
            {
                datasource = sourceItem.ParentItem.Items;
            }
 
            //ObservableCollection<TreeItemData> datasource =
            //   (ObservableCollection<TreeItemData>)dropInfo.TargetCollection.TryGetList();
 
            int uiSourceIndex = collection.IndexOf(sourceItem);
            int uiTargetIndex = collection.IndexOf(targetItem);
 
            if (uiSourceIndex == uiTargetIndex + 1)
            {
                return;
            }
 
            StepGroups group = target.StepGroup;
            if (sourceIndex == targetIndex) {
                return;
            }
 
            if (sourceIndex < targetIndex)
            {
                source.sequence.DeleteStep(sourceIndex, source.StepGroup);
                target.sequence.InsertStep(source.step, targetIndex, group);
 
                collection.Move(uiSourceIndex, uiTargetIndex);
 
                //datasource.RemoveAt(uiSourceIndex);
                //datasource.Insert(uiTargetIndex, sourceItem);
            }
            else if (sourceIndex > targetIndex)
            {
                source.sequence.DeleteStep(sourceIndex, source.StepGroup);
                target.sequence.InsertStep(source.step, targetIndex + 1, group);
 
                collection.Move(uiSourceIndex, uiTargetIndex);
            }
            model.CurrentSeqUI.IsChanged = true;
            //var task = Task.Run(() =>
            //{
            //    collection.ResetItems();
            //    model.TreeItemData = sourceItem;
            //});
            //task.Wait();
        }
    }
}