Sic.Framework/MECF.Framework.UI.Client/RecipeEditorLib/RecipeModel/Params/Param.cs

311 lines
7.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Windows;
using Caliburn.Micro.Core;
using ExtendedGrid.Microsoft.Windows.Controls;
using MECF.Framework.UI.Client.ClientBase;
namespace MECF.Framework.UI.Client.RecipeEditorLib.RecipeModel.Params
{
public abstract class Param : PropertyChangedBase, IParam
{
#region Variables
public event EventHandler<object> OnValueChanged;
protected string _name;
protected string _displayName;
protected string _unitName;
protected MenuPermissionEnum _perm;
protected MenuPermissionEnum _permStep;
protected MenuPermissionEnum _permColumn;
protected bool _isEnabled;
protected bool _isSaved;
protected bool _isColumnSelected;
protected Visibility _visibility;
protected bool _isValid;
protected string _validationError;
protected bool _isEqualsToPrevious;
protected bool _isHighlighted;
#endregion
#region Constructors
protected Param()
{
_isSaved = true;
_isEnabled = true;
_isValid = true;
_validationError = null;
_isEqualsToPrevious = true;
_visibility = Visibility.Visible;
_isHighlighted = false;
}
#endregion
#region Properties
/// <summary>
/// 设置或返回当前参数所在的列的访问权限变更时,执行的回调函数。
/// </summary>
public Action<Param> ColumnPermChangedCallback { get; set; }
public Func<Param, bool> Check { get; set; }
/// <inheritdoc />
public RecipeStep Parent { get; set; }
/// <inheritdoc />
public DataGridRow RowOwner { get; set; }
/// <inheritdoc />
public DataGridColumn ColumnOwner { get; set; }
/// <inheritdoc />
public virtual IParam Previous { get; set; }
/// <inheritdoc />
public virtual IParam Next { get; set; }
/// <inheritdoc />
public string Name
{
get => _name;
set
{
_name = value;
NotifyOfPropertyChange();
}
}
/// <inheritdoc />
public string DisplayName
{
get => _displayName;
set
{
_displayName = value;
NotifyOfPropertyChange();
}
}
/// <summary>
/// 设置或返回当前参数的单位。
/// </summary>
public string UnitName
{
get => _unitName;
set
{
_unitName = value;
NotifyOfPropertyChange();
}
}
/// <inheritdoc />
public MenuPermissionEnum StepPermission
{
get => _permStep;
set
{
_permStep = value;
NotifyOfPropertyChange(nameof(Permission));
}
}
/// <inheritdoc />
public MenuPermissionEnum ColumnPermission
{
get => _permColumn;
set
{
_permColumn = value;
NotifyOfPropertyChange(nameof(Permission));
}
}
/// <inheritdoc />
public virtual MenuPermissionEnum Permission => (MenuPermissionEnum)Math.Min((int)StepPermission, (int)ColumnPermission);
/// <inheritdoc />
public bool IsEnabled
{
get => _isEnabled;
set
{
_isEnabled = value;
NotifyOfPropertyChange();
}
}
/// <inheritdoc />
public virtual bool IsSaved
{
get => _isSaved;
set
{
_isSaved = value;
NotifyOfPropertyChange();
}
}
/// <inheritdoc />
public bool IsEqualsToPrevious
{
get => _isEqualsToPrevious;
set
{
_isEqualsToPrevious = value;
NotifyOfPropertyChange();
}
}
/// <inheritdoc />
public bool IsValid
{
get => _isValid;
internal set
{
_isValid = value;
NotifyOfPropertyChange();
}
}
/// <inheritdoc />
public string ValidationError
{
get => _validationError;
protected set
{
_validationError = value;
NotifyOfPropertyChange();
}
}
/// <inheritdoc />
public bool IsHighlighted
{
get => _isHighlighted;
private set
{
_isHighlighted = value;
NotifyOfPropertyChange();
}
}
public bool IsColumnSelected
{
get => _isColumnSelected;
set
{
_isColumnSelected = value;
NotifyOfPropertyChange();
}
}
public Visibility Visible
{
get => _visibility;
set
{
_visibility = value;
NotifyOfPropertyChange();
}
}
public bool EnableConfig { get; set; }
public bool EnableTolerance { get; set; }
public Visibility StepCheckVisibility { get; set; }
#region Methods
/// <summary>
/// 高亮显示当前参数。
/// </summary>
public void Highlight()
{
IsHighlighted = true;
}
/// <summary>
/// 取消高亮显示。
/// </summary>
public void ResetHighlight()
{
IsHighlighted = false;
}
/// <summary>
/// 将当前参数所对应的前序、后续参数链表转换为数组。
/// </summary>
/// <returns></returns>
public virtual List<IParam> Flatten()
{
if (Previous != null)
return Previous.Flatten();
var list = new List<IParam>();
IParam p = this;
while (p != null)
{
list.Add(p);
p = p.Next;
}
return list;
}
/// <summary>
/// 校验当前参数的数值。
/// </summary>
/// <returns></returns>
public virtual void Validate()
{
IsValid = true;
ValidationError = "";
}
/// <summary>
/// 将当前参数标记为已保存状态。
/// </summary>
public virtual void Save()
{
IsSaved = true;
}
/// <summary>
/// 获取当前参数的数值。
/// </summary>
/// <returns></returns>
public virtual object GetValue()
{
return default;
}
public void SetValue(object value)
{
HandleSetValue(value);
OnValueChanged?.Invoke(this, value);
}
public override string ToString()
{
return $"{DisplayName}, ControlName={Name}";
}
protected virtual void HandleSetValue(object value)
{
}
#endregion
#endregion
}
}