ChartDrawingToolbar.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using MuchInfo.Chart.Data.EnumTypes;
  2. using MuchInfo.Chart.Infrastructure.Helpers;
  3. using MuchInfo.Chart.WPF.Helpers;
  4. using MuchInfo.Chart.WPF.Primitives.Drawing;
  5. using MuchInfo.Chart.WPF.Primitives.Interfaces;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media;
  11. namespace MuchInfo.Chart.WPF.Controls.Drawing
  12. {
  13. /// <summary>
  14. /// ChartDrawingToolbar.xaml 的交互逻辑
  15. /// </summary>
  16. public partial class ChartDrawingToolbar : UserControl
  17. {
  18. #region Fields
  19. private IDrawingTool currentDrawingTool;
  20. private bool flag;
  21. private bool showNarrow;
  22. #endregion Fields
  23. #region Constructors
  24. internal ChartDrawingToolbar(Chart parentChart)
  25. {
  26. this.InitializeComponent();
  27. this.flag = false;
  28. this.showNarrow = false;
  29. this.ParentChart = parentChart;
  30. this.InitializeToolbars();
  31. //设成透明色
  32. this.Background = new SolidColorBrush(Colors.Transparent);
  33. //this.Background = parentChart.ChartBackground;
  34. this.FontSize = parentChart.ChartFontSize;
  35. this.Foreground = parentChart.IsDarkBackground ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Colors.Black);
  36. }
  37. #endregion Constructors
  38. #region Delegates
  39. public delegate void VisibleChangedEventHandler();
  40. #endregion Delegates
  41. #region Events
  42. public event ChartDrawingToolbar.VisibleChangedEventHandler VisibleChanged;
  43. #endregion Events
  44. #region Properties
  45. #region Public Properties
  46. public Chart ParentChart
  47. {
  48. get;
  49. set;
  50. }
  51. public bool ShowNarrow
  52. {
  53. get
  54. {
  55. return this.showNarrow;
  56. }
  57. set
  58. {
  59. this.showNarrow = value;
  60. this.InitializeToolbars();
  61. }
  62. }
  63. public bool Visible
  64. {
  65. get
  66. {
  67. return this.borderToolbar.Visibility == Visibility.Visible;
  68. }
  69. set
  70. {
  71. bool flag = this.borderToolbar.Visibility == Visibility.Visible;
  72. if (flag != value)
  73. {
  74. bool flag3;
  75. if (value)
  76. {
  77. this.borderToolbar.Visibility = Visibility.Visible;
  78. flag3 = (this.currentDrawingTool != null);
  79. if (flag3)
  80. {
  81. ParentChart.CurrentDrawingTool = this.currentDrawingTool;
  82. }
  83. }
  84. else
  85. {
  86. this.currentDrawingTool = ParentChart.CurrentDrawingTool;
  87. this.borderToolbar.Visibility = Visibility.Collapsed;
  88. }
  89. OnVisibleChanged();
  90. }
  91. }
  92. }
  93. #endregion Public Properties
  94. #endregion Properties
  95. #region Methods
  96. #region Internal Methods
  97. internal void ButtonPress()
  98. {
  99. SetIsNotPressed();
  100. if (ParentChart.PointerType == PointerType.Drawing)
  101. {
  102. this.SetIsPressedByRootName();
  103. }
  104. else
  105. {
  106. this.SetIsPressedByPointerType();
  107. }
  108. }
  109. internal void ButtonSelectChanged(ChartDrawingToolbarButton aBtn)
  110. {
  111. PointerType command = aBtn.Command;
  112. bool flag = command == PointerType.Drawing;
  113. if (flag)
  114. {
  115. ParentChart.CurrentDrawingTool = aBtn.DrawingTarget;
  116. }
  117. else
  118. {
  119. ParentChart.PointerType = aBtn.Command;
  120. }
  121. this.ButtonPress();
  122. }
  123. internal bool CanShowNarrow()
  124. {
  125. return this.theStack2.Visibility == Visibility.Collapsed;
  126. }
  127. /// <summary>
  128. /// 根据画图类型找到相应的button
  129. /// </summary>
  130. /// <param name="type">The type.</param>
  131. internal ChartDrawingToolbarButton GetToolbarButton(DrawingToolType type)
  132. {
  133. if (this.theStack1 != null && this.theStack1.Children != null && this.theStack1.Children.Count > 0)
  134. {
  135. var buttons = this.theStack1.Children.OfType<ChartDrawingToolbarButton>();
  136. var current = buttons.FirstOrDefault(z => z.DrawingToolType == type);
  137. if (current != null) return current;
  138. }
  139. if (this.theStack2 != null && this.theStack2.Children != null && this.theStack2.Children.Count > 0)
  140. {
  141. var buttons = this.theStack2.Children.OfType<ChartDrawingToolbarButton>();
  142. return buttons.FirstOrDefault(button => button.DrawingToolType == type);
  143. }
  144. return null;
  145. }
  146. /// <summary>
  147. /// 根据画线工具位置调整stackpanel的orientation
  148. /// </summary>
  149. internal void UpdateLayout(Orientation orientation)
  150. {
  151. this.theStack1.Orientation = orientation;
  152. this.theStack2.Orientation = orientation;
  153. }
  154. #endregion Internal Methods
  155. #region Private Methods
  156. private void AddDrawingToolbarButton(ImageSource imageSource, PointerType command, string labelText, string toolTip, DrawingToolType toolType, IDrawingTool target = null)
  157. {
  158. var button = new ChartDrawingToolbarButton(this)
  159. {
  160. ButtonImage = { Source = imageSource },
  161. Command = command,
  162. //LabelText = labelText,
  163. ToolTip = toolTip,
  164. ToolTipText = toolTip,
  165. DrawingTarget = target,
  166. DrawingToolType = toolType
  167. };
  168. bool flag = this.flag || this.theStack2.Visibility == Visibility.Collapsed;
  169. if (flag)
  170. {
  171. this.theStack1.Children.Add(button);
  172. }
  173. else
  174. {
  175. this.theStack2.Children.Add(button);
  176. }
  177. flag = (this.theStack2.Visibility == Visibility.Visible || this.showNarrow);
  178. if (flag)
  179. {
  180. button.LabelText = "";
  181. }
  182. this.flag = !this.flag;
  183. }
  184. private void borderToolbar_SizeChanged(object sender, SizeChangedEventArgs e)
  185. {
  186. bool flag = this.theStack2.Visibility == Visibility.Visible;
  187. if (flag)
  188. {
  189. bool flag2 = this.borderToolbar.ActualHeight >= this.theStack1.ActualHeight + this.theStack2.ActualHeight;
  190. if (flag2)
  191. {
  192. this.theStack2.Visibility = Visibility.Collapsed;
  193. this.InitializeToolbars();
  194. }
  195. else
  196. {
  197. this.borderToolbar.Height = this.theStack1.ActualHeight + this.theStack2.ActualHeight;
  198. }
  199. }
  200. else
  201. {
  202. bool flag2 = this.borderToolbar.ActualHeight < this.theStack1.ActualHeight;
  203. if (flag2)
  204. {
  205. this.theStack2.Visibility = Visibility.Visible;
  206. this.InitializeToolbars();
  207. }
  208. else
  209. {
  210. this.borderToolbar.Height = this.theStack1.ActualHeight + this.theStack2.ActualHeight;
  211. }
  212. }
  213. }
  214. private IEnumerable<IDrawingTool> CreateDrawingTools()
  215. {
  216. var list = new List<IDrawingTool>();
  217. var list2 = new List<IDrawingTool>();
  218. list.Add(new DrawingTrendLine());
  219. list.Add(new DrawingText());
  220. //list.Add(new drawingFibArc());
  221. list.Add(new DrawingFibFan());
  222. list.Add(new DrawingFibRetracement());
  223. list.Add(new DrawingFibTimeZones());
  224. list2.Add(new DrawingHorizontalLine());
  225. list2.Add(new DrawingVerticalLine());
  226. //list2.Add(new drawingRegressionChannel());
  227. //list2.Add(new drawingRegressionLine());
  228. list2.Add(new DrawingGannFann());
  229. list2.Add(new DrawingGannBox());
  230. //list2.Add(new drawingAndrewsPitchfork());
  231. list2.Add(new DrawingQuadrantLines());
  232. //list2.Add(new drawingRaffRegression());
  233. list2.Add(new DrawingSpeedLines());
  234. list2.Add(new DrawingTironeLevels());
  235. list2.Add(new DrawingErrorChannel());
  236. list2.Sort(new ChartDrawingToolbar.DrawingToolComparer());
  237. list.AddRange(list2);
  238. list.Add(new DrawingEllipse());
  239. list.Add(new DrawingRectangle());
  240. list.Add(new DrawingArrow());
  241. return list;
  242. }
  243. private void InitializeToolbars()
  244. {
  245. this.theStack1.Children.Clear();
  246. this.theStack2.Children.Clear();
  247. this.AddDrawingToolbarButton(ImageHelper.GetImage("CrossHair.png"), PointerType.CursorWithData,
  248. LanguageManager.FindResource(LanguageConst.DrawingTool_Title_Cross),
  249. LanguageManager.FindResource(LanguageConst.DrawingTool_Title_CrossToolTip),
  250. DrawingToolType.Cross);
  251. this.AddDrawingToolbarButton(ImageHelper.GetImage("CrossHair.png"), PointerType.Cursor,
  252. LanguageManager.FindResource(LanguageConst.DrawingTool_Title_Plain),
  253. LanguageManager.FindResource(LanguageConst.DrawingTool_Title_PlainToolTip),
  254. DrawingToolType.Plain);
  255. this.AddDrawingToolbarButton(ImageHelper.GetImage("HandTransparent.png"), PointerType.Pan,
  256. LanguageManager.FindResource(LanguageConst.DrawingTool_Title_Pan),
  257. LanguageManager.FindResource(LanguageConst.DrawingTool_Title_PanToolTip),
  258. DrawingToolType.Pan);
  259. this.AddDrawingToolbarButton(ImageHelper.GetImage("Eraser.png"), PointerType.Erase,
  260. LanguageManager.FindResource(LanguageConst.DrawingTool_Title_Erase),
  261. LanguageManager.FindResource(LanguageConst.DrawingTool_Title_EraseToolTip),
  262. DrawingToolType.Erase);
  263. var drawingTools = CreateDrawingTools();
  264. foreach (var current in drawingTools)
  265. {
  266. this.AddDrawingToolbarButton(current.Icon(), PointerType.Drawing, current.Abbreviation(), current.Description(), current.ToolType(), current);
  267. }
  268. this.ButtonPress();
  269. }
  270. private void OnVisibleChanged()
  271. {
  272. if (VisibleChanged != null)
  273. {
  274. VisibleChanged();
  275. }
  276. }
  277. private void SetIsNotPressed()
  278. {
  279. if (this.theStack1 != null && this.theStack1.Children != null)
  280. {
  281. foreach (var item in this.theStack1.Children)
  282. {
  283. var frameworkElement = (FrameworkElement)item;
  284. if (frameworkElement is ChartDrawingToolbarButton)
  285. {
  286. ((ChartDrawingToolbarButton)frameworkElement).IsPressed = false;
  287. }
  288. }
  289. }
  290. if (this.theStack2 != null && this.theStack2.Children != null)
  291. {
  292. foreach (var item in this.theStack1.Children)
  293. {
  294. var frameworkElement = (FrameworkElement)item;
  295. flag = (frameworkElement is ChartDrawingToolbarButton);
  296. if (frameworkElement is ChartDrawingToolbarButton)
  297. {
  298. ((ChartDrawingToolbarButton)frameworkElement).IsPressed = false;
  299. }
  300. }
  301. }
  302. }
  303. private void SetIsPressedByPointerType()
  304. {
  305. if (this.theStack1 != null && this.theStack1.Children != null)
  306. {
  307. foreach (var item in this.theStack1.Children)
  308. {
  309. var button = (ChartDrawingToolbarButton)item;
  310. flag = (button != null && ParentChart != null && button.Command == ParentChart.PointerType);
  311. if (flag)
  312. {
  313. button.IsPressed = true;
  314. }
  315. }
  316. }
  317. if (this.theStack2 != null && this.theStack2.Children != null)
  318. {
  319. foreach (var item in this.theStack2.Children)
  320. {
  321. var button = (ChartDrawingToolbarButton)item;
  322. flag = (button != null && ParentChart != null && button.Command == ParentChart.PointerType);
  323. if (flag)
  324. {
  325. button.IsPressed = true;
  326. }
  327. }
  328. }
  329. }
  330. private void SetIsPressedByRootName()
  331. {
  332. if (this.theStack1 != null && this.theStack1.Children != null)
  333. {
  334. foreach (var item in this.theStack1.Children)
  335. {
  336. var button = (ChartDrawingToolbarButton)item;
  337. flag = button.DrawingTarget != null && ParentChart != null && ParentChart.CurrentDrawingTool != null &&
  338. button.DrawingTarget != null && System.String.CompareOrdinal(ParentChart.CurrentDrawingTool.RootName, button.DrawingTarget.RootName) == 0;
  339. if (flag)
  340. {
  341. button.IsPressed = true;
  342. }
  343. }
  344. }
  345. if (this.theStack2 != null && this.theStack2.Children != null)
  346. {
  347. foreach (var item in this.theStack2.Children)
  348. {
  349. var button = (ChartDrawingToolbarButton)item;
  350. flag = button.DrawingTarget != null && ParentChart != null && ParentChart.CurrentDrawingTool != null &&
  351. button.DrawingTarget != null && System.String.CompareOrdinal(ParentChart.CurrentDrawingTool.RootName, button.DrawingTarget.RootName) == 0;
  352. if (flag)
  353. {
  354. button.IsPressed = true;
  355. }
  356. }
  357. }
  358. }
  359. #endregion Private Methods
  360. #endregion Methods
  361. #region Nested Types
  362. private class DrawingToolComparer : IComparer<IDrawingTool>
  363. {
  364. #region Constructors
  365. public DrawingToolComparer()
  366. {
  367. }
  368. #endregion Constructors
  369. #region Methods
  370. #region Public Methods
  371. public int Compare(IDrawingTool x, IDrawingTool y)
  372. {
  373. return x.Abbreviation().CompareTo(y.Abbreviation());
  374. }
  375. #endregion Public Methods
  376. #endregion Methods
  377. }
  378. #endregion Nested Types
  379. }
  380. }