chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
using CommunityToolkit.Mvvm.ComponentModel;
using OpenTap;
using OpenTapEditor.Provider;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
 
namespace OpenTapEditor
{
    /// <summary>
    /// SettingWin.xaml 的交互逻辑
    /// </summary>
    [ObservableObject]
    public partial class SettingWin : Window
    {
 
        private IEnumerable<TypeData> settings;
 
        private DutSettings dutSettings;
        private InstrumentSettings instSettings;
        private IEnumerable<ITypeData> dutComponents;
        private IEnumerable<ITypeData> instComponents;
 
        [ObservableProperty]
        public ObservableCollection<IDut> duts;
 
        [ObservableProperty]
        public ObservableCollection<IInstrument> insts;
 
 
 
        public SettingWin()
        {
            DataContext = this;
            InitializeComponent();
            LoadDUTs();
        }
 
        public static Type GetEnumerableElementType(Type enumType)
        {
            if (enumType.IsArray)
                return enumType.GetElementType();
 
            var ienumInterface = enumType.GetInterface("IEnumerable`1") ?? enumType;
            if (ienumInterface != null)
                return ienumInterface.GetGenericArguments().FirstOrDefault();
 
            return typeof(object);
        }
 
 
        private void LoadDUTs()
        {
            try
            {
                var settings = TypeData.GetDerivedTypes<ComponentSettings>()
                         .Where(x => x.CanCreateInstance && (x.GetAttribute<BrowsableAttribute>()?.Browsable ?? true));
                this.settings = settings.OfType<TypeData>();
                foreach (var setting in this.settings)
                {
                    ComponentSettings obj = null;
                    try
                    {
                        obj = ComponentSettings.GetCurrent(setting.Load());
                        if (obj == null) continue;
                        if (obj is DutSettings ds)
                        {
                            dutSettings = ds;
                            Duts = new ObservableCollection<IDut>(ds);
                        }
                        else if (obj is InstrumentSettings ist)
                        {
                            instSettings = ist;
                            Insts = new ObservableCollection<IInstrument>(ist);
                        }
                        var setgroup = setting.GetAttribute<SettingsGroupAttribute>()?.GroupName ?? "Settings";
                        var name = setting.GetDisplayAttribute().Name;
                    }
                    catch (Exception ex)
                    {
                        //Log.Error(ex);
                        //ComponentSettings.SaveAllCurrentSettings();
                    }
                }
                ITypeData parentType = null;
                TypeData AsTypeData(ITypeData typedata)
                {
                    do
                    {
                        if (typedata is TypeData td)
                            return td;
                        typedata = typedata?.BaseType;
                    } while (typedata != null);
                    return null;
                }
 
                dutComponents = TypeData.GetDerivedTypes(TypeData.FromType(GetEnumerableElementType(typeof(DutSettings))))
                        .Where(x => x.CanCreateInstance)
                        .Where(x => x.GetAttribute<BrowsableAttribute>()?.Browsable ?? true)
                        .Where(x => parentType == null ? true : TestStepList.AllowChild(AsTypeData(parentType).Type, AsTypeData(x)?.Type));
 
                instComponents = TypeData.GetDerivedTypes(TypeData.FromType(GetEnumerableElementType(typeof(InstrumentSettings))))
                        .Where(x => x.CanCreateInstance)
                        .Where(x => x.GetAttribute<BrowsableAttribute>()?.Browsable ?? true)
                        .Where(x => parentType == null ? true : TestStepList.AllowChild(AsTypeData(parentType).Type, AsTypeData(x)?.Type));
 
                // ComponentSettings.SaveAllCurrentSettings();
 
            }
            catch (Exception ex)
            {
 
            }
        }
 
        private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems == null || e.AddedItems.Count <= 0) return;
            if (sender == dutsView)
            {
                var obj = (Dut)e.AddedItems[0];
                AnnotationCollection annotation = AnnotationCollection.Annotate(obj);
                StackPanel stack = DutControlProvider.GetControl(annotation);
                dutSettingPanel.Child = stack;
            }
            else {
                var obj = (Instrument)e.AddedItems[0];
                AnnotationCollection annotation = AnnotationCollection.Annotate(obj);
                StackPanel stack = DutControlProvider.GetControl(annotation);
                instSettingPanel.Child = stack;
            }
        }
 
        private void Window_Closing(object sender, CancelEventArgs e)
        {
            ComponentSettings.SaveAllCurrentSettings();
        }
    }
}