CadParamPluging/UI/SettingsForm.cs

337 lines
12 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 System;
using System.Drawing;
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(560, 420);
StartPosition = FormStartPosition.CenterScreen;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
var layout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
Padding = new Padding(10),
RowCount = 2,
ColumnCount = 1
};
layout.RowStyles.Add(new RowStyle(SizeType.Percent, 100f)); // Content area
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize)); // Buttons area
_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);
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));
// Bottom area
var bottom = new TableLayoutPanel
{
Dock = DockStyle.Fill,
ColumnCount = 2,
RowCount = 1
};
bottom.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f));
bottom.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
var flowLeft = new FlowLayoutPanel
{
Dock = DockStyle.Fill,
FlowDirection = FlowDirection.LeftToRight,
AutoSize = true
};
var btnParamCatalog = new Button { Text = "参数总表...", AutoSize = true };
btnParamCatalog.Click += (_, __) => OnOpenParamCatalog();
var btnTemplateBinding = new Button { Text = "模板参数绑定...", AutoSize = true };
btnTemplateBinding.Click += (_, __) => OnOpenTemplateBinding();
flowLeft.Controls.Add(btnParamCatalog);
flowLeft.Controls.Add(btnTemplateBinding);
var flowRight = new FlowLayoutPanel
{
Dock = DockStyle.Fill,
FlowDirection = FlowDirection.RightToLeft,
AutoSize = true
};
var btnCancel = new Button { Text = "取消", DialogResult = DialogResult.Cancel };
var btnOk = new Button { Text = "确定" };
btnOk.Click += (_, __) => OnOk();
flowRight.Controls.Add(btnCancel);
flowRight.Controls.Add(btnOk);
bottom.Controls.Add(flowLeft, 0, 0);
bottom.Controls.Add(flowRight, 1, 0);
layout.Controls.Add(tabs, 0, 0);
layout.Controls.Add(bottom, 0, 1);
Controls.Add(layout);
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<object>().Select(o => (o as string) ?? string.Empty).ToList();
var drawingTypes = _lbProcessMethods.Items.Cast<object>().Select(o => (o as string) ?? string.Empty).ToList();
var sheetSizes = _lbStructuralFeatures.Items.Cast<object>().Select(o => (o as string) ?? string.Empty).ToList();
var scales = _lbSpecialConditions.Items.Cast<object>().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<string> 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);
var layout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
ColumnCount = 2,
RowCount = 1,
Padding = new Padding(8)
};
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f));
layout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
listBox.Dock = DockStyle.Fill;
var actions = new FlowLayoutPanel
{
Dock = DockStyle.Fill,
FlowDirection = FlowDirection.TopDown,
WrapContents = false,
AutoSize = true
};
var btnAdd = new Button { Text = "新增", AutoSize = true };
btnAdd.Click += (_, __) => OnAddItem(title, listBox);
var btnEdit = new Button { Text = "修改", AutoSize = true };
btnEdit.Click += (_, __) => OnEditItem(title, listBox);
var btnDelete = new Button { Text = "删除", AutoSize = true };
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<object>().Select(o => (o as string) ?? string.Empty).ToList(),
ProcessMethods = _lbProcessMethods.Items.Cast<object>().Select(o => (o as string) ?? string.Empty).ToList(),
StructuralFeatures = _lbStructuralFeatures.Items.Cast<object>().Select(o => (o as string) ?? string.Empty).ToList(),
SpecialConditions = _lbSpecialConditions.Items.Cast<object>().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;
}
}
}