using System.Data;
using AntdUI;
using DH.Commons.Helper;
using DH.Commons.Models;
using DHSoftware.Utils;
using DH.Commons.Base;
namespace DHSoftware.Views
{
public partial class SettingWindow : Window
{
private UserControl currControl;
public SettingWindow()
{
InitializeComponent();
BindEventHandler();
InitData();
}
///
/// 窗体对象实例
///
private static SettingWindow _instance;
internal static SettingWindow Instance
{
get
{
if (_instance == null || _instance.IsDisposed)
_instance = new SettingWindow();
return _instance;
}
}
private void BindEventHandler()
{
Resize += SettingWindow1_Resize;
btnAdd.SelectedValueChanged += btnAdd_SelectedValueChanged;
btnSave.Click += BtnSave_Click;
menu1.SelectChanged += Menu1_SelectChanged;
}
private void Menu1_SelectChanged(object sender, MenuSelectEventArgs e)
{
MenuItem clickedItem = e.Value;
if (clickedItem != null)
{
if (clickedItem.PARENTITEM == null)
{
return;
}
switch (clickedItem.PARENTITEM.Text)
{
case "相机设置":
foreach (var tab in tabs1.Pages)
{
if (tab is AntdUI.TabPage existingTab && existingTab.Text == $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}")
{
isUpdatingTabs = true;
tabs1.SelectedTab = existingTab; // 直接跳转到已存在的 TabPage
isUpdatingTabs = false;
currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
return;
}
}
//先获取是否存在该名称的配置
//如果没有新建项
CameraBase? CameraBase = ConfigModel.CameraBaseList.Where(c => c.CameraName == clickedItem.Text).FirstOrDefault();
if (CameraBase == null)
{
CameraBase = new CameraBase();
}
UserControl control = null;
control = new CameraControl(this, CameraBase);
if (control != null)
{
//容器添加控件,需要调整dpi
control.Dock = DockStyle.Fill;
AutoDpi(control);
AntdUI.TabPage tabPage = new AntdUI.TabPage()
{
Text = $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}",
ReadOnly = false,
};
tabPage.Controls.Add(control);
tabs1.Pages.Add(tabPage);
isUpdatingTabs = true;
tabs1.SelectedTab = tabPage;
isUpdatingTabs = false;
currControl = control;
}
break;
case "工位设置":
// 检查是否已存在同名 TabPage
foreach (var tab in tabs1.Pages)
{
if (tab is AntdUI.TabPage existingTab && existingTab.Text == $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}")
{
isUpdatingTabs = true;
tabs1.SelectedTab = existingTab; // 直接跳转到已存在的 TabPage
isUpdatingTabs = false;
currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
return;
}
}
DetectionConfig? detectionConfig = ConfigModel.DetectionList.Where(c => c.Name == clickedItem.Text).FirstOrDefault();
if (detectionConfig == null)
{
detectionConfig = new DetectionConfig();
}
UserControl control1 = null;
control1 = new DetectControl(this, detectionConfig);
if (control1 != null)
{
//容器添加控件,需要调整dpi
control1.Dock = DockStyle.Fill;
AutoDpi(control1);
AntdUI.TabPage tabPage = new AntdUI.TabPage()
{
Text = $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}",
ReadOnly = false,
};
tabPage.Controls.Add(control1);
tabs1.Pages.Add(tabPage);
isUpdatingTabs = true;
tabs1.SelectedTab = tabPage;
isUpdatingTabs = false;
currControl = control1;
}
break;
case "运控设置":
foreach (var tab in tabs1.Pages)
{
if (tab is AntdUI.TabPage existingTab && existingTab.Text == $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}")
{
isUpdatingTabs = true;
tabs1.SelectedTab = existingTab; // 直接跳转到已存在的 TabPage
isUpdatingTabs = false;
currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
return;
}
}
//先获取是否存在该名称的配置
//如果没有新建项
PLCBase? pLCBase = ConfigModel.PLCBaseList.Where(c => c.PLCName == clickedItem.Text).FirstOrDefault();
if (pLCBase == null)
{
pLCBase = new PLCBase();
}
UserControl control2 = null;
control = new MotionControl(this, pLCBase);
if (control != null)
{
//容器添加控件,需要调整dpi
control.Dock = DockStyle.Fill;
AutoDpi(control);
AntdUI.TabPage tabPage = new AntdUI.TabPage()
{
Text = $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}",
ReadOnly = false,
};
tabPage.Controls.Add(control);
tabs1.Pages.Add(tabPage);
isUpdatingTabs = true;
tabs1.SelectedTab = tabPage;
isUpdatingTabs = false;
currControl = control;
}
break;
}
}
}
private float x; //定义当前窗体的宽度
private float y; //定义当前窗体的高度
private void InitData()
{
btnAdd.Items.Clear();
btnAdd.Items.Add("相机设置");
btnAdd.Items.Add("工位设置");
btnAdd.Items.Add("运控设置");
x = Width;
y = Height;
AdaptiveHelper.setTag(this);
if (ConfigModel.CameraBaseList.Count > 0)
{
var workstationItem = FindMenuItem(menu1.Items, "相机设置");
if (workstationItem != null)
{
foreach (var item in ConfigModel.CameraBaseList)
{
var newItem = new MenuItem();
newItem.Text = item.CameraName;
newItem.IconSvg = "VideoCameraOutlined";
workstationItem.Sub.Add(newItem);
}
}
}
if (ConfigModel.DetectionList.Count > 0)
{
var workstationItem = FindMenuItem(menu1.Items, "工位设置");
if (workstationItem != null)
{
foreach (var item in ConfigModel.DetectionList)
{
var newItem = new MenuItem();
newItem.Text = item.Name;
newItem.IconSvg = "AppstoreOutlined";
workstationItem.Sub.Add(newItem);
}
}
}
if (ConfigModel.PLCBaseList.Count > 0)
{
var workstationItem = FindMenuItem(menu1.Items, "运控设置");
if (workstationItem != null)
{
foreach (var item in ConfigModel.PLCBaseList)
{
var newItem = new MenuItem();
newItem.Text = item.PLCName;
newItem.IconSvg = "ControlOutlined";
workstationItem.Sub.Add(newItem);
}
}
}
}
private void BtnSave_Click(object? sender, EventArgs e)
{
ConfigHelper.SaveConfig();
AntdUI.Message.success(this, "保存成功!", autoClose: 3);
}
private void SettingWindow1_Resize(object? sender, EventArgs e)
{
var newx = Width / x;
var newy = Height / y;
AdaptiveHelper.setControls(newx, newy, this);
}
// 递归查找菜单项
private MenuItem FindMenuItem(MenuItemCollection items, string targetText)
{
foreach (MenuItem item in items)
{
if (item.Text == targetText) return item;
if (item.Sub != null)
{
var subResult = FindMenuItem(item.Sub, targetText);
if (subResult != null) return subResult;
}
}
return null;
}
private bool isUpdatingTabs = false;
private void menu1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
// 转换坐标到控件内部坐标系(考虑滚动条)
Point clickPoint = new Point(e.X, e.Y + menu1.ScrollBar.Value);
// 递归查找命中的菜单项
MenuItem clickedItem = FindClickedItem(menu1.Items, clickPoint);
if (clickedItem != null)
{
if (clickedItem.PARENTITEM == null)
{
return;
}
switch (clickedItem.PARENTITEM.Text)
{
case "相机设置":
var menulist = new AntdUI.IContextMenuStripItem[]
{
new AntdUI.ContextMenuStripItem("重命名", "")
{
IconSvg = "RetweetOutlined"
},
new AntdUI.ContextMenuStripItem("删除相机", "")
{
IconSvg = "DeleteOutlined"
}
};
AntdUI.ContextMenuStrip.open(menu1, it =>
{
switch (it.Text)
{
case "删除相机":
menu1.Remove(clickedItem);
foreach (var tab in tabs1.Pages)
{
if (tab is AntdUI.TabPage existingTab && existingTab.Text == $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}")
{
isUpdatingTabs = true;
tabs1.Pages.Remove(tab);
ConfigModel.CameraBaseList.RemoveAll(c => c.CameraName == clickedItem.Text);
isUpdatingTabs = false;
currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
return;
}
}
ConfigModel.CameraBaseList.RemoveAll(c => c.CameraName == clickedItem.Text);
break;
case "重命名":
// 保存原始名称用于查找
string originalName = clickedItem.Text;
var form = new AddCameraControl(this, "重命名相机操作")
{
Size = new Size(300, 200),
// 传递当前名称到表单
CubicleName = originalName
};
AntdUI.Modal.open(new AntdUI.Modal.Config(this, "", form, TType.None)
{
BtnHeight = 0,
});
if (form.submit && !string.IsNullOrWhiteSpace(form.CubicleName))
{
string newName = form.CubicleName.Trim();
if (ConfigModel.CameraBaseList.Any(c =>
c.CameraName.Equals(newName, StringComparison.OrdinalIgnoreCase) &&
c.CameraName != originalName))
{
AntdUI.Message.warn(this, $"相机名称 '{newName}' 已存在!", autoClose: 3);
return;
}
clickedItem.Text = newName;
var camera = ConfigModel.CameraBaseList.FirstOrDefault(c =>
c.CameraName.Equals(originalName, StringComparison.OrdinalIgnoreCase));
camera.CameraName = newName;
foreach (var tab in tabs1.Pages)
{
if (tab is AntdUI.TabPage existingTab && existingTab.Text == $"{clickedItem.PARENTITEM.Text}-{originalName}")
{
isUpdatingTabs = true;
existingTab.Text = $"{clickedItem.PARENTITEM.Text}-{newName}";
isUpdatingTabs = false;
currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
return;
}
}
AntdUI.Message.success(this, $"已将 '{originalName}重命名成{newName}'!", autoClose: 3);
}
break;
}
}, menulist);
break;
case "工位设置":
var menulist1 = new AntdUI.IContextMenuStripItem[]
{ new AntdUI.ContextMenuStripItem("重命名", "")
{
IconSvg = "RetweetOutlined"
},
new AntdUI.ContextMenuStripItem("删除工位", "")
{
IconSvg = "DeleteOutlined"
}
};
AntdUI.ContextMenuStrip.open(menu1, it =>
{
switch (it.Text)
{
case "删除工位":
menu1.Remove(clickedItem);
foreach (var tab in tabs1.Pages)
{
if (tab is AntdUI.TabPage existingTab && existingTab.Text == $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}")
{
isUpdatingTabs = true;
tabs1.Pages.Remove(tab);
isUpdatingTabs = false;
currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
return;
}
}
ConfigModel.DetectionList.RemoveAll(c => c.Name == clickedItem.Text);
break;
case "重命名":
// 保存原始名称用于查找
string originalName = clickedItem.Text;
var form = new AddCubicleControl(this, "重命名工位操作")
{
Size = new Size(300, 200),
// 传递当前名称到表单
CubicleName = originalName
};
AntdUI.Modal.open(new AntdUI.Modal.Config(this, "", form, TType.None)
{
BtnHeight = 0,
});
if (form.submit && !string.IsNullOrWhiteSpace(form.CubicleName))
{
string newName = form.CubicleName.Trim();
if (ConfigModel.DetectionList.Any(c =>
c.Name.Equals(newName, StringComparison.OrdinalIgnoreCase) &&
c.Name != originalName))
{
AntdUI.Message.warn(this, $"工位名称 '{newName}' 已存在!", autoClose: 3);
return;
}
clickedItem.Text = newName;
var detection = ConfigModel.DetectionList.FirstOrDefault(c =>
c.Name.Equals(originalName, StringComparison.OrdinalIgnoreCase));
detection.Name = newName;
foreach (var tab in tabs1.Pages)
{
if (tab is AntdUI.TabPage existingTab && existingTab.Text == $"{clickedItem.PARENTITEM.Text}-{originalName}")
{
isUpdatingTabs = true;
existingTab.Text = $"{clickedItem.PARENTITEM.Text}-{newName}";
isUpdatingTabs = false;
currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
return;
}
}
AntdUI.Message.success(this, $"已将 '{originalName}重命名成{newName}'!", autoClose: 3);
}
break;
}
}, menulist1);
break;
case "运控设置":
var menulist2 = new AntdUI.IContextMenuStripItem[]
{
new AntdUI.ContextMenuStripItem("重命名", "")
{
IconSvg = "RetweetOutlined"
},
new AntdUI.ContextMenuStripItem("删除运控", "")
{
IconSvg = "DeleteOutlined"
}
};
AntdUI.ContextMenuStrip.open(menu1, it =>
{
switch (it.Text)
{
case "删除运控":
menu1.Remove(clickedItem);
foreach (var tab in tabs1.Pages)
{
if (tab is AntdUI.TabPage existingTab && existingTab.Text == $"{clickedItem.PARENTITEM.Text}-{clickedItem.Text}")
{
isUpdatingTabs = true;
//tabs1.SelectedTab = existingTab; // 直接跳转到已存在的 TabPage
tabs1.Pages.Remove(tab);
ConfigModel.PLCBaseList.RemoveAll(c => c.PLCName == clickedItem.Text);
isUpdatingTabs = false;
currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
return;
}
}
ConfigModel.PLCBaseList.RemoveAll(c => c.PLCName == clickedItem.Text);
break;
case "重命名":
var form = new AddMotionControl(this, "重命名运动控制操作") { Size = new Size(300, 200) };
AntdUI.Modal.open(new AntdUI.Modal.Config(this, "", form, TType.None)
{
BtnHeight = 0,
});
if (form.submit)
{
string originalName = clickedItem.Text;
if (form.submit && !string.IsNullOrWhiteSpace(form.MotionName))
{
string newName = form.MotionName.Trim();
if (ConfigModel.PLCBaseList.Any(c =>
c.PLCName.Equals(newName, StringComparison.OrdinalIgnoreCase) &&
c.PLCName != originalName))
{
AntdUI.Message.warn(this, $"运动控制名称 '{newName}' 已存在!", autoClose: 3);
return;
}
clickedItem.Text = newName;
var plc = ConfigModel.PLCBaseList.FirstOrDefault(c =>
c.PLCName.Equals(originalName, StringComparison.OrdinalIgnoreCase));
plc.PLCName = newName;
foreach (var tab in tabs1.Pages)
{
if (tab is AntdUI.TabPage existingTab && existingTab.Text == $"{clickedItem.PARENTITEM.Text}-{originalName}")
{
isUpdatingTabs = true;
existingTab.Text = $"{clickedItem.PARENTITEM.Text}-{newName}";
isUpdatingTabs = false;
currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
return;
}
}
AntdUI.Message.success(this, $"已将 '{originalName}重命名成{newName}'!", autoClose: 3);
}
}
break;
}
}, menulist2);
break;
}
}
}
}
private MenuItem FindClickedItem(MenuItemCollection items, Point clickPoint)
{
foreach (MenuItem item in items)
{
// 检查当前项是否可见且包含点击坐标
if (item.Visible && item.Rect.Contains(clickPoint))
{
return item;
}
// 递归检查子项(如果展开)
if (item.Expand && item.Sub != null)
{
var childResult = FindClickedItem(item.Sub, clickPoint);
if (childResult != null) return childResult;
}
}
return null;
}
private void btnAdd_SelectedValueChanged(object sender, ObjectNEventArgs e)
{
string selectedValue = e.Value.ToString();
switch (selectedValue)
{
case "相机设置":
var workstationItem = FindMenuItem(menu1.Items, "相机设置");
if (workstationItem != null)
{
var form = new AddCameraControl(this, "新增相机操作") { Size = new Size(300, 200) };
AntdUI.Modal.open(new AntdUI.Modal.Config(this, "", form, TType.None)
{
BtnHeight = 0,
});
if (form.submit)
{
var newItem = new MenuItem(form.CubicleName);
newItem.IconSvg = "VideoCameraOutlined";
// 防止重复添加
if (!workstationItem.Sub.Cast