RS232TriggerSetControl.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Uestc.Auto6.Dso.Core;
  11. using Uestc.Auto6.Dso.Core.Decode;
  12. using Uestc.Auto6.Dso.ComModel;
  13. using Uni_Trend.MSO7000X.Common.Helper;
  14. using Uni_Trend.MSO7000X.Common.Structs;
  15. using Uni_Trend.MSO7000X.UserControls;
  16. using Uni_Trend.MSO7000X.Language;
  17. using Uni_Trend.MSO7000X.UserControls.Style;
  18. namespace Uestc.Auto6.Dso.Protocol.RS232
  19. {
  20. [System.ComponentModel.ToolboxItem(false)]
  21. public partial class RS232TriggerSetControl : UserControl, ITriggerSerialView
  22. {
  23. public RS232TriggerSetControl()
  24. {
  25. InitializeComponent();
  26. InitView();
  27. this.Load += (_, _) =>
  28. {
  29. Init();
  30. this.BtnValue.Click += (_, _) => Action?.Invoke();
  31. SetControl(Presenter.Conditions);
  32. this.SizeChanged += (_, _) => SetControl(Presenter.Conditions);
  33. };
  34. }
  35. private void InitView()
  36. {
  37. Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(this, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize);
  38. }
  39. protected new Boolean DesignMode
  40. {
  41. get
  42. {
  43. Boolean rtnflag = false;
  44. #if DEBUG
  45. rtnflag = DesignTimeHelper.InDesignMode(this);
  46. #endif
  47. return rtnflag;
  48. }
  49. }
  50. private Int32 _RowHeight = 76;
  51. private void SetControl(ProtocolRS232.Conditions conditions)
  52. {
  53. switch (conditions)
  54. {
  55. case ProtocolRS232.Conditions.SpecialData:
  56. DataAutoLayout();
  57. break;
  58. case ProtocolRS232.Conditions.FrameStart:
  59. case ProtocolRS232.Conditions.CheckError:
  60. case ProtocolRS232.Conditions.PackageEnd:
  61. StartAutoLayout();
  62. break;
  63. }
  64. }
  65. private Action Action;
  66. private void EditData()
  67. {
  68. Boolean lastflag = true;
  69. if(this.FindForm() is FloatForm form)
  70. {
  71. lastflag = form.CanClose;
  72. form.CanClose = false;
  73. }
  74. FloatForm floatForm = new FloatForm();
  75. floatForm.BackColor = BackColor;
  76. floatForm.ForeColor = ForeColor;
  77. floatForm.Title = "设置数据";
  78. floatForm.Width = 380;
  79. floatForm.Height = 500 + floatForm.HeadHeight;
  80. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  81. Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(numberKeyboard, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize);
  82. //numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(x, Uni_Trend.MSO7000X.UserControls.Style.StyleFlag.FontSize));
  83. //numberKeyboard.Dock = DockStyle.Fill;
  84. numberKeyboard.Top = floatForm.HeadHeight;
  85. numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
  86. numberKeyboard.Width = floatForm.Width;
  87. numberKeyboard.ForeColor = ForeColor;
  88. numberKeyboard.BackColor = BackColor;
  89. numberKeyboard.MaxValue = 0xFF;
  90. numberKeyboard.MinValue = 0x00;
  91. numberKeyboard.ValueType = Uni_Trend.MSO7000X.UserControls.HexNumberKeyboard.HexValueType.Hex;
  92. numberKeyboard.Value = Presenter.Data;
  93. numberKeyboard.OkClick += (sender, args) =>
  94. {
  95. Presenter.Data = (Char)args.Data;
  96. this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Int32)Presenter.Data);
  97. floatForm.Close();
  98. };
  99. numberKeyboard.CancelClick += (_, _) => floatForm.Close();
  100. floatForm.FormClosing += (_, _) =>
  101. {
  102. if (this.FindForm() is FloatForm form)
  103. form.CanClose = lastflag;
  104. };
  105. floatForm.Controls.Add(numberKeyboard);
  106. floatForm.ShowDialogByPosition();
  107. }
  108. private void EditEOP()
  109. {
  110. FloatForm floatForm = new FloatForm();
  111. Boolean lastflag = true;
  112. if (this.FindForm() is FloatForm form)
  113. {
  114. lastflag = form.CanClose;
  115. form.CanClose = false;
  116. }
  117. floatForm.BackColor = BackColor;
  118. floatForm.ForeColor = ForeColor;
  119. floatForm.Title = "设置EOP";
  120. floatForm.Width = 380;
  121. floatForm.Height = 500 + floatForm.HeadHeight;
  122. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  123. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.DefaultStyleManager.Instance.RegisterControlRecursion(x, StyleFlag.FontSize));
  124. //numberKeyboard.Dock = DockStyle.Fill;
  125. numberKeyboard.Top = floatForm.HeadHeight;
  126. numberKeyboard.Height = floatForm.Height - floatForm.HeadHeight;
  127. numberKeyboard.Width = floatForm.Width;
  128. numberKeyboard.ForeColor = ForeColor;
  129. numberKeyboard.BackColor = BackColor;
  130. numberKeyboard.MaxValue = 0xFF;
  131. numberKeyboard.MinValue = 0x00;
  132. numberKeyboard.ValueType = Uni_Trend.MSO7000X.UserControls.HexNumberKeyboard.HexValueType.Hex;
  133. numberKeyboard.Value = Presenter.EOPChar;
  134. numberKeyboard.OkClick += (sender, args) =>
  135. {
  136. Presenter.EOPChar = (Char)args.Data;
  137. String charstring = ASCIIConverter.GetASCIIStr(Presenter.EOPChar);
  138. if(String.IsNullOrEmpty(charstring))
  139. this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Int32)Presenter.EOPChar);
  140. else
  141. this.BtnValue.Text = "0x" + String.Format("{0:X2}({1})", (Int32)Presenter.EOPChar, charstring);
  142. floatForm.Close();
  143. };
  144. numberKeyboard.CancelClick += (_, _) => floatForm.Close();
  145. floatForm.FormClosing += (_, _) =>
  146. {
  147. if (this.FindForm() is FloatForm form)
  148. form.CanClose = lastflag;
  149. };
  150. floatForm.Controls.Add(numberKeyboard);
  151. DefaultStyleManager.Instance.RegisterControlRecursion(floatForm, StyleFlag.FontSize);
  152. floatForm.ShowDialogByPosition();
  153. }
  154. private void Init()
  155. {
  156. SetControl(Presenter.Conditions);
  157. this.CbxCompare.DataSource = Enum.GetValues<PulseCondition>().Select(x =>
  158. {
  159. String key = String.Empty;
  160. key = Properties.Resources.ResourceManager.GetString(x.GetType().UnderlyingSystemType.FullName + "."+ x.ToString());
  161. if (String.IsNullOrEmpty(key))
  162. {
  163. key = x.ToString();
  164. }
  165. return new KeyValuePair<String, PulseCondition>(key, x);
  166. }).ToList();
  167. this.CbxCompare.DisplayMember = "Key";
  168. this.CbxCompare.ValueMember = "Value";
  169. this.CbxCompare.SelectedValueChanged += (sender, args) =>
  170. {
  171. if (ParentForm == null)
  172. return;
  173. Presenter.Compare = (PulseCondition)this.CbxCompare.SelectedValue;
  174. };
  175. this.CbxCompare.SelectedValue = Presenter.Compare;
  176. }
  177. public RS232TrigSerialPrsnt Presenter
  178. {
  179. get => (RS232TrigSerialPrsnt)(ParentForm as ITriggerView).Presenter;
  180. set => (ParentForm as ITriggerView).Presenter = value;
  181. }
  182. ITriggerPrsnt IView<ITriggerPrsnt>.Presenter
  183. {
  184. get => Presenter;
  185. set => Presenter = (RS232TrigSerialPrsnt)value;
  186. }
  187. private void EOPAutoLayout()
  188. {
  189. this.LblValue.Text = "结束标志";
  190. String charstring = ASCIIConverter.GetASCIIStr(Presenter.EOPChar);
  191. if (String.IsNullOrEmpty(charstring))
  192. this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Int32)Presenter.EOPChar);
  193. else
  194. this.BtnValue.Text = "0x" + String.Format("{0:X2}({1})", (Int32)Presenter.EOPChar, charstring);
  195. this.LblValue.Visible = true;
  196. this.BtnValue.Visible = true;
  197. this.LblCompare.Visible = false;
  198. this.CbxCompare.Visible = false;
  199. SetControlPosition(BtnValue, LblValue, 0, 0);
  200. this.Height = BtnValue.Bottom;
  201. Visible = true;
  202. Action = () => EditEOP();
  203. }
  204. private void StartAutoLayout()
  205. {
  206. this.LblValue.Visible = false;
  207. this.BtnValue.Visible = false;
  208. this.CbxCompare.Visible = false;
  209. this.LblCompare.Visible = false;
  210. Height = 0;
  211. Visible = false;
  212. Action = null;
  213. }
  214. private void DataAutoLayout()
  215. {
  216. this.LblValue.Text = "数据(Hex)";
  217. this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Byte)Presenter.Data);
  218. this.BtnValue.Visible = true;
  219. this.LblValue.Visible = true;
  220. this.CbxCompare.Visible = true;
  221. this.LblCompare.Visible = true;
  222. SetControlPosition(CbxCompare,LblCompare, 0, 0);
  223. SetControlPosition(BtnValue,LblValue, 0, 1);
  224. this.Height = BtnValue.Bottom;
  225. Visible = true;
  226. Action = () => EditData();
  227. }
  228. private void SetControlPosition<TControl>(TControl control, UestcLabel label, Int32 row = 0, Int32 col = 0) where TControl : Control
  229. {
  230. label.Height = label.Font.Height;
  231. Int32 tempheight = control.Height + label.Height;
  232. label.Left = Width / 2 * col + 10;
  233. label.Top = row * _RowHeight + (_RowHeight - tempheight) / 2;
  234. control.Left = label.Left;
  235. control.Top = label.Bottom + 6;
  236. }
  237. public void UpdateView(Object presenter, String propertyName)
  238. {
  239. if(String.IsNullOrEmpty(propertyName))
  240. {
  241. UpdateView();
  242. return;
  243. }
  244. switch(propertyName)
  245. {
  246. case nameof(RS232TrigSerialPrsnt.Compare):
  247. if(Presenter.Conditions == ProtocolRS232.Conditions.SpecialData)
  248. CbxCompare.SelectedValue = Presenter.Compare;
  249. break;
  250. case nameof(RS232TrigSerialPrsnt.Data):
  251. if(Presenter.Conditions == ProtocolRS232.Conditions.SpecialData)
  252. BtnValue.Text = "0x" + String.Format("{0:X2}", (Byte)Presenter.Data);
  253. break;
  254. //case nameof(RS232TrigSerialPrsnt.DataIndex):
  255. // if (Presenter.RS232Conditions == ProtocolRS232.Conditions.SpecialData) CbxDataIndex.SelectedValue = Presenter.DataIndex;
  256. // break;
  257. case nameof(RS232TrigSerialPrsnt.EOPChar):
  258. if(Presenter.Conditions == ProtocolRS232.Conditions.CheckError)
  259. {
  260. String charstring = ASCIIConverter.GetASCIIStr(Presenter.EOPChar);
  261. if (String.IsNullOrEmpty(charstring))
  262. this.BtnValue.Text = "0x" + String.Format("{0:X2}", (Int32)Presenter.EOPChar);
  263. else
  264. this.BtnValue.Text = "0x" + String.Format("{0:X2}({1})", (Int32)Presenter.EOPChar, charstring);
  265. }
  266. break;
  267. case nameof(RS232TrigSerialPrsnt.Conditions):
  268. SetControl(Presenter.Conditions);
  269. break;
  270. default:
  271. break;
  272. }
  273. }
  274. private void UpdateView()
  275. {
  276. }
  277. }
  278. }