| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
-
- using MuchInfo.Chart.Data.Models;
- using MuchInfo.Chart.DataAdapter;
- using System.Windows;
- using System.Windows.Controls;
- namespace MuchInfo.Chart.WPF.Helpers
- {
- public class FormulaHelper
- {
- /// <summary>
- /// 创建 指标菜单
- /// </summary>
- /// <param name="fileName">(flieName为空时取Main指标所在的指标)</param>
- /// <param name="indicatorHandler">选择指标时处理函数</param>
- /// <returns></returns>
- public static MenuItem CreateIndicatorMenu(string fileName, RoutedEventHandler indicatorHandler)
- {
- IDataService iDataService = new DataService();
- var spaceModel = iDataService.LoadSpaceFromFile(fileName);
- if (spaceModel != null)
- {
- MenuItem modelItem = new MenuItem();
- modelItem.Header = spaceModel.SpaceName;
- modelItem.Tag = spaceModel;
- MenuByRecursion(modelItem, spaceModel, indicatorHandler);
- return modelItem;
- }
- return null;
- }
- /// <summary>
- /// 加载指标分类
- /// </summary>
- /// <param name="fileName">指标地址</param>
- /// <returns></returns>
- public static SpaceModel LoadIndicatorSpaceModel(string fileName)
- {
- IDataService iDataService = new DataService();
- return iDataService.LoadSpaceFromFile(fileName);
- }
- /// <summary>
- /// 递归生成指标
- /// </summary>
- /// <param name="parentItem">父菜单</param>
- /// <param name="spaceModel">转换父菜单的对象</param>
- /// /// <param name="indicatorHandler">选择指标时处理函数</param>
- private static void MenuByRecursion(MenuItem parentItem, SpaceModel spaceModel,
- RoutedEventHandler indicatorHandler)
- {
- foreach (var space in spaceModel.SpaceModels) //加载命名空间
- {
- var modelItem = new MenuItem();
- modelItem.Header = space.SpaceName;
- modelItem.Tag = space;
- parentItem.Items.Add(modelItem);
- MenuByRecursion(modelItem, space, indicatorHandler);
- }
- if (spaceModel.ProgramModels != null)
- {
- foreach (var program in spaceModel.ProgramModels)
- {
- var programItem = new MenuItem();
- programItem.Header = program.Name;
- programItem.Tag = program;
- programItem.Click += indicatorHandler;
- parentItem.Items.Add(programItem);
- //programItem.IsMainView = program.IsMainView;
- }
- }
- }
- /// <summary>
- /// 通过名称查找公式,找不到返回空
- /// </summary>
- /// <param name="Name">名称</param>
- /// <param name="filePath">公式路径</param>
- /// <returns></returns>
- public static FormulaModel GetFormulaByName(string Name, string filePath)
- {
- IDataService iDataService = new DataService();
- var spacel = iDataService.LoadSpaceFromFile(filePath);
- if (spacel != null)
- {
- var programModel = GetProgramModelByName(Name, spacel);
- if (!string.IsNullOrEmpty(programModel.Name))
- {
- return programModel.CovertToFormulaModel();
- }
- }
- return null;
- }
- /// <summary>
- /// 获取编辑公式
- /// </summary>
- /// <param name="programName">查找的公式名称</param>
- /// <param name="spaceModel">命名空间</param>
- /// <returns></returns>
- private static ProgramModel GetProgramModelByName(string programName, SpaceModel spaceModel)
- {
- //modified By:ouyang.hongbin
- //TODO;修正查找方法
- //foreach (var programModel in spaceModel.ProgramModels)
- //{
- // if (programName.Equals(programModel.Name))
- // {
- // return programModel;
- // }
- //}
- //foreach (var spaceItem in spaceModel.SpaceModels)
- //{
- // return GetProgramModelByName(programName, spaceItem);
- //}
- //return null;
- foreach (var spaceModelItem in spaceModel.SpaceModels)
- {
- foreach (var programModelsItem in spaceModelItem.ProgramModels)
- {
- if (programName.ToUpper().Equals(programModelsItem.Name.ToUpper()))
- {
- return programModelsItem;
- }
- }
- }
- return null;
- }
- }
- }
|