ValveControl.xaml.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using Aitex.Core.Common.DeviceData;
  2. using MECF.Framework.Common.OperationCenter;
  3. using MECF.Framework.UI.Core.Control;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace Aitex.Core.UI.Control
  19. {
  20. /// <summary>
  21. /// ValveControl.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class ValveControl : UserControl
  24. {
  25. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  26. "Command", typeof(ICommand), typeof(ValveControl),
  27. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  28. public ICommand Command
  29. {
  30. get
  31. {
  32. return (ICommand)this.GetValue(CommandProperty);
  33. }
  34. set
  35. {
  36. this.SetValue(CommandProperty, value);
  37. }
  38. }
  39. public Window AITGasOwner { get; set; }
  40. private SwitchDialog dialog;
  41. public bool IsVirtualSwitchOpen { get; set; }
  42. public bool IsEnableMouseRight
  43. {
  44. get { return (bool)GetValue(IsEnableMouseRightProperty); }
  45. set { SetValue(IsEnableMouseRightProperty, value); }
  46. }
  47. // Using a DependencyProperty as the backing store for IsEnableMouseRight. This enables animation, styling, binding, etc...
  48. public static readonly DependencyProperty IsEnableMouseRightProperty =
  49. DependencyProperty.Register("IsEnableMouseRight", typeof(bool), typeof(ValveControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  50. public bool IsOpen
  51. {
  52. get
  53. {
  54. return (bool)this.GetValue(IsOpenProperty);
  55. }
  56. set
  57. {
  58. this.SetValue(IsOpenProperty, value);
  59. }
  60. }
  61. public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register(
  62. "IsOpen", typeof(bool), typeof(ValveControl),
  63. new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  64. public static readonly DependencyProperty EnableServiceControlProperty = DependencyProperty.Register(
  65. "EnableServiceControl", typeof(bool), typeof(ValveControl),
  66. new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  67. public bool EnableServiceControl
  68. {
  69. get
  70. {
  71. return (bool)this.GetValue(EnableServiceControlProperty);
  72. }
  73. set
  74. {
  75. this.SetValue(EnableServiceControlProperty, value);
  76. }
  77. }
  78. public static readonly DependencyProperty DeviceDataProperty = DependencyProperty.Register(
  79. "DeviceData", typeof(AITValveData), typeof(ValveControl),
  80. new FrameworkPropertyMetadata(new AITValveData(), FrameworkPropertyMetadataOptions.AffectsRender));
  81. public AITValveData DeviceData
  82. {
  83. get
  84. {
  85. var data = (AITValveData)this.GetValue(DeviceDataProperty);
  86. return data;
  87. }
  88. set
  89. {
  90. this.SetValue(DeviceDataProperty, value);
  91. }
  92. }
  93. private int _rotationAngle;
  94. public int RotationAngle
  95. {
  96. get => _rotationAngle;
  97. set
  98. {
  99. _rotationAngle = value;
  100. canvasRotate.Angle = _rotationAngle;
  101. }
  102. }
  103. public ValveControl()
  104. {
  105. InitializeComponent();
  106. IsEnableMouseRight = false;
  107. }
  108. protected override void OnRender(DrawingContext drawingContext)
  109. {
  110. base.OnRender(drawingContext);
  111. if (IsOpen)
  112. {
  113. imgClose.Visibility = Visibility.Hidden;
  114. imgOpen.Visibility = Visibility.Visible;
  115. }
  116. else
  117. {
  118. imgClose.Visibility = Visibility.Visible;
  119. imgOpen.Visibility = Visibility.Hidden;
  120. }
  121. }
  122. /// <summary>
  123. /// open
  124. /// </summary>
  125. /// <param name="sender"></param>
  126. /// <param name="e"></param>
  127. private void TurnOnValve(object sender, RoutedEventArgs e)
  128. {
  129. if (EnableServiceControl)
  130. {
  131. IsOpen = true;
  132. InvokeClient.Instance.Service.DoOperation($"{DeviceData.UniqueName}.{AITValveOperation.GVTurnValve}", true);
  133. this.UpdateLayout();
  134. }
  135. }
  136. /// <summary>
  137. /// close
  138. /// </summary>
  139. /// <param name="sender"></param>
  140. /// <param name="e"></param>
  141. private void TurnOffValve(object sender, RoutedEventArgs e)
  142. {
  143. IsOpen = false;
  144. InvokeClient.Instance.Service.DoOperation($"{DeviceData.UniqueName}.{AITValveOperation.GVTurnValve}", false);
  145. this.UpdateLayout();
  146. }
  147. private void nopass_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  148. {
  149. if (DeviceData == null || string.IsNullOrEmpty(DeviceData.DeviceName))
  150. return;
  151. ContextMenu mouseClickMenu = new ContextMenu();
  152. MenuItem item = new MenuItem();
  153. item.Header = "_" + DeviceData.DisplayName;
  154. item.Background = Brushes.Gray;
  155. item.Foreground = Brushes.White;
  156. item.IsEnabled = false;
  157. mouseClickMenu.Items.Add(item);
  158. addOpenMenu(mouseClickMenu, item, false);
  159. addCloseMenu(mouseClickMenu, item, false);
  160. mouseClickMenu.IsOpen = true;
  161. }
  162. private void passtrigle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  163. {
  164. if (DeviceData == null || string.IsNullOrEmpty(DeviceData.DeviceName))
  165. return;
  166. ContextMenu mouseClickMenu = new ContextMenu();
  167. MenuItem item = new MenuItem();
  168. item.Header = "_" + DeviceData.DeviceName;
  169. item.Background = Brushes.Gray;
  170. item.Foreground = Brushes.White;
  171. item.IsEnabled = false;
  172. mouseClickMenu.Items.Add(item);
  173. addCloseMenu(mouseClickMenu, item, true);
  174. addOpenMenu(mouseClickMenu, item, true);
  175. mouseClickMenu.IsOpen = true;
  176. }
  177. void addOpenMenu(ContextMenu mouseClickMenu, MenuItem item, bool isEnable)
  178. {
  179. item = new MenuItem();
  180. item.Header = "打开 (_O)";
  181. item.Click += TurnOnValve;
  182. item.Tag = this.Tag;
  183. item.IsEnabled = isEnable;
  184. mouseClickMenu.Items.Add(item);
  185. }
  186. void addCloseMenu(ContextMenu mouseClickMenu, MenuItem item, bool isEnable)
  187. {
  188. item = new MenuItem();
  189. item.Header = "关闭 (_C)";
  190. item.Tag = this.Tag;
  191. item.Click += TurnOffValve;
  192. item.IsEnabled = isEnable;
  193. mouseClickMenu.Items.Add(item);
  194. }
  195. private void imgValve_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  196. {
  197. if (!IsEnableMouseRight)
  198. { return; }
  199. if (EnableServiceControl)
  200. {
  201. passtrigle_MouseLeftButtonDown(null, null);
  202. }
  203. else
  204. {
  205. nopass_MouseLeftButtonDown(null, null);
  206. }
  207. }
  208. private void imgValve_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  209. {
  210. if (!IsEnableMouseRight)
  211. { return; }
  212. dialog = new SwitchDialog { };
  213. dialog.IsOpen = EnableServiceControl ? IsOpen : IsVirtualSwitchOpen;
  214. dialog.Owner = AITGasOwner;
  215. dialog.Topmost = true;
  216. //dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  217. dialog.DeviceName = "Valve Name: " + DeviceData.DeviceName.Replace("Valve", "");
  218. //var point = this.PointFromScreen(new Point(0, 0));
  219. var point = System.Windows.Forms.Control.MousePosition;
  220. double x = SystemParameters.WorkArea.Width;
  221. double y = SystemParameters.WorkArea.Height;
  222. if (point.Y + this.ActualHeight + 15 + dialog.Height < y)
  223. {
  224. dialog.Top = point.Y + dialog.ActualHeight + 15;
  225. }
  226. else
  227. {
  228. dialog.Top = point.Y - dialog.Height - 15;
  229. }
  230. if (point.X + dialog.Width < x)
  231. {
  232. dialog.Left = point.X;
  233. }
  234. else
  235. {
  236. dialog.Left = point.X - (dialog.Width - this.ActualWidth);
  237. }
  238. //Point centerP = new Point(dialog.Left + 100, dialog.Top + 100);
  239. //MoveMouseToPoint(centerP);
  240. dialog.ShowDialog();
  241. if ((bool)dialog.IsSave)
  242. {
  243. if (EnableServiceControl)
  244. {
  245. IsOpen = dialog.IsOpen;
  246. InvokeClient.Instance.Service.DoOperation($"{DeviceData.UniqueName}.{AITValveOperation.GVTurnValve}", dialog.IsOpen);
  247. return;
  248. }
  249. // IsVirtualSwitchOpen = dialog.IsOpen;
  250. if (Command == null)
  251. return;
  252. //Command.Execute(new object[] { DeviceData.UniqueName, AITValveOperation.GVVirtualTurnValve, dialog.IsOpen });
  253. //if (DeviceData2 != null && !string.IsNullOrEmpty(DeviceData2.DeviceName))
  254. //{
  255. // Command.Execute(new object[] { DeviceData2.DeviceName, AITValveOperation.GVTurnValve, !dialog.IsOpen });
  256. //}
  257. }
  258. }
  259. }
  260. }