Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
  ]
 ]
share|improve this question
    
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? –  Phab Jul 9 at 14:44
    
EDIT: second try: everything works fine. try to select deselect the plot! This worked for me, I could remove and move the nodes. –  Phab Jul 9 at 14:49

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer

You might combine the EventHandlers into a single one:

(*Combined*)
DynamicModule[{edglst, vlst, VPOS, ind},
 With[{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[
  Dynamic@Refresh[
    Graph[vlst, edglst, VertexCoordinates -> VPOS, 
     VertexSize -> {.02, .02}, VertexLabels -> "Name", PlotRange -> 1,
      EdgeStyle -> {Thick}],
    TrackedSymbols :> {VPOS}],
  {"MouseDown" :> (With[{pos = MousePosition["Graphics"]}, 
      ind = Nearest[VPOS -> Range@Length@VPOS, pos]]), 
   "MouseDragged" :> (VPOS[[First@ind]] = MousePosition["Graphics"]),
   "MouseClicked" :> If[
     CurrentValue["MouseClickCount"] == 2,
     With[{pos = MousePosition["Graphics"]},
      (ind = Nearest[VPOS -> Range@Length@VPOS, pos, {1, 0.05}];
       If[Length[ind] > 0,
        edglst = Delete[edglst, 
          Map[List, Position[edglst, vlst[[First@ind]]][[All, 1]]]];
        vlst = Drop[vlst, ind];
        VPOS = Drop[VPOS, ind];
        ])
      ]]},
  PassEventsDown -> True]
 ]

I also localized your real variables in the DynamicModule. For double-clicking to delete a vertex, I changed the code to a similar use of Nearest you have for dragging. The difference is that a mouse-click only counts if it is within 0.05 of a vertex (the distance was determined by trial). Finally, I restricted the tracked variables to only the VPOS array. It's not strictly necessary, but it simplifies what the programmer has to keep track of in developing the code.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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