CustomFormulaForm.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Xml;
  9. using Uestc.Auto6.Dso.Core;
  10. using Uestc.Auto6.Dso.ComModel;
  11. using Uestc.Auto6.Dso.MathExt;
  12. using Uni_Trend.MSO7000X.UserControls;
  13. using Uestc.Auto6.Dso.Core.Tools;
  14. using System.Windows.Forms.VisualStyles;
  15. namespace Uestc.Auto6.Dso.U2
  16. {
  17. public partial class CustomFormulaForm : FloatForm, IChnlView
  18. {
  19. private sealed record FuncInfo(String Id, String Symbol, String Expression, String ImageKey, String WeakTipWrapper);
  20. private readonly Dictionary<String, FuncInfo> _Functions = new();
  21. private String _Formula;
  22. private MathCustomArg _CustomArg;
  23. public MathPrsnt Presenter
  24. {
  25. get;
  26. set;
  27. }
  28. IBadge IView<IBadge>.Presenter
  29. {
  30. get => Presenter;
  31. set => Presenter = (MathPrsnt)value;
  32. }
  33. public CustomFormulaForm()
  34. {
  35. InitializeComponent();
  36. }
  37. protected override void OnLoad(EventArgs e)
  38. {
  39. base.OnLoad(e);
  40. _CustomArg = (MathCustomArg)Presenter.GetOrMakeArg(MathType.Custom);
  41. if (LoadCustomFormula(""))
  42. {
  43. LvSelection.Items.Clear();
  44. foreach (var item in _Functions)
  45. {
  46. var lvi = new ListViewItem(item.Key, item.Value.ImageKey)
  47. {
  48. ToolTipText = item.Value.WeakTipWrapper.Replace("\\n", Environment.NewLine)
  49. };
  50. LvSelection.Items.Add(lvi);
  51. }
  52. }
  53. UpdateFormulaText();
  54. //rtbxFormulaEditor.Focus();
  55. //rtbxFormulaEditor.Select(rtbxFormulaEditor.Text.Length, 0);
  56. LvSelection.Focus();
  57. //Presenter.SetFormula(MathType.Custom, _CustomArg.Formula);
  58. _CustomArg.Formula = _Formula;
  59. //_CustomArg.UpdateFormula();
  60. //风格调整
  61. Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControlRecursion(this);
  62. }
  63. private Boolean LoadCustomFormula(String path)
  64. {
  65. XmlReader reader;
  66. //Load CustomFormula.xml
  67. try
  68. {
  69. if (File.Exists(path))
  70. reader = new XmlTextReader(path);
  71. else
  72. {
  73. Type type = typeof(CustomFormulaForm);
  74. Stream sm = type.Assembly.GetManifestResourceStream(type.Namespace + ".Resources.CustomFormula.xml");
  75. reader = XmlReader.Create(sm);
  76. }
  77. reader.ReadToFollowing("ExpItems");
  78. XmlReader subreader = reader.ReadSubtree();
  79. while (subreader.Read())
  80. {
  81. if (subreader.NodeType == XmlNodeType.Element && subreader.Name == "Item")
  82. _Functions.Add(subreader.GetAttribute("name"),
  83. new FuncInfo(
  84. subreader.GetAttribute("id"),
  85. subreader.GetAttribute("symbol"),
  86. subreader.GetAttribute("expression"),
  87. subreader.GetAttribute("image"),
  88. subreader.GetAttribute("info")));
  89. }
  90. }
  91. catch
  92. {
  93. Logger.Error($"The load of CustomFormula.xml fails or its format is wrong!");
  94. #if DEBUG
  95. throw;
  96. #else
  97. return false;
  98. #endif
  99. }
  100. return true;
  101. }
  102. private void UpdateFormulaText()
  103. {
  104. StringBuilder vsb = new();
  105. StringBuilder expsb = new();
  106. foreach (var item in _CustomArg.Token)
  107. {
  108. vsb.Append(_Functions[item].Symbol);
  109. expsb.Append(_Functions[item].Expression);
  110. }
  111. RtbxEditor.Text = vsb.ToString();
  112. _Formula = expsb.ToString();
  113. }
  114. private void BtnClear_Click(object sender, EventArgs e)
  115. {
  116. _CustomArg.Token.Clear();
  117. _Formula = "";
  118. TbxDescription.Text = "";
  119. RtbxEditor.Text = "";
  120. }
  121. private void BtnBackspace_Click(object sender, EventArgs e)
  122. {
  123. if (_CustomArg.Token.Count != 0)
  124. {
  125. _CustomArg.Token.RemoveAt(_CustomArg.Token.Count - 1);
  126. UpdateFormulaText();
  127. }
  128. }
  129. private void LvSelection_Click(object sender, EventArgs e)
  130. {
  131. if (LvSelection.SelectedItems.Count > 0)
  132. {
  133. foreach (ListViewItem item in LvSelection.SelectedItems)
  134. {
  135. Int32 id = Int32.Parse(_Functions[item.Text].Id);
  136. //if it is function, add description.
  137. if (id > 20 && id < 60)
  138. TbxDescription.Text = _Functions[item.Text].WeakTipWrapper.Replace("\\n", Environment.NewLine);
  139. _CustomArg.Token.Add(item.Text);
  140. UpdateFormulaText();
  141. }
  142. }
  143. }
  144. private void BtnCancel_Click(object sender, EventArgs e)
  145. {
  146. this.DialogResult = DialogResult.Cancel;
  147. this.Close();
  148. }
  149. private void BtnAccept_Click(object sender, EventArgs e)
  150. {
  151. var res = DynamicExecute.Evaluate("Custom", _Formula, out String errmsg);
  152. if (res)
  153. {
  154. this.DialogResult = DialogResult.OK;
  155. //Presenter.SetFormula(Presenter.CalcType, _Formula);
  156. //_CustomArg.UpdateFormula();
  157. _CustomArg.Expression = RtbxEditor.Text;
  158. _CustomArg.Formula = _Formula;
  159. this.Close();
  160. }
  161. else
  162. {
  163. WeakTip.Default.Write("Formula Editor", MsgTipId.InputFormatError);
  164. }
  165. }
  166. private void TbxDescription_MouseDown(object sender, MouseEventArgs e)
  167. {
  168. NativeMethods.HideCaret((sender as TextBox).Handle);
  169. LvSelection.Focus();
  170. }
  171. private void RtbxEditor_MouseDown(object sender, MouseEventArgs e)
  172. {
  173. NativeMethods.HideCaret((sender as RichTextBox).Handle);
  174. LvSelection.Focus();
  175. }
  176. public void UpdateView(string propertyName)
  177. {
  178. }
  179. protected override void OnFormClosed(FormClosedEventArgs e)
  180. {
  181. base.OnFormClosed(e);
  182. }
  183. }
  184. }