using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; using AntdUI; using DH.Commons.Enums; using DH.Devices.Vision; using XKRS.CanFly; using static AntdUI.Table; using static DH.Commons.Enums.EnumHelper; namespace DHSoftware.Views { public partial class DetectControl : UserControl { Window window; public DetectControl(Window _window) { window = _window; InitializeComponent(); //初始化表格列头 InitTableColumns(); InitData(); BindEventHandler(); } private void BindEventHandler() { //预处理 btnPath.Click += btnPath_Click; btnPreAdd.Click += BtnPreAdd_Click; ; btnPreDelete.Click += BtnPreDelete_Click; ; preTable.CellClick += PreTable_CellClick; ; preTable.CellButtonClick += PreTable_CellButtonClick; btnOutAdd.Click += BtnOutAdd_Click; ; btnOutDelete.Click += BtnOutDelete_Click; ; PreOutTable.CellButtonClick += PreOutTable_CellButtonClick; ; //中处理 btnPath2.Click += BtnPath2_Click; btnCorrelatedCamera.Click += BtnCorrelatedCamera_Click; btnPic.Click += BtnPic_Click; btnLableAdd.Click += BtnLableAdd_Click; btnLableDelete.Click += BtnLableDelete_Click; lableTable.CellButtonClick += LableTable_CellButtonClick; btnSizeAdd.Click += BtnSizeAdd_Click; btnSizeDel.Click += BtnSizeDelete_Click; SizeTable.CellButtonClick += SizeTable_CellButtonClick; } private void LableTable_CellButtonClick(object sender, TableButtonEventArgs e) { var buttontext = e.Btn.Text; if (e.Record is DetectionLable DetectionLable) { curDetectionLable = DetectionLable; switch (buttontext) { //暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据 case "编辑": var form = new DetectionLableEdit(window, curDetectionLable) { Size = new Size(500, 300) }; AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form) { OnLoad = () => { AntdUI.Message.info(window, "进入编辑", autoClose: 1); }, OnClose = () => { AntdUI.Message.info(window, "结束编辑", autoClose: 1); } }); break; case "删除": var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn); if (result == DialogResult.OK) DetectionLableList.Remove(DetectionLable); break; } } } private void SizeTable_CellButtonClick(object sender, TableButtonEventArgs e) { var buttontext = e.Btn.Text; if (e.Record is SizeTreatParam sizeTreat) { SizeParamLable = sizeTreat; switch (buttontext) { //暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据 case "编辑": var form = new SizeLabelEdit(window, SizeParamLable) { Size = new Size(500, 300) }; AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form) { OnLoad = () => { AntdUI.Message.info(window, "进入编辑", autoClose: 1); }, OnClose = () => { AntdUI.Message.info(window, "结束编辑", autoClose: 1); } }); break; case "删除": var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn); if (result == DialogResult.OK) SizeLableList.Remove(sizeTreat); break; case "进行测量": var sizeType = ((int)SizeParamLable.PreType).ToString(); // 根据测量类型打开不同的窗口 switch (sizeType) { case "1": case "2": case "3": case "4": case "5": FrmMain3 frmMain3 = new FrmMain3(sizeType); frmMain3.ShowDialog(); if (!string.IsNullOrEmpty(frmMain3.inputtext)) { sizeTreat.ResultShow = frmMain3.inputtext; } if (!string.IsNullOrEmpty(frmMain3.outtext)) { sizeTreat.OutResultShow = frmMain3.outtext; } break; default: MessageBox.Show("未定义的测量类型!"); break; } //使用clone可以防止table中的image被修改 //Preview.open(new Preview.Config(window, (Image)SizeParamLable.CellImages[0].Image.Clone())); break; } } } private void BtnLableDelete_Click(object? sender, EventArgs e) { if (DetectionLableList.Count == 0 || !DetectionLableList.Any(x => x.Selected)) { AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3); return; } var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn); if (result == DialogResult.OK) { // 使用反转for循环删除主列表中选中的项 for (int i = DetectionLableList.Count - 1; i >= 0; i--) { // 删除选中的主列表项 if (DetectionLableList[i].Selected) { DetectionLableList.RemoveAt(i); } } // 提示删除完成 AntdUI.Message.success(window, "删除成功!", autoClose: 3); } } private void BtnLableAdd_Click(object? sender, EventArgs e) { DetectionLable detectionLable = new DetectionLable() { CellLinks = new CellLink[] { new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Primary), new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error), } }; var form = new DetectionLableEdit(window, detectionLable) { Size = new Size(450, 500) }; AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None) { BtnHeight = 0, }); if (form.submit) { DetectionLableList.Add(detectionLable); } } private void BtnSizeDelete_Click(object? sender, EventArgs e) { if (SizeLableList.Count == 0 || !SizeLableList.Any(x => x.Selected)) { AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3); return; } var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn); if (result == DialogResult.OK) { // 使用反转for循环删除主列表中选中的项 for (int i = SizeLableList.Count - 1; i >= 0; i--) { // 删除选中的主列表项 if (SizeLableList[i].Selected) { SizeLableList.RemoveAt(i); } } // 提示删除完成 AntdUI.Message.success(window, "删除成功!", autoClose: 3); } } private void BtnSizeAdd_Click(object? sender, EventArgs e) { SizeTreatParam SizeParamLable = new SizeTreatParam() { //CellBadge = new CellBadge(SizeEnum.Circle.GetEnumDescription()), CellLinks = new CellLink[] { new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Primary), new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error), new CellButton(Guid.NewGuid().ToString(),"进行测量",TTypeMini.Primary) } }; var form = new SizeLabelEdit(window, SizeParamLable) { Size = new Size(450, 500) }; AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None) { BtnHeight = 0, }); if (form.submit) { SizeLableList.Add(SizeParamLable); } } private void BtnPic_Click(object? sender, EventArgs e) { } List relatedCameras = new List(); private void BtnCorrelatedCamera_Click(object? sender, EventArgs e) { var form = new CorrelatedCameraEdit(window, relatedCameras) { Size = new Size(500, 400) }; AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None) { BtnHeight = 0, }); if (form.submit) { flowPanel1.Controls.Clear(); if (relatedCameras.Count > 0) { foreach (var item in relatedCameras) { var control = new AntdUI.Tag() { Font = new System.Drawing.Font("Microsoft YaHei UI", 9F), Size = new Size(90, 42), Text = item.CameraSourceId, CloseIcon = true }; control.CloseChanged += (sender, e) => { var tag = sender as Tag; foreach (var item in relatedCameras) { if (item.CameraSourceId.Equals(tag.Text)) { relatedCameras.Remove(item); break; } } return true; }; // 通过主窗口设置DPI控制添加控件保持缩放比例 window.AutoDpi(control); flowPanel1.Controls.Add(control); control.BringToFront(); } } } } private bool Control_CloseChanged(object sender, EventArgs e) { throw new NotImplementedException(); } private void BtnPath2_Click(object? sender, EventArgs e) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { // 设置对话框标题 openFileDialog.Title = "选择中处理文件"; // 限制文件后缀为 .onnx、trt openFileDialog.Filter = "中处理文件 (*.onnx, *.trt)|*.onnx;*.trt"; // 禁止多选 openFileDialog.Multiselect = false; // 显示对话框并等待用户操作 if (openFileDialog.ShowDialog() == DialogResult.OK) { string filePath = openFileDialog.FileName; iptPath2.Text = filePath; } } } private void PreOutTable_CellButtonClick(object sender, TableButtonEventArgs e) { var buttontext = e.Btn.Text; if (e.Record is PreTreatParam PreTreat) { curPreOutTreat = PreTreat; switch (buttontext) { //暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据 case "编辑": var form = new PreTreatEdit(window, curPreOutTreat) { Size = new Size(500, 300) }; AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form) { OnLoad = () => { AntdUI.Message.info(window, "进入编辑", autoClose: 1); }, OnClose = () => { AntdUI.Message.info(window, "结束编辑", autoClose: 1); } }); break; case "删除": var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn); if (result == DialogResult.OK) PreOutTreatList.Remove(PreTreat); break; } } } private void BtnOutDelete_Click(object? sender, EventArgs e) { if (PreOutTreatList.Count == 0 || !PreOutTreatList.Any(x => x.Selected)) { AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3); return; } var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn); if (result == DialogResult.OK) { // 使用反转for循环删除主列表中选中的项 for (int i = PreOutTreatList.Count - 1; i >= 0; i--) { // 删除选中的主列表项 if (PreOutTreatList[i].Selected) { PreOutTreatList.RemoveAt(i); } } // 提示删除完成 AntdUI.Message.success(window, "删除成功!", autoClose: 3); } } private void BtnOutAdd_Click(object? sender, EventArgs e) { PreTreatParam preParam = new PreTreatParam() { CellLinks = new CellLink[] { new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Primary), new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error), } }; var form = new PreTreatEdit(window, preParam) { Size = new Size(400, 300) }; AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None) { BtnHeight = 0, }); if (form.submit) { PreOutTreatList.Add(preParam); } } private void PreTable_CellButtonClick(object sender, TableButtonEventArgs e) { var buttontext = e.Btn.Text; if (e.Record is PreTreatParam PreTreat) { curPreTreat = PreTreat; switch (buttontext) { //暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据 case "编辑": var form = new PreTreatEdit(window, curPreTreat) { Size = new Size(500, 300) }; AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form) { OnLoad = () => { AntdUI.Message.info(window, "进入编辑", autoClose: 1); }, OnClose = () => { AntdUI.Message.info(window, "结束编辑", autoClose: 1); } }); break; case "删除": var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn); if (result == DialogResult.OK) PreTreatList.Remove(PreTreat); break; } } } private void PreTable_CellClick(object sender, TableClickEventArgs e) { } private void BtnPreDelete_Click(object? sender, EventArgs e) { if (PreTreatList.Count == 0 || !PreTreatList.Any(x => x.Selected)) { AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3); return; } var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn); if (result == DialogResult.OK) { // 使用反转for循环删除主列表中选中的项 for (int i = PreTreatList.Count - 1; i >= 0; i--) { // 删除选中的主列表项 if (PreTreatList[i].Selected) { PreTreatList.RemoveAt(i); } } // 提示删除完成 AntdUI.Message.success(window, "删除成功!", autoClose: 3); } } private void BtnPreAdd_Click(object? sender, EventArgs e) { PreTreatParam preParam = new PreTreatParam() { CellLinks = new CellLink[] { new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Primary), new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error), } }; var form = new PreTreatEdit(window, preParam) { Size = new Size(400, 300) }; AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None) { BtnHeight = 0, }); if (form.submit) { PreTreatList.Add(preParam); } } AntList PreTreatList; AntList PreOutTreatList; AntList DetectionLableList; AntList SizeLableList; PreTreatParam curPreTreat; PreTreatParam curPreOutTreat; DetectionLable curDetectionLable; SizeTreatParam SizeParamLable; private void InitData() { PreTreatList = new AntList(); preTable.Binding(PreTreatList); PreOutTreatList = new AntList(); PreOutTable.Binding(PreOutTreatList); foreach (var item in MLModelTypes) { stDetectType.Items.Add(item.Key); } DetectionLableList = new AntList(); lableTable.Binding(DetectionLableList); SizeLableList = new AntList(); SizeTable.Binding(SizeLableList); } private void InitTableColumns() { preTable.Columns = new ColumnCollection() { new ColumnCheck("Selected"){Fixed = true}, new Column("Name", "名称", ColumnAlign.Center), new Column("Value", "值",ColumnAlign.Center), new Column("CellLinks", "操作", ColumnAlign.Center) }; PreOutTable.Columns = new ColumnCollection() { new ColumnCheck("Selected"){Fixed = true}, new Column("Name", "名称", ColumnAlign.Center), new Column("Value", "值",ColumnAlign.Center), new Column("CellLinks", "操作", ColumnAlign.Center) }; lableTable.Columns = new ColumnCollection() { new ColumnCheck("Selected"){Fixed = true}, new Column("LabelName", "名称", ColumnAlign.Center), new Column("MinSource", "最小得分",ColumnAlign.Center), new Column("MaxArea", "最大得分", ColumnAlign.Center), new Column("MinSource", "最小面积",ColumnAlign.Center), new Column("MaxArea", "最大面积", ColumnAlign.Center), new Column("ResultState", "结果", ColumnAlign.Center), new Column("CellLinks", "操作", ColumnAlign.Center) }; SizeTable.Columns = new ColumnCollection() { new ColumnCheck("Selected"){Fixed = true}, new ColumnSwitch("IsEnable", "是否启用", ColumnAlign.Center), new Column("PreName", "测量名称",ColumnAlign.Center), new Column("PreType", "测量类型", ColumnAlign.Center), new Column("PrePix", "阈值", ColumnAlign.Center), new Column("ResultShow", "输入参数", ColumnAlign.Center), new Column("OutResultShow", "输出参数", ColumnAlign.Center), new Column("CellLinks", "操作", ColumnAlign.Center) }; } private void btnPath_Click(object? sender, EventArgs e) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { // 设置对话框标题 openFileDialog.Title = "选择预处理文件"; // 限制文件后缀为 .hdvp openFileDialog.Filter = "中处理 (*.hdvp)|*.hdvp"; // 禁止多选 openFileDialog.Multiselect = false; // 显示对话框并等待用户操作 if (openFileDialog.ShowDialog() == DialogResult.OK) { string filePath = openFileDialog.FileName; iptPath.Text = filePath; } } } List> MLModelTypes = GetFilteredEnumDescriptionsAndValues(); List> SizeEnum = GetFilteredEnumDescriptionsAndValues(); private void tabPage2_Click(object sender, EventArgs e) { } public static List> GetFilteredEnumDescriptionsAndValues() where T : Enum { return Enum.GetValues(typeof(T)) .Cast() .Select(e => { // 获取枚举的 Description 属性,如果没有,则使用枚举的名称 var description = e.GetType() .GetField(e.ToString()) ?.GetCustomAttribute() ?.Description ?? e.ToString(); // 返回枚举的描述和对应的整数值 return new KeyValuePair(description, Convert.ToInt32(e)); }) .ToList(); } } }