领胜LDS 键盘AOI检测项目
wells.liu
2020-07-11 a7d80280f67e3a498b7aa6622bbf00c15407da05
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
using Bro.Common.Factory;
using Bro.Common.Helper;
using Bro.Common.Interface;
using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
 
namespace Bro.UI.Config
{
    public partial class CtrlDeviceState : UserControl
    {
        public event Action<string, bool> OnDeviceCheckedChanged;
 
        private IDevice device = null;
        public IDevice Device
        {
            get => device;
            set
            {
                if (value != null)
                {
                    device = value;
 
                    lblName.Text = device.Name;
                    device.OnDeviceStateChanged += Device_OnDeviceStateChanged;
 
                    Device_OnDeviceStateChanged(device, device.CurrentState);
 
                    CheckDeviceRunCtrlEnabled();
 
                    chkDevice.CheckedChanged -= ChkDevice_CheckedChanged;
                    chkDevice.CheckedChanged += ChkDevice_CheckedChanged;
                }
            }
        }
 
        public bool IsChecked
        {
            get => chkDevice.Checked;
            set
            {
                chkDevice.CheckedChanged -= ChkDevice_CheckedChanged;
                chkDevice.Checked = value;
                chkDevice.CheckedChanged += ChkDevice_CheckedChanged;
            }
        }
 
        private void ChkDevice_CheckedChanged(object sender, EventArgs e)
        {
            OnDeviceCheckedChanged?.Invoke(Device.Id, chkDevice.Checked);
        }
 
        private void CheckDeviceRunCtrlEnabled()
        {
            string typeCode = Device.GetType().GetCustomAttribute<DeviceAttribute>()?.TypeCode;
 
            if (!string.IsNullOrWhiteSpace(typeCode))
            {
                chkDevice.Enabled = UIFactory.IsDeviceCtrlExisted(typeCode, EnumHelper.DeviceAttributeType.RunCtrl);
            }
            else
            {
                chkDevice.Enabled = false;
            }
 
            //if (chkDevice.Enabled)
            //{
            //    chkDevice.Checked = true;
            //}
        }
 
        private void Device_OnDeviceStateChanged(IDevice device, EnumHelper.DeviceState deviceState)
        {
            //Graphics g = plState.CreateGraphics();
            //g.Clear(SystemColors.Control);
            //g.FillEllipse(new SolidBrush(deviceState.GetEnumSelectedColor()), plState.ClientRectangle);
            plState.Refresh();
        }
 
        public CtrlDeviceState()
        {
            InitializeComponent();
        }
 
        public CtrlDeviceState(IDevice _device)
        {
            InitializeComponent();
            Device = _device;
 
            plState.Paint += PlState_Paint;
        }
 
        private void PlState_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.Clear(SystemColors.Control);
            g.FillEllipse(new SolidBrush(Device.CurrentState.GetEnumSelectedColor()), plState.ClientRectangle);
        }
    }
}