0
$\begingroup$

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
  ]
 ]
$\endgroup$
  • $\begingroup$ Removing by double click works fine in the combinated programm. And I noticed the nodes move slightly the the direction I want them to with drag 'n drop. It might be a problem differing between drag 'n drop and double clicking? $\endgroup$ – Phab Jul 9 '14 at 14:44
  • $\begingroup$ EDIT: second try: everything works fine. try to select deselect the plot! This worked for me, I could remove and move the nodes. $\endgroup$ – Phab Jul 9 '14 at 14:49
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.