microsoft / automatic-graph-layout

A set of tools for graph layout and viewing
Other
1.36k stars 308 forks source link

Calling ResizeNodeToLabel causes a crash #308

Closed MVoloshin closed 2 years ago

MVoloshin commented 2 years ago

Hello once again) Now I'm trying to edit labels on some Nodes displayed in GViewer and I also have to resize Nodes in order to make changed labels fit into them. But I faced a strange problem: image

Calling ResizeNodeToLabel on every Node added to the graph after it had been attached to GViewer results in NullPointerException or IndexOutOfBoundsException (if graph already had some previously added nodes). I found a temporary workaround for this problem, now I'm just commenting out this line in GViewer.cs: //LayoutHelpers.IncrementalLayout(Graph.GeometryGraph, node.GeometryNode, // Graph.LayoutAlgorithmSettings as SugiyamaLayoutSettings); but I don't think that's the correct solution.

In order to reproduce the issue modify MouseDown handler in EdgeDirectionTest as follows:

//ResizeNodeToLabel shouldn't cause a crash here but it does.
private void viewer_MouseDown(object sender, MsaglMouseEventArgs e)
        {
            if (e.RightButtonIsPressed) {
                IViewerObject ob = gv.ObjectUnderMouseCursor;
                if (ob == null) {
                    Microsoft.Msagl.Core.Geometry.Point p = gv.ScreenToSource(e.X, e.Y);
                    Node n = new Node(this.FindNewId());
                    IViewerNode iwn = gv.CreateIViewerNode(n, p, true);
                    gv.AddNode(iwn, true);
                    gv.ResizeNodeToLabel(n);
                }
                else if (ob is IViewerNode) {
                    IViewerNode existingNode = ob as IViewerNode;
                    gv.RemoveNode(existingNode, true);
                }
            }
        }

@levnach , please look at this when you have time.

MVoloshin commented 2 years ago

Still actual

levnach commented 2 years ago

I cleaned up a little bit: there was some experimental code that tried to update the remaining structure of layered layout. The crash is gone now.

MVoloshin commented 2 years ago

Thanks)