Exemple #1
0
        void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            DesignerItem   designerItem = this.DataContext as DesignerItem;
            DesignerCanvas designer     = VisualTreeHelper.GetParent(designerItem) as DesignerCanvas;

            if (designerItem != null && designer != null && designerItem.IsSelected)
            {
                double minLeft = double.MaxValue;
                double minTop  = double.MaxValue;

                // we only move DesignerItems
                var designerItems = designer.SelectionService.CurrentSelection.OfType <DesignerItem>();

                foreach (DesignerItem item in designerItems)
                {
                    double left = Canvas.GetLeft(item);
                    double top  = Canvas.GetTop(item);

                    minLeft = double.IsNaN(left) ? 0 : Math.Min(left, minLeft);
                    minTop  = double.IsNaN(top) ? 0 : Math.Min(top, minTop);
                }

                double deltaHorizontal = Math.Max(-minLeft, e.HorizontalChange);
                double deltaVertical   = Math.Max(-minTop, e.VerticalChange);

                foreach (DesignerItem item in designerItems)
                {
                    double left = Canvas.GetLeft(item);
                    double top  = Canvas.GetTop(item);

                    if (double.IsNaN(left))
                    {
                        left = 0;
                    }
                    if (double.IsNaN(top))
                    {
                        top = 0;
                    }

                    if (designer.UseRaster)
                    {
                        Canvas.SetLeft(item, Math.Round((left + deltaHorizontal) / designer.Raster, 0) * designer.Raster);
                        Canvas.SetTop(item, Math.Round((top + deltaVertical) / designer.Raster, 0) * designer.Raster);
                    }
                    else
                    {
                        Canvas.SetLeft(item, Math.Round(left + deltaHorizontal, 0));
                        Canvas.SetTop(item, Math.Round(top + deltaVertical, 0));
                    }
                }

                designer.InvalidateMeasure();
                e.Handled = true;
            }
        }
Exemple #2
0
 void ChangeParent(DesignerItem designerItem)
 {
     DiagramControl.DiagramManager.SetDragItemChildFlag();
     NewParent = DiagramControl.DiagramManager.ChangeParent(designerItem);
     if (_shadows == null)
     {
         _shadows = DiagramControl.DiagramManager.CreateShadows(designerItem, NewParent);
     }
     DiagramControl.DiagramManager.CreateHelperConnection(NewParent, designerItem);
     DiagramControl.DiagramManager.MoveUpAndDown(NewParent, designerItem);
 }
Exemple #3
0
 private void SetConnectorDecoratorTemplate(DesignerItem item)
 {
     if (item.ApplyTemplate() && item.Content is UIElement)
     {
         ControlTemplate template  = DesignerItem.GetConnectorDecoratorTemplate(item.Content as UIElement);
         Control         decorator = item.Template.FindName("PART_ConnectorDecorator", item) as Control;
         if (decorator != null && template != null)
         {
             decorator.Template = template;
         }
     }
 }
        private Connector GetConnector(Guid itemID, String connectorName)
        {
            DesignerItem designerItem = (from item in this.Children.OfType <DesignerItem>()
                                         where item.ID == itemID
                                         select item).FirstOrDefault();

            Control connectorDecorator = designerItem.Template.FindName("PART_ConnectorDecorator", designerItem) as Control;

            connectorDecorator.ApplyTemplate();

            return(connectorDecorator.Template.FindName(connectorName, connectorDecorator) as Connector);
        }
Exemple #5
0
        private void DragTop(double scale, DesignerItem item, SelectionService selectionService)
        {
            IEnumerable <DesignerItem> groupItems = selectionService.GetGroupMembers(item).Cast <DesignerItem>();
            double groupBottom = Canvas.GetTop(item) + item.Height;

            foreach (DesignerItem groupItem in groupItems)
            {
                double groupItemTop = Canvas.GetTop(groupItem);
                double delta        = (groupBottom - groupItemTop) * (scale - 1);
                Canvas.SetTop(groupItem, groupItemTop - delta);
                groupItem.Height = groupItem.ActualHeight * scale;
            }
        }
