NavisworksTransport/LogisticsPropertyEditDialog.cs

367 lines
11 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
namespace NavisworksTransport
{
/// <summary>
/// 物流属性编辑对话框
/// </summary>
public partial class LogisticsPropertyEditDialog : Form
{
#region
private ComboBox _elementTypeComboBox;
private CheckBox _isTraversableCheckBox;
private NumericUpDown _priorityNumericUpDown;
private ComboBox _vehicleSizeComboBox;
private NumericUpDown _speedLimitNumericUpDown;
private Button _okButton;
private Button _cancelButton;
#endregion
#region
/// <summary>
/// 选择的物流元素类型
/// </summary>
public CategoryAttributeManager.LogisticsElementType SelectedElementType
{
get
{
if (_elementTypeComboBox.SelectedItem is ComboBoxItem item)
{
return (CategoryAttributeManager.LogisticsElementType)item.Value;
}
return CategoryAttributeManager.LogisticsElementType.;
}
set
{
foreach (ComboBoxItem item in _elementTypeComboBox.Items)
{
if ((CategoryAttributeManager.LogisticsElementType)item.Value == value)
{
_elementTypeComboBox.SelectedItem = item;
break;
}
}
}
}
/// <summary>
/// 是否可通行
/// </summary>
public bool IsTraversable
{
get => _isTraversableCheckBox.Checked;
set => _isTraversableCheckBox.Checked = value;
}
/// <summary>
/// 优先级
/// </summary>
public int Priority
{
get => (int)_priorityNumericUpDown.Value;
set => _priorityNumericUpDown.Value = Math.Max(1, Math.Min(10, value));
}
/// <summary>
/// 车辆尺寸
/// </summary>
public string VehicleSize
{
get => _vehicleSizeComboBox.Text;
set => _vehicleSizeComboBox.Text = value;
}
/// <summary>
/// 速度限制
/// </summary>
public double SpeedLimit
{
get => (double)_speedLimitNumericUpDown.Value;
set => _speedLimitNumericUpDown.Value = (decimal)Math.Max(1.0, Math.Min(100.0, value));
}
#endregion
#region
/// <summary>
/// 初始化新的属性编辑对话框
/// </summary>
public LogisticsPropertyEditDialog()
{
InitializeComponent();
InitializeComboBoxItems();
}
/// <summary>
/// 使用现有属性信息初始化对话框
/// </summary>
/// <param name="existingInfo">现有属性信息</param>
public LogisticsPropertyEditDialog(LogisticsAttributeInfo existingInfo) : this()
{
if (existingInfo != null)
{
SelectedElementType = existingInfo.GetElementTypeEnum();
IsTraversable = existingInfo.IsTraversable;
Priority = existingInfo.Priority;
VehicleSize = existingInfo.VehicleSize;
SpeedLimit = existingInfo.SpeedLimit;
}
}
#endregion
#region
/// <summary>
/// 初始化组件
/// </summary>
private void InitializeComponent()
{
this.Text = "编辑物流属性";
this.Size = new Size(350, 280);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.StartPosition = FormStartPosition.CenterParent;
this.Font = new Font("微软雅黑", 9);
// 确保对话框显示在最前面
this.TopMost = true;
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Normal;
// 元素类型标签和组合框
var elementTypeLabel = new Label
{
Text = "元素类型:",
Location = new Point(20, 20),
Size = new Size(80, 23),
TextAlign = ContentAlignment.MiddleLeft
};
this.Controls.Add(elementTypeLabel);
_elementTypeComboBox = new ComboBox
{
Location = new Point(110, 20),
Size = new Size(180, 23),
DropDownStyle = ComboBoxStyle.DropDownList
};
this.Controls.Add(_elementTypeComboBox);
// 可通行复选框
_isTraversableCheckBox = new CheckBox
{
Text = "可通行",
Location = new Point(20, 55),
Size = new Size(80, 23),
Checked = true
};
this.Controls.Add(_isTraversableCheckBox);
// 优先级标签和数值输入框
var priorityLabel = new Label
{
Text = "优先级:",
Location = new Point(20, 90),
Size = new Size(80, 23),
TextAlign = ContentAlignment.MiddleLeft
};
this.Controls.Add(priorityLabel);
_priorityNumericUpDown = new NumericUpDown
{
Location = new Point(110, 90),
Size = new Size(60, 23),
Minimum = 1,
Maximum = 10,
Value = 5
};
this.Controls.Add(_priorityNumericUpDown);
// 车辆尺寸标签和组合框
var vehicleSizeLabel = new Label
{
Text = "车辆尺寸:",
Location = new Point(20, 125),
Size = new Size(80, 23),
TextAlign = ContentAlignment.MiddleLeft
};
this.Controls.Add(vehicleSizeLabel);
_vehicleSizeComboBox = new ComboBox
{
Location = new Point(110, 125),
Size = new Size(180, 23)
};
_vehicleSizeComboBox.Items.AddRange(new[] { "小型", "标准", "大型", "超大型" });
_vehicleSizeComboBox.Text = "标准";
this.Controls.Add(_vehicleSizeComboBox);
// 速度限制标签和数值输入框
var speedLimitLabel = new Label
{
Text = "速度限制:",
Location = new Point(20, 160),
Size = new Size(80, 23),
TextAlign = ContentAlignment.MiddleLeft
};
this.Controls.Add(speedLimitLabel);
_speedLimitNumericUpDown = new NumericUpDown
{
Location = new Point(110, 160),
Size = new Size(80, 23),
Minimum = 1,
Maximum = 100,
Value = 10,
DecimalPlaces = 1
};
this.Controls.Add(_speedLimitNumericUpDown);
var kmhLabel = new Label
{
Text = "km/h",
Location = new Point(200, 160),
Size = new Size(40, 23),
TextAlign = ContentAlignment.MiddleLeft
};
this.Controls.Add(kmhLabel);
// 按钮
_okButton = new Button
{
Text = "确定",
Location = new Point(130, 200),
Size = new Size(75, 30),
DialogResult = DialogResult.OK
};
_okButton.Click += OkButton_Click;
this.Controls.Add(_okButton);
_cancelButton = new Button
{
Text = "取消",
Location = new Point(215, 200),
Size = new Size(75, 30),
DialogResult = DialogResult.Cancel
};
this.Controls.Add(_cancelButton);
this.AcceptButton = _okButton;
this.CancelButton = _cancelButton;
}
/// <summary>
/// 初始化组合框选项
/// </summary>
private void InitializeComboBoxItems()
{
// 添加所有物流元素类型
var elementTypes = Enum.GetValues(typeof(CategoryAttributeManager.LogisticsElementType));
foreach (CategoryAttributeManager.LogisticsElementType elementType in elementTypes)
{
_elementTypeComboBox.Items.Add(new ComboBoxItem(elementType.ToString(), elementType));
}
if (_elementTypeComboBox.Items.Count > 0)
{
_elementTypeComboBox.SelectedIndex = 0;
}
}
#endregion
#region
/// <summary>
/// 重写SetVisibleCore以确保对话框正确显示
/// </summary>
/// <param name="value">是否可见</param>
protected override void SetVisibleCore(bool value)
{
base.SetVisibleCore(value);
if (value && this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
if (value)
{
this.Activate();
this.BringToFront();
}
}
/// <summary>
/// 重写ShowDialog以确保正确的显示行为
/// </summary>
/// <param name="owner">父窗口</param>
/// <returns>对话框结果</returns>
public new DialogResult ShowDialog(IWin32Window owner)
{
// 在显示前确保窗口状态正确
this.TopMost = true;
this.WindowState = FormWindowState.Normal;
// 调用基类的ShowDialog
var result = base.ShowDialog(owner);
// 显示后确保焦点
this.Activate();
this.BringToFront();
return result;
}
#endregion
#region
/// <summary>
/// 确定按钮点击事件
/// </summary>
private void OkButton_Click(object sender, EventArgs e)
{
// 这里可以添加验证逻辑
if (_elementTypeComboBox.SelectedItem == null)
{
MessageBox.Show("请选择元素类型", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
this.DialogResult = DialogResult.OK;
this.Close();
}
#endregion
#region
/// <summary>
/// 组合框项目类
/// </summary>
private class ComboBoxItem
{
public string Text { get; }
public object Value { get; }
public ComboBoxItem(string text, object value)
{
Text = text;
Value = value;
}
public override string ToString()
{
return Text;
}
}
#endregion
}
}