using HMI.Core.Entity;
|
using HMI.Core.Util;
|
using HMI.UI.DpUtil;
|
using HMI.UI.Util;
|
using Microsoft.Win32;
|
using NPOI.SS.Formula.Functions;
|
using NPOI.SS.Formula.PTG;
|
using Panuon.WPF.UI;
|
using System;
|
using System.Collections.Generic;
|
using System.Collections.ObjectModel;
|
using System.ComponentModel;
|
using System.Linq;
|
using System.Windows.Controls;
|
|
namespace HMI.ParameterManage
|
{
|
public class BoolData
|
{
|
public int Index { get; }
|
public bool Value { get; set; }
|
|
public BoolData(int index)
|
{
|
this.Index = index;
|
}
|
}
|
|
public class IntData
|
{
|
public int Index { get; }
|
public int Value { get; set; }
|
|
public IntData(int index)
|
{
|
this.Index = index;
|
}
|
}
|
|
public class DoubleData
|
{
|
public int Index { get; }
|
public double Value { get; set; }
|
|
public DoubleData(int index)
|
{
|
this.Index = index;
|
}
|
}
|
|
public class StringData
|
{
|
public int Index { get; }
|
public string Value { get; set; } = string.Empty;
|
|
public StringData(int index)
|
{
|
this.Index = index;
|
}
|
}
|
|
/// <summary>
|
/// ModalWindow.xaml 的交互逻辑
|
/// </summary>
|
public partial class SettingModal : WindowX
|
{
|
public SettingModal()
|
{
|
this.DataContext = this;
|
InitializeComponent();
|
}
|
|
public ParameterData Data { get; set; }
|
private ObservableCollection<ParameterData> Datasource;
|
|
private bool _isProcessing;
|
|
protected override void OnClosing(CancelEventArgs e)
|
{
|
//Press Continue Button
|
if (DialogResult == true)
|
{
|
if (_isProcessing)
|
{
|
e.Cancel = true;
|
return;
|
}
|
var okButton = WindowXModalDialog.GetOKButton(this);
|
try
|
{
|
_isProcessing = true;
|
ButtonHelper.SetIsPending(okButton, true);
|
if (!ValidateForm())
|
{
|
e.Cancel = true;
|
return;
|
}
|
|
SaveOrUpdateParameter(Data);
|
}
|
catch (Exception ex)
|
{
|
ToastHelper.Error(this, $"Error: {ex.Message}");
|
e.Cancel = true;
|
return;
|
}
|
finally
|
{
|
_isProcessing = false;
|
ButtonHelper.SetIsPending(okButton, false);
|
}
|
}
|
base.OnClosing(e);
|
}
|
|
public bool ValidateName(object value, out string err)
|
{
|
err = null;
|
var data = Datasource?.FirstOrDefault(e => e.Name == value.ToString());
|
if (data != null && data != Data)
|
{
|
err = this.lang("App.MustBeUnique", this.lang("Pm.Parameter.Name"));
|
return false;
|
}
|
return true;
|
}
|
|
private void SaveOrUpdateParameter(ParameterData data)
|
{
|
}
|
|
private bool ValidateForm()
|
{
|
bool flag = FormGroupHelper.ValidateForm(formPanel, new Dictionary<string, object> {
|
{ "FgValue",GetTargetValue()}
|
});
|
if (flag)
|
{
|
Data.Name = TbName.Text;
|
Data.Type = CbType.Text;
|
Data.IsArray = CbIsArray.IsChecked == true;
|
Data.Length = int.Parse(NiLength.Text);
|
|
Data.Value = GetTargetValue();
|
Data.Unit = TbUnit.Text;
|
Data.Description = TbDescription.Text;
|
Data.PGroup = TbGroup.Text;
|
Data.Source = TbSource.Text;
|
}
|
return flag;
|
}
|
|
public bool? Open(ParameterData data, ObservableCollection<ParameterData> datasource, List<string> groups,string productName = null)
|
{
|
if (!string.IsNullOrEmpty(productName))
|
{
|
FgProductName.Visibility = System.Windows.Visibility.Visible;
|
TbProductName.Text = productName;
|
}
|
|
Datasource = datasource;
|
Data = data;
|
TbGroup.ItemsSource = groups;
|
|
TbName.Text = Data.Name;
|
CbType.SelectedValue = Data.Type;
|
CbIsArray.IsChecked = Data.IsArray;
|
NiLength.Text = Data.Length.ToString();
|
|
UpdateWindow(Data.Type);
|
UpdateForm(Data);
|
|
TbUnit.Text = Data.Unit;
|
TbDescription.Text = Data.Description;
|
TbGroup.Text = Data.PGroup;
|
TbSource.Text = Data.Source;
|
return this.ShowDialog();
|
}
|
|
private string GetTargetValue()
|
{
|
var type = CbType.Text;
|
if (CbIsArray.IsChecked == true)
|
{
|
var arr = (object[])arrayInput.ItemsSource;
|
return string.Join(",", arr.Select(e => e.GetType().GetProperty("Value").GetValue(e).ToString()).ToArray());
|
}
|
|
if (type == "Int" || type == "Double")
|
{
|
return NiNumberValue.Text;
|
}
|
else if (type == "Bool")
|
{
|
return CbBoolValue.Text;
|
}
|
else
|
{
|
return TbStringValue.Text;
|
}
|
}
|
|
private void UpdateForm(ParameterData data)
|
{
|
if (data.IsArray)
|
{
|
string[] arr = data.Value.Split(',');
|
arrayInput.ItemsSource = RefreshArray(arr, Data.Type);
|
}
|
else
|
{
|
if (Data.Type == "Int" || Data.Type == "Int")
|
{
|
NiNumberValue.Text = Data.Value;
|
}
|
else if (Data.Type == "Bool")
|
{
|
CbBoolValue.SelectedValue = Data.Value;
|
}
|
else
|
{
|
TbStringValue.Text = Data.Value;
|
}
|
}
|
}
|
|
private void UpdateWindow(string type)
|
{
|
if (type == "Int" || type == "Double")
|
{
|
NiNumberValue.Visibility = System.Windows.Visibility.Visible;
|
StringValueGrid.Visibility = System.Windows.Visibility.Collapsed;
|
CbBoolValue.Visibility = System.Windows.Visibility.Collapsed;
|
}
|
else if (type == "Bool")
|
{
|
NiNumberValue.Visibility = System.Windows.Visibility.Collapsed;
|
StringValueGrid.Visibility = System.Windows.Visibility.Collapsed;
|
CbBoolValue.Visibility = System.Windows.Visibility.Visible;
|
}
|
else
|
{
|
NiNumberValue.Visibility = System.Windows.Visibility.Collapsed;
|
StringValueGrid.Visibility = System.Windows.Visibility.Visible;
|
CbBoolValue.Visibility = System.Windows.Visibility.Collapsed;
|
}
|
}
|
|
private void CbType_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
|
{
|
string val = null;
|
if (e.AddedItems != null && e.AddedItems.Count > 0)
|
{
|
val = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
|
}
|
UpdateWindow(val);
|
if (CbIsArray.IsChecked == true)
|
{
|
arrayInput.ItemsSource = RefreshArray(GetLength(), val);
|
}
|
}
|
|
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
|
{
|
OpenFileDialog ofd = new OpenFileDialog();
|
if (ofd.ShowDialog() == true)
|
{
|
TbStringValue.Text = ofd.FileName;
|
}
|
}
|
|
private void CbIsArray_Checked(object sender, System.Windows.RoutedEventArgs e)
|
{
|
bool isChecked = ((CheckBox)sender).IsChecked == true;
|
if (isChecked)
|
{
|
FgLength.Visibility = System.Windows.Visibility.Visible;
|
arrayInput.Visibility = System.Windows.Visibility.Visible;
|
arrayInput.ItemsSource = RefreshArray(GetLength(), CbType.Text);
|
}
|
else
|
{
|
FgLength.Visibility = System.Windows.Visibility.Collapsed;
|
arrayInput.Visibility = System.Windows.Visibility.Collapsed;
|
}
|
}
|
|
private object[] RefreshArray(int length, string type)
|
{
|
var arr = new object[length];
|
for (int i = 0; i < length; i++)
|
{
|
if (type == "Int")
|
{
|
arr[i] = new IntData(i);
|
}
|
else if (type == "Double")
|
{
|
arr[i] = new DoubleData(i);
|
}
|
else if (type == "Bool")
|
{
|
arr[i] = new BoolData(i);
|
}
|
else
|
{
|
arr[i] = new StringData(i);
|
}
|
}
|
return arr;
|
}
|
|
private object[] RefreshArray(string[] source, string type)
|
{
|
var arr = new object[source.Length];
|
type = string.IsNullOrEmpty(type) ? "String" : type;
|
for (int i = 0; i < arr.Length; i++)
|
{
|
if (type == "Int")
|
{
|
arr[i] = new IntData(i)
|
{
|
Value = int.Parse(source[i])
|
};
|
}
|
else if (type == "Double")
|
{
|
arr[i] = new DoubleData(i)
|
{
|
Value = double.Parse(source[i])
|
};
|
}
|
else if (type == "Bool")
|
{
|
arr[i] = new BoolData(i)
|
{
|
Value = bool.Parse(source[i])
|
};
|
}
|
else
|
{
|
arr[i] = new StringData(i)
|
{
|
Value = source[i]
|
};
|
}
|
}
|
return arr;
|
}
|
|
private int GetLength()
|
{
|
try
|
{
|
string text = NiLength.Text;
|
if (text.Contains("."))
|
{
|
text = text.Split('.')[0];
|
}
|
int length = int.Parse(text);
|
return length > 0 ? length : 1;
|
}
|
catch
|
{
|
return 1;
|
}
|
}
|
|
private void NiLength_TextChanged(object sender, TextChangedEventArgs e)
|
{
|
if (CbIsArray.IsChecked != true)
|
{
|
return;
|
}
|
arrayInput.ItemsSource = RefreshArray(GetLength(), CbType.Text);
|
}
|
}
|
}
|