Exemple #6
0
        void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            DesignerItem designerItem = this.DataContext as DesignerItem;

            Functionality.DesignerCanvas designer = VisualTreeHelper.GetParent(designerItem) as Functionality.DesignerCanvas;
            if (designerItem != null && designer != null && designerItem.IsSelected)
            {
                double minLeft = double.MaxValue;
                double minTop  = double.MaxValue;

                // we only move DesignerItems
                var designerItems = designer.SelectionService.CurrentSelection.OfType <DesignerItem>();

                foreach (DesignerItem item in designerItems)
                {
                    double left = Canvas.GetLeft(item);
                    double top  = Canvas.GetTop(item);

                    minLeft = double.IsNaN(left) ? 0 : Math.Min(left, minLeft);
                    minTop  = double.IsNaN(top) ? 0 : Math.Min(top, minTop);
                }

                double deltaHorizontal = Math.Max(-minLeft, e.HorizontalChange);
                double deltaVertical   = Math.Max(-minTop, e.VerticalChange);

                foreach (DesignerItem item in designerItems)
                {
                    double left = Canvas.GetLeft(item);
                    double top  = Canvas.GetTop(item);

                    if (double.IsNaN(left))
                    {
                        left = 0;
                    }
                    if (double.IsNaN(top))
                    {
                        top = 0;
                    }

                    Canvas.SetLeft(item, left + deltaHorizontal);
                    Canvas.SetTop(item, top + deltaVertical);
                }

                // UPD: Check to create automatic links if the object being moved is one
                //if (designerItems.Count() == 1)
                CheckAutoCreateConnection(designerItem, designer);

                designer.InvalidateMeasure();
                e.Handled = true;
            }
        }
Exemple #7
0
        private void DragLeft(double scale, DesignerItem item, SelectionService selectionService)
        {
            IEnumerable <DesignerItem> groupItems = selectionService.GetGroupMembers(item).Cast <DesignerItem>();

            double groupLeft = Canvas.GetLeft(item) + item.Width;

            foreach (DesignerItem groupItem in groupItems)
            {
                double groupItemLeft = Canvas.GetLeft(groupItem);
                double delta         = (groupLeft - groupItemLeft) * (scale - 1);
                Canvas.SetLeft(groupItem, groupItemLeft - delta);
                groupItem.Width = groupItem.ActualWidth * scale;
            }
        }
Exemple #8
0
        void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            DesignerItem   designerItem = this.DataContext as DesignerItem;
            DesignerCanvas designer     = designerItem.Parent as DesignerCanvas;

            if (designerItem != null && designer != null && designerItem.IsSelected)
            {
                double minLeft = double.MaxValue;
                double minTop  = double.MaxValue;

                // we only move DesignerItems
                var designerItems = designer.SelectionService.CurrentSelection.OfType <DesignerItem>();

                foreach (DesignerItem item in designerItems)
                {
                    double left = Canvas.GetLeft(item);
                    double top  = Canvas.GetTop(item);
                    if (item.DataContext is ElementVM elementVM)
                    {
                        minLeft = double.IsNaN(elementVM.Left) ? 0 : Math.Min(elementVM.Left, minLeft);
                        minTop  = double.IsNaN(elementVM.Top) ? 0 : Math.Min(elementVM.Top, minTop);
                    }
                }

                double deltaHorizontal = Math.Max(-minLeft, e.HorizontalChange);
                double deltaVertical   = Math.Max(-minTop, e.VerticalChange);

                foreach (DesignerItem item in designerItems)
                {
                    if (item.DataContext is ElementVM elementVM)
                    {
                        if (double.IsNaN(elementVM.Left))
                        {
                            elementVM.Left = 0;
                        }
                        if (double.IsNaN(elementVM.Top))
                        {
                            elementVM.Top = 0;
                        }

                        elementVM.Left += deltaHorizontal;
                        elementVM.Top  += deltaVertical;
                    }
                }

                designer.InvalidateMeasure();
                e.Handled = true;
            }
        }
