123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Windows.Forms;
- using System.Xml;
- using Uestc.Auto6.Dso.Core;
- using Uestc.Auto6.Dso.ComModel;
- using Uestc.Auto6.Dso.MathExt;
- using Uni_Trend.MSO7000X.UserControls;
- using Uestc.Auto6.Dso.Core.Tools;
- using System.Windows.Forms.VisualStyles;
- namespace Uestc.Auto6.Dso.U2
- {
- public partial class CustomFormulaForm : FloatForm, IChnlView
- {
- private sealed record FuncInfo(String Id, String Symbol, String Expression, String ImageKey, String WeakTipWrapper);
- private readonly Dictionary<String, FuncInfo> _Functions = new();
- private String _Formula;
- private MathCustomArg _CustomArg;
-
- public MathPrsnt Presenter
- {
- get;
- set;
- }
- IBadge IView<IBadge>.Presenter
- {
- get => Presenter;
- set => Presenter = (MathPrsnt)value;
- }
- public CustomFormulaForm()
- {
- InitializeComponent();
- }
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- _CustomArg = (MathCustomArg)Presenter.GetOrMakeArg(MathType.Custom);
- if (LoadCustomFormula(""))
- {
- LvSelection.Items.Clear();
- foreach (var item in _Functions)
- {
- var lvi = new ListViewItem(item.Key, item.Value.ImageKey)
- {
- ToolTipText = item.Value.WeakTipWrapper.Replace("\\n", Environment.NewLine)
- };
- LvSelection.Items.Add(lvi);
- }
- }
- UpdateFormulaText();
- //rtbxFormulaEditor.Focus();
- //rtbxFormulaEditor.Select(rtbxFormulaEditor.Text.Length, 0);
- LvSelection.Focus();
- //Presenter.SetFormula(MathType.Custom, _CustomArg.Formula);
- _CustomArg.Formula = _Formula;
- //_CustomArg.UpdateFormula();
- //风格调整
- Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControlRecursion(this);
- }
- private Boolean LoadCustomFormula(String path)
- {
- XmlReader reader;
- //Load CustomFormula.xml
- try
- {
- if (File.Exists(path))
- reader = new XmlTextReader(path);
- else
- {
-
- Type type = typeof(CustomFormulaForm);
- Stream sm = type.Assembly.GetManifestResourceStream(type.Namespace + ".Resources.CustomFormula.xml");
- reader = XmlReader.Create(sm);
- }
- reader.ReadToFollowing("ExpItems");
- XmlReader subreader = reader.ReadSubtree();
- while (subreader.Read())
- {
- if (subreader.NodeType == XmlNodeType.Element && subreader.Name == "Item")
- _Functions.Add(subreader.GetAttribute("name"),
- new FuncInfo(
- subreader.GetAttribute("id"),
- subreader.GetAttribute("symbol"),
- subreader.GetAttribute("expression"),
- subreader.GetAttribute("image"),
- subreader.GetAttribute("info")));
- }
- }
- catch
- {
- Logger.Error($"The load of CustomFormula.xml fails or its format is wrong!");
- #if DEBUG
- throw;
- #else
- return false;
- #endif
- }
- return true;
- }
-
- private void UpdateFormulaText()
- {
- StringBuilder vsb = new();
- StringBuilder expsb = new();
- foreach (var item in _CustomArg.Token)
- {
- vsb.Append(_Functions[item].Symbol);
- expsb.Append(_Functions[item].Expression);
- }
- RtbxEditor.Text = vsb.ToString();
- _Formula = expsb.ToString();
- }
- private void BtnClear_Click(object sender, EventArgs e)
- {
- _CustomArg.Token.Clear();
- _Formula = "";
- TbxDescription.Text = "";
- RtbxEditor.Text = "";
- }
- private void BtnBackspace_Click(object sender, EventArgs e)
- {
- if (_CustomArg.Token.Count != 0)
- {
- _CustomArg.Token.RemoveAt(_CustomArg.Token.Count - 1);
- UpdateFormulaText();
- }
- }
- private void LvSelection_Click(object sender, EventArgs e)
- {
- if (LvSelection.SelectedItems.Count > 0)
- {
- foreach (ListViewItem item in LvSelection.SelectedItems)
- {
- Int32 id = Int32.Parse(_Functions[item.Text].Id);
- //if it is function, add description.
- if (id > 20 && id < 60)
- TbxDescription.Text = _Functions[item.Text].WeakTipWrapper.Replace("\\n", Environment.NewLine);
- _CustomArg.Token.Add(item.Text);
- UpdateFormulaText();
- }
- }
- }
- private void BtnCancel_Click(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.Cancel;
- this.Close();
- }
- private void BtnAccept_Click(object sender, EventArgs e)
- {
- var res = DynamicExecute.Evaluate("Custom", _Formula, out String errmsg);
- if (res)
- {
- this.DialogResult = DialogResult.OK;
- //Presenter.SetFormula(Presenter.CalcType, _Formula);
- //_CustomArg.UpdateFormula();
- _CustomArg.Expression = RtbxEditor.Text;
- _CustomArg.Formula = _Formula;
- this.Close();
- }
- else
- {
- WeakTip.Default.Write("Formula Editor", MsgTipId.InputFormatError);
- }
- }
- private void TbxDescription_MouseDown(object sender, MouseEventArgs e)
- {
- NativeMethods.HideCaret((sender as TextBox).Handle);
- LvSelection.Focus();
- }
- private void RtbxEditor_MouseDown(object sender, MouseEventArgs e)
- {
- NativeMethods.HideCaret((sender as RichTextBox).Handle);
- LvSelection.Focus();
- }
- public void UpdateView(string propertyName)
- {
-
- }
- protected override void OnFormClosed(FormClosedEventArgs e)
- {
- base.OnFormClosed(e);
- }
- }
- }
|