| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 | #region Copyright Syncfusion Inc. 2001-2020.// Copyright Syncfusion Inc. 2001-2020. All rights reserved.// Use of this code is subject to the terms of our license.// A copy of the current license can be obtained at any time by e-mailing// licensing@syncfusion.com. Any infringement will be prosecuted under// applicable laws. #endregionusing System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.Windows;using System.Windows.Controls;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;using Syncfusion.UI.Xaml.Diagram;using Syncfusion.UI.Xaml.Diagram.Controls;namespace PRSDesktop{    internal class Command : ICommand    {        private readonly Func<object, bool> canExecute;        private bool canExecuteCache;        private readonly Action<object> executeAction;        /// <summary>        ///     Initializes a new instance of the <see cref="Command" /> class.        /// </summary>        /// <param name="executeAction">The execute action.</param>        /// <param name="canExecute">The can execute.</param>        public Command(Action<object> executeAction,            Func<object, bool> canExecute = null)        {            this.executeAction = executeAction;            this.canExecute = canExecute;        }        /// <summary>        ///     Occurs when changes occur that affect whether the command should execute.        /// </summary>        public event EventHandler CanExecuteChanged;        #region ICommand Members        /// <summary>        ///     Defines the method that determines whether the command        ///     can execute in its current state.        /// </summary>        /// <param name="parameter">        ///     Data used by the command.        ///     If the command does not require data to be passed,        ///     this object can be set to null.        /// </param>        /// <returns>        ///     true if this command can be executed; otherwise, false.        /// </returns>        public bool CanExecute(object parameter)        {            if (parameter == null || canExecute == null) return true;            var tempCanExecute = canExecute(parameter);            if (canExecuteCache != tempCanExecute)            {                canExecuteCache = tempCanExecute;                if (CanExecuteChanged != null) CanExecuteChanged(this, new EventArgs());            }            return canExecuteCache;        }        /// <summary>        ///     Defines the method to be called when the command is invoked.        /// </summary>        /// <param name="parameter">        ///     Data used by the command.        ///     If the command does not require data to be passed,        ///     this object can be set to null.        /// </param>        public void Execute(object parameter)        {            executeAction(parameter);        }        #endregion    }    public class DrawingTools : DiagramViewModel    {        #region Constructor        public DrawingTools()        {            Nodes = new NodeCollection();            Connectors = new ConnectorCollection();            DrawingTool = DrawingTool.Node;            Tool = Tool.ContinuesDraw;            SelectedItems = new SelectorViewModel            {                SelectorConstraints = SelectorConstraints.Default & ~SelectorConstraints.QuickCommands            };            GetDrawTypeCommand = new Command(OnGetDrawTypeCommandExecute);            ItemUnSelectedCommand = new Command(OnItemUnSelectedCommandExecute);            HorizontalRuler = new Ruler();            VerticalRuler = new Ruler            {                Orientation = Orientation.Vertical            };            SnapSettings = new SnapSettings            {                SnapConstraints = SnapConstraints.All            };            SelectShapeCommand = new Command(OnSelectShapeCommandExecute);            ContiniousDrawCommand = new Command(OnContiniousDrawCommandExecute);        }        #endregion        #region fields        private string ShapeName = "Rectangle";        public ICommand SelectShapeCommand { get; set; }        public ICommand ContiniousDrawCommand { get; set; }        public Button prevbutton;        #endregion        #region Helper Methods        private void OnItemUnSelectedCommandExecute(object parameter)        {            if ((parameter as DiagramEventArgs).Item is INode)                foreach (IAnnotation annotation in ((parameter as DiagramEventArgs).Item as INode).Annotations as IEnumerable<object>)                    if (annotation.Content != null)                        if (string.IsNullOrEmpty(annotation.Content.ToString()))                        {                            (Nodes as ObservableCollection<NodeViewModel>).Remove((parameter as DiagramEventArgs).Item as NodeViewModel);                            break;                        }        }        private void OnGetDrawTypeCommandExecute(object parameter)        {            if (ShapeName.Equals("Rectangle"))            {                (parameter as DrawTypeEventArgs).DrawItem = new Rectangle                {                    StrokeThickness = 1.0, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.White),                    Stretch = Stretch.Fill                };            }            else if (ShapeName.Equals("Circle"))            {                (parameter as DrawTypeEventArgs).DrawItem = new Ellipse                    { StrokeThickness = 1.0, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.White) };            }            else if (ShapeName.Equals("Pentagon"))            {                (parameter as DrawTypeEventArgs).DrawItem = new Path                {                    Stretch = Stretch.Fill,                    Data =                        Geometry.Parse(                            "M370.9702,194.9961L359.5112,159.7291L389.5112,137.9341L419.5112,159.7291L408.0522,194.9961L370.9702,194.9961z"),                    StrokeThickness = 1.0, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.White)                };            }            else if (ShapeName.Equals("Hexagon"))            {                (parameter as DrawTypeEventArgs).DrawItem = new Path                {                    Stretch = Stretch.Fill,                    Data = Geometry.Parse("M165.5,-1.50000000000001L-2.5,213 167,444 444.5,442.5 621.5,214.5 438.5,-1.50000000000002z"),                    StrokeThickness = 1.0, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.White)                };            }            else if (ShapeName.Equals("Triangle"))            {                (parameter as DrawTypeEventArgs).DrawItem = new Path                {                    Stretch = Stretch.Fill, Data = Geometry.Parse("M81.1582,85.8677L111.1582,33.9067L141.1582,85.8677L81.1582,85.8677z"),                    StrokeThickness = 1.0, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.White)                };            }            else if (ShapeName.Equals("Star"))            {                (parameter as DrawTypeEventArgs).DrawItem = new Path                {                    Stretch = Stretch.Fill,                    Data = Geometry.Parse(                        "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"),                    StrokeThickness = 1.0, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.White)                };            }            else if (ShapeName.Equals("TextBox"))            {                var node = new NodeViewModel();                node.UnitWidth = 10;                node.UnitHeight = 10;                node.Annotations = new AnnotationCollection                {                    new AnnotationEditorViewModel                    {                        Mode = ContentEditorMode.Edit,                        UnitHeight = 100,                        UnitWidth = 100,                        Content = ""                    }                };                (parameter as DrawTypeEventArgs).DrawItem = node;            }            else if (ShapeName.Equals("User"))            {                var image = new Image();                image.Stretch = Stretch.Fill;                image.StretchDirection = StretchDirection.Both;                image.Source = Application.Current.Resources["UserImage"] as BitmapImage;                (parameter as DrawTypeEventArgs).DrawItem = image;            }            else if (ShapeName.Equals("SVG"))            {                var node = new NodeViewModel();                node.ContentTemplate = Application.Current.Resources["ContentTemplateforNodeContent"] as DataTemplate;                (parameter as DrawTypeEventArgs).DrawItem = node;            }        }        private void OnSelectShapeCommandExecute(object obj)        {            var button = obj as Button;            ShapeName = button.Name;            if (prevbutton != null) prevbutton.Style = Application.Current.Resources["ButtonStyle"] as Style;            button.Style = Application.Current.Resources["SelectedButtonStyle"] as Style;            if (Tool == Tool.MultipleSelect) Tool |= Tool.DrawOnce;            if (ShapeName.Equals("StraightConnector"))            {                DefaultConnectorType = ConnectorType.Line;                DrawingTool = DrawingTool.Connector;            }            else if (ShapeName.Equals("OrthogonalConnector"))            {                DefaultConnectorType = ConnectorType.Orthogonal;                DrawingTool = DrawingTool.Connector;            }            else if (ShapeName.Equals("BezierConnector"))            {                DefaultConnectorType = ConnectorType.CubicBezier;                DrawingTool = DrawingTool.Connector;            }            else if (ShapeName.Equals("polyLine"))            {                DefaultConnectorType = ConnectorType.PolyLine;                DrawingTool = DrawingTool.Connector;            }            else if (ShapeName.Equals("freeHand"))            {                DrawingTool = DrawingTool.FreeHand;                DefaultConnectorType = ConnectorType.Orthogonal;            }            else            {                DrawingTool = DrawingTool.Node;                DefaultConnectorType = ConnectorType.Orthogonal;            }            prevbutton = obj as Button;        }        private void OnContiniousDrawCommandExecute(object parameter)        {            if (this != null)            {                if ((bool)parameter)                    Tool = Tool.ContinuesDraw;                else                    Tool = Tool.MultipleSelect | Tool.DrawOnce;            }        }        #endregion    }}
 |