10.20PLC+相机2.3视觉修改
This commit is contained in:
		
							
								
								
									
										163
									
								
								Check.Main/UI/FrmConfig.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										163
									
								
								Check.Main/UI/FrmConfig.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,163 @@ | ||||
| using Check.Main.Camera; | ||||
| using Check.Main.Common; | ||||
| using Check.Main.Dispatch; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.ComponentModel; | ||||
| using System.Data; | ||||
| using System.Drawing; | ||||
| using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
| using System.Windows.Forms; | ||||
| using System.Xml.Serialization; | ||||
| using WeifenLuo.WinFormsUI.Docking; | ||||
|  | ||||
| namespace Check.Main.UI | ||||
| { | ||||
|     public partial class FrmConfig : DockContent | ||||
|     { | ||||
|         //private ProcessConfig _mainSettings=new ProcessConfig(); | ||||
|         //private readonly string _configFilePath = Path.Combine(Application.StartupPath, "main_config.xml"); | ||||
|         public FrmConfig() | ||||
|         { | ||||
|             InitializeComponent(); | ||||
|             ProductManager.OnProductChanged += UpdateUIForNewProduct; | ||||
|             // 初始化UI | ||||
|             InitializeProductComboBox(); | ||||
|             //LoadSettings(); // 窗体加载时,读取主配置文件 | ||||
|             propertyGrid1.SelectedObject = ProductManager.CurrentConfig; //_mainSettings; // 将配置对象绑定到属性网格 | ||||
|             propertyGrid1.PropertyValueChanged += (s, e) => { ProductManager.SaveCurrentProductConfig(); }; // 任何属性改变后自动保存 | ||||
|         } | ||||
|         // 【新增】初始化产品下拉列表 | ||||
|         private void InitializeProductComboBox() | ||||
|         { | ||||
|             cmbProducts.DataSource = null; // 先清空数据源 | ||||
|             cmbProducts.DataSource = ProductManager.ProductList; | ||||
|             cmbProducts.SelectedItem = ProductManager.CurrentProductName; | ||||
|         } | ||||
|         // 【新增】当产品管理器中的产品切换后,此方法被调用以更新整个UI | ||||
|         private void UpdateUIForNewProduct() | ||||
|         { | ||||
|             // 使用Invoke确保线程安全 | ||||
|             if (this.InvokeRequired) | ||||
|             { | ||||
|                 this.Invoke(new Action(UpdateUIForNewProduct)); | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             ThreadSafeLogger.Log($"UI正在更新以显示产品 '{ProductManager.CurrentProductName}' 的配置。"); | ||||
|             // 更新下拉列表的显示(如果产品列表也变了) | ||||
|             InitializeProductComboBox(); | ||||
|  | ||||
|             // 【关键】将PropertyGrid重新绑定到新产品的配置对象上 | ||||
|             propertyGrid1.SelectedObject = ProductManager.CurrentConfig; | ||||
|             propertyGrid1.Refresh(); // 强制刷新UI | ||||
|         } | ||||
|         private void toolBtnApply_Click(object sender, EventArgs e) | ||||
|         { | ||||
|             ThreadSafeLogger.Log("用户点击“应用”按钮..."); | ||||
|  | ||||
|             // 1. 确保在应用前,任何可能未触发 PropertyValueChanged 的更改都被保存。 | ||||
|             ConfigurationManager.SaveChanges(); | ||||
|  | ||||
|             // 2. 获取主窗体引用 | ||||
|             var mainForm = this.DockPanel.FindForm() as FrmMain; | ||||
|             mainForm?.ClearStatusStrip(); // 清理UI状态 | ||||
|  | ||||
|             // 3. 从 ConfigurationManager 获取最新的相机配置列表 | ||||
|             var cameraSettings = ConfigurationManager.GetCurrentConfig();//.CameraSettings; | ||||
|  | ||||
|             if (cameraSettings != null) | ||||
|             { | ||||
|                 // 使用全局配置来初始化或重新初始化相机。 | ||||
|                 // CameraManager.Initialize 内部会首先调用 Shutdown,所以这是个完整的重启流程。 | ||||
|                 CameraManager.Initialize(cameraSettings, mainForm); | ||||
|             } | ||||
|  | ||||
|             ThreadSafeLogger.Log("中央配置已成功应用到相机系统。"); | ||||
|             //MessageBox.Show("主配置已应用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); | ||||
|         } | ||||
|  | ||||
|         private void cmbProducts_SelectedIndexChanged(object sender, EventArgs e) | ||||
|         { | ||||
|             if (cmbProducts.SelectedItem is string selectedProduct && selectedProduct != ProductManager.CurrentProductName) | ||||
|             { | ||||
|                 ProductManager.SwitchToProduct(selectedProduct); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         private void toolBtnAddProduct_Click(object sender, EventArgs e) | ||||
|         { | ||||
|             // 使用一个简单的输入框来获取新产品名称 | ||||
|             string newName = ShowInputDialog("请输入新产品名称:"); | ||||
|             if (!string.IsNullOrWhiteSpace(newName)) | ||||
|             { | ||||
|                 if (ProductManager.AddNewProduct(newName)) | ||||
|                 { | ||||
|                     ThreadSafeLogger.Log($"成功添加新产品: {newName}"); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     MessageBox.Show("产品名称无效或已存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|         } | ||||
|         // 一个简单的输入对话框辅助方法 | ||||
|         public static string ShowInputDialog(string text) | ||||
|         { | ||||
|             Form prompt = new Form() | ||||
|             { | ||||
|                 Width = 300, | ||||
|                 Height = 150, | ||||
|                 FormBorderStyle = FormBorderStyle.FixedDialog, | ||||
|                 Text = text, | ||||
|                 StartPosition = FormStartPosition.CenterScreen | ||||
|             }; | ||||
|             Label textLabel = new Label() { Left = 50, Top = 20, Text = "产品名称:" }; | ||||
|             TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 200 }; | ||||
|             Button confirmation = new Button() { Text = "确定", Left = 150, Width = 100, Top = 80, DialogResult = DialogResult.OK }; | ||||
|             confirmation.Click += (sender, e) => { prompt.Close(); }; | ||||
|             prompt.Controls.Add(textBox); | ||||
|             prompt.Controls.Add(confirmation); | ||||
|             prompt.Controls.Add(textLabel); | ||||
|             prompt.AcceptButton = confirmation; | ||||
|  | ||||
|             return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : ""; | ||||
|         } | ||||
|  | ||||
|         private void toolBtnDeleteProduct_Click(object sender, EventArgs e) | ||||
|         { | ||||
|             // 1. 获取当前选中的产品 | ||||
|             string productToDelete = ProductManager.CurrentProductName; | ||||
|  | ||||
|             if (string.IsNullOrWhiteSpace(productToDelete) || productToDelete == "DefaultProduct") | ||||
|             { | ||||
|                 MessageBox.Show("不能删除默认产品或无效产品!", "操作无效", MessageBoxButtons.OK, MessageBoxIcon.Warning); | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             // 2. 弹出安全确认对话框 | ||||
|             var confirmResult = MessageBox.Show($"您确定要永久删除产品 '{productToDelete}' 吗?\n\n此操作不可恢复,将删除其所有相关配置!", | ||||
|                                                  "确认删除", | ||||
|                                                  MessageBoxButtons.YesNo, | ||||
|                                                  MessageBoxIcon.Warning); | ||||
|  | ||||
|             if (confirmResult == DialogResult.Yes) | ||||
|             { | ||||
|                 // 3. 调用核心删除逻辑 | ||||
|                 if (ProductManager.DeleteProduct(productToDelete)) | ||||
|                 { | ||||
|                     ThreadSafeLogger.Log($"用户成功删除了产品 '{productToDelete}'。"); | ||||
|                     // 无需在这里手动更新UI,因为DeleteProduct方法内部会触发 OnProductChanged 事件, | ||||
|                     // 而我们的 UpdateUIForNewProduct 方法会自动响应这个事件并刷新整个界面。 | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     MessageBox.Show($"删除产品 '{productToDelete}' 失败,请查看日志获取详细信息。", "删除失败", MessageBoxButtons.OK, MessageBoxIcon.Error); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user