Пример #1
0
        public void PasteNodesFromClipboard()
        {
            if (Clipboard.ContainsText() && dockPanel.ActiveDocument is GraphLayoutForm)
            {
                string xml = Clipboard.GetText();

                try
                {
                    YAXSerializer networkSerializer = new YAXSerializer(typeof(MyNetwork), YAXExceptionHandlingPolicies.ThrowErrorsOnly, YAXExceptionTypes.Error, YAXSerializationOptions.DontSerializeNullObjects);

                    MyNetwork networkToPaste = (MyNetwork)networkSerializer.Deserialize(xml);
                    networkToPaste.UpdateAfterDeserialization(0, Project);

                    GraphLayoutForm activeLayout = dockPanel.ActiveDocument as GraphLayoutForm;

                    activeLayout.Target.AppendGroupContent(networkToPaste);
                    activeLayout.ReloadContent();

                    HashSet <int> pastedNodes = new HashSet <int>();
                    networkToPaste.Children.ForEach(node => pastedNodes.Add(node.Id));

                    List <MyNodeView> pastedNodeViews = new List <MyNodeView>();
                    RectangleF?       pastedBounds    = null;
                    Graphics          context         = activeLayout.Desktop.CreateGraphics();

                    foreach (MyNodeView nodeView in activeLayout.Desktop.Nodes)
                    {
                        if (pastedNodes.Contains(nodeView.Node.Id))
                        {
                            pastedNodeViews.Add(nodeView);

                            SizeF      size   = GraphRenderer.Measure(context, nodeView);
                            RectangleF bounds = new RectangleF(nodeView.Location, size);

                            if (pastedBounds.HasValue)
                            {
                                pastedBounds = RectangleF.Union(pastedBounds.Value, bounds);
                            }
                            else
                            {
                                pastedBounds = bounds;
                            }
                        }
                    }

                    PointF pasteLocation = activeLayout.Desktop.UnprojectPoint(new PointF(20, 20));

                    if (pastedBounds.HasValue)
                    {
                        foreach (MyNodeView nodeView in pastedNodeViews)
                        {
                            nodeView.Node.Location = new MyLocation()
                            {
                                X = nodeView.Location.X - pastedBounds.Value.Left + pasteLocation.X,
                                Y = nodeView.Location.Y - pastedBounds.Value.Top + pasteLocation.Y
                            };
                            nodeView.UpdateView();
                        }
                    }


                    //select pasted nodes
                    activeLayout.Desktop.FocusElement = new NodeSelection(pastedNodeViews);
                }
                catch (Exception e)
                {
                    MyLog.ERROR.WriteLine("Paste failed: " + e.Message);
                }
            }
        }