DHDHSoftware/DHSoftware/Views/DetectConfigControl.cs
2025-03-16 13:28:24 +08:00

460 lines
18 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AntdUI;
using AntdUIDemo.Views.Table;
using DH.Commons.Enums;
using DH.Devices.Vision;
using DHSoftware.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DH.Devices.Vision;
namespace DHSoftware.Views
{
public partial class DetectConfigControl : UserControl
{
private DetectionConfig _currentConfig = new DetectionConfig();
private readonly string _configName;
List<KeyValuePair<string, int>> MLModelTypes = GetFilteredEnumDescriptionsAndValues<MLModelType>();
public static List<KeyValuePair<string, int>> GetFilteredEnumDescriptionsAndValues<T>() where T : Enum
{
return Enum.GetValues(typeof(T))
.Cast<T>()
.Select(e =>
{
// 获取枚举的 Description 属性,如果没有,则使用枚举的名称
var description = e.GetType()
.GetField(e.ToString())
?.GetCustomAttribute<DescriptionAttribute>()
?.Description ?? e.ToString();
// 返回枚举的描述和对应的整数值
return new KeyValuePair<string, int>(description, Convert.ToInt32(e));
})
.ToList();
}
List<KeyValuePair<string, int>> resultStates = GetFilteredEnumDescriptionsAndValues<ResultState>();
// 获取枚举的描述和对应的值,只筛选出 OK 和 NG
public static List<KeyValuePair<string, int>> GetFilteredEnumDescriptionsAndValuesres<T>() where T : Enum
{
return Enum.GetValues(typeof(T))
.Cast<T>()
.Where(e => e.Equals(ResultState.OK) || e.Equals(ResultState.DetectNG)) // 只保留 OK 和 NG
.Select(e =>
{
// 通过反射获取 DescriptionAttribute 描述,如果没有描述,则使用枚举项名称
var description = e.GetType()
.GetField(e.ToString())
?.GetCustomAttribute<DescriptionAttribute>()
?.Description ?? e.ToString(); // 如果没有 DescriptionAttribute则使用枚举名称
// 返回描述和值的键值对
return new KeyValuePair<string, int>(description, Convert.ToInt32(e));
})
.ToList();
}
DetectionConfig Detection = new DetectionConfig();
AntList<DefectRow> antList;
public AntdUI.Window _window;
DefectRow curUser;
public DetectConfigControl()
{
InitializeComponent();
InitTableColumns();
InitData();
BindEventHandler();
foreach (var item in MLModelTypes)
{
cbxDetectType.Items.Add(item.Key);
}
cbxDetectType.SelectedIndex = (int)Detection.ModelType - 1;
tbDetectName.Text = Detection.Name;
}
public bool IsEnabled
{
get => switchEnable.AutoCheck;
set => switchEnable.AutoCheck = value;
}
// 模型路径属性
public string ModelPath
{
get => Detection.ModelPath;
set
{
Detection.ModelPath = value;
tbModelpath.Text = value; // 更新到文本框
}
}
public bool IsPicNGEnable
{
get => sthSaveNGPic.AutoCheck;
set => sthSaveNGPic.AutoCheck = value;
}
public bool IsPicEnable
{
get => sthPic.AutoCheck;
set => sthPic.AutoCheck = value;
}
//public MLModelType LModelType
//{
//}
private string _picTag;
public string picTag
{
get => _picTag;
set
{
_picTag = value;
}
}
private string _afterTag;
public string afterTag
{
get => _afterTag;
set
{
_afterTag = value;
}
}
private void InitTableColumns()
{
table_base.Columns = new ColumnCollection() {
new ColumnCheck("Selected"){Fixed = true},
new Column("LabelDescription", "标签名", ColumnAlign.Center)
{
Width="120",
//设置树节点名称需和User里的User[]名称保持一致
KeyTree = "Users"
},
new ColumnSwitch("IsEnable", "是否启用", ColumnAlign.Center){
//支持点击回调
//Call= (value,record, i_row, i_col) =>{
// //执行耗时操作
// Thread.Sleep(10);
// // AntdUI.Message.info(window, value.ToString(),autoClose:1);
// return value;
//}
},
new Column("ScoreMinValue", "最小得分",ColumnAlign.Center),
new Column("ScoreMaxValue", "最大得分",ColumnAlign.Center),
new Column("AreaMinValue", "最小面积",ColumnAlign.Center),
new Column("AreaMaxValue", "最大面积",ColumnAlign.Center),
//new Column("CellBadge", "徽标",ColumnAlign.Center),
//new Column("CellText", "富文本")
//{
// ColAlign = ColumnAlign.Center,//支持表头位置单独设置
//},
//new Column("CellProgress", "进度条",ColumnAlign.Center),
//new Column("CellDivider", "分割线",ColumnAlign.Center),
//new Column("CellLinks", "链接", ColumnAlign.Center)
//{
// Fixed = true,//冻结列
//},
};
}
private void InitData()
{
antList = new AntList<DefectRow>();
for (int i = 0; i < 10; i++)
{
antList.Add(new DefectRow
{
LabelDescription = "张三",
});
}
table_base.Binding(antList);
//设置行禁用
// table_base.SetRowEnable(0, false, true);
}
private void BindEventHandler()
{
buttonADD.Click += ButtonADD_Click;
buttonDEL.Click += ButtonDEL_Click;
table_base.CellClick += Table_base_CellClick;
table_base.CellButtonClick += Table_base_CellButtonClick;
}
private AntdUI.Table.CellStyleInfo Table_base_SetRowStyle(object sender, TableSetRowStyleEventArgs e)
{
if (e.RowIndex % 2 == 0)
{
return new AntdUI.Table.CellStyleInfo
{
BackColor = AntdUI.Style.Db.ErrorBg,
};
}
return null;
}
private void ButtonADD_Click(object sender, EventArgs e)
{
DefectRow useradd = new DefectRow()
{
LabelDescription="xinquexian",
IsEnable=true,
ScoreMinValue=0.3,
ScoreMaxValue=1,
AreaMinValue=1,
AreaMaxValue=999999999,
};
var form = new DefectRowEdit(_window, useradd) { Size = new Size(700, 500) };
AntdUI.Modal.open(new AntdUI.Modal.Config(_window, "", form, TType.None)
{
BtnHeight = 0,
});
if (form.submit)
{
antList.Add(useradd);
}
}
private void Table_base_CellClick(object sender, TableClickEventArgs e)
{
var record = e.Record;
if (record is DefectRow user)
{
curUser = user;
//判断是否右键
if (e.Button == MouseButtons.Right)
{
if (antList.Count == 0) return;
AntdUI.ContextMenuStrip.open(new AntdUI.ContextMenuStrip.Config(table_base,
(item) =>
{
if (item.Text == "开启")
{
user.IsEnable = true;
}
else if (item.Text == "关闭")
{
user.IsEnable = false;
}
else if (item.Text == "编辑")
{
var form = new DefectRowEdit(_window, user) { 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);
}
});
}
else if (item.Text == "删除")
{
var result = Modal.open(_window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
{
//父元素没有勾选或者子元素也没有勾选,则删除当前行
bool delCurrent = !antList.Any(x => x.Selected /*|| (x.?.Any(u => u.Selected) ?? false)*/);
if (delCurrent)
{
//删除当前行,先判断是否父元素,再判断是否子元素,只支持一层子元素,需实现嵌套查询
for (int i = 0; i < antList.Count; i++)
{
if (antList[i] == user)
{
antList.RemoveAt(i);
}
//else
//{
// antList[i].Users = antList[i].Users?.Where(x => x != user).ToArray();
//}
}
}
else
{
// 使用反转for循环删除主列表中选中的项
for (int i = antList.Count - 1; i >= 0; i--)
{
// 1.删除选中的主列表项
if (antList[i].Selected)
{
antList.RemoveAt(i);
}
//else
//{
// // 删除子列表中选中的项
// antList[i].Users = antList[i].Users?.Where(childUser => !childUser.Selected).ToArray();
//}
}
}
}
}
else if (item.Text == "查看图片")
{
//查看其他来源的高清图片
Preview.open(new Preview.Config(_window, Properties.Resources.head2));
}
else
AntdUI.Message.info(_window, item.Text, autoClose: 1);
},
new IContextMenuStripItem[] {
//根据行数据动态修改右键菜单
user.IsEnable? new ContextMenuStripItem("关闭")
{
IconSvg = "CloseOutlined"
}:new ContextMenuStripItem("开启")
{
IconSvg = "CheckOutlined"
},
new AntdUI.ContextMenuStripItem("编辑"){
IconSvg = "EditOutlined",
},
new AntdUI.ContextMenuStripItem("删除"){
IconSvg = "DeleteOutlined"
},
new ContextMenuStripItem("查看图片")
{
IconSvg = "FundViewOutlined"
},
new ContextMenuStripItemDivider(),
new AntdUI.ContextMenuStripItem("详情"){
Sub = new IContextMenuStripItem[]{ new AntdUI.ContextMenuStripItem("打印", "Ctrl + P") { },
new AntdUI.ContextMenuStripItem("另存为", "Ctrl + S") { } },
IconSvg = "<svg t=\"1725101601993\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"1414\" width=\"200\" height=\"200\"><path d=\"M450.23 831.7c-164.87 0-316.85-108.51-366.94-269.68-30.4-97.82-20.9-201.62 26.76-292.29s127.79-157.35 225.6-187.75c97.83-30.42 201.61-20.9 292.29 26.76 90.67 47.67 157.35 127.79 187.75 225.61 35.78 115.12 16.24 237.58-53.6 335.99a383.494 383.494 0 0 1-43 50.66c-15.04 14.89-39.34 14.78-54.23-0.29-14.9-15.05-14.77-39.34 0.29-54.23a307.844 307.844 0 0 0 34.39-40.52c55.9-78.76 71.54-176.75 42.92-268.84-50.21-161.54-222.49-252.1-384.03-201.9-78.26 24.32-142.35 77.67-180.48 150.2-38.14 72.53-45.74 155.57-21.42 233.83 44.58 143.44 190.03 234.7 338.26 212.42 20.98-3.14 40.48 11.26 43.64 32.2 3.16 20.95-11.26 40.48-32.2 43.64a377.753 377.753 0 0 1-56 4.19z\" p-id=\"1415\"></path><path d=\"M919.84 959.5c-9.81 0-19.63-3.74-27.11-11.24L666.75 722.29c-14.98-14.97-14.98-39.25 0-54.23 14.97-14.98 39.26-14.98 54.23 0l225.97 225.97c14.98 14.97 14.98 39.25 0 54.23-7.48 7.5-17.3 11.24-27.11 11.24z\" p-id=\"1416\"></path></svg>",
}
}));
}
else
{
}
}
}
//表格内部按钮事件
private void Table_base_CellButtonClick(object sender, TableButtonEventArgs e)
{
var buttontext = e.Btn.Text;
if (e.Record is DefectRow user)
{
curUser = user;
switch (buttontext)
{
//暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
case "编辑":
var form = new DefectRowEdit(_window, user) { 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)
antList.Remove(user);
break;
case "AntdUI":
//超链接内容
// AntdUI.Message.info(_window, user.CellLinks.FirstOrDefault().Id, autoClose: 1);
break;
case "查看图片":
//使用clone可以防止table中的image被修改
// Preview.open(new Preview.Config(window, (Image)curUser.CellImages[0].Image.Clone()));
break;
}
}
}
private void ButtonDEL_Click(object sender, EventArgs e)
{
if (antList.Count == 0 || !antList.Any(x => x.Selected))
{
bool isSubSelected = false;
//// 判断子元素是否勾选
//for (int i = 0; i < antList.Count; i++)
//{
// if (antList[i].Users != null && antList[i].Users.Any(x => x.Selected))
// {
// isSubSelected = true;
// break;
// }
//}
if (!isSubSelected)
{
AntdUI.Message.warn(_window, "请选择要删除的行!", autoClose: 3);
return;
}
}
var result = Modal.open(_window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
{
// 使用反转for循环删除主列表中选中的项
for (int i = antList.Count - 1; i >= 0; i--)
{
// 删除选中的主列表项
if (antList[i].Selected)
{
antList.RemoveAt(i);
}
else
{
// 删除子列表中选中的项
// antList[i].Users = antList[i].Users?.Where(user => !user.Selected).ToArray();
}
}
// 提示删除完成
// AntdUI.Message.success(this.w, "删除成功!", autoClose: 3);
MessageBox.Show("删除成功!");
}
}
}
}