I2CDataPage.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. [System.ComponentModel.ToolboxItem(false)]
  20. public partial class I2CDataPage : UserControl, ITriggerSerialView
  21. {
  22. #region 属性
  23. private FloatForm _FloatForm;
  24. public I2CTrigSerialPrsnt Presenter { get; set; }
  25. ITriggerPrsnt IView<ITriggerPrsnt>.Presenter
  26. {
  27. get => Presenter;
  28. set => Presenter = (I2CTrigSerialPrsnt)value;
  29. }
  30. protected new Boolean DesignMode
  31. {
  32. get
  33. {
  34. Boolean rtnflag = false;
  35. #if DEBUG
  36. rtnflag = DesignTimeHelper.InDesignMode(this);
  37. #endif
  38. return rtnflag;
  39. }
  40. }
  41. #endregion 属性
  42. public I2CDataPage()
  43. {
  44. InitializeComponent();
  45. Controls.Cast<Control>().ToList().ForEach(x => DefaultStyleManager.Instance.RegisterControl(x, StyleFlag.FontSize));
  46. }
  47. protected override void OnLoad(EventArgs e)
  48. {
  49. base.OnLoad(e);
  50. InitView();
  51. }
  52. private void InitView()
  53. {
  54. SetDataBytesCount();
  55. SetData();
  56. InitRelation();
  57. }
  58. private void InitRelation()
  59. {
  60. this.CbxRelation.DataSource = Enum.GetValues<ComModel.ProtocolI2C.DataRelation>().Select(x =>
  61. {
  62. String key = String.Empty;
  63. key = Properties.Resources.ResourceManager.GetString(x.GetType().ReflectedType.FullName + "." + x.GetType().Name + "." + x.ToString());
  64. if (String.IsNullOrEmpty(key))
  65. {
  66. key = x.ToString();
  67. }
  68. return new KeyValuePair<String, ComModel.ProtocolI2C.DataRelation>(key, x);
  69. }).ToList();
  70. this.CbxRelation.ValueMember = "Value";
  71. this.CbxRelation.DisplayMember = "Key";
  72. this.CbxRelation.SelectedValueChanged += (_, _) =>
  73. {
  74. Presenter.Relation = (ComModel.ProtocolI2C.DataRelation) this.CbxRelation.SelectedValue;
  75. };
  76. this.CbxRelation.SelectedValue = Presenter.Relation;
  77. }
  78. public void UpdateView(Object presenter, String propertyName)
  79. {
  80. if(String.IsNullOrEmpty(propertyName))
  81. {
  82. UpdateView();
  83. return;
  84. }
  85. this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance| BindingFlags.Public).Where(x =>
  86. {
  87. UpdatePropertyAttribute attribute = x.GetCustomAttribute<UpdatePropertyAttribute>();
  88. return attribute != null && attribute.PropertyName == propertyName;
  89. }).FirstOrDefault()?.Invoke(this, null);
  90. }
  91. private void UpdateView()
  92. {
  93. this.GetType().GetMethods( BindingFlags.Public| BindingFlags.Instance| BindingFlags.NonPublic).Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
  94. this.Refresh();
  95. }
  96. #region UpdateFunction
  97. private void SetDataBytesCount()
  98. {
  99. this.CbxDataBytesCount.Text = Presenter.DataBytesCount.ToString();
  100. this.CbxDataBytesCount.Click += (_, _) =>
  101. {
  102. Boolean lastflag = true;
  103. if (this.FindForm() is FloatForm form)
  104. {
  105. lastflag = form.CanClose;
  106. form.CanClose = false;
  107. }
  108. if (_FloatForm != null && !_FloatForm.IsDisposed)
  109. _FloatForm?.Close();
  110. _FloatForm = new FloatForm();
  111. _FloatForm.BackColor = BackColor;
  112. _FloatForm.ForeColor = ForeColor;
  113. _FloatForm.Title = "设置位宽";
  114. _FloatForm.Width = 380;
  115. _FloatForm.Height = 500 + _FloatForm.HeadHeight;
  116. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  117. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(x, StyleFlag.FontSize));
  118. numberKeyboard.Top = _FloatForm.HeadHeight;
  119. numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight;
  120. numberKeyboard.Width = _FloatForm.Width;
  121. numberKeyboard.ForeColor = ForeColor;
  122. numberKeyboard.BackColor = BackColor;
  123. numberKeyboard.MaxValue = Presenter.MaxByteCount;
  124. numberKeyboard.MinValue = Presenter.MinByteCount;
  125. numberKeyboard.ValueType = HexNumberKeyboard.HexValueType.Dec;
  126. numberKeyboard.Value = Presenter.DataBytesCount;
  127. numberKeyboard.OkClick += (sender, args) =>
  128. {
  129. Presenter.DataBytesCount = (Byte)args.Data;
  130. _FloatForm.Close();
  131. };
  132. numberKeyboard.CancelClick += (_, _) => _FloatForm.Close();
  133. _FloatForm.Controls.Add(numberKeyboard);
  134. DefaultStyleManager.Instance.RegisterControlRecursion(_FloatForm, StyleFlag.FontSize);
  135. _FloatForm.FormClosing += (_, _) =>
  136. {
  137. if (this.FindForm() is FloatForm form)
  138. form.CanClose = lastflag;
  139. };
  140. _FloatForm.ShowDialogByPosition();
  141. };
  142. }
  143. private void SetData()
  144. {
  145. this.BtnData.Text = "0x" + Convert.ToString(Presenter.Data, 16).ToUpper().PadLeft(2*Presenter.DataBytesCount, '0');
  146. this.BtnData.Click += (_, _) =>
  147. {
  148. Boolean lastflag = true;
  149. if (this.FindForm() is FloatForm form)
  150. {
  151. lastflag = form.CanClose;
  152. form.CanClose = false;
  153. }
  154. if (_FloatForm != null && !_FloatForm.IsDisposed)
  155. _FloatForm?.Close();
  156. _FloatForm = new FloatForm();
  157. _FloatForm.BackColor = BackColor;
  158. _FloatForm.ForeColor = ForeColor;
  159. _FloatForm.Title = "设置数据";
  160. _FloatForm.Width = 380;
  161. _FloatForm.Height = 500 + _FloatForm.HeadHeight;
  162. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  163. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(x, StyleFlag.FontSize));
  164. numberKeyboard.Top = _FloatForm.HeadHeight;
  165. numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight;
  166. numberKeyboard.Width = _FloatForm.Width;
  167. numberKeyboard.ForeColor = ForeColor;
  168. numberKeyboard.BackColor = BackColor;
  169. numberKeyboard.MaxValue = Presenter.MaxData;
  170. numberKeyboard.MinValue = Presenter.MinData;
  171. numberKeyboard.ValueType = HexNumberKeyboard.HexValueType.Hex;
  172. numberKeyboard.Value = Presenter.Data;
  173. numberKeyboard.OkClick += (sender, args) =>
  174. {
  175. Presenter.Data = (Int64)args.Data;
  176. _FloatForm.Close();
  177. };
  178. numberKeyboard.CancelClick += (_, _) => _FloatForm.Close();
  179. _FloatForm.Controls.Add(numberKeyboard);
  180. DefaultStyleManager.Instance.RegisterControlRecursion(_FloatForm, StyleFlag.FontSize);
  181. _FloatForm.FormClosing += (_, _) =>
  182. {
  183. if (this.FindForm() is FloatForm form)
  184. form.CanClose = lastflag;
  185. };
  186. _FloatForm.ShowDialogByPosition();
  187. };
  188. }
  189. [UpdateProperty(nameof(I2CTrigSerialPrsnt.DataBytesCount))]
  190. private void UpdateDataCount()
  191. {
  192. this.CbxDataBytesCount.Text = Presenter.DataBytesCount.ToString();
  193. UpdateData();
  194. this.LblData.Visible = Presenter.DataBytesCount > 0;
  195. this.BtnData.Visible = this.LblData.Visible;
  196. }
  197. [UpdateProperty(nameof(I2CTrigSerialPrsnt.Relation))]
  198. private void SetRelation()
  199. {
  200. this.CbxRelation.SelectedValue = Presenter.Relation;
  201. }
  202. [UpdateProperty(nameof(I2CTrigSerialPrsnt.Data))]
  203. private void UpdateData()
  204. {
  205. this.BtnData.Text = "0x" + Convert.ToString(Presenter.Data, 16).ToUpper().PadLeft(2*Presenter.DataBytesCount, '0');
  206. }
  207. #endregion UpdateFunction
  208. }
  209. }