Sic04/SicUI/Controls/AnalogControl2.xaml.cs

401 lines
14 KiB
C#
Raw Normal View History

2022-09-19 09:16:33 +08:00
using Aitex.Core.Common.DeviceData;
using Aitex.Core.UI.ControlDataContext;
using Caliburn.Micro;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.UI.Client.Ctrlib.UnitControls;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace SicUI.Controls
{
/// <summary>
/// AnalogControl2.xaml 的交互逻辑
/// </summary>
public partial class AnalogControl2 : UserControl
{
public AnalogControl2()
{
InitializeComponent();
// LabelSet = "0.0";
2022-09-19 09:16:33 +08:00
}
public event Action<string, string> clickAct;
2022-09-19 09:16:33 +08:00
// define dependency properties
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
"Command", typeof(ICommand), typeof(AnalogControl2),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
2022-09-19 09:16:33 +08:00
public static readonly DependencyProperty DeviceDataProperty = DependencyProperty.Register(
"DeviceData", typeof(AITMfcData), typeof(AnalogControl2),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender,
OnDeviceDataChanged));
2022-09-19 09:16:33 +08:00
public static readonly DependencyProperty OperationNameProperty = DependencyProperty.Register(
"OperationName", typeof(string), typeof(AnalogControl2),
new FrameworkPropertyMetadata(AnalogDeviceOperation.Ramp, FrameworkPropertyMetadataOptions.AffectsRender));
2022-09-19 09:16:33 +08:00
public static readonly DependencyProperty BackColorProperty = DependencyProperty.Register(
"BackColor", typeof(Brush), typeof(AnalogControl2),
new FrameworkPropertyMetadata(Brushes.Green, FrameworkPropertyMetadataOptions.AffectsRender));
2022-09-19 09:16:33 +08:00
public static readonly DependencyProperty HideDialogProperty = DependencyProperty.Register(
"HideDialog", typeof(bool), typeof(AnalogControl2),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
2022-09-19 09:16:33 +08:00
public static readonly DependencyProperty LabelSetProperty = DependencyProperty.Register(
"LabelSet", typeof(object), typeof(AnalogControl2),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
2022-09-19 09:16:33 +08:00
private static void OnDeviceDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
public static readonly DependencyProperty HasPermissionProperty = DependencyProperty.Register(
nameof(HasPermission), typeof(bool), typeof(AnalogControl2), new PropertyMetadata(default(bool)));
/// <summary>
/// 设置或返回是否有权限查看实际值。
/// <para>该属性通常根据权限进行配置。</para>
/// </summary>
public bool HasPermission
{
get => (bool)GetValue(HasPermissionProperty);
set => SetValue(HasPermissionProperty, value);
}
2022-09-19 09:16:33 +08:00
/// <summary>
/// 输入值是否百分比,默认否
/// </summary>
public bool IsPercent { get; set; }
public ICommand Command
{
get => (ICommand)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
2022-09-19 09:16:33 +08:00
}
/// <summary>
/// set, get current progress value AnalogDeviceData
/// </summary>
public AITMfcData DeviceData
{
get => (AITMfcData)GetValue(DeviceDataProperty);
set => SetValue(DeviceDataProperty, value);
2022-09-19 09:16:33 +08:00
}
public Brush BackColor
{
get => (Brush)GetValue(BackColorProperty);
set => SetValue(BackColorProperty, value);
2022-09-19 09:16:33 +08:00
}
public string OperationName
{
get => (string)GetValue(OperationNameProperty);
set => SetValue(OperationNameProperty, value);
2022-09-19 09:16:33 +08:00
}
public bool HideDialog
{
get => (bool)GetValue(HideDialogProperty);
set => SetValue(HideDialogProperty, value);
2022-09-19 09:16:33 +08:00
}
2022-09-19 09:16:33 +08:00
public object LabelSet
{
get => (object)GetValue(LabelSetProperty);
set => SetValue(LabelSetProperty, value);
2022-09-19 09:16:33 +08:00
}
2022-09-19 09:16:33 +08:00
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
//draw background color
rectBkground.Fill = BackColor;
if (DeviceData != null)
{
var format = "0.0";
2022-09-19 09:16:33 +08:00
#region FeedBack
//draw red board if mfc meets a warning
rectBkground.Stroke = DeviceData.IsWarning ? Brushes.Red : Brushes.Gray;
//draw reading value
var feedback = IsPercent ? DeviceData.FeedBack * 100 : DeviceData.FeedBack;
if (HasPermission == false)
labelValue.Text = feedback <= 0.1 ? "-" : "***"; // 没流量时显示为“-”
2022-09-19 09:16:33 +08:00
else
labelValue.Text = feedback.ToString(format);
2022-09-19 09:16:33 +08:00
labelValue.Foreground = DeviceData.IsWarning ? Brushes.Pink : Brushes.LightYellow;
rectBkground.StrokeThickness = DeviceData.IsWarning ? 2 : 1;
if (dialogBox != null)
{
dialogBox.DeviceData = DeviceData;
dialogBox.DeviceData.InvokePropertyChanged();
}
#endregion
#region SetPoint
//draw red board if mfc meets a warning
rectSetPoint.Stroke = DeviceData.IsWarning ? Brushes.Red : Brushes.Gray;
//draw reading value
var sp = IsPercent ? DeviceData.SetPoint * 100 : DeviceData.SetPoint;
if (HasPermission == false)
labelSetPoint.Text = "***";
2022-09-19 09:16:33 +08:00
else
labelSetPoint.Text = feedback.ToString(format);
2022-09-19 09:16:33 +08:00
//VerticalContentAlignment = "Center" HorizontalContentAlignment = "Center"
labelSetPoint.Foreground = DeviceData.IsWarning ? Brushes.Pink : Brushes.LightYellow;
rectSetPoint.StrokeThickness = DeviceData.IsWarning ? 2 : 1;
#endregion
}
}
private MfcSettingDialogViewModel dialogBox;
public Window AnalogOwner { get; set; }
private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (HasPermission == false)
return;
if (DeviceData == null)
return;
if (HideDialog)
return;
2022-09-19 09:16:33 +08:00
if (DeviceData.ActMode != (double)MfcCtrlMode.Normal)
{
MessageBox.Show("Not normal mode,Click mouse right buttom to change!");
return;
}
//dialogBox = new InputDialogBox
//{
// CommandDelegate = Execute,
// DeviceName = string.Format("{0}: {1}", DeviceData.Type, DeviceData.DisplayName),
// DeviceId = DeviceData.DeviceSchematicId,
// DefaultValue = DeviceData.DefaultValue,
// RealValue = DeviceData.FeedBack.ToString("F1"),
// SetPoint = Math.Round(DeviceData.SetPoint, 1),
// MaxValue = DeviceData.Scale,
// Unit = DeviceData.Unit,
//};
//dialogBox.IsPercent = IsPercent;
//if (IsPercent)
// dialogBox.SetPoint = Math.Round(DeviceData.SetPoint * 100.0, 1);
//if (AnalogOwner != null)
// dialogBox.Owner = AnalogOwner;
//dialogBox.Topmost = true;
//dialogBox.WindowStartupLocation = WindowStartupLocation.CenterScreen;
//dialogBox.FocasAll();
//dialogBox.ShowDialog();
//dialogBox = null;
dialogBox = new MfcSettingDialogViewModel($"MFC {DeviceData.DisplayName} Setting");
dialogBox.DeviceData = DeviceData;
dialogBox.InputSetPoint = DeviceData.SetPoint.ToString("F1");
var wm = new WindowManager();
2022-09-19 09:16:33 +08:00
var owner = Application.Current.MainWindow;
2022-09-19 09:16:33 +08:00
if (owner != null)
{
Mouse.Capture(owner);
var pointToWindow = Mouse.GetPosition(owner);
var pointToScreen = owner.PointToScreen(pointToWindow);
2022-09-19 09:16:33 +08:00
pointToScreen.X = pointToScreen.X + 50;
pointToScreen.Y = pointToScreen.Y - 150;
Mouse.Capture(null);
//wm.ShowDialog(dialogBox, pointToScreen);
wm.ShowDialog(dialogBox);
}
else
{
wm.ShowDialog(dialogBox);
}
}
#region MouseRightButton
2022-09-19 09:16:33 +08:00
private void Grid_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
if (HasPermission == false)
return;
var mouseClickMenu = new ContextMenu();
var item = new MenuItem
{
Header = "_" + DeviceData.DeviceName,
IsEnabled = false
};
2022-09-19 09:16:33 +08:00
mouseClickMenu.Items.Add(item);
addNormalMenu(mouseClickMenu, item);
addCloseMenu(mouseClickMenu, item);
addOpenMenu(mouseClickMenu, item);
addHoldMenu(mouseClickMenu, item);
mouseClickMenu.IsOpen = true;
}
void addNormalMenu(ContextMenu mouseClickMenu, MenuItem item)
{
item = new MenuItem();
item.Header = "Normal";
item.Click += NormalMode;
item.Tag = Tag;
2022-09-19 09:16:33 +08:00
mouseClickMenu.Items.Add(item);
}
private void NormalMode(object sender, RoutedEventArgs e)
{
SetNormalMode(MfcCtrlMode.Normal);
}
void addCloseMenu(ContextMenu mouseClickMenu, MenuItem item)
{
item = new MenuItem();
item.Header = "Close";
item.Click += CloseMode;
item.Tag = Tag;
2022-09-19 09:16:33 +08:00
mouseClickMenu.Items.Add(item);
}
private void CloseMode(object sender, RoutedEventArgs e)
{
SetCloseMode(MfcCtrlMode.Close);
}
void addOpenMenu(ContextMenu mouseClickMenu, MenuItem item)
{
item = new MenuItem();
item.Header = "Open";
item.Click += OpenMode;
item.Tag = Tag;
2022-09-19 09:16:33 +08:00
mouseClickMenu.Items.Add(item);
}
private void OpenMode(object sender, RoutedEventArgs e)
{
SetOpenMode(MfcCtrlMode.Open);
}
void addHoldMenu(ContextMenu mouseClickMenu, MenuItem item)
{
item = new MenuItem();
item.Header = "Hold";
item.Click += HoldMode;
item.Tag = Tag;
2022-09-19 09:16:33 +08:00
mouseClickMenu.Items.Add(item);
}
private void HoldMode(object sender, RoutedEventArgs e)
{
SetHoldMode(MfcCtrlMode.Hold);
}
private void SetNormalMode(MfcCtrlMode value)
{
InvokeClient.Instance.Service.DoOperation($"{DeviceData.Module}.{DeviceData.DeviceName}.SetMode",
value.ToString());
2022-09-19 09:16:33 +08:00
}
private void SetCloseMode(MfcCtrlMode value)
{
InvokeClient.Instance.Service.DoOperation($"{DeviceData.Module}.{DeviceData.DeviceName}.SetMode",
value.ToString());
2022-09-19 09:16:33 +08:00
}
private void SetOpenMode(MfcCtrlMode value)
{
InvokeClient.Instance.Service.DoOperation($"{DeviceData.Module}.{DeviceData.DeviceName}.SetMode",
value.ToString());
2022-09-19 09:16:33 +08:00
}
private void SetHoldMode(MfcCtrlMode value)
{
InvokeClient.Instance.Service.DoOperation($"{DeviceData.Module}.{DeviceData.DeviceName}.SetMode",
value.ToString());
2022-09-19 09:16:33 +08:00
}
2022-09-19 09:16:33 +08:00
#endregion
private void Execute(double value)
{
Command.Execute(new object[] { DeviceData.UniqueName, OperationName, value });
2022-09-19 09:16:33 +08:00
}
private void Grid_MouseEnter(object sender, MouseEventArgs e)
{
if (HasPermission == false)
{
ToolTip = null;
return;
}
var format = "0.0";
2022-09-19 09:16:33 +08:00
if (DeviceData != null)
{
var tooltipValue =
string.Format(
"{0}{1}\r\n\r\nID{2}\r\nScale{3} {4}\r\nSetPoint{5} {4} \r\nFeedback{6} {4}\r\nTolerance{7}%\r\nStatus{8}",
2022-09-19 09:16:33 +08:00
DeviceData.Type,
DeviceData.DisplayName,
DeviceData.DeviceSchematicId,
DeviceData.Scale,
DeviceData.Unit,
IsPercent
? (DeviceData.SetPoint * 100).ToString(format) + "%"
: DeviceData.SetPoint.ToString(format),
IsPercent
? (DeviceData.FeedBack * 100).ToString(format) + "%"
: DeviceData.FeedBack.ToString(format),
DeviceData.Scale > 0
? ((DeviceData.FeedBack - DeviceData.SetPoint) / DeviceData.Scale * 100).ToString(format)
: "0",
DeviceData.IsWarning ? "Tolerance Warning" : "Normal");
2022-09-19 09:16:33 +08:00
ToolTip = tooltipValue;
}
if (DeviceData != null)
{
var tooltipValue =
2022-09-19 09:16:33 +08:00
$"{DeviceData.Type}{DeviceData.DisplayName}\r\n\r\nID{DeviceData.DeviceSchematicId}\r\nScale{DeviceData.Scale} {DeviceData.Unit}\r\nSetPoint{DeviceData.SetPoint.ToString(format)} {DeviceData.Unit} \r\nFeedback{DeviceData.FeedBack.ToString(format)} {DeviceData.Unit}";
ToolTip = tooltipValue;
}
}
}
}