DHDHSoftware/DHSoftware/Views/SettingWindow1.cs

497 lines
19 KiB
C#
Raw Normal View History

2025-03-16 13:14:05 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
2025-03-18 14:19:33 +08:00
using System.Text.Json;
2025-03-16 13:14:05 +08:00
using System.Threading.Tasks;
using System.Windows.Forms;
using AntdUI;
2025-03-18 14:19:33 +08:00
using AntdUI.Svg;
using DH.Commons.Enums;
using DH.Commons.Helper;
using DH.Devices.Devices;
using DH.Devices.PLC;
using DHSoftware.Utils;
2025-03-16 13:14:05 +08:00
namespace DHSoftware.Views
{
public partial class SettingWindow1 : Window
{
private UserControl currControl;
2025-03-18 14:19:33 +08:00
List<CameraBase> CameraBaseList = new List<CameraBase>();
List<PLCBase> PLCBaseList = new List<PLCBase>();
List<DetectionConfig> DetectionList = new List<DetectionConfig>();
2025-03-16 13:14:05 +08:00
public SettingWindow1()
{
InitializeComponent();
2025-03-18 14:19:33 +08:00
BindEventHandler();
InitData();
2025-03-16 13:14:05 +08:00
}
2025-03-18 14:19:33 +08:00
private void BindEventHandler()
{
Resize += SettingWindow1_Resize;
btnAdd.SelectedValueChanged += btnAdd_SelectedValueChanged;
btnSave.Click += BtnSave_Click;
2025-03-16 13:14:05 +08:00
2025-03-18 14:19:33 +08:00
}
private float x; //定义当前窗体的宽度
private float y; //定义当前窗体的高度
private void InitData()
2025-03-16 13:14:05 +08:00
{
2025-03-18 14:19:33 +08:00
btnAdd.Items.Clear();
btnAdd.Items.Add("相机设置");
btnAdd.Items.Add("工位设置");
btnAdd.Items.Add("运控设置");
2025-03-16 13:14:05 +08:00
2025-03-18 14:19:33 +08:00
x = Width;
y = Height;
AdaptiveHelper.setTag(this);
// 从文件加载配置
var loadedConfig = ConfigManager.LoadConfig();
if (loadedConfig != null)
2025-03-16 13:14:05 +08:00
{
2025-03-18 14:19:33 +08:00
// 更新当前列表
CameraBaseList = loadedConfig.Cameras;
PLCBaseList = loadedConfig.PLCs;
DetectionList = loadedConfig.Detections;
if (CameraBaseList.Count > 0)
2025-03-16 13:14:05 +08:00
{
2025-03-18 14:19:33 +08:00
var workstationItem = FindMenuItem(menu1.Items, "相机设置");
if (workstationItem != null)
2025-03-16 13:14:05 +08:00
{
2025-03-18 14:19:33 +08:00
foreach (var item in CameraBaseList)
{
var newItem = new MenuItem();
newItem.Text = item.CameraName;
newItem.IconSvg = "VideoCameraOutlined";
workstationItem.Sub.Add(newItem);
}
}
}
if (DetectionList.Count > 0)
{
var workstationItem = FindMenuItem(menu1.Items, "工位设置");
2025-03-16 13:14:05 +08:00
2025-03-18 14:19:33 +08:00
if (workstationItem != null)
{
foreach (var item in DetectionList)
{
var newItem = new MenuItem();
newItem.Text = item.Name;
newItem.IconSvg = "AppstoreOutlined";
workstationItem.Sub.Add(newItem);
2025-03-16 13:14:05 +08:00
2025-03-18 14:19:33 +08:00
}
2025-03-16 13:14:05 +08:00
}
2025-03-18 14:19:33 +08:00
}
if (PLCBaseList.Count > 0)
{
var workstationItem = FindMenuItem(menu1.Items, "运控设置");
if (workstationItem != null)
2025-03-16 13:14:05 +08:00
{
2025-03-18 14:19:33 +08:00
foreach (var item in PLCBaseList)
{
var newItem = new MenuItem();
newItem.Text = item.PLCName;
newItem.IconSvg = "ControlOutlined";
workstationItem.Sub.Add(newItem);
}
2025-03-16 13:14:05 +08:00
}
}
}
}
2025-03-18 14:19:33 +08:00
private void BtnSave_Click(object? sender, EventArgs e)
{
var config = ConfigManager.CreateConfig(CameraBaseList, PLCBaseList, DetectionList);
ConfigManager.SaveConfig(config);
MessageBox.Show("保存成功!");
}
private void SettingWindow1_Resize(object? sender, EventArgs e)
{
var newx = Width / x;
var newy = Height / y;
AdaptiveHelper.setControls(newx, newy, this);
}
2025-03-16 13:14:05 +08:00
// 递归查找菜单项
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;
}
2025-03-18 14:19:33 +08:00
2025-03-16 13:14:05 +08:00
bool isUpdatingTabs = false;
2025-03-18 14:19:33 +08:00
2025-03-16 13:14:05 +08:00
2025-03-18 14:20:11 +08:00
}
}
private void LoadMenu(string filter = "")
{
menu1.Items.Clear();
Dictionary<string, List<MenuItems>> Menu_decetion = new Dictionary<string, List<MenuItems>>();
string lang = AntdUI.Localization.CurrentLanguage;
var menuItems = DataUtil.Menu_decetion;
//var menuIcons = DataUtil.MenuIcons_zhcn;
//if (lang.StartsWith("en"))
//{
// menuItems = DataUtil.MenuItems_enus;
// menuIcons = DataUtil.MenuIcons_enus;
//}
foreach (var rootItem in Menu_decetion)
{
var rootKey = rootItem.Key.ToLower();
var rootMenu = new AntdUI.MenuItem
{
Text = rootItem.Key,
//IconSvg = menuIcons.TryGetValue(rootItem.Key, out var icon) ? icon : "UnorderedListOutlined",
};
bool rootVisible = false; // 用于标记是否显示根节点
foreach (var item in rootItem.Value)
{
var childText = item.Text.ToLower();
// 如果子节点包含搜索文本
if (childText.Contains(filter))
{
var menuItem = new AntdUI.MenuItem
{
Text = item.Text,
IconSvg = item.IconSvg,
Tag = item.Tag,
};
rootMenu.Sub.Add(menuItem);
rootVisible = true; // 如果有子节点包含,则显示根节点
}
}
// 如果根节点包含搜索文本,或有可见的子节点,则显示根节点
if (rootKey.Contains(filter) || rootVisible)
{
menu1.Items.Add(rootMenu);
}
}
}
2025-03-16 13:14:05 +08:00
private void menu1_MouseDown(object sender, MouseEventArgs e)
{
2025-03-18 14:19:33 +08:00
if (e.Button == MouseButtons.Left)
2025-03-16 13:14:05 +08:00
{
2025-03-18 14:19:33 +08:00
// 转换坐标到控件内部坐标系(考虑滚动条)
Point clickPoint = new Point(e.X, e.Y + menu1.ScrollBar.Value);
// 递归查找命中的菜单项
MenuItem clickedItem = FindClickedItem(menu1.Items, clickPoint);
2025-03-16 13:14:05 +08:00
2025-03-18 14:19:33 +08:00
if (clickedItem != null)
2025-03-16 13:14:05 +08:00
{
2025-03-18 14:19:33 +08:00
if (clickedItem.PARENTITEM == null)
{
return;
}
2025-03-16 13:14:05 +08:00
2025-03-18 14:19:33 +08:00
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= CameraBaseList.Where(c => c.CameraName == clickedItem.Text).FirstOrDefault();
if (CameraBase == null)
2025-03-16 13:14:05 +08:00
{
2025-03-18 14:19:33 +08:00
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);
2025-03-16 13:14:05 +08:00
isUpdatingTabs = true;
2025-03-18 14:19:33 +08:00
tabs1.SelectedTab = tabPage;
2025-03-16 13:14:05 +08:00
isUpdatingTabs = false;
2025-03-18 14:19:33 +08:00
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;
}
2025-03-16 13:14:05 +08:00
}
2025-03-18 14:19:33 +08:00
UserControl control1 = null;
control1 = new DetectControl(this);
if (control1 != null)
2025-03-16 13:14:05 +08:00
{
2025-03-18 14:19:33 +08:00
//容器添加控件需要调整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 "运控设置":
2025-03-16 13:14:05 +08:00
2025-03-18 14:19:33 +08:00
break;
2025-03-16 13:14:05 +08:00
2025-03-18 14:19:33 +08:00
}
2025-03-16 13:14:05 +08:00
}
2025-03-18 14:19:33 +08:00
2025-03-16 13:14:05 +08:00
}
2025-03-18 14:19:33 +08:00
else 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 = "DeleteOutlined"
}
};
AntdUI.ContextMenuStrip.open(menu1, it =>
{
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);
CameraBaseList.RemoveAll(c => c.CameraName == clickedItem.Text);
isUpdatingTabs = false;
currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
return;
}
}
CameraBaseList.RemoveAll(c => c.CameraName == clickedItem.Text);
}, menulist);
break;
case "工位设置":
var menulist1 = new AntdUI.IContextMenuStripItem[]
{
new AntdUI.ContextMenuStripItem("删除工位", "")
{
IconSvg = "DeleteOutlined"
}
};
AntdUI.ContextMenuStrip.open(menu1, it =>
{
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);
isUpdatingTabs = false;
currControl = existingTab.Controls.Count > 0 ? existingTab.Controls[0] as UserControl : null;
return;
}
}
}, menulist1);
break;
case "运控设置":
break;
}
}
}
}
2025-03-16 13:14:05 +08:00
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;
}
2025-03-18 14:19:33 +08:00
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 (!menu1.Items.Cast<MenuItem>().Any(m => m.Text == newItem.Text))
{
workstationItem.Sub.Add(newItem);
CameraBase cameraBase = new CameraBase();
cameraBase.CameraName = form.CubicleName;
CameraBaseList.Add(cameraBase);
}
else
{
AntdUI.Notification.warn(this, "新增相机失败", $"{form.CubicleName}已存在!", autoClose: 3, align: TAlignFrom.TR);
}
}
}
break;
case "工位设置":
// 查找工位设置项
var workstationItem1= FindMenuItem(menu1.Items, "工位设置");
if (workstationItem1 != null)
{
var form = new AddCubicleControl(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 = "AppstoreOutlined";
// 防止重复添加
if (!menu1.Items.Cast<MenuItem>().Any(m => m.Text == newItem.Text))
{
workstationItem1.Sub.Add(newItem);
}
else
{
AntdUI.Notification.warn(this, "新增工位失败", $"{form.CubicleName}已存在!", autoClose: 3, align: TAlignFrom.TR);
}
}
}
break;
case "运控设置":
break;
}
}
2025-03-16 13:14:05 +08:00
}
}