Exemple #9
0
        void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            DesignerItem   designerItem = this.DataContext as DesignerItem;
            DesignerCanvas designer     = VisualTreeHelper.GetParent(designerItem) as DesignerCanvas;

            if (designerItem != null && designer != null && designerItem.IsSelected)
            {
                double minLeft = double.MaxValue;
                double minTop  = double.MaxValue;

                // we only move DesignerItems
                var designerItems = from item in designer.SelectedItems
                                    where item is DesignerItem
                                    select item;

                foreach (DesignerItem item in designerItems)
                {
                    double left = Canvas.GetLeft(item);
                    double top  = Canvas.GetTop(item);

                    minLeft = double.IsNaN(left) ? 0 : Math.Min(left, minLeft);
                    minTop  = double.IsNaN(top) ? 0 : Math.Min(top, minTop);
                }

                double deltaHorizontal = Math.Max(-minLeft, e.HorizontalChange);
                double deltaVertical   = Math.Max(-minTop, e.VerticalChange);

                foreach (DesignerItem item in designerItems)
                {
                    double left = Canvas.GetLeft(item);
                    double top  = Canvas.GetTop(item);

                    if (double.IsNaN(left))
                    {
                        left = 0;
                    }
                    if (double.IsNaN(top))
                    {
                        top = 0;
                    }

                    Canvas.SetLeft(item, left + deltaHorizontal);
                    Canvas.SetTop(item, top + deltaVertical);
                }

                designer.InvalidateMeasure();
                e.Handled = true;
            }
        }
        private static DesignerItem DeserializeDesignerItem(XElement itemXML, Guid id, double OffsetX, double OffsetY)
        {
            DesignerItem item = new DesignerItem(id);

            item.Width    = Double.Parse(itemXML.Element("Width").Value, CultureInfo.InvariantCulture);
            item.Height   = Double.Parse(itemXML.Element("Height").Value, CultureInfo.InvariantCulture);
            item.ParentID = new Guid(itemXML.Element("ParentID").Value);
            item.IsGroup  = Boolean.Parse(itemXML.Element("IsGroup").Value);
            Canvas.SetLeft(item, Double.Parse(itemXML.Element("Left").Value, CultureInfo.InvariantCulture) + OffsetX);
            Canvas.SetTop(item, Double.Parse(itemXML.Element("Top").Value, CultureInfo.InvariantCulture) + OffsetY);
            Canvas.SetZIndex(item, Int32.Parse(itemXML.Element("zIndex").Value));
            Object content = XamlReader.Load(XmlReader.Create(new StringReader(itemXML.Element("Content").Value)));

            item.Content = content;
            return(item);
        }
Exemple #11
0
        protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            var diagramControl = DiagramControl;

            if (diagramControl == null)
            {
                return;
            }
            if (_shadows != null)
            {
                diagramControl.DiagramManager.FinishChangeParent(NewParent);
            }
            _shadows  = null;
            NewParent = null;
            DiagramControl.DiagramManager.RestoreDragItemChildFlag();
            _verticalOffset   = 0;
            _horizontalOffset = 0;
        }
        private void CopyCurrentSelection()
        {
            IEnumerable <DesignerItem> selectedDesignerItems =
                this.SelectionService.CurrentSelection.OfType <DesignerItem>();

            List <Connection> selectedConnections =
                this.SelectionService.CurrentSelection.OfType <Connection>().ToList();

            foreach (Connection connection in this.Children.OfType <Connection>())
            {
                if (!selectedConnections.Contains(connection))
                {
                    DesignerItem sourceItem = (from item in selectedDesignerItems
                                               where item.ID == connection.Source.ParentDesignerItem.ID
                                               select item).FirstOrDefault();

                    DesignerItem sinkItem = (from item in selectedDesignerItems
                                             where item.ID == connection.Sink.ParentDesignerItem.ID
                                             select item).FirstOrDefault();

                    if (sourceItem != null &&
                        sinkItem != null &&
                        BelongToSameGroup(sourceItem, sinkItem))
                    {
                        selectedConnections.Add(connection);
                    }
                }
            }

            XElement designerItemsXML = SerializeDesignerItems(selectedDesignerItems);
            XElement connectionsXML   = SerializeConnections(selectedConnections);

            XElement root = new XElement("Root");

            root.Add(designerItemsXML);
            root.Add(connectionsXML);

            root.Add(new XAttribute("OffsetX", 10));
            root.Add(new XAttribute("OffsetY", 10));

            Clipboard.Clear();
            Clipboard.SetData(DataFormats.Xaml, root);
        }
        private void Open_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            XElement root = LoadSerializedDataFromFile();

            if (root == null)
            {
                return;
            }

            this.Children.Clear();
            this.SelectionService.ClearSelection();

            IEnumerable <XElement> itemsXML = root.Elements("DesignerItems").Elements("DesignerItem");

            foreach (XElement itemXML in itemsXML)
            {
                Guid         id   = new Guid(itemXML.Element("ID").Value);
                DesignerItem item = DeserializeDesignerItem(itemXML, id, 0, 0);
                this.Children.Add(item);
                SetConnectorDecoratorTemplate(item);
            }

            this.InvalidateVisual();

            IEnumerable <XElement> connectionsXML = root.Elements("Connections").Elements("Connection");

            foreach (XElement connectionXML in connectionsXML)
            {
                Guid sourceID = new Guid(connectionXML.Element("SourceID").Value);
                Guid sinkID   = new Guid(connectionXML.Element("SinkID").Value);

                String sourceConnectorName = connectionXML.Element("SourceConnectorName").Value;
                String sinkConnectorName   = connectionXML.Element("SinkConnectorName").Value;

                Connector sourceConnector = GetConnector(sourceID, sourceConnectorName);
                Connector sinkConnector   = GetConnector(sinkID, sinkConnectorName);

                Connection connection = new Connection(sourceConnector, sinkConnector);
                Canvas.SetZIndex(connection, Int32.Parse(connectionXML.Element("zIndex").Value));
                this.Children.Add(connection);
            }
        }
