using System; using System.Drawing; using System.IO; using System.Linq; using System.Windows.Forms; using CadParamPluging.Common; namespace CadParamPluging.UI { public class SettingsForm : Form { private readonly ListBox _lbDeliveryStatuses; private readonly ListBox _lbProcessMethods; private readonly ListBox _lbStructuralFeatures; private readonly ListBox _lbSpecialConditions; public DropdownOptions Result { get; private set; } public SettingsForm(DropdownOptions current) { Text = "设置"; Size = new Size(600, 500); StartPosition = FormStartPosition.CenterScreen; FormBorderStyle = FormBorderStyle.FixedDialog; MaximizeBox = false; MinimizeBox = false; ShowInTaskbar = false; Font = SystemFonts.MessageBoxFont; // Use system font for consistent look var mainLayout = new TableLayoutPanel { Dock = DockStyle.Fill, Padding = new Padding(12), RowCount = 2, ColumnCount = 1 }; mainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100f)); // Tabs mainLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize)); // Buttons _lbDeliveryStatuses = new ListBox(); _lbProcessMethods = new ListBox(); _lbStructuralFeatures = new ListBox(); _lbSpecialConditions = new ListBox(); var options = (current ?? DropdownOptions.CreateDefault()).Clone(); options.Normalize(); FillList(_lbDeliveryStatuses, options.DeliveryStatuses); FillList(_lbProcessMethods, options.ProcessMethods); FillList(_lbStructuralFeatures, options.StructuralFeatures); FillList(_lbSpecialConditions, options.SpecialConditions); // 2. Tabs Section var tabs = new TabControl { Dock = DockStyle.Fill }; tabs.TabPages.Add(BuildEditableTab("交付状态", _lbDeliveryStatuses)); tabs.TabPages.Add(BuildEditableTab("工艺方法", _lbProcessMethods)); tabs.TabPages.Add(BuildEditableTab("结构特征", _lbStructuralFeatures)); tabs.TabPages.Add(BuildEditableTab("特殊条件", _lbSpecialConditions)); // 3. Bottom Section (Separated into Left (Tools) and Right (Dialog)) var bottomGrid = new TableLayoutPanel { Dock = DockStyle.Fill, RowCount = 1, ColumnCount = 2, AutoSize = true, Margin = new Padding(0, 8, 0, 0) // Top margin for spacing }; bottomGrid.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f)); bottomGrid.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize)); // Left tools var leftTools = new FlowLayoutPanel { Dock = DockStyle.Fill, AutoSize = true, FlowDirection = FlowDirection.LeftToRight }; var btnParamCatalog = new Button { Text = "参数总表", AutoSize = true }; btnParamCatalog.Click += (_, __) => OnOpenParamCatalog(); var btnTemplateBinding = new Button { Text = "模板绑定", AutoSize = true }; btnTemplateBinding.Click += (_, __) => OnOpenTemplateBinding(); leftTools.Controls.Add(btnParamCatalog); leftTools.Controls.Add(btnTemplateBinding); // Right actions var rightActions = new FlowLayoutPanel { Dock = DockStyle.Fill, AutoSize = true, FlowDirection = FlowDirection.RightToLeft }; var btnCancel = new Button { Text = "取消", DialogResult = DialogResult.Cancel, Width = 80 }; var btnOk = new Button { Text = "确定", Width = 80 }; btnOk.Click += (_, __) => OnOk(); rightActions.Controls.Add(btnCancel); // Add in reverse order due to RightToLeft flow rightActions.Controls.Add(btnOk); bottomGrid.Controls.Add(leftTools, 0, 0); bottomGrid.Controls.Add(rightActions, 1, 0); // Assemble Main mainLayout.Controls.Add(tabs, 0, 0); mainLayout.Controls.Add(bottomGrid, 0, 1); Controls.Add(mainLayout); AcceptButton = btnOk; CancelButton = btnCancel; } private void OnOpenParamCatalog() { try { var catalog = ParamCatalogStore.Load(); using (var f = new ParamCatalogForm(catalog)) { f.ShowDialog(this); } } catch (Exception ex) { MessageBox.Show(this, $"打开参数总表失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void OnOpenTemplateBinding() { try { var catalog = ParamCatalogStore.Load(); var projectTypes = _lbDeliveryStatuses.Items.Cast().Select(o => (o as string) ?? string.Empty).ToList(); var drawingTypes = _lbProcessMethods.Items.Cast().Select(o => (o as string) ?? string.Empty).ToList(); var sheetSizes = _lbStructuralFeatures.Items.Cast().Select(o => (o as string) ?? string.Empty).ToList(); var scales = _lbSpecialConditions.Items.Cast().Select(o => (o as string) ?? string.Empty).ToList(); using (var f = new TemplateSchemaBindingForm(projectTypes, drawingTypes, sheetSizes, scales, catalog)) { f.ShowDialog(this); } } catch (Exception ex) { MessageBox.Show(this, $"打开模板参数绑定失败:\n\n{ex}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private static void FillList(ListBox listBox, System.Collections.Generic.IEnumerable items) { listBox.Items.Clear(); if (items == null) { return; } foreach (var item in items) { if (!string.IsNullOrWhiteSpace(item)) { listBox.Items.Add(item.Trim()); } } if (listBox.Items.Count > 0) { listBox.SelectedIndex = 0; } } private TabPage BuildEditableTab(string title, ListBox listBox) { var page = new TabPage(title); page.Padding = new Padding(8); page.UseVisualStyleBackColor = true; var layout = new TableLayoutPanel { Dock = DockStyle.Fill, ColumnCount = 2, RowCount = 1, }; layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f)); layout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 100f)); // Fixed width for button column listBox.Dock = DockStyle.Fill; listBox.IntegralHeight = false; // smoother resizing var actions = new FlowLayoutPanel { Dock = DockStyle.Fill, FlowDirection = FlowDirection.TopDown, WrapContents = false, Padding = new Padding(4, 0, 0, 0) }; var btnAdd = new Button { Text = "新增", Width = 90, Height = 30 }; btnAdd.Click += (_, __) => OnAddItem(title, listBox); var btnEdit = new Button { Text = "修改", Width = 90, Height = 30 }; btnEdit.Click += (_, __) => OnEditItem(title, listBox); var btnDelete = new Button { Text = "删除", Width = 90, Height = 30 }; btnDelete.Click += (_, __) => OnDeleteItem(title, listBox); actions.Controls.Add(btnAdd); actions.Controls.Add(btnEdit); actions.Controls.Add(btnDelete); layout.Controls.Add(listBox, 0, 0); layout.Controls.Add(actions, 1, 0); page.Controls.Add(layout); return page; } private void OnAddItem(string category, ListBox listBox) { if (!TextInputForm.TryGetValue(this, $"{category} - 新增", "请输入内容:", string.Empty, out var value)) { return; } value = (value ?? string.Empty).Trim(); if (value.Length == 0) { MessageBox.Show(this, "内容不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } listBox.Items.Add(value); listBox.SelectedIndex = listBox.Items.Count - 1; } private void OnEditItem(string category, ListBox listBox) { var selected = listBox.SelectedItem as string; if (string.IsNullOrWhiteSpace(selected)) { MessageBox.Show(this, "请先选择一项再修改。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (!TextInputForm.TryGetValue(this, $"{category} - 修改", "请输入内容:", selected, out var value)) { return; } value = (value ?? string.Empty).Trim(); if (value.Length == 0) { MessageBox.Show(this, "内容不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var idx = listBox.SelectedIndex; listBox.Items[idx] = value; listBox.SelectedIndex = idx; } private void OnDeleteItem(string category, ListBox listBox) { var selected = listBox.SelectedItem as string; if (string.IsNullOrWhiteSpace(selected)) { MessageBox.Show(this, "请先选择一项再删除。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } var confirm = MessageBox.Show( this, $"确认删除:{selected}?", $"{category} - 删除", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (confirm != DialogResult.Yes) { return; } var idx = listBox.SelectedIndex; listBox.Items.RemoveAt(idx); if (listBox.Items.Count > 0) { listBox.SelectedIndex = Math.Min(idx, listBox.Items.Count - 1); } } private void OnOk() { var result = new DropdownOptions { DeliveryStatuses = _lbDeliveryStatuses.Items.Cast().Select(o => (o as string) ?? string.Empty).ToList(), ProcessMethods = _lbProcessMethods.Items.Cast().Select(o => (o as string) ?? string.Empty).ToList(), StructuralFeatures = _lbStructuralFeatures.Items.Cast().Select(o => (o as string) ?? string.Empty).ToList(), SpecialConditions = _lbSpecialConditions.Items.Cast().Select(o => (o as string) ?? string.Empty).ToList() }; result.Normalize(); var err = ValidateResult(result); if (err != null) { MessageBox.Show(this, err, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } Result = result; DialogResult = DialogResult.OK; Close(); } private static string ValidateResult(DropdownOptions options) { if (options.DeliveryStatuses == null || options.DeliveryStatuses.Count == 0) { return "交付状态至少需要 1 项。"; } if (options.ProcessMethods == null || options.ProcessMethods.Count == 0) { return "工艺方法至少需要 1 项。"; } if (options.StructuralFeatures == null || options.StructuralFeatures.Count == 0) { return "结构特征至少需要 1 项。"; } if (options.SpecialConditions == null || options.SpecialConditions.Count == 0) { return "特殊条件至少需要 1 项。"; } return null; } } }