Skip to main content

dynamic - What is wrong with combining two programs with event handlers?


I can delete the unwanted nodes of a RandomGraph by double-clicking on them from the first program, and I can change the positions of the nodes by dragging them from the second program. They both work nicely. However, when I combined the two programs into one, the nodes can't be dragged anymore. What is wrong with the combined promgram? Please help.


(* First program, nodes can be deleted by double-clicking on them *)
DynamicModule[{rg },
rg = RandomGraph[{7, 10}];
edglst = EdgeList[rg];
vlst = VertexList[rg];
VPOS = VertexCoordinates /. AbsoluteOptions[rg, VertexCoordinates];

VPOS = Rescale[#, 1 + Through[{Min, Max}[VPOS]], {0, 1}] & /@ VPOS;

Vnthlrs = MapThread[EventHandler[#, {
"MouseDown" :> If[CurrentValue["MouseClickCount"] == 2, (
ep = First@Position[vlst, #];
vlst = Drop[vlst, ep ];
VPOS = Drop[VPOS, ep ];
edglst =
Delete[edglst, Map[List, Position[edglst, #][[All, 1]]]];
Vnthlrs = Drop[Vnthlrs, ep ])]

}] &, {vlst}];

g = Dynamic@Graph[Vnthlrs, edglst,
VertexCoordinates -> VPOS,
VertexSize -> {.02, .02},
VertexLabels -> "Name",
PlotRange -> 1,
EdgeStyle -> {Thick}
]
]


(* Second program, nodes can be moved by dragging them *)
DynamicModule[{rg, refr = True},
rg = RandomGraph[{7, 10}];
edglst = EdgeList[rg];
vlst = VertexList[rg];
VPOS = VertexCoordinates /. AbsoluteOptions[rg, VertexCoordinates];
VPOS = Rescale[#, 1 + Through[{Min, Max}[VPOS]], {0, 1}] & /@ VPOS;

EventHandler[

g = Dynamic@
Graph[vlst, edglst, VertexCoordinates -> VPOS,
VertexSize -> {.02, .02},
VertexLabels -> "Name",
PlotRange -> 1,
EdgeStyle -> {Thick}],
{"MouseDown" :> (
With[{pos = MousePosition["Graphics"]},
ind = Position[VPOS, Nearest[VPOS, pos][[1]]][[1, 1]]]),


"MouseDragged" :>
(VPOS[[ind ]] = MousePosition["Graphics"] )
},
PassEventsDown -> True
]
]

(* Combined *)
DynamicModule[{rg },
rg = RandomGraph[{7, 10}];

edglst = EdgeList[rg];
vlst = VertexList[rg];
VPOS = VertexCoordinates /. AbsoluteOptions[rg, VertexCoordinates];
VPOS = Rescale[#, 1 + Through[{Min, Max}[VPOS]], {0, 1}] & /@ VPOS;

Vnthlrs = MapThread[EventHandler[#, {
"MouseDown" :> If[CurrentValue["MouseClickCount"] == 2, (
ep = First@Position[vlst, #];
vlst = Drop[vlst, ep ];
VPOS = Drop[VPOS, ep ];

edglst =
Delete[edglst, Map[List, Position[edglst, #][[All, 1]]]];
Vnthlrs = Drop[Vnthlrs, ep ])]
}] &, {vlst}];

EventHandler[
g = Dynamic@
Graph[Vnthlrs, edglst, VertexCoordinates -> VPOS,
VertexSize -> {.02, .02},
VertexLabels -> "Name",

PlotRange -> 1,
EdgeStyle -> {Thick}],
{"MouseDown" :> (
With[{pos = MousePosition["Graphics"]},
ind = Position[VPOS, Nearest[VPOS, pos][[1]]][[1, 1]]]),

"MouseDragged" :>
(VPOS[[ind ]] = MousePosition["Graphics"] )
},
PassEventsDown -> True

]
]

Answer



Finally, I discovered that the combined program works fine as long as the dragging just begin from the point close to the node (not right on the top of it). Thanks.


Comments