Exemple #14
0
 void VerticalScroll(DesignerItem designerItem, double VerticalChange)
 {
     _verticalOffset += VerticalChange;
     if (_verticalOffset < designerItem.ActualHeight)
     {
         var yPos = Canvas.GetTop(designerItem);
         var sv   = (ScrollViewer)DiagramControl.Template.FindName("DesignerScrollViewer", DiagramControl);
         if (sv.VerticalOffset + sv.ViewportHeight - 100 < yPos && VerticalChange > 0)
         {
             sv.ScrollToVerticalOffset(sv.VerticalOffset + VerticalChange);
         }
         else if (yPos < sv.VerticalOffset + 100 && VerticalChange < 0)
         {
             sv.ScrollToVerticalOffset(sv.VerticalOffset + VerticalChange);
         }
     }
     else if (_verticalOffset > designerItem.ActualHeight)
     {
         _verticalOffset = 0;
     }
 }
Exemple #15
0
 void HorizontalScroll(DesignerItem designerItem, double HorizontalChange)
 {
     _horizontalOffset += HorizontalChange;
     if (_horizontalOffset < designerItem.ActualWidth)
     {
         var xPos = Canvas.GetLeft(designerItem);
         var sv   = (ScrollViewer)DiagramControl.Template.FindName("DesignerScrollViewer", DiagramControl);
         if (sv.HorizontalOffset + sv.ViewportWidth - designerItem.ActualWidth < xPos && HorizontalChange > 0)
         {
             sv.ScrollToHorizontalOffset(sv.HorizontalOffset + HorizontalChange);
         }
         else if (xPos < sv.HorizontalOffset + designerItem.ActualWidth && HorizontalChange < 0)
         {
             sv.ScrollToHorizontalOffset(sv.HorizontalOffset + HorizontalChange);
         }
     }
     else if (_horizontalOffset > designerItem.ActualWidth)
     {
         _horizontalOffset = 0;
     }
 }
