QuoteDiagramDrawingTools.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #region Copyright Syncfusion Inc. 2001-2020.
  2. // Copyright Syncfusion Inc. 2001-2020. All rights reserved.
  3. // Use of this code is subject to the terms of our license.
  4. // A copy of the current license can be obtained at any time by e-mailing
  5. // licensing@syncfusion.com. Any infringement will be prosecuted under
  6. // applicable laws.
  7. #endregion
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. using Syncfusion.UI.Xaml.Diagram;
  18. using Syncfusion.UI.Xaml.Diagram.Controls;
  19. namespace PRSDesktop
  20. {
  21. internal class Command : ICommand
  22. {
  23. private readonly Func<object, bool> canExecute;
  24. private bool canExecuteCache;
  25. private readonly Action<object> executeAction;
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="Command" /> class.
  28. /// </summary>
  29. /// <param name="executeAction">The execute action.</param>
  30. /// <param name="canExecute">The can execute.</param>
  31. public Command(Action<object> executeAction,
  32. Func<object, bool> canExecute = null)
  33. {
  34. this.executeAction = executeAction;
  35. this.canExecute = canExecute;
  36. }
  37. /// <summary>
  38. /// Occurs when changes occur that affect whether the command should execute.
  39. /// </summary>
  40. public event EventHandler CanExecuteChanged;
  41. #region ICommand Members
  42. /// <summary>
  43. /// Defines the method that determines whether the command
  44. /// can execute in its current state.
  45. /// </summary>
  46. /// <param name="parameter">
  47. /// Data used by the command.
  48. /// If the command does not require data to be passed,
  49. /// this object can be set to null.
  50. /// </param>
  51. /// <returns>
  52. /// true if this command can be executed; otherwise, false.
  53. /// </returns>
  54. public bool CanExecute(object parameter)
  55. {
  56. if (parameter == null || canExecute == null) return true;
  57. var tempCanExecute = canExecute(parameter);
  58. if (canExecuteCache != tempCanExecute)
  59. {
  60. canExecuteCache = tempCanExecute;
  61. if (CanExecuteChanged != null) CanExecuteChanged(this, new EventArgs());
  62. }
  63. return canExecuteCache;
  64. }
  65. /// <summary>
  66. /// Defines the method to be called when the command is invoked.
  67. /// </summary>
  68. /// <param name="parameter">
  69. /// Data used by the command.
  70. /// If the command does not require data to be passed,
  71. /// this object can be set to null.
  72. /// </param>
  73. public void Execute(object parameter)
  74. {
  75. executeAction(parameter);
  76. }
  77. #endregion
  78. }
  79. public class DrawingTools : DiagramViewModel
  80. {
  81. #region Constructor
  82. public DrawingTools()
  83. {
  84. Nodes = new NodeCollection();
  85. Connectors = new ConnectorCollection();
  86. DrawingTool = DrawingTool.Node;
  87. Tool = Tool.ContinuesDraw;
  88. SelectedItems = new SelectorViewModel
  89. {
  90. SelectorConstraints = SelectorConstraints.Default & ~SelectorConstraints.QuickCommands
  91. };
  92. GetDrawTypeCommand = new Command(OnGetDrawTypeCommandExecute);
  93. ItemUnSelectedCommand = new Command(OnItemUnSelectedCommandExecute);
  94. HorizontalRuler = new Ruler();
  95. VerticalRuler = new Ruler
  96. {
  97. Orientation = Orientation.Vertical
  98. };
  99. SnapSettings = new SnapSettings
  100. {
  101. SnapConstraints = SnapConstraints.All
  102. };
  103. SelectShapeCommand = new Command(OnSelectShapeCommandExecute);
  104. ContiniousDrawCommand = new Command(OnContiniousDrawCommandExecute);
  105. }
  106. #endregion
  107. #region fields
  108. private string ShapeName = "Rectangle";
  109. public ICommand SelectShapeCommand { get; set; }
  110. public ICommand ContiniousDrawCommand { get; set; }
  111. public Button prevbutton;
  112. #endregion
  113. #region Helper Methods
  114. private void OnItemUnSelectedCommandExecute(object parameter)
  115. {
  116. if ((parameter as DiagramEventArgs).Item is INode)
  117. foreach (IAnnotation annotation in ((parameter as DiagramEventArgs).Item as INode).Annotations as IEnumerable<object>)
  118. if (annotation.Content != null)
  119. if (string.IsNullOrEmpty(annotation.Content.ToString()))
  120. {
  121. (Nodes as ObservableCollection<NodeViewModel>).Remove((parameter as DiagramEventArgs).Item as NodeViewModel);
  122. break;
  123. }
  124. }
  125. private void OnGetDrawTypeCommandExecute(object parameter)
  126. {
  127. if (ShapeName.Equals("Rectangle"))
  128. {
  129. (parameter as DrawTypeEventArgs).DrawItem = new Rectangle
  130. {
  131. StrokeThickness = 1.0, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.White),
  132. Stretch = Stretch.Fill
  133. };
  134. }
  135. else if (ShapeName.Equals("Circle"))
  136. {
  137. (parameter as DrawTypeEventArgs).DrawItem = new Ellipse
  138. { StrokeThickness = 1.0, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.White) };
  139. }
  140. else if (ShapeName.Equals("Pentagon"))
  141. {
  142. (parameter as DrawTypeEventArgs).DrawItem = new Path
  143. {
  144. Stretch = Stretch.Fill,
  145. Data =
  146. Geometry.Parse(
  147. "M370.9702,194.9961L359.5112,159.7291L389.5112,137.9341L419.5112,159.7291L408.0522,194.9961L370.9702,194.9961z"),
  148. StrokeThickness = 1.0, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.White)
  149. };
  150. }
  151. else if (ShapeName.Equals("Hexagon"))
  152. {
  153. (parameter as DrawTypeEventArgs).DrawItem = new Path
  154. {
  155. Stretch = Stretch.Fill,
  156. Data = Geometry.Parse("M165.5,-1.50000000000001L-2.5,213 167,444 444.5,442.5 621.5,214.5 438.5,-1.50000000000002z"),
  157. StrokeThickness = 1.0, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.White)
  158. };
  159. }
  160. else if (ShapeName.Equals("Triangle"))
  161. {
  162. (parameter as DrawTypeEventArgs).DrawItem = new Path
  163. {
  164. Stretch = Stretch.Fill, Data = Geometry.Parse("M81.1582,85.8677L111.1582,33.9067L141.1582,85.8677L81.1582,85.8677z"),
  165. StrokeThickness = 1.0, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.White)
  166. };
  167. }
  168. else if (ShapeName.Equals("Star"))
  169. {
  170. (parameter as DrawTypeEventArgs).DrawItem = new Path
  171. {
  172. Stretch = Stretch.Fill,
  173. Data = Geometry.Parse(
  174. "M230,712.7559L233.314,723.9179L244.46,723.7749L235.362,730.5289L238.937,741.6029L230,734.6149L221.063,741.6029L224.638,730.5289L215.54,723.7749L226.686,723.9179L230,712.7559z"),
  175. StrokeThickness = 1.0, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.White)
  176. };
  177. }
  178. else if (ShapeName.Equals("TextBox"))
  179. {
  180. var node = new NodeViewModel();
  181. node.UnitWidth = 10;
  182. node.UnitHeight = 10;
  183. node.Annotations = new AnnotationCollection
  184. {
  185. new AnnotationEditorViewModel
  186. {
  187. Mode = ContentEditorMode.Edit,
  188. UnitHeight = 100,
  189. UnitWidth = 100,
  190. Content = ""
  191. }
  192. };
  193. (parameter as DrawTypeEventArgs).DrawItem = node;
  194. }
  195. else if (ShapeName.Equals("User"))
  196. {
  197. var image = new Image();
  198. image.Stretch = Stretch.Fill;
  199. image.StretchDirection = StretchDirection.Both;
  200. image.Source = Application.Current.Resources["UserImage"] as BitmapImage;
  201. (parameter as DrawTypeEventArgs).DrawItem = image;
  202. }
  203. else if (ShapeName.Equals("SVG"))
  204. {
  205. var node = new NodeViewModel();
  206. node.ContentTemplate = Application.Current.Resources["ContentTemplateforNodeContent"] as DataTemplate;
  207. (parameter as DrawTypeEventArgs).DrawItem = node;
  208. }
  209. }
  210. private void OnSelectShapeCommandExecute(object obj)
  211. {
  212. var button = obj as Button;
  213. ShapeName = button.Name;
  214. if (prevbutton != null) prevbutton.Style = Application.Current.Resources["ButtonStyle"] as Style;
  215. button.Style = Application.Current.Resources["SelectedButtonStyle"] as Style;
  216. if (Tool == Tool.MultipleSelect) Tool |= Tool.DrawOnce;
  217. if (ShapeName.Equals("StraightConnector"))
  218. {
  219. DefaultConnectorType = ConnectorType.Line;
  220. DrawingTool = DrawingTool.Connector;
  221. }
  222. else if (ShapeName.Equals("OrthogonalConnector"))
  223. {
  224. DefaultConnectorType = ConnectorType.Orthogonal;
  225. DrawingTool = DrawingTool.Connector;
  226. }
  227. else if (ShapeName.Equals("BezierConnector"))
  228. {
  229. DefaultConnectorType = ConnectorType.CubicBezier;
  230. DrawingTool = DrawingTool.Connector;
  231. }
  232. else if (ShapeName.Equals("polyLine"))
  233. {
  234. DefaultConnectorType = ConnectorType.PolyLine;
  235. DrawingTool = DrawingTool.Connector;
  236. }
  237. else if (ShapeName.Equals("freeHand"))
  238. {
  239. DrawingTool = DrawingTool.FreeHand;
  240. DefaultConnectorType = ConnectorType.Orthogonal;
  241. }
  242. else
  243. {
  244. DrawingTool = DrawingTool.Node;
  245. DefaultConnectorType = ConnectorType.Orthogonal;
  246. }
  247. prevbutton = obj as Button;
  248. }
  249. private void OnContiniousDrawCommandExecute(object parameter)
  250. {
  251. if (this != null)
  252. {
  253. if ((bool)parameter)
  254. Tool = Tool.ContinuesDraw;
  255. else
  256. Tool = Tool.MultipleSelect | Tool.DrawOnce;
  257. }
  258. }
  259. #endregion
  260. }
  261. }