MOSource.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using Aitex.Core.RT.Log;
  15. using System.Xml;
  16. using Aitex.Core.UI.ControlDataContext;
  17. using System.Collections.ObjectModel;
  18. namespace Aitex.Core.UI.Control
  19. {
  20. /// <summary>
  21. /// Interaction logic for MOSource.xaml
  22. /// </summary>
  23. public partial class MOSource : UserControl
  24. {
  25. string _moConfig;
  26. public MOSource()
  27. {
  28. InitializeComponent();
  29. }
  30. public static readonly DependencyProperty MOSourceDataListProperty = DependencyProperty.Register(
  31. "MOSourceDataList", typeof(ObservableCollection<MOSourceDataItem>), typeof(MOSource),
  32. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  33. public ObservableCollection<MOSourceDataItem> MOSourceDataList
  34. {
  35. get
  36. {
  37. return (ObservableCollection<MOSourceDataItem>)this.GetValue(MOSourceDataListProperty);
  38. }
  39. set
  40. {
  41. this.SetValue(MOSourceDataListProperty, value);
  42. }
  43. }
  44. protected override void OnRender(DrawingContext drawingContext)
  45. {
  46. base.OnRender(drawingContext);
  47. UpdateMOLayout(_moConfig);
  48. }
  49. public void SetMOLayout(string moConfig)
  50. {
  51. _moConfig = moConfig;
  52. }
  53. void UpdateMOLayout(string moConfig)
  54. {
  55. double left = 0;
  56. double width = this.Width;
  57. double intervalWidth = 0;
  58. int splitBath = -1;
  59. double bathWidth = 0;
  60. int objIndex = 0;
  61. UserControl bathUc;
  62. double bathLeft = -1;
  63. int bathIntervalCount = 0;
  64. ObservableCollection<UILayoutDataItem> obj = new ObservableCollection<UILayoutDataItem>();
  65. if (rootCanvas.Children.Count > 0)
  66. {
  67. return;
  68. }
  69. if (string.IsNullOrEmpty(moConfig))
  70. {
  71. LOG.Error("MO源配置文件为空");
  72. return;
  73. }
  74. //index, name, type
  75. List<Tuple<int, string, string,int>> items = new List<Tuple<int, string, string,int>>();
  76. XmlDocument doc = new XmlDocument();
  77. doc.LoadXml(moConfig);
  78. XmlNodeList lstNode = doc.SelectNodes("MOSourceConfig/MOSources/MOSourceItem");
  79. foreach (XmlNode node in lstNode)
  80. {
  81. try
  82. {
  83. int index = int.Parse(node.Attributes["DisplayIndex"].Value) - 1;
  84. string name = node.Attributes["Name"].Value;
  85. string type = node.Attributes["Type"].Value;
  86. int bathindex = int.Parse(node.Attributes["BathIndex"].Value) - 1;
  87. if (!string.IsNullOrEmpty(name))
  88. {
  89. items.Add(new Tuple<int, string, string,int>(index, name, type,bathindex));
  90. }
  91. }
  92. catch (Exception ex)
  93. {
  94. LOG.Error("MO源配置项错误," + node.Name, ex);
  95. }
  96. }
  97. items.Sort((p1, p2) => p1.Item1.CompareTo(p2.Item1));
  98. foreach (var item in items)
  99. {
  100. UserControl uc;
  101. switch (item.Item3)
  102. {
  103. case "WithDilute": uc = new MODilute(); break;
  104. case "WithPush": uc = new MOPush(); break;
  105. case "General": uc = new MOGeneral();break;
  106. default:
  107. uc = new MOGeneral(); break;
  108. }
  109. obj.Add(new UILayoutDataItem());
  110. obj[objIndex].uc = uc;
  111. obj[objIndex].index = item.Item1;
  112. if (splitBath == -1 && item.Item4!=-1) //第一个Bath,如果bathIndex为-1,表示没有水槽
  113. {
  114. bathWidth = uc.Width;
  115. obj[objIndex].bathWidth = bathWidth;
  116. }
  117. else if ((item.Item4 == splitBath) && (item.Item4 != -1)) //一个Bath对应多个MO源
  118. {
  119. bathWidth += uc.Width;
  120. if (obj.Count>1)
  121. obj[objIndex-1].bathWidth = 0;
  122. obj[objIndex].bathWidth = bathWidth;
  123. }
  124. else if (item.Item4 != -1) //一个Bath只有一个对应的MO源
  125. {
  126. bathWidth = uc.Width;
  127. obj[objIndex].bathWidth = bathWidth;
  128. }
  129. else //Bath没有对应的MO源
  130. {
  131. bathWidth = 0;
  132. obj[objIndex].bathWidth = -1;
  133. }
  134. objIndex++;
  135. splitBath = item.Item4;
  136. width -= uc.Width;
  137. }
  138. intervalWidth = width / items.Count;
  139. foreach (var item in obj)
  140. {
  141. //Mo layout
  142. UserControl uc = item.uc as UserControl;
  143. Canvas.SetLeft(uc, left);
  144. uc.DataContext = MOSourceDataList[item.index];
  145. rootCanvas.Children.Add(uc);
  146. bathIntervalCount++;
  147. //bath layout
  148. if(item.bathWidth != -1)
  149. {
  150. if(bathLeft == -1)
  151. { bathLeft = left; }
  152. if(item.bathWidth>0)
  153. {
  154. bathUc = new Bath();
  155. bathUc.Width = item.bathWidth + ((bathIntervalCount-1) * intervalWidth);
  156. Canvas.SetLeft(bathUc, bathLeft);
  157. Canvas.SetTop(bathUc, 220);
  158. bathUc.SetBinding(Bath.DeviceDataProperty, new Binding("BathData") { Source = MOSourceDataList[item.index] });
  159. //bathUc.DataContext = MOSourceDataList[item.index - 1];
  160. rootCanvas.Children.Add(bathUc);
  161. bathLeft = -1;
  162. bathIntervalCount = 0;
  163. }
  164. }
  165. left += uc.Width + intervalWidth;
  166. }
  167. }
  168. public class UILayoutDataItem
  169. {
  170. public int index;
  171. public UserControl uc;
  172. public double bathWidth;
  173. }
  174. }
  175. }