Exemple #16
0
        void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            DesignerItem   designerItem = this.DataContext as DesignerItem;
            DesignerCanvas designer     = VisualTreeHelper.GetParent(designerItem) as DesignerCanvas;

            if (designerItem != null && designer != null && designerItem.IsSelected)
            {
                double minLeft, minTop, minDeltaHorizontal, minDeltaVertical;
                double dragDeltaVertical, dragDeltaHorizontal, scale;

                IEnumerable <DesignerItem> selectedDesignerItems = designer.SelectionService.CurrentSelection.OfType <DesignerItem>();

                CalculateDragLimits(selectedDesignerItems, out minLeft, out minTop,
                                    out minDeltaHorizontal, out minDeltaVertical);

                foreach (DesignerItem item in selectedDesignerItems)
                {
                    if (item != null && item.ParentID == Guid.Empty)
                    {
                        switch (base.VerticalAlignment)
                        {
                        case VerticalAlignment.Bottom:
                            dragDeltaVertical = Math.Min(-e.VerticalChange, minDeltaVertical);
                            scale             = (item.ActualHeight - dragDeltaVertical) / item.ActualHeight;
                            DragBottom(scale, item, designer.SelectionService);
                            break;

                        case VerticalAlignment.Top:
                            double top = Canvas.GetTop(item);
                            dragDeltaVertical = Math.Min(Math.Max(-minTop, e.VerticalChange), minDeltaVertical);
                            scale             = (item.ActualHeight - dragDeltaVertical) / item.ActualHeight;
                            DragTop(scale, item, designer.SelectionService);
                            break;

                        default:
                            break;
                        }

                        switch (base.HorizontalAlignment)
                        {
                        case HorizontalAlignment.Left:
                            double left = Canvas.GetLeft(item);
                            dragDeltaHorizontal = Math.Min(Math.Max(-minLeft, e.HorizontalChange), minDeltaHorizontal);
                            scale = (item.ActualWidth - dragDeltaHorizontal) / item.ActualWidth;
                            DragLeft(scale, item, designer.SelectionService);
                            break;

                        case HorizontalAlignment.Right:
                            dragDeltaHorizontal = Math.Min(-e.HorizontalChange, minDeltaHorizontal);
                            scale = (item.ActualWidth - dragDeltaHorizontal) / item.ActualWidth;
                            DragRight(scale, item, designer.SelectionService);
                            break;

                        default:
                            break;
                        }
                    }
                }
                e.Handled = true;
                Focus();
            }
        }
        private void Paste_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            XElement root = LoadSerializedDataFromClipBoard();

            if (root == null)
            {
                return;
            }

            // create DesignerItems
            Dictionary <Guid, Guid> mappingOldToNewIDs = new Dictionary <Guid, Guid>();
            List <ISelectable>      newItems           = new List <ISelectable>();
            IEnumerable <XElement>  itemsXML           = root.Elements("DesignerItems").Elements("DesignerItem");

            double offsetX = Double.Parse(root.Attribute("OffsetX").Value, CultureInfo.InvariantCulture);
            double offsetY = Double.Parse(root.Attribute("OffsetY").Value, CultureInfo.InvariantCulture);

            foreach (XElement itemXML in itemsXML)
            {
                Guid oldID = new Guid(itemXML.Element("ID").Value);
                Guid newID = Guid.NewGuid();
                mappingOldToNewIDs.Add(oldID, newID);
                DesignerItem item = DeserializeDesignerItem(itemXML, newID, offsetX, offsetY);
                this.Children.Add(item);
                SetConnectorDecoratorTemplate(item);
                newItems.Add(item);
            }

            // update group hierarchy
            SelectionService.ClearSelection();
            foreach (DesignerItem el in newItems)
            {
                if (el.ParentID != Guid.Empty)
                {
                    el.ParentID = mappingOldToNewIDs[el.ParentID];
                }
            }


            foreach (DesignerItem item in newItems)
            {
                if (item.ParentID == Guid.Empty)
                {
                    SelectionService.AddToSelection(item);
                }
            }

            // create Connections
            IEnumerable <XElement> connectionsXML = root.Elements("Connections").Elements("Connection");

            foreach (XElement connectionXML in connectionsXML)
            {
                Guid oldSourceID = new Guid(connectionXML.Element("SourceID").Value);
                Guid oldSinkID   = new Guid(connectionXML.Element("SinkID").Value);

                if (mappingOldToNewIDs.ContainsKey(oldSourceID) && mappingOldToNewIDs.ContainsKey(oldSinkID))
                {
                    Guid newSourceID = mappingOldToNewIDs[oldSourceID];
                    Guid newSinkID   = mappingOldToNewIDs[oldSinkID];

                    String sourceConnectorName = connectionXML.Element("SourceConnectorName").Value;
                    String sinkConnectorName   = connectionXML.Element("SinkConnectorName").Value;

                    Connector sourceConnector = GetConnector(newSourceID, sourceConnectorName);
                    Connector sinkConnector   = GetConnector(newSinkID, sinkConnectorName);

                    Connection connection = new Connection(sourceConnector, sinkConnector);
                    Canvas.SetZIndex(connection, Int32.Parse(connectionXML.Element("zIndex").Value));
                    this.Children.Add(connection);

                    SelectionService.AddToSelection(connection);
                }
            }

            DesignerCanvas.BringToFront.Execute(null, this);

            // update paste offset
            root.Attribute("OffsetX").Value = (offsetX + 10).ToString();
            root.Attribute("OffsetY").Value = (offsetY + 10).ToString();
            Clipboard.Clear();
            Clipboard.SetData(DataFormats.Xaml, root);
        }
