I2CDataPage.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using Uestc.Auto6.Dso.Core;
  12. using Uestc.Auto6.Dso.Core.Decode;
  13. using Uni_Trend.MSO7000X.Common.Helper;
  14. using Uni_Trend.MSO7000X.UserControls;
  15. using Uni_Trend.MSO7000X.UserControls.Style;
  16. using Uni_Trend.MSO7000X.Language;
  17. namespace Uestc.Auto6.Dso.Protocol.I2C
  18. {
  19. public partial class I2CDataPage : UserControl, ITriggerSerialView
  20. {
  21. #region 属性
  22. private FloatForm _FloatForm;
  23. private Int32 _RowHeight = 56;
  24. private Int32 _ColnumCount = 2;
  25. public I2CTrigSerialPrsnt Presenter { get; set; }
  26. ITriggerPrsnt IView<ITriggerPrsnt>.Presenter
  27. {
  28. get => Presenter;
  29. set => Presenter = (I2CTrigSerialPrsnt)value;
  30. }
  31. protected new Boolean DesignMode
  32. {
  33. get
  34. {
  35. Boolean rtnflag = false;
  36. #if DEBUG
  37. rtnflag = DesignTimeHelper.InDesignMode(this);
  38. #endif
  39. return rtnflag;
  40. }
  41. }
  42. #endregion 属性
  43. public I2CDataPage()
  44. {
  45. InitializeComponent();
  46. Controls.Cast<Control>().ToList().ForEach(x => StyleManager.Instance.RegisterControl(x));
  47. StyleManager.Instance.RegisterControl(this);
  48. }
  49. protected override void OnLoad(EventArgs e)
  50. {
  51. base.OnLoad(e);
  52. InitView();
  53. }
  54. private void InitView()
  55. {
  56. Int32 rowscount = 1;
  57. this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute<RowColAttribute>() != null && x.GetValue(this) is Control).Select(x =>
  58. {
  59. RowColAttribute attribute = x.GetCustomAttribute<RowColAttribute>();
  60. Control control = x.GetValue(this) as Control;
  61. return (attribute, control);
  62. }).GroupBy(x => x.attribute, (x => x.control)).ToList().ForEach(x =>
  63. {
  64. Control lable = x.First(x => x is UestcLabel);
  65. Control control = x.First(x => !(x is UestcLabel));
  66. lable.Left = Width / _ColnumCount * x.Key.Colnum;
  67. lable.Width = control.Width;
  68. lable.Height = lable.Font.Height;
  69. lable.Top = 10 + _RowHeight * x.Key.Row;
  70. control.Left = lable.Left;
  71. control.Top = lable.Bottom + 2;
  72. lable.Width = 120;
  73. control.Width = 120;
  74. rowscount = Math.Max(rowscount, x.Key.Row + 1);
  75. });
  76. Height = rowscount * _RowHeight;
  77. SetDataBytesCount();
  78. SetData();
  79. SetDataByteIndex();
  80. InitRelation();
  81. }
  82. private void InitRelation()
  83. {
  84. this.CbxRelation.DataSource = Presenter.Relation.GetEnumList();
  85. this.CbxRelation.ValueMember = "Value";
  86. this.CbxRelation.DisplayMember = "Key";
  87. this.CbxRelation.SelectedValueChanged += (_, _) =>
  88. {
  89. Presenter.Relation = (ComModel.PulseCondition) this.CbxRelation.SelectedValue;
  90. };
  91. this.CbxRelation.SelectedValue = Presenter.Relation;
  92. }
  93. public void UpdateView(String propertyName)
  94. {
  95. if(String.IsNullOrEmpty(propertyName))
  96. {
  97. UpdateView();
  98. return;
  99. }
  100. this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x =>
  101. {
  102. UpdatePropertyAttribute attribute = x.GetCustomAttribute<UpdatePropertyAttribute>();
  103. return attribute != null && attribute.PropertyName == propertyName;
  104. }).FirstOrDefault()?.Invoke(this, null);
  105. }
  106. private void UpdateView()
  107. {
  108. this.GetType().GetMethods().Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
  109. this.Refresh();
  110. }
  111. #region UpdateFunction
  112. [UpdateProperty(nameof(I2CTrigSerialPrsnt.DataBytesCount))]
  113. private void SetDataBytesCount()
  114. {
  115. this.CbxDataBytesCount.Text = Presenter.DataBytesCount.ToString();
  116. if (Presenter.DataByteIndex >= Presenter.DataBytesCount) Presenter.DataByteIndex = Presenter.DataBytesCount - 1;
  117. this.CbxDataBytesCount.Click += (_, _) =>
  118. {
  119. Boolean lastflag = true;
  120. if (this.FindForm() is FloatForm form)
  121. {
  122. lastflag = form.CanClose;
  123. form.CanClose = false;
  124. }
  125. if (_FloatForm != null && !_FloatForm.IsDisposed)
  126. _FloatForm?.Close();
  127. _FloatForm = new FloatForm();
  128. _FloatForm.BackColor = BackColor;
  129. _FloatForm.ForeColor = ForeColor;
  130. _FloatForm.Title = "设置位宽";
  131. _FloatForm.Width = 380;
  132. _FloatForm.Height = 500 + _FloatForm.HeadHeight;
  133. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  134. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
  135. numberKeyboard.Top = _FloatForm.HeadHeight;
  136. numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight;
  137. numberKeyboard.Width = _FloatForm.Width;
  138. numberKeyboard.ForeColor = ForeColor;
  139. numberKeyboard.BackColor = BackColor;
  140. numberKeyboard.MaxValue = 10;
  141. numberKeyboard.MinValue = 1;
  142. numberKeyboard.ValueType = HexNumberKeyboard.HexValueType.Dec;
  143. numberKeyboard.Value = Presenter.DataBytesCount;
  144. numberKeyboard.OkClick += (sender, args) =>
  145. {
  146. Presenter.DataBytesCount = (Int32)args.Data;
  147. if (Presenter.DataByteIndex >= Presenter.DataBytesCount) Presenter.DataByteIndex = Presenter.DataBytesCount - 1;
  148. _FloatForm.Close();
  149. };
  150. numberKeyboard.CancelClick += (_, _) => _FloatForm.Close();
  151. _FloatForm.Controls.Add(numberKeyboard);
  152. _FloatForm.FormClosing += (_, _) =>
  153. {
  154. if (this.FindForm() is FloatForm form)
  155. form.CanClose = lastflag;
  156. };
  157. EventBus.EventBroker.Instance.GetEvent<Uni_Trend.MSO7000X.Common.Structs.FormEventArgs>().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs()
  158. {
  159. Current = _FloatForm,
  160. Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm,
  161. });
  162. };
  163. }
  164. [UpdateProperty(nameof(I2CTrigSerialPrsnt.Data))]
  165. private void SetData()
  166. {
  167. this.BtnData.Text = "0x" + Convert.ToString(Presenter.Data, 16).ToUpper().PadLeft(2, '0');
  168. this.BtnData.Click += (_, _) =>
  169. {
  170. Boolean lastflag = true;
  171. if (this.FindForm() is FloatForm form)
  172. {
  173. lastflag = form.CanClose;
  174. form.CanClose = false;
  175. }
  176. if (_FloatForm != null && !_FloatForm.IsDisposed)
  177. _FloatForm?.Close();
  178. _FloatForm = new FloatForm();
  179. _FloatForm.BackColor = BackColor;
  180. _FloatForm.ForeColor = ForeColor;
  181. _FloatForm.Title = "设置数据";
  182. _FloatForm.Width = 380;
  183. _FloatForm.Height = 500 + _FloatForm.HeadHeight;
  184. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  185. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
  186. numberKeyboard.Top = _FloatForm.HeadHeight;
  187. numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight;
  188. numberKeyboard.Width = _FloatForm.Width;
  189. numberKeyboard.ForeColor = ForeColor;
  190. numberKeyboard.BackColor = BackColor;
  191. numberKeyboard.MaxValue = Byte.MaxValue;
  192. numberKeyboard.MinValue = 0;
  193. numberKeyboard.ValueType = HexNumberKeyboard.HexValueType.Hex;
  194. numberKeyboard.Value = Presenter.Data;
  195. numberKeyboard.OkClick += (sender, args) =>
  196. {
  197. Presenter.Data = (Byte)args.Data;
  198. _FloatForm.Close();
  199. };
  200. numberKeyboard.CancelClick += (_, _) => _FloatForm.Close();
  201. _FloatForm.Controls.Add(numberKeyboard);
  202. _FloatForm.FormClosing += (_, _) =>
  203. {
  204. if (this.FindForm() is FloatForm form)
  205. form.CanClose = lastflag;
  206. };
  207. EventBus.EventBroker.Instance.GetEvent<Uni_Trend.MSO7000X.Common.Structs.FormEventArgs>().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs()
  208. {
  209. Current = _FloatForm,
  210. Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm,
  211. });
  212. };
  213. }
  214. [UpdateProperty(nameof(I2CTrigSerialPrsnt.DataByteIndex))]
  215. private void SetDataByteIndex()
  216. {
  217. this.BtnDataByteIndex.Text = Presenter.DataByteIndex.ToString() ;
  218. this.BtnDataByteIndex.Click += (_, _) =>
  219. {
  220. Boolean lastflag = true;
  221. if (this.FindForm() is FloatForm form)
  222. {
  223. lastflag = form.CanClose;
  224. form.CanClose = false;
  225. }
  226. if (_FloatForm != null && !_FloatForm.IsDisposed)
  227. _FloatForm?.Close();
  228. _FloatForm = new FloatForm();
  229. _FloatForm.BackColor = BackColor;
  230. _FloatForm.ForeColor = ForeColor;
  231. _FloatForm.Title = "设置字节号";
  232. _FloatForm.Width = 380;
  233. _FloatForm.Height = 500 + _FloatForm.HeadHeight;
  234. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  235. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
  236. numberKeyboard.Top = _FloatForm.HeadHeight;
  237. numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight;
  238. numberKeyboard.Width = _FloatForm.Width;
  239. numberKeyboard.ForeColor = ForeColor;
  240. numberKeyboard.BackColor = BackColor;
  241. numberKeyboard.MaxValue = Presenter.DataBytesCount-1;
  242. numberKeyboard.MinValue = 0;
  243. numberKeyboard.ValueType = HexNumberKeyboard.HexValueType.Dec;
  244. numberKeyboard.Value = Presenter.DataByteIndex;
  245. numberKeyboard.OkClick += (sender, args) =>
  246. {
  247. Presenter.DataByteIndex = (Int32)args.Data;
  248. _FloatForm.Close();
  249. };
  250. numberKeyboard.CancelClick += (_, _) => _FloatForm.Close();
  251. _FloatForm.Controls.Add(numberKeyboard);
  252. _FloatForm.FormClosing += (_, _) =>
  253. {
  254. if (this.FindForm() is FloatForm form)
  255. form.CanClose = lastflag;
  256. };
  257. EventBus.EventBroker.Instance.GetEvent<Uni_Trend.MSO7000X.Common.Structs.FormEventArgs>().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs()
  258. {
  259. Current = _FloatForm,
  260. Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm,
  261. });
  262. };
  263. }
  264. [UpdateProperty(nameof(I2CTrigSerialPrsnt.Relation))]
  265. private void SetRelation()
  266. {
  267. this.CbxRelation.SelectedValue = Presenter.Relation;
  268. }
  269. #endregion UpdateFunction
  270. }
  271. }