| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- using MuchInfo.Chart.Data.EnumTypes;
- using MuchInfo.Chart.Infrastructure.Helpers;
- using MuchInfo.Chart.WPF.Controls.Utilities;
- using MuchInfo.Chart.WPF.Helpers;
- using MuchInfo.Chart.WPF.Primitives.Drawing;
- using MuchInfo.Chart.WPF.Primitives.Interfaces;
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- namespace MuchInfo.Chart.WPF.Controls.Drawing
- {
- /// <summary>
- /// DrawingSelectionGrab.xaml 的交互逻辑
- /// </summary>
- public partial class DrawingSelectionGrab : UserControl
- {
- #region Fields
- private Chart _chart;
- private IDrawingTool _drawingTool;
- private bool _flag1;
- private bool _flag2;
- private bool _isMouseEnter;
- private Point _position;
- #endregion Fields
- #region Constructors
- //private bool DrawingSelectionGrab_854;
- public DrawingSelectionGrab()
- {
- this._isMouseEnter = false;
- this._flag1 = false;
- this.InitializeComponent();
- this.mSelectionLine.Cursor = (Cursors.Hand);
- }
- #endregion Constructors
- #region Methods
- #region Public Methods
- public void SetPosition(Point p1, Point p2)
- {
- var newP1 = AdjustPointByPaintArea(p1);
- var newP2 = AdjustPointByPaintArea(p2);
- this.mSelectionLine.X1 = newP1.X;
- this.mSelectionLine.Y1 = newP1.Y;
- this.mSelectionLine.X2 = newP2.X;
- this.mSelectionLine.Y2 = newP2.Y;
- }
- public void Setup(IDrawingTool aTool, Chart theChart)
- {
- this._drawingTool = aTool;
- this._chart = theChart;
- }
- #endregion Public Methods
- #region Internal Methods
- internal Point AdjustPointByPaintArea(Point point)
- {
- var result = new Point()
- {
- X = point.X,
- Y = point.Y
- };
- var paintArea = this.GetPaintAreaCanvas();
- if (paintArea == null) return result;
- var x = paintArea.ActualWidth;
- var y = paintArea.ActualHeight - 6;
- result.X = result.X < 0 ? 0 : result.X;
- result.Y = result.Y < 0 ? 0 : result.Y;
- result.X = result.X > x ? x : result.X;
- result.Y = result.Y > y ? y : result.Y;
- return result;
- }
- #endregion Internal Methods
- #region Private Methods
- /// <summary>
- /// Builds the context menu.
- /// </summary>
- private void ShowContextMenu()
- {
- var contextMenu = new ContextMenu();
- contextMenu.Items.Add(MenuItemHelper.CreateMenuItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Edit), ImageHelper.GetImage("PropertiesHS.png"), new EventHandler(this.ShowToolEdit)));
- bool flag = this._drawingTool is DrawingTrendLine;
- if (flag)
- {
- contextMenu.Items.Add(MenuItemHelper.CreateMenuItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_CreateParallelLine), null, new EventHandler(this.CreateParallel)));
- }
- ////flag = Conversions.ToBoolean((!Conversions.ToBoolean(this._drawingTool is DrawingTrendLine && ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3758()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true);
- //flag = (this._drawingTool is DrawingTrendLine && ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3758());
- //if (flag)
- //{
- // PopupMenu.AddItem("Set Alert", ImageHelper.GetImage("SymbolBolt.png"), new EventHandler(this.SetAlert));
- //}
- //flag = false;// Conversions.ToBoolean((!Conversions.ToBoolean(this._drawingTool is DrawingHorizontalLine && ((DrawingHorizontalLine)this._drawingTool).drawingHorizontalLine_3532()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true);
- //if (flag)
- //{
- // PopupMenu.AddItem("Set Alert", ImageHelper.GetImage("SymbolBolt.png"), new EventHandler(this.SetPriceAlert));
- //}
- contextMenu.Items.Add(new Separator());
- contextMenu.Items.Add(MenuItemHelper.CreateMenuItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Remove), ImageHelper.GetImage("DeleteHS.png"), new EventHandler(this.RemoveTool)));
- this.ContextMenu = contextMenu;
- this.ContextMenu.IsOpen = true;
- }
- /// <summary>
- /// Builds the popup menu.
- /// </summary>
- /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
- private void ShowPopupMenu(MouseEventArgs e)
- {
- var popupMenu = new PopupMenu(this._chart.RootPanel) { FontSize = this._drawingTool.GetChartFontSize() };
- popupMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Edit), ImageHelper.GetImage("PropertiesHS.png"), new EventHandler(this.ShowToolEdit));
- bool flag = this._drawingTool is DrawingTrendLine;
- if (flag)
- {
- popupMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_CreateParallelLine), null, new EventHandler(this.CreateParallel));
- }
- ////flag = Conversions.ToBoolean((!Conversions.ToBoolean(this._drawingTool is DrawingTrendLine && ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3758()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true);
- //flag = (this._drawingTool is DrawingTrendLine && ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3758());
- //if (flag)
- //{
- // PopupMenu.AddItem("Set Alert", ImageHelper.GetImage("SymbolBolt.png"), new EventHandler(this.SetAlert));
- //}
- //flag = false;// Conversions.ToBoolean((!Conversions.ToBoolean(this._drawingTool is DrawingHorizontalLine && ((DrawingHorizontalLine)this._drawingTool).drawingHorizontalLine_3532()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true);
- //if (flag)
- //{
- // PopupMenu.AddItem("Set Alert", ImageHelper.GetImage("SymbolBolt.png"), new EventHandler(this.SetPriceAlert));
- //}
- popupMenu.AddSeperator();
- popupMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Remove), ImageHelper.GetImage("DeleteHS.png"), new EventHandler(this.RemoveTool));
- var offset = new Point(-5.0, -5.0);
- popupMenu.Show(e, offset);
- }
- private void CreateParallel(object sender, EventArgs e)
- {
- var trendLine = _drawingTool as DrawingTrendLine;
- if (trendLine == null) return;
- trendLine.CreateParallel();
- }
- /// <summary>
- /// 获取ChartPane的PaintArea Canvas
- /// </summary>
- /// <returns>PaintArea Canvas</returns>
- private FrameworkElement GetPaintAreaCanvas()
- {
- var parent = this.Parent as FrameworkElement;
- if (parent == null) return null;
- var grandparent = parent.Parent as FrameworkElement;
- if (grandparent == null) return null;
- var paintArea = grandparent.Parent as FrameworkElement;
- return paintArea;
- }
- private void MovePlot(Point curPos)
- {
- if (_drawingTool == null) return;
- var newPositon = AdjustPointByPaintArea(curPos);
- var paintArea = this.GetPaintAreaCanvas();
- if (paintArea != null)
- {
- var maxPoint = new Point()
- {
- X = paintArea.ActualWidth,
- Y = paintArea.ActualHeight - 6
- };
- this._drawingTool.MovePlot(this._position, newPositon, maxPoint);
- }
- else
- {
- this._drawingTool.MovePlot(this._position, newPositon);
- }
- }
- private void mSelectionLine_LostMouseCapture(object sender, MouseEventArgs e)
- {
- this._chart.LockPainting = false;
- this._flag1 = false;
- this._chart.Refresh();
- this.UpdateSelectionLine();
- }
- private void mSelectionLine_MouseEnter(object sender, MouseEventArgs e)
- {
- if (this._chart.PointerType != PointerType.Drawing)
- {
- this._isMouseEnter = true;
- this.UpdateSelectionLine();
- }
- else
- {
- this.mSelectionLine.Cursor = Cursors.None;
- }
- }
- private void mSelectionLine_MouseLeave(object sender, MouseEventArgs e)
- {
- this._isMouseEnter = false;
- this.UpdateSelectionLine();
- }
- private void mSelectionLine_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- bool flag = this._chart.PointerType == PointerType.Drawing;
- if (!flag)
- {
- this._chart.LockPainting = true;
- this._position = e.GetPosition((UIElement)this.mSelectionLine.Parent);
- this._flag1 = true;
- this._flag2 = false;
- this.mSelectionLine.CaptureMouse();
- flag = (this._drawingTool != null);
- if (flag)
- {
- this._drawingTool.PrepareToMove();
- }
- }
- }
- private void mSelectionLine_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- this._chart.LockPainting = false;
- bool flag = this._drawingTool != null && !this._flag2;
- if (flag)
- {
- var flag2 = this._chart.PointerType == PointerType.Erase;
- if (flag2)
- {
- this._drawingTool.Delete();
- }
- else
- {
- //右键菜单
- //this.BuildPopupMenu(e);
- }
- }
- else
- {
- bool flag2 = this._drawingTool != null;
- if (flag2)
- {
- this.MovePlot(e.GetPosition((UIElement)this.mSelectionLine.Parent));
- flag2 = (this._drawingTool.OwnerSpec != null);
- if (flag2)
- {
- this._drawingTool.OwnerSpec.ToolChanged(this._drawingTool);
- }
- this._flag1 = false;
- //不用重新画图,MouseMove处理了MovePlot
- //this._chart.Refresh();
- }
- }
- this.mSelectionLine.ReleaseMouseCapture();
- this._flag1 = false;
- this._flag2 = false;
- this.UpdateSelectionLine();
- }
- private void mSelectionLine_MouseMove(object sender, MouseEventArgs e)
- {
- if (this._chart != null && this._chart.PointerType == PointerType.Erase)
- {
- this._chart.SetCursorImagePos(true, e);
- }
- var flag = this._flag1;
- if (flag)
- {
- Point position = e.GetPosition((UIElement)this.mSelectionLine.Parent);
- float num = GeometryHelper.DistanceBetweenPoints(this._position, position);
- flag = (num > 8f);
- if (flag)
- {
- this._flag2 = true;
- }
- flag = this._flag2;
- if (flag)
- {
- this.MovePlot(position);
- }
- }
- }
- /// <summary>
- /// Up弹出Popup,PopupManager里用Down关闭Popup
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
- private void mSelectionLine_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
- {
- this._chart.LockPainting = false;
- if (this._drawingTool == null) return;
- ////使用上下文菜单代替Popup,以免以免父控件有右键菜单
- //this.ShowContextMenu();
- //修改popup父容器,即每个Chart提供RootPanel,不使用PopupRoot和TrueRootVisual
- //因为应用程序可能开多个图表窗口
- this.ShowPopupMenu(e);
- e.Handled = true;
- }
- //private void SetAlert(object sender, EventArgs e)
- //{
- // try
- // {
- // ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3759((MouseEventArgs)e);
- // }
- // catch (Exception expr_3F)
- // {
- // ProjectData.SetProjectError(expr_3F);
- // ProjectData.ClearProjectError();
- // }
- //}
- //private void SetPriceAlert(object sender, EventArgs e)
- //{
- // try
- // {
- // //((DrawingHorizontalLine)this._drawingTool).drawingHorizontalLine_3533((MouseEventArgs)e);
- // }
- // catch (Exception expr_3F)
- // {
- // ProjectData.SetProjectError(expr_3F);
- // ProjectData.ClearProjectError();
- // }
- //}
- private void RemoveTool(object sender, EventArgs e)
- {
- this._drawingTool.Delete();
- }
- private void ShowToolEdit(object sender, EventArgs e)
- {
- this._drawingTool.ShowEditor((MouseEventArgs)e);
- }
- private void UpdateSelectionLine()
- {
- bool flag = this._isMouseEnter || this._flag1;
- if (flag)
- {
- this.mSelectionLine.Stroke = (new SolidColorBrush(Color.FromArgb(125, 125, 125, 125)));
- switch (this._chart.PointerType)
- {
- case PointerType.Erase:
- this.mSelectionLine.Cursor = Cursors.Arrow;
- break;
- case PointerType.Drawing:
- this.mSelectionLine.Cursor = Cursors.None;
- break;
- default:
- this.mSelectionLine.Cursor = Cursors.Hand;
- break;
- }
- }
- else
- {
- this.mSelectionLine.Stroke = (new SolidColorBrush(Colors.Transparent));
- }
- }
- #endregion Private Methods
- #endregion Methods
- }
- }
|