Exemple #18
0
        private void CheckAutoCreateConnection(DesignerItem movingDesignerItem, DesignerCanvas designer)
        {
            // Find the nearest Item
            Tuple <DesignerItem, double> nearestItem = new Tuple <DesignerItem, double>(null, double.MaxValue);

            foreach (var designerChild in designer.Children)
            {
                if ((designerChild is DesignerItem neighboringItem) && (neighboringItem != movingDesignerItem))
                {
                    var distance = GetDistance(movingDesignerItem, neighboringItem);
                    if (nearestItem.Item2 > distance)
                    {
                        nearestItem = new Tuple <DesignerItem, double>(neighboringItem, distance);
                    }
                }
            }

            // If not founded, then return
            if ((nearestItem.Item1 == null) || (nearestItem.Item2 > 150))
            {
                if (lastAutoCreatedConnection != null)
                {
                    designer.Children.Remove(lastAutoCreatedConnection);
                }
                return;
            }

            // Find the nearest Connectors
            Control cd = movingDesignerItem.Template.FindName("PART_ConnectorDecorator", movingDesignerItem) as Control;
            Control nd = nearestItem.Item1.Template.FindName("PART_ConnectorDecorator", nearestItem.Item1) as Control;

            List <Connector> connectorsMoving  = new List <Connector>();
            List <Connector> connectorsNearest = new List <Connector>();

            designer.GetConnectors(cd, connectorsMoving);
            designer.GetConnectors(nd, connectorsNearest);

            Tuple <Connector, Connector, double> nearestConnectors = new Tuple <Connector, Connector, double>(null, null, double.MaxValue);

            foreach (var connectorM in connectorsMoving)
            {
                foreach (var connectorN in connectorsNearest)
                {
                    Point delta = new Point(Math.Abs(connectorM.Position.X - connectorN.Position.X), Math.Abs(connectorM.Position.Y - connectorN.Position.Y));

                    var distance = Math.Sqrt(delta.X * delta.X + delta.Y * delta.Y);
                    if (nearestConnectors.Item3 > distance)
                    {
                        nearestConnectors = new Tuple <Connector, Connector, double>(connectorM, connectorN, distance);
                    }
                }
            }

            // If not founded, then return
            if (nearestConnectors.Item1 == null || nearestConnectors.Item2 == null)
            {
                return;
            }

            // Check connection already exist
            foreach (var designerChild in designer.Children)
            {
                if (designerChild is Connection connection)
                {
                    if (((connection.Source.ParentDesignerItem == movingDesignerItem && connection.Sink.ParentDesignerItem == nearestItem.Item1) ||
                         (connection.Source.ParentDesignerItem == nearestItem.Item1 && connection.Sink.ParentDesignerItem == movingDesignerItem)) &&
                        connection.StrokeDashArray == null)
                    {
                        return;
                    }
                }
            }

            Connection newConnection = null;

            if (movingDesignerItem.DateTimeCreated < nearestItem.Item1.DateTimeCreated)
            {
                if ((nearestConnectors.Item1.ValidateAddOutConnection()) &&
                    (nearestConnectors.Item2.ValidateAddInConnection()))
                {
                    newConnection = new Connection(nearestConnectors.Item1, nearestConnectors.Item2);
                }
                else
                {
                    return;
                }
            }
            else
            {
                if ((nearestConnectors.Item2.ValidateAddOutConnection()) &&
                    (nearestConnectors.Item1.ValidateAddInConnection()))
                {
                    newConnection = new Connection(nearestConnectors.Item2, nearestConnectors.Item1);
                }
                else
                {
                    return;
                }
            }

            // Remove last preview connection if it exist
            if (lastAutoCreatedConnection != null)
            {
                designer.Children.Remove(lastAutoCreatedConnection);
                lastAutoCreatedConnection = null;
            }

            // Create auto connection preview
            newConnection.StrokeDashArray = new DoubleCollection(new double[] { 1, 2 });

            Canvas.SetZIndex(newConnection, designer.Children.Count);
            designer.Children.Add(newConnection);
            lastAutoCreatedConnection = newConnection;
        }
