using MuchInfo.Chart.Data.EnumTypes; using MuchInfo.Chart.Infrastructure.Helpers; using MuchInfo.Chart.WPF.Helpers; using MuchInfo.Chart.WPF.Primitives.Drawing; using MuchInfo.Chart.WPF.Primitives.Interfaces; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace MuchInfo.Chart.WPF.Controls.Drawing { /// /// ChartDrawingToolbar.xaml 的交互逻辑 /// public partial class ChartDrawingToolbar : UserControl { #region Fields private IDrawingTool currentDrawingTool; private bool flag; private bool showNarrow; #endregion Fields #region Constructors internal ChartDrawingToolbar(Chart parentChart) { this.InitializeComponent(); this.flag = false; this.showNarrow = false; this.ParentChart = parentChart; this.InitializeToolbars(); //设成透明色 this.Background = new SolidColorBrush(Colors.Transparent); //this.Background = parentChart.ChartBackground; this.FontSize = parentChart.ChartFontSize; this.Foreground = parentChart.IsDarkBackground ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Colors.Black); } #endregion Constructors #region Delegates public delegate void VisibleChangedEventHandler(); #endregion Delegates #region Events public event ChartDrawingToolbar.VisibleChangedEventHandler VisibleChanged; #endregion Events #region Properties #region Public Properties public Chart ParentChart { get; set; } public bool ShowNarrow { get { return this.showNarrow; } set { this.showNarrow = value; this.InitializeToolbars(); } } public bool Visible { get { return this.borderToolbar.Visibility == Visibility.Visible; } set { bool flag = this.borderToolbar.Visibility == Visibility.Visible; if (flag != value) { bool flag3; if (value) { this.borderToolbar.Visibility = Visibility.Visible; flag3 = (this.currentDrawingTool != null); if (flag3) { ParentChart.CurrentDrawingTool = this.currentDrawingTool; } } else { this.currentDrawingTool = ParentChart.CurrentDrawingTool; this.borderToolbar.Visibility = Visibility.Collapsed; } OnVisibleChanged(); } } } #endregion Public Properties #endregion Properties #region Methods #region Internal Methods internal void ButtonPress() { SetIsNotPressed(); if (ParentChart.PointerType == PointerType.Drawing) { this.SetIsPressedByRootName(); } else { this.SetIsPressedByPointerType(); } } internal void ButtonSelectChanged(ChartDrawingToolbarButton aBtn) { PointerType command = aBtn.Command; bool flag = command == PointerType.Drawing; if (flag) { ParentChart.CurrentDrawingTool = aBtn.DrawingTarget; } else { ParentChart.PointerType = aBtn.Command; } this.ButtonPress(); } internal bool CanShowNarrow() { return this.theStack2.Visibility == Visibility.Collapsed; } /// /// 根据画图类型找到相应的button /// /// The type. internal ChartDrawingToolbarButton GetToolbarButton(DrawingToolType type) { if (this.theStack1 != null && this.theStack1.Children != null && this.theStack1.Children.Count > 0) { var buttons = this.theStack1.Children.OfType(); var current = buttons.FirstOrDefault(z => z.DrawingToolType == type); if (current != null) return current; } if (this.theStack2 != null && this.theStack2.Children != null && this.theStack2.Children.Count > 0) { var buttons = this.theStack2.Children.OfType(); return buttons.FirstOrDefault(button => button.DrawingToolType == type); } return null; } /// /// 根据画线工具位置调整stackpanel的orientation /// internal void UpdateLayout(Orientation orientation) { this.theStack1.Orientation = orientation; this.theStack2.Orientation = orientation; } #endregion Internal Methods #region Private Methods private void AddDrawingToolbarButton(ImageSource imageSource, PointerType command, string labelText, string toolTip, DrawingToolType toolType, IDrawingTool target = null) { var button = new ChartDrawingToolbarButton(this) { ButtonImage = { Source = imageSource }, Command = command, //LabelText = labelText, ToolTip = toolTip, ToolTipText = toolTip, DrawingTarget = target, DrawingToolType = toolType }; bool flag = this.flag || this.theStack2.Visibility == Visibility.Collapsed; if (flag) { this.theStack1.Children.Add(button); } else { this.theStack2.Children.Add(button); } flag = (this.theStack2.Visibility == Visibility.Visible || this.showNarrow); if (flag) { button.LabelText = ""; } this.flag = !this.flag; } private void borderToolbar_SizeChanged(object sender, SizeChangedEventArgs e) { bool flag = this.theStack2.Visibility == Visibility.Visible; if (flag) { bool flag2 = this.borderToolbar.ActualHeight >= this.theStack1.ActualHeight + this.theStack2.ActualHeight; if (flag2) { this.theStack2.Visibility = Visibility.Collapsed; this.InitializeToolbars(); } else { this.borderToolbar.Height = this.theStack1.ActualHeight + this.theStack2.ActualHeight; } } else { bool flag2 = this.borderToolbar.ActualHeight < this.theStack1.ActualHeight; if (flag2) { this.theStack2.Visibility = Visibility.Visible; this.InitializeToolbars(); } else { this.borderToolbar.Height = this.theStack1.ActualHeight + this.theStack2.ActualHeight; } } } private IEnumerable CreateDrawingTools() { var list = new List(); var list2 = new List(); list.Add(new DrawingTrendLine()); list.Add(new DrawingText()); //list.Add(new drawingFibArc()); list.Add(new DrawingFibFan()); list.Add(new DrawingFibRetracement()); list.Add(new DrawingFibTimeZones()); list2.Add(new DrawingHorizontalLine()); list2.Add(new DrawingVerticalLine()); //list2.Add(new drawingRegressionChannel()); //list2.Add(new drawingRegressionLine()); list2.Add(new DrawingGannFann()); list2.Add(new DrawingGannBox()); //list2.Add(new drawingAndrewsPitchfork()); list2.Add(new DrawingQuadrantLines()); //list2.Add(new drawingRaffRegression()); list2.Add(new DrawingSpeedLines()); list2.Add(new DrawingTironeLevels()); list2.Add(new DrawingErrorChannel()); list2.Sort(new ChartDrawingToolbar.DrawingToolComparer()); list.AddRange(list2); list.Add(new DrawingEllipse()); list.Add(new DrawingRectangle()); list.Add(new DrawingArrow()); return list; } private void InitializeToolbars() { this.theStack1.Children.Clear(); this.theStack2.Children.Clear(); this.AddDrawingToolbarButton(ImageHelper.GetImage("CrossHair.png"), PointerType.CursorWithData, LanguageManager.FindResource(LanguageConst.DrawingTool_Title_Cross), LanguageManager.FindResource(LanguageConst.DrawingTool_Title_CrossToolTip), DrawingToolType.Cross); this.AddDrawingToolbarButton(ImageHelper.GetImage("CrossHair.png"), PointerType.Cursor, LanguageManager.FindResource(LanguageConst.DrawingTool_Title_Plain), LanguageManager.FindResource(LanguageConst.DrawingTool_Title_PlainToolTip), DrawingToolType.Plain); this.AddDrawingToolbarButton(ImageHelper.GetImage("HandTransparent.png"), PointerType.Pan, LanguageManager.FindResource(LanguageConst.DrawingTool_Title_Pan), LanguageManager.FindResource(LanguageConst.DrawingTool_Title_PanToolTip), DrawingToolType.Pan); this.AddDrawingToolbarButton(ImageHelper.GetImage("Eraser.png"), PointerType.Erase, LanguageManager.FindResource(LanguageConst.DrawingTool_Title_Erase), LanguageManager.FindResource(LanguageConst.DrawingTool_Title_EraseToolTip), DrawingToolType.Erase); var drawingTools = CreateDrawingTools(); foreach (var current in drawingTools) { this.AddDrawingToolbarButton(current.Icon(), PointerType.Drawing, current.Abbreviation(), current.Description(), current.ToolType(), current); } this.ButtonPress(); } private void OnVisibleChanged() { if (VisibleChanged != null) { VisibleChanged(); } } private void SetIsNotPressed() { if (this.theStack1 != null && this.theStack1.Children != null) { foreach (var item in this.theStack1.Children) { var frameworkElement = (FrameworkElement)item; if (frameworkElement is ChartDrawingToolbarButton) { ((ChartDrawingToolbarButton)frameworkElement).IsPressed = false; } } } if (this.theStack2 != null && this.theStack2.Children != null) { foreach (var item in this.theStack1.Children) { var frameworkElement = (FrameworkElement)item; flag = (frameworkElement is ChartDrawingToolbarButton); if (frameworkElement is ChartDrawingToolbarButton) { ((ChartDrawingToolbarButton)frameworkElement).IsPressed = false; } } } } private void SetIsPressedByPointerType() { if (this.theStack1 != null && this.theStack1.Children != null) { foreach (var item in this.theStack1.Children) { var button = (ChartDrawingToolbarButton)item; flag = (button != null && ParentChart != null && button.Command == ParentChart.PointerType); if (flag) { button.IsPressed = true; } } } if (this.theStack2 != null && this.theStack2.Children != null) { foreach (var item in this.theStack2.Children) { var button = (ChartDrawingToolbarButton)item; flag = (button != null && ParentChart != null && button.Command == ParentChart.PointerType); if (flag) { button.IsPressed = true; } } } } private void SetIsPressedByRootName() { if (this.theStack1 != null && this.theStack1.Children != null) { foreach (var item in this.theStack1.Children) { var button = (ChartDrawingToolbarButton)item; flag = button.DrawingTarget != null && ParentChart != null && ParentChart.CurrentDrawingTool != null && button.DrawingTarget != null && System.String.CompareOrdinal(ParentChart.CurrentDrawingTool.RootName, button.DrawingTarget.RootName) == 0; if (flag) { button.IsPressed = true; } } } if (this.theStack2 != null && this.theStack2.Children != null) { foreach (var item in this.theStack2.Children) { var button = (ChartDrawingToolbarButton)item; flag = button.DrawingTarget != null && ParentChart != null && ParentChart.CurrentDrawingTool != null && button.DrawingTarget != null && System.String.CompareOrdinal(ParentChart.CurrentDrawingTool.RootName, button.DrawingTarget.RootName) == 0; if (flag) { button.IsPressed = true; } } } } #endregion Private Methods #endregion Methods #region Nested Types private class DrawingToolComparer : IComparer { #region Constructors public DrawingToolComparer() { } #endregion Constructors #region Methods #region Public Methods public int Compare(IDrawingTool x, IDrawingTool y) { return x.Abbreviation().CompareTo(y.Abbreviation()); } #endregion Public Methods #endregion Methods } #endregion Nested Types } }