298 lines
7.9 KiB
C#
298 lines
7.9 KiB
C#
using System;
|
||
using System.Collections.ObjectModel;
|
||
using System.ComponentModel;
|
||
using System.Linq;
|
||
using SciChart.Core.Extensions;
|
||
|
||
namespace RecipeEditorLib.RecipeModel.Params
|
||
{
|
||
public class RecipeStep : ObservableCollection<Param>
|
||
{
|
||
|
||
/// <summary>
|
||
/// 步骤起始编号。
|
||
/// </summary>
|
||
public const int START_INDEX = 1;
|
||
|
||
#region Variables
|
||
|
||
private bool _isHideValue;
|
||
|
||
|
||
/// <summary>
|
||
/// 当配方步骤的IsSaved属性发生变化时,触发此事件。
|
||
/// </summary>
|
||
public event EventHandler<bool> SavedStateChanged;
|
||
|
||
|
||
#endregion
|
||
|
||
#region Constructors
|
||
|
||
public RecipeStep(RecipeStep previous)
|
||
{
|
||
Previous = previous;
|
||
IsSaved = true;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Properties
|
||
|
||
/// <summary>
|
||
/// 父级配方集合。
|
||
/// </summary>
|
||
public RecipeStepCollection Parent { get; internal set; }
|
||
|
||
/// <summary>
|
||
/// 返回配方步骤序号参数。
|
||
/// </summary>
|
||
public StepParam StepNoParam => this.FirstOrDefault(x => x is StepParam) as StepParam;
|
||
|
||
/// <summary>
|
||
/// 返回步骤序号。
|
||
/// </summary>
|
||
public int? StepNo
|
||
{
|
||
get
|
||
{
|
||
var param = StepNoParam;
|
||
return param?.Value ?? null;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 返回步骤执行时长。
|
||
/// </summary>
|
||
public double? StepTime
|
||
{
|
||
get
|
||
{
|
||
var param = this.FirstOrDefault(x => x is DoubleParam && string.Equals(x.Name,
|
||
RecipColNo.Time.ToString(), StringComparison.CurrentCultureIgnoreCase)) as DoubleParam;
|
||
return param?.Value;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 返回当前配方步骤是否已保存。
|
||
/// </summary>
|
||
public bool IsSaved { get; set;}
|
||
|
||
/// <summary>
|
||
/// 返回前序配方。
|
||
/// </summary>
|
||
public RecipeStep Previous { get; private set; }
|
||
|
||
/// <summary>
|
||
/// 返回后序配方。
|
||
/// </summary>
|
||
public RecipeStep Next { get; private set;}
|
||
|
||
/// <summary>
|
||
/// 返回是否隐藏当前步骤的参数值。
|
||
/// </summary>
|
||
public bool IsHideValue
|
||
{
|
||
get => _isHideValue;
|
||
set
|
||
{
|
||
_isHideValue = value;
|
||
this.ToList().ForEach(x => x.IsHideValue = value);
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Methods
|
||
|
||
/// <summary>
|
||
/// 当配方中的参数的属性发生变化时,触发此事件。
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ParamOnPropertyChanged(object sender, PropertyChangedEventArgs e)
|
||
{
|
||
if (!(sender is IParam param))
|
||
return;
|
||
|
||
if (e.PropertyName == nameof(Param.IsSaved))
|
||
{
|
||
if (param.IsSaved == false)
|
||
{
|
||
IsSaved = false;
|
||
}
|
||
else
|
||
{
|
||
IsSaved = this.ToList().FirstOrDefault(x => x.IsSaved == false) == null;
|
||
}
|
||
|
||
SavedStateChanged?.Invoke(this, IsSaved);
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 将指定的Param链接到前序Param。
|
||
/// </summary>
|
||
/// <param name="param">当前Step中的参数。</param>
|
||
private void LinkWithPreviousParam(IParam param)
|
||
{
|
||
var preParam = Previous?.FirstOrDefault(x => x.Name == param.Name);
|
||
param.Previous = preParam;
|
||
if (preParam != null)
|
||
preParam.Next = param;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将指定的Param链接到后序Param。
|
||
/// </summary>
|
||
/// <param name="param">当前Step中的参数。</param>
|
||
private void LinkWithNextParam(IParam param)
|
||
{
|
||
var nextParam = Next?.FirstOrDefault(x => x.Name == param.Name);
|
||
param.Next = nextParam;
|
||
if (nextParam != null)
|
||
nextParam.Previous = param;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 校验步骤中的所有参数。
|
||
/// </summary>
|
||
public void Validate()
|
||
{
|
||
this.ToList().ForEach(x=>x.Validate());
|
||
}
|
||
|
||
/// <summary>
|
||
/// 取消所有参数的Highlight状态。
|
||
/// </summary>
|
||
public void ResetHighlight()
|
||
{
|
||
this.ToList().ForEach(x => x.ResetHighlight());
|
||
}
|
||
|
||
/// <summary>
|
||
/// 保存当前配方步骤。
|
||
/// </summary>
|
||
public void Save()
|
||
{
|
||
this.ToList().ForEach(x=>x.Save());
|
||
}
|
||
|
||
/// <summary>
|
||
/// 設置前序步驟。
|
||
/// </summary>
|
||
/// <param name="step"></param>
|
||
public void SetPreviousStep(RecipeStep step)
|
||
{
|
||
Previous = step;
|
||
|
||
// 调整Step中Param的链接关系
|
||
this.ToList().ForEach(LinkWithPreviousParam);
|
||
|
||
if (Previous == null)
|
||
return;
|
||
|
||
// 将前序步骤的后序步骤链接到我自己。
|
||
Previous.Next = this;
|
||
}
|
||
|
||
public void SetNextStep(RecipeStep step)
|
||
{
|
||
Next = step;
|
||
this.ToList().ForEach(LinkWithNextParam);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查StepNo是否为有效值。
|
||
/// </summary>
|
||
/// <param name="stepNo"></param>
|
||
/// <param name="validatedStepNo"></param>
|
||
/// <returns></returns>
|
||
public static bool ValidateStepNo(int? stepNo, out int validatedStepNo)
|
||
{
|
||
validatedStepNo = int.MinValue;
|
||
var isValid = stepNo >= START_INDEX;
|
||
if (isValid)
|
||
validatedStepNo = stepNo.Value;
|
||
|
||
return isValid;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Override Methods
|
||
|
||
protected override void InsertItem(int index, Param item)
|
||
{
|
||
if (item != null)
|
||
{
|
||
item.Parent = this;
|
||
LinkWithPreviousParam(item);
|
||
LinkWithNextParam(item);
|
||
|
||
item.PropertyChanged += ParamOnPropertyChanged;
|
||
}
|
||
|
||
base.InsertItem(index, item);
|
||
}
|
||
|
||
protected override void SetItem(int index, Param item)
|
||
{
|
||
throw new NotSupportedException();
|
||
}
|
||
|
||
protected override void RemoveItem(int index)
|
||
{
|
||
if (Count > 0)
|
||
{
|
||
if (index >= 0 && index < Count)
|
||
{
|
||
this[index].PropertyChanged -= ParamOnPropertyChanged;
|
||
}
|
||
}
|
||
|
||
// 将前后两个Param链接起来
|
||
var preParam = this[index].Previous;
|
||
var nextParam = this[index].Next;
|
||
if (preParam != null && nextParam != null)
|
||
{
|
||
preParam.Next = nextParam;
|
||
nextParam.Previous = preParam;
|
||
}
|
||
else if (preParam == null && nextParam != null)
|
||
{
|
||
nextParam.Previous = null;
|
||
}
|
||
else if (preParam != null)
|
||
{
|
||
preParam.Next = null;
|
||
}
|
||
|
||
|
||
IsSaved = false;
|
||
SavedStateChanged?.Invoke(this, true);
|
||
|
||
base.RemoveItem(index);
|
||
}
|
||
|
||
protected override void ClearItems()
|
||
{
|
||
this.ToList().ForEach(x => x.PropertyChanged -= ParamOnPropertyChanged);
|
||
|
||
IsSaved = Count <= 0;
|
||
SavedStateChanged?.Invoke(this, true);
|
||
|
||
base.ClearItems();
|
||
}
|
||
|
||
public override string ToString()
|
||
{
|
||
return Count > 0 ? (StepNoParam.ToString()??"Recipe Step, Item={Count}" ): "Empty Recipe Step";
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
}
|