Exemple #19
0
        protected override void OnDrop(DragEventArgs e)
        {
            base.OnDrop(e);
            DragObject dragObject = e.Data.GetData(typeof(DragObject)) as DragObject;

            if (dragObject != null && !String.IsNullOrEmpty(dragObject.Xaml))
            {
                //子流程的新建
                if (dragObject.Xaml.Contains("子流程"))
                {
                    FlowChar flowChar       = new FlowChar();
                    string   str1           = Directory.GetCurrentDirectory();
                    string   Enclosure_Path = str1 + "\\FlowChar";
                    if (!Directory.Exists(Enclosure_Path))         // 返回bool类型,存在返回true,不存在返回false
                    {
                        Directory.CreateDirectory(Enclosure_Path); //不存在则创建路径
                    }
                    var filepath = Enclosure_Path + $"\\{DateTime.Now.ToString("yyyyMMddhhmmssMM")}" + ".xml";
                    flowChar.FlowcharPath = filepath;
                    flowChar.Flowname     = "子流程1";
                    flowChar.IcoImage     = "/WpfApp3;component/Image/flowchild.png";
                    flowChar.Flowtype     = 1;
                    XElement root = new XElement("Root");
                    try
                    {
                        root.Save(filepath);
                        RememberClass rememberClass = this.Tag as RememberClass;
                        rememberClass.AddRemember(flowChar, false);
                        if (flowChar != null)
                        {
                            Grid grid = new Grid();
                            grid.Width  = 80;
                            grid.Height = 70;
                            RowDefinition row1 = new RowDefinition();
                            RowDefinition row2 = new RowDefinition();
                            row2.Height = GridLength.Auto;
                            grid.RowDefinitions.Add(row1);
                            grid.RowDefinitions.Add(row2);
                            Image image = new Image();
                            image.Source = new BitmapImage(new Uri(flowChar.IcoImage, UriKind.RelativeOrAbsolute));
                            Button button = new Button();
                            button.Content = "编辑";
                            button.Margin  = new Thickness(5, 2, 5, 2);
                            button.Tag     = flowChar;
                            button.Click  += OpenFlowChar;
                            grid.Children.Add(image);
                            grid.Children.Add(button);
                            Grid.SetRow(image, 0);
                            Grid.SetRow(button, 1);
                            DesignerItem newItem = null;
                            // Object content = XamlReader.Load(XmlReader.Create(new StringReader(dragObject.Xaml)));
                            newItem         = new DesignerItem();
                            newItem.Content = grid;
                            Point position = e.GetPosition(this);
                            // Size desiredSize = dragObject.DesiredSize.Value;
                            newItem.Width  = grid.Width;
                            newItem.Height = grid.Height;
                            DesignerCanvas.SetLeft(newItem, Math.Max(0, position.X - newItem.Width / 2));
                            DesignerCanvas.SetTop(newItem, Math.Max(0, position.Y - newItem.Height / 2));
                            //else
                            //{
                            //    DesignerCanvas.SetLeft(newItem, Math.Max(0, position.X));
                            //    DesignerCanvas.SetTop(newItem, Math.Max(0, position.Y));
                            //}
                            Canvas.SetZIndex(newItem, this.Children.Count);
                            this.Children.Add(newItem);
                            SetConnectorDecoratorTemplate(newItem);
                            //update selection
                            this.SelectionService.SelectItem(newItem);
                            newItem.Focus();
                            rememberClass.LoadFlowMD();
                            e.Handled = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.StackTrace, ex.Message, MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                //基本流程
                else
                {
                    DesignerItem newItem = null;
                    Object       content = XamlReader.Load(XmlReader.Create(new StringReader(dragObject.Xaml)));

                    if (content != null)
                    {
                        newItem         = new DesignerItem();
                        newItem.Content = content;

                        Point position = e.GetPosition(this);

                        if (dragObject.DesiredSize.HasValue)
                        {
                            Size desiredSize = dragObject.DesiredSize.Value;
                            newItem.Width  = desiredSize.Width;
                            newItem.Height = desiredSize.Height;

                            DesignerCanvas.SetLeft(newItem, Math.Max(0, position.X - newItem.Width / 2));
                            DesignerCanvas.SetTop(newItem, Math.Max(0, position.Y - newItem.Height / 2));
                        }
                        else
                        {
                            DesignerCanvas.SetLeft(newItem, Math.Max(0, position.X));
                            DesignerCanvas.SetTop(newItem, Math.Max(0, position.Y));
                        }

                        Canvas.SetZIndex(newItem, this.Children.Count);
                        this.Children.Add(newItem);
                        SetConnectorDecoratorTemplate(newItem);

                        //update selection
                        this.SelectionService.SelectItem(newItem);
                        newItem.Focus();
                    }

                    e.Handled = true;
                }
            }
            else
            {
                FlowChar flowChar = e.Data.GetData(typeof(FlowChar)) as FlowChar;
                if (flowChar != null)
                {
                    Grid grid = new Grid();
                    grid.Width  = 80;
                    grid.Height = 70;
                    RowDefinition row1 = new RowDefinition();
                    RowDefinition row2 = new RowDefinition();
                    row2.Height = GridLength.Auto;
                    grid.RowDefinitions.Add(row1);
                    grid.RowDefinitions.Add(row2);
                    Image image = new Image();
                    image.Source = new BitmapImage(new Uri(flowChar.IcoImage, UriKind.RelativeOrAbsolute));
                    Button button = new Button();
                    button.Content = "编辑";
                    button.Margin  = new Thickness(5, 2, 5, 2);
                    button.Tag     = flowChar;
                    button.Click  += OpenFlowChar;
                    grid.Children.Add(image);
                    grid.Children.Add(button);
                    Grid.SetRow(image, 0);
                    Grid.SetRow(button, 1);
                    DesignerItem newItem = null;
                    // Object content = XamlReader.Load(XmlReader.Create(new StringReader(dragObject.Xaml)));


                    newItem         = new DesignerItem();
                    newItem.Content = grid;

                    Point position = e.GetPosition(this);


                    // Size desiredSize = dragObject.DesiredSize.Value;
                    newItem.Width  = grid.Width;
                    newItem.Height = grid.Height;

                    DesignerCanvas.SetLeft(newItem, Math.Max(0, position.X - newItem.Width / 2));
                    DesignerCanvas.SetTop(newItem, Math.Max(0, position.Y - newItem.Height / 2));

                    //else
                    //{
                    //    DesignerCanvas.SetLeft(newItem, Math.Max(0, position.X));
                    //    DesignerCanvas.SetTop(newItem, Math.Max(0, position.Y));
                    //}

                    Canvas.SetZIndex(newItem, this.Children.Count);
                    this.Children.Add(newItem);
                    SetConnectorDecoratorTemplate(newItem);

                    //update selection
                    this.SelectionService.SelectItem(newItem);
                    newItem.Focus();


                    e.Handled = true;
                }
            }
        }
Exemple #20
0
        void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            DesignerItem   designerItem = this.DataContext as DesignerItem;
            DesignerCanvas designer     = VisualTreeHelper.GetParent(designerItem) as DesignerCanvas;

            if (designerItem != null && designer != null && designerItem.IsSelected)
            {
                double minLeft, minTop, minDeltaHorizontal, minDeltaVertical;
                double dragDeltaVertical, dragDeltaHorizontal;

                // only resize DesignerItems
                var selectedDesignerItems = from item in designer.SelectedItems
                                            where item is DesignerItem
                                            select item;

                CalculateDragLimits(selectedDesignerItems, out minLeft, out minTop,
                                    out minDeltaHorizontal, out minDeltaVertical);

                foreach (DesignerItem item in selectedDesignerItems)
                {
                    if (item != null)
                    {
                        switch (base.VerticalAlignment)
                        {
                        case VerticalAlignment.Bottom:
                            dragDeltaVertical = Math.Min(-e.VerticalChange, minDeltaVertical);
                            item.Height       = item.ActualHeight - dragDeltaVertical;
                            break;

                        case VerticalAlignment.Top:
                            double top = Canvas.GetTop(item);
                            dragDeltaVertical = Math.Min(Math.Max(-minTop, e.VerticalChange), minDeltaVertical);
                            Canvas.SetTop(item, top + dragDeltaVertical);
                            item.Height = item.ActualHeight - dragDeltaVertical;
                            break;

                        default:
                            break;
                        }

                        switch (base.HorizontalAlignment)
                        {
                        case HorizontalAlignment.Left:
                            double left = Canvas.GetLeft(item);
                            dragDeltaHorizontal = Math.Min(Math.Max(-minLeft, e.HorizontalChange), minDeltaHorizontal);
                            Canvas.SetLeft(item, left + dragDeltaHorizontal);
                            item.Width = item.ActualWidth - dragDeltaHorizontal;
                            break;

                        case HorizontalAlignment.Right:
                            dragDeltaHorizontal = Math.Min(-e.HorizontalChange, minDeltaHorizontal);
                            item.Width          = item.ActualWidth - dragDeltaHorizontal;
                            break;

                        default:
                            break;
                        }
                    }
                }
                e.Handled = true;
            }
        }