public ChartToolButtonSettings(ChartToolButtonSettings spec)
 {
     Group          = spec.Group;
     Image          = spec.Image;
     Title          = spec.Title;
     defaultVisible = spec.defaultVisible;
     DisplayName    = spec.DisplayName;
     Tool           = spec.Tool;
     toolParams     =
         spec.toolParams.Select(t => new SeriesEditParameter
     {
         defaultValue = t.defaultValue,
         paramType    = t.paramType,
         title        = t.title,
         Name         = t.Name
     })
         .ToList();
     ButtonType = spec.ButtonType;
 }
        private bool LoadSettings()
        {
            groups.Clear();
            selButtons.Clear();
            selSystemButtons.Clear();
            var nodeToolSets = ToolSettingsStorageFile.LoadNode(ToolSettingsStorageFile.NodeNameToolButtons);

            if (nodeToolSets == null)
            {
                return(false);
            }

            var nodeGroups = nodeToolSets.ChildNodes.Cast <XmlNode>().FirstOrDefault(n => n.Name == "groups");

            if (nodeGroups != null)
            {
                foreach (XmlElement node in nodeGroups.ChildNodes)
                {
                    var group = ToolButtonGroup.LoadFromXml(node);
                    if (group != null)
                    {
                        groups.Add(group);
                    }
                }
            }

            // todo: simplify to one tag-container "buttons"
            // кнопки графика
            var nodeIndex   = 0; // 4 log
            var nodeButtons = nodeToolSets.ChildNodes.Cast <XmlNode>().FirstOrDefault(n => n.Name == "buttons");

            if (nodeButtons == null)
            {
                return(false);
            }
            foreach (XmlElement node in nodeButtons.ChildNodes)
            {
                nodeIndex++;
                try
                {
                    var btn = ChartToolButtonSettings.LoadFromXml(node, groups, ChartToolButtonSettings.ToolButtonType.Chart);
                    if (btn == null)
                    {
                        continue;
                    }

                    // поддержка старых версий файлов (установка функции кнупки по названию)
                    if (btn.ButtonType == ChartToolButtonSettings.ToolButtonType.Chart && btn.Tool == CandleChartControl.ChartTool.None)
                    {
                        var toolButton = allButtons.FirstOrDefault(b => b.ButtonType == ChartToolButtonSettings.ToolButtonType.Chart && b.Title == btn.Title);
                        if (toolButton != null)
                        {
                            btn.Tool = toolButton.Tool;
                        }
                    }

                    selButtons.Add(btn);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("Ошибка загрузки кнопки графика {0}: {1}", nodeIndex, ex);
                }
            }

            // кнопки терминала
            nodeIndex   = 0;
            nodeButtons = nodeToolSets.ChildNodes.Cast <XmlNode>().FirstOrDefault(n => n.Name == "systembuttons");
            if (nodeButtons == null)
            {
                return(false);
            }
            foreach (XmlElement node in nodeButtons.ChildNodes)
            {
                nodeIndex++;
                try
                {
                    var btn = ChartToolButtonSettings.LoadFromXml(node, groups, ChartToolButtonSettings.ToolButtonType.System);
                    if (btn == null)
                    {
                        continue;
                    }

                    // поддержка старых версий файлов (установка функции кнупки по названию)
                    if (btn.ButtonType == ChartToolButtonSettings.ToolButtonType.Chart && btn.Tool == CandleChartControl.ChartTool.None)
                    {
                        var toolButton = allButtons.FirstOrDefault(b => b.ButtonType == ChartToolButtonSettings.ToolButtonType.System && b.Title == btn.Title);
                        if (toolButton != null)
                        {
                            btn.SystemTool = toolButton.SystemTool;
                        }
                    }

                    selSystemButtons.Add(btn);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("Ошибка загрузки терминальной кнопки {0}: {1}", nodeIndex, ex);
                }
            }

            // set localized title
            var buttons = new List <ChartToolButtonSettings>();

            buttons.AddRange(selButtons);
            buttons.AddRange(selSystemButtons);
            foreach (var btn in buttons)
            {
                var storageButton =
                    allButtons.FirstOrDefault(
                        b => b.ButtonType == btn.ButtonType && b.Tool == btn.Tool && b.SystemTool == btn.SystemTool);
                if (storageButton != null)
                {
                    btn.Title = storageButton.Title;
                }
            }

            return(true);
        }
        private void InitAllButtons()
        {
            // предустановленные режимы
            allButtons.Add(new ChartToolButtonSettings
            {
                Image          = (int)ToolButtonImageIndex.Arrow,
                Tool           = CandleChartControl.ChartTool.Cursor,
                DefaultVisible = true,
                Title          = Localizer.GetString("TitleCursor"),
                ButtonType     = ChartToolButtonSettings.ToolButtonType.Chart
            });
            allButtons.Add(new ChartToolButtonSettings
            {
                Image          = (int)ToolButtonImageIndex.Ruller,
                Tool           = CandleChartControl.ChartTool.TrendLine,
                DefaultVisible = true,
                Title          = Localizer.GetString("TitleMeasurement"),
                toolParams     = new List <SeriesEditParameter>
                {
                    new SeriesEditParameter("MeasureOnly", typeof(bool), true),
                    new SeriesEditParameter("Subtitles", typeof(bool), true),
                    new SeriesEditParameter("Edit", typeof(bool), false)
                },
                ButtonType = ChartToolButtonSettings.ToolButtonType.Chart
            });
            allButtons.Add(new ChartToolButtonSettings
            {
                Image          = (int)ToolButtonImageIndex.CrossLines,
                Tool           = CandleChartControl.ChartTool.Cross,
                DefaultVisible = true,
                Title          = Localizer.GetString("TitleCrosshair"),
                ButtonType     = ChartToolButtonSettings.ToolButtonType.Chart
            });
            allButtons.Add(new ChartToolButtonSettings
            {
                Image          = (int)ToolButtonImageIndex.Script,
                Tool           = CandleChartControl.ChartTool.Script,
                DefaultVisible = true,
                Title          = Localizer.GetString("TitleScript"),
                ButtonType     = ChartToolButtonSettings.ToolButtonType.Chart
            });

            // кнопки графика
            var lstSeries = InteractiveObjectSeries.GetObjectSeriesTypes();

            foreach (var t in lstSeries)
            {
                var attrBtn = (SeriesToolButtonAttribute)t.GetCustomAttributes(typeof(SeriesToolButtonAttribute), false)[0];

                var button = new ChartToolButtonSettings
                {
                    Image          = (int)attrBtn.ImageIndex,
                    DefaultVisible = attrBtn.DefaultTool,
                    Title          = attrBtn.Title,
                    ButtonType     = ChartToolButtonSettings.ToolButtonType.Chart,
                    Tool           = attrBtn.Tool
                };

                foreach (SeriesToolButtonParamAttribute ptrAttr in t.GetCustomAttributes(typeof(SeriesToolButtonParamAttribute), false))
                {
                    button.toolParams.Add(new SeriesEditParameter
                    {
                        Name         = ptrAttr.ParamName,
                        title        = ptrAttr.ParamTitle,
                        defaultValue = ptrAttr.DefaultValue,
                        paramType    = ptrAttr.ParamType
                    });
                }
                allButtons.Add(button);
            }

            // терминальные кнопки
            foreach (var systemButtonType in Enum.GetValues(typeof(SystemToolButton)).Cast <SystemToolButton>().ToList())
            {
                if (systemButtonType == SystemToolButton.None)
                {
                    continue;
                }
                var systemButton = CommonToolButtonName.GetButtonDisplayName(systemButtonType);
                if (HiddenModes.ManagerMode || !systemButton.b)
                {
                    allButtons.Add(new ChartToolButtonSettings
                    {
                        Title          = systemButton.a,
                        Image          = CommonToolButtonName.buttonImageIndex[systemButtonType],
                        DefaultVisible = true,
                        ButtonType     = ChartToolButtonSettings.ToolButtonType.System,
                        SystemTool     = systemButtonType
                    });
                }
            }
        }
        public static ChartToolButtonSettings LoadFromXml(XmlElement node, List <ToolButtonGroup> groups, ToolButtonType defaultButtonType)
        {
            /*if (node.Attributes["tool"] == null && node.Attributes["systemTool"] == null) // игнорируем - все равно не будет работать
             *  return null;*/
            var imgIndex    = node.Attributes["image"].Value.ToInt();
            var isShowName  = node.Attributes["showDisplayName"] != null && node.Attributes["showDisplayName"].Value.ToBool();
            var groupTitle  = node.Attributes["group"] == null ? string.Empty : node.Attributes["group"].Value;
            var displayName = node.Attributes["displayName"] == null ? string.Empty : node.Attributes["displayName"].Value;
            var type        = defaultButtonType;

            if (node.Attributes["type"] != null)
            {
                Enum.TryParse(node.Attributes["type"].Value, out type);
            }
            var tool = CandleChartControl.ChartTool.None;

            if (node.Attributes["tool"] != null)
            {
                Enum.TryParse(node.Attributes["tool"].Value, out tool);
            }
            var systemTool = SystemToolButton.None;

            if (node.Attributes["systemTool"] != null)
            {
                Enum.TryParse(node.Attributes["systemTool"].Value, out systemTool);
            }

            // поддержка старых версий файлов
            var title = node.Attributes["title"] == null ? string.Empty : node.Attributes["title"].Value;

            var btn = new ChartToolButtonSettings
            {
                Image = imgIndex,
                Title = title, // поддержка старых версий файлов
                IsVisibleDisplayName = isShowName,
                DisplayName          = displayName,
                ButtonType           = type,
                Tool       = type == ToolButtonType.Chart ? tool : CandleChartControl.ChartTool.None,
                SystemTool = type == ToolButtonType.System ? systemTool : SystemToolButton.None
            };

            if (!string.IsNullOrEmpty(groupTitle))
            {
                btn.Group = groups.FirstOrDefault(g => g.Title == groupTitle);
                // корректируем тип группы
                if (btn.Group != null)
                {
                    btn.Group.ButtonType = btn.ButtonType;
                }
            }

            foreach (XmlElement ptrNode in node.ChildNodes)
            {
                if (ptrNode.Attributes["name"] == null || ptrNode.Attributes["paramType"] == null ||
                    ptrNode.Attributes["value"] == null)
                {
                    continue;
                }
                var ptrName  = ptrNode.Attributes["name"].Value;
                var ptrType  = Type.GetType(ptrNode.Attributes["paramType"].Value);
                var ptrValue = StringFormatter.StringToObject(ptrNode.Attributes["value"].Value, ptrType);
                btn.toolParams.Add(new SeriesEditParameter(ptrName, ptrType, ptrValue));
            }

            return(btn);
        }