DrawingSelectionGrab.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using MuchInfo.Chart.Data.EnumTypes;
  2. using MuchInfo.Chart.Infrastructure.Helpers;
  3. using MuchInfo.Chart.WPF.Controls.Utilities;
  4. using MuchInfo.Chart.WPF.Helpers;
  5. using MuchInfo.Chart.WPF.Primitives.Drawing;
  6. using MuchInfo.Chart.WPF.Primitives.Interfaces;
  7. using System;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. namespace MuchInfo.Chart.WPF.Controls.Drawing
  13. {
  14. /// <summary>
  15. /// DrawingSelectionGrab.xaml 的交互逻辑
  16. /// </summary>
  17. public partial class DrawingSelectionGrab : UserControl
  18. {
  19. #region Fields
  20. private Chart _chart;
  21. private IDrawingTool _drawingTool;
  22. private bool _flag1;
  23. private bool _flag2;
  24. private bool _isMouseEnter;
  25. private Point _position;
  26. #endregion Fields
  27. #region Constructors
  28. //private bool DrawingSelectionGrab_854;
  29. public DrawingSelectionGrab()
  30. {
  31. this._isMouseEnter = false;
  32. this._flag1 = false;
  33. this.InitializeComponent();
  34. this.mSelectionLine.Cursor = (Cursors.Hand);
  35. }
  36. #endregion Constructors
  37. #region Methods
  38. #region Public Methods
  39. public void SetPosition(Point p1, Point p2)
  40. {
  41. var newP1 = AdjustPointByPaintArea(p1);
  42. var newP2 = AdjustPointByPaintArea(p2);
  43. this.mSelectionLine.X1 = newP1.X;
  44. this.mSelectionLine.Y1 = newP1.Y;
  45. this.mSelectionLine.X2 = newP2.X;
  46. this.mSelectionLine.Y2 = newP2.Y;
  47. }
  48. public void Setup(IDrawingTool aTool, Chart theChart)
  49. {
  50. this._drawingTool = aTool;
  51. this._chart = theChart;
  52. }
  53. #endregion Public Methods
  54. #region Internal Methods
  55. internal Point AdjustPointByPaintArea(Point point)
  56. {
  57. var result = new Point()
  58. {
  59. X = point.X,
  60. Y = point.Y
  61. };
  62. var paintArea = this.GetPaintAreaCanvas();
  63. if (paintArea == null) return result;
  64. var x = paintArea.ActualWidth;
  65. var y = paintArea.ActualHeight - 6;
  66. result.X = result.X < 0 ? 0 : result.X;
  67. result.Y = result.Y < 0 ? 0 : result.Y;
  68. result.X = result.X > x ? x : result.X;
  69. result.Y = result.Y > y ? y : result.Y;
  70. return result;
  71. }
  72. #endregion Internal Methods
  73. #region Private Methods
  74. /// <summary>
  75. /// Builds the context menu.
  76. /// </summary>
  77. private void ShowContextMenu()
  78. {
  79. var contextMenu = new ContextMenu();
  80. contextMenu.Items.Add(MenuItemHelper.CreateMenuItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Edit), ImageHelper.GetImage("PropertiesHS.png"), new EventHandler(this.ShowToolEdit)));
  81. bool flag = this._drawingTool is DrawingTrendLine;
  82. if (flag)
  83. {
  84. contextMenu.Items.Add(MenuItemHelper.CreateMenuItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_CreateParallelLine), null, new EventHandler(this.CreateParallel)));
  85. }
  86. ////flag = Conversions.ToBoolean((!Conversions.ToBoolean(this._drawingTool is DrawingTrendLine && ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3758()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true);
  87. //flag = (this._drawingTool is DrawingTrendLine && ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3758());
  88. //if (flag)
  89. //{
  90. // PopupMenu.AddItem("Set Alert", ImageHelper.GetImage("SymbolBolt.png"), new EventHandler(this.SetAlert));
  91. //}
  92. //flag = false;// Conversions.ToBoolean((!Conversions.ToBoolean(this._drawingTool is DrawingHorizontalLine && ((DrawingHorizontalLine)this._drawingTool).drawingHorizontalLine_3532()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true);
  93. //if (flag)
  94. //{
  95. // PopupMenu.AddItem("Set Alert", ImageHelper.GetImage("SymbolBolt.png"), new EventHandler(this.SetPriceAlert));
  96. //}
  97. contextMenu.Items.Add(new Separator());
  98. contextMenu.Items.Add(MenuItemHelper.CreateMenuItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Remove), ImageHelper.GetImage("DeleteHS.png"), new EventHandler(this.RemoveTool)));
  99. this.ContextMenu = contextMenu;
  100. this.ContextMenu.IsOpen = true;
  101. }
  102. /// <summary>
  103. /// Builds the popup menu.
  104. /// </summary>
  105. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  106. private void ShowPopupMenu(MouseEventArgs e)
  107. {
  108. var popupMenu = new PopupMenu(this._chart.RootPanel) { FontSize = this._drawingTool.GetChartFontSize() };
  109. popupMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Edit), ImageHelper.GetImage("PropertiesHS.png"), new EventHandler(this.ShowToolEdit));
  110. bool flag = this._drawingTool is DrawingTrendLine;
  111. if (flag)
  112. {
  113. popupMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_CreateParallelLine), null, new EventHandler(this.CreateParallel));
  114. }
  115. ////flag = Conversions.ToBoolean((!Conversions.ToBoolean(this._drawingTool is DrawingTrendLine && ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3758()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true);
  116. //flag = (this._drawingTool is DrawingTrendLine && ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3758());
  117. //if (flag)
  118. //{
  119. // PopupMenu.AddItem("Set Alert", ImageHelper.GetImage("SymbolBolt.png"), new EventHandler(this.SetAlert));
  120. //}
  121. //flag = false;// Conversions.ToBoolean((!Conversions.ToBoolean(this._drawingTool is DrawingHorizontalLine && ((DrawingHorizontalLine)this._drawingTool).drawingHorizontalLine_3532()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true);
  122. //if (flag)
  123. //{
  124. // PopupMenu.AddItem("Set Alert", ImageHelper.GetImage("SymbolBolt.png"), new EventHandler(this.SetPriceAlert));
  125. //}
  126. popupMenu.AddSeperator();
  127. popupMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Remove), ImageHelper.GetImage("DeleteHS.png"), new EventHandler(this.RemoveTool));
  128. var offset = new Point(-5.0, -5.0);
  129. popupMenu.Show(e, offset);
  130. }
  131. private void CreateParallel(object sender, EventArgs e)
  132. {
  133. var trendLine = _drawingTool as DrawingTrendLine;
  134. if (trendLine == null) return;
  135. trendLine.CreateParallel();
  136. }
  137. /// <summary>
  138. /// 获取ChartPane的PaintArea Canvas
  139. /// </summary>
  140. /// <returns>PaintArea Canvas</returns>
  141. private FrameworkElement GetPaintAreaCanvas()
  142. {
  143. var parent = this.Parent as FrameworkElement;
  144. if (parent == null) return null;
  145. var grandparent = parent.Parent as FrameworkElement;
  146. if (grandparent == null) return null;
  147. var paintArea = grandparent.Parent as FrameworkElement;
  148. return paintArea;
  149. }
  150. private void MovePlot(Point curPos)
  151. {
  152. if (_drawingTool == null) return;
  153. var newPositon = AdjustPointByPaintArea(curPos);
  154. var paintArea = this.GetPaintAreaCanvas();
  155. if (paintArea != null)
  156. {
  157. var maxPoint = new Point()
  158. {
  159. X = paintArea.ActualWidth,
  160. Y = paintArea.ActualHeight - 6
  161. };
  162. this._drawingTool.MovePlot(this._position, newPositon, maxPoint);
  163. }
  164. else
  165. {
  166. this._drawingTool.MovePlot(this._position, newPositon);
  167. }
  168. }
  169. private void mSelectionLine_LostMouseCapture(object sender, MouseEventArgs e)
  170. {
  171. this._chart.LockPainting = false;
  172. this._flag1 = false;
  173. this._chart.Refresh();
  174. this.UpdateSelectionLine();
  175. }
  176. private void mSelectionLine_MouseEnter(object sender, MouseEventArgs e)
  177. {
  178. if (this._chart.PointerType != PointerType.Drawing)
  179. {
  180. this._isMouseEnter = true;
  181. this.UpdateSelectionLine();
  182. }
  183. else
  184. {
  185. this.mSelectionLine.Cursor = Cursors.None;
  186. }
  187. }
  188. private void mSelectionLine_MouseLeave(object sender, MouseEventArgs e)
  189. {
  190. this._isMouseEnter = false;
  191. this.UpdateSelectionLine();
  192. }
  193. private void mSelectionLine_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  194. {
  195. bool flag = this._chart.PointerType == PointerType.Drawing;
  196. if (!flag)
  197. {
  198. this._chart.LockPainting = true;
  199. this._position = e.GetPosition((UIElement)this.mSelectionLine.Parent);
  200. this._flag1 = true;
  201. this._flag2 = false;
  202. this.mSelectionLine.CaptureMouse();
  203. flag = (this._drawingTool != null);
  204. if (flag)
  205. {
  206. this._drawingTool.PrepareToMove();
  207. }
  208. }
  209. }
  210. private void mSelectionLine_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  211. {
  212. this._chart.LockPainting = false;
  213. bool flag = this._drawingTool != null && !this._flag2;
  214. if (flag)
  215. {
  216. var flag2 = this._chart.PointerType == PointerType.Erase;
  217. if (flag2)
  218. {
  219. this._drawingTool.Delete();
  220. }
  221. else
  222. {
  223. //右键菜单
  224. //this.BuildPopupMenu(e);
  225. }
  226. }
  227. else
  228. {
  229. bool flag2 = this._drawingTool != null;
  230. if (flag2)
  231. {
  232. this.MovePlot(e.GetPosition((UIElement)this.mSelectionLine.Parent));
  233. flag2 = (this._drawingTool.OwnerSpec != null);
  234. if (flag2)
  235. {
  236. this._drawingTool.OwnerSpec.ToolChanged(this._drawingTool);
  237. }
  238. this._flag1 = false;
  239. //不用重新画图,MouseMove处理了MovePlot
  240. //this._chart.Refresh();
  241. }
  242. }
  243. this.mSelectionLine.ReleaseMouseCapture();
  244. this._flag1 = false;
  245. this._flag2 = false;
  246. this.UpdateSelectionLine();
  247. }
  248. private void mSelectionLine_MouseMove(object sender, MouseEventArgs e)
  249. {
  250. if (this._chart != null && this._chart.PointerType == PointerType.Erase)
  251. {
  252. this._chart.SetCursorImagePos(true, e);
  253. }
  254. var flag = this._flag1;
  255. if (flag)
  256. {
  257. Point position = e.GetPosition((UIElement)this.mSelectionLine.Parent);
  258. float num = GeometryHelper.DistanceBetweenPoints(this._position, position);
  259. flag = (num > 8f);
  260. if (flag)
  261. {
  262. this._flag2 = true;
  263. }
  264. flag = this._flag2;
  265. if (flag)
  266. {
  267. this.MovePlot(position);
  268. }
  269. }
  270. }
  271. /// <summary>
  272. /// Up弹出Popup,PopupManager里用Down关闭Popup
  273. /// </summary>
  274. /// <param name="sender">The source of the event.</param>
  275. /// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
  276. private void mSelectionLine_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  277. {
  278. this._chart.LockPainting = false;
  279. if (this._drawingTool == null) return;
  280. ////使用上下文菜单代替Popup,以免以免父控件有右键菜单
  281. //this.ShowContextMenu();
  282. //修改popup父容器,即每个Chart提供RootPanel,不使用PopupRoot和TrueRootVisual
  283. //因为应用程序可能开多个图表窗口
  284. this.ShowPopupMenu(e);
  285. e.Handled = true;
  286. }
  287. //private void SetAlert(object sender, EventArgs e)
  288. //{
  289. // try
  290. // {
  291. // ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3759((MouseEventArgs)e);
  292. // }
  293. // catch (Exception expr_3F)
  294. // {
  295. // ProjectData.SetProjectError(expr_3F);
  296. // ProjectData.ClearProjectError();
  297. // }
  298. //}
  299. //private void SetPriceAlert(object sender, EventArgs e)
  300. //{
  301. // try
  302. // {
  303. // //((DrawingHorizontalLine)this._drawingTool).drawingHorizontalLine_3533((MouseEventArgs)e);
  304. // }
  305. // catch (Exception expr_3F)
  306. // {
  307. // ProjectData.SetProjectError(expr_3F);
  308. // ProjectData.ClearProjectError();
  309. // }
  310. //}
  311. private void RemoveTool(object sender, EventArgs e)
  312. {
  313. this._drawingTool.Delete();
  314. }
  315. private void ShowToolEdit(object sender, EventArgs e)
  316. {
  317. this._drawingTool.ShowEditor((MouseEventArgs)e);
  318. }
  319. private void UpdateSelectionLine()
  320. {
  321. bool flag = this._isMouseEnter || this._flag1;
  322. if (flag)
  323. {
  324. this.mSelectionLine.Stroke = (new SolidColorBrush(Color.FromArgb(125, 125, 125, 125)));
  325. switch (this._chart.PointerType)
  326. {
  327. case PointerType.Erase:
  328. this.mSelectionLine.Cursor = Cursors.Arrow;
  329. break;
  330. case PointerType.Drawing:
  331. this.mSelectionLine.Cursor = Cursors.None;
  332. break;
  333. default:
  334. this.mSelectionLine.Cursor = Cursors.Hand;
  335. break;
  336. }
  337. }
  338. else
  339. {
  340. this.mSelectionLine.Stroke = (new SolidColorBrush(Colors.Transparent));
  341. }
  342. }
  343. #endregion Private Methods
  344. #endregion Methods
  345. }
  346. }