gui/graph_displayer_canvas-event.cc
changeset 1526 8c14aa8f27a2
parent 1524 587a823bcdd0
child 1550 4dcbb4ab1d7a
equal deleted inserted replaced
2:7fe12ab792b5 3:31371942cb89
    26   actual_tool=newtool;
    26   actual_tool=newtool;
    27 
    27 
    28   switch(newtool)
    28   switch(newtool)
    29     {
    29     {
    30     case MOVE:
    30     case MOVE:
    31       actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
    31       actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
    32       break;
    32       break;
    33 
    33 
    34       //it has to assigned to canvas, because all the canvas has to be monitored, not only the elements of the already drawn group
       
    35     case CREATE_NODE:
    34     case CREATE_NODE:
    36       actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false);
    35       actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false);
    37       break;
    36       break;
    38 
    37 
    39     case CREATE_EDGE:
    38     case CREATE_EDGE:
    40       actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false);
    39       actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false);
    41       break;
    40       break;
    42 
    41 
    43     case ERASER:
    42     case ERASER:
    44       actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false);
    43       actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false);
    45       break;
    44       break;
    46 
    45 
    47     default:
    46     default:
    48       break;
    47       break;
    49     }
    48     }
    58 {
    57 {
    59   switch(e->type)
    58   switch(e->type)
    60   {
    59   {
    61     case GDK_BUTTON_PRESS:
    60     case GDK_BUTTON_PRESS:
    62       //we mark the location of the event to be able to calculate parameters of dragging
    61       //we mark the location of the event to be able to calculate parameters of dragging
    63       clicked_x=e->button.x;
    62       window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
    64       clicked_y=e->button.y;
    63 
    65       active_item=(get_item_at(e->button.x, e->button.y));
    64       active_item=(get_item_at(clicked_x, clicked_y));
    66       active_node=INVALID;
    65       active_node=INVALID;
    67       for (NodeIt i(g); i!=INVALID; ++i)
    66       for (NodeIt i(g); i!=INVALID; ++i)
    68 	{
    67 	{
    69 	  if(nodesmap[i]==active_item)
    68 	  if(nodesmap[i]==active_item)
    70 	    {
    69 	    {
    83       break;
    82       break;
    84     case GDK_BUTTON_RELEASE:
    83     case GDK_BUTTON_RELEASE:
    85       isbutton=0;
    84       isbutton=0;
    86       active_item=NULL;
    85       active_item=NULL;
    87       active_node=INVALID;
    86       active_node=INVALID;
    88       updateScrollRegion();
       
    89       break;
    87       break;
    90     case GDK_MOTION_NOTIFY:
    88     case GDK_MOTION_NOTIFY:
    91       //we only have to do sg. if the mouse button is pressed AND the click was on a node that was found in the set of nodes
    89       //we only have to do sg. if the mouse button is pressed AND the click was on a node that was found in the set of nodes
    92       if(active_node!=INVALID)
    90       if(active_node!=INVALID)
    93       {
    91       {
    94 	//new coordinates will be the old values,
    92 	//new coordinates will be the old values,
    95 	//because the item will be moved to the
    93 	//because the item will be moved to the
    96 	//new coordinate therefore the new movement
    94 	//new coordinate therefore the new movement
    97 	//has to be calculated from here
    95 	//has to be calculated from here
    98 
    96 
    99         double dx=e->motion.x-clicked_x;
    97 	double new_x, new_y;
   100         double dy=e->motion.y-clicked_y;
    98 
       
    99 	window_to_world (e->motion.x, e->motion.y, new_x, new_y);
       
   100 
       
   101         double dx=new_x-clicked_x;
       
   102         double dy=new_y-clicked_y;
   101 
   103 
   102 	//repositioning node and its text
   104 	//repositioning node and its text
   103         active_item->move(dx, dy);
   105         active_item->move(dx, dy);
   104 	nodetextmap[active_node]->move(dx, dy);
   106 	nodetextmap[active_node]->move(dx, dy);
   105 
   107 
   106         clicked_x=e->motion.x;
   108         clicked_x=new_x;
   107         clicked_y=e->motion.y;
   109         clicked_y=new_y;
   108 
   110 
   109 	//all the edges connected to the moved point has to be redrawn
   111 	//all the edges connected to the moved point has to be redrawn
   110         EdgeIt ei;
   112         EdgeIt ei;
   111 
   113 
   112         g.firstOut(ei,active_node);
   114         g.firstOut(ei,active_node);
   166         }
   168         }
   167       }
   169       }
   168     default: break;
   170     default: break;
   169   }
   171   }
   170 
   172 
   171   return true;
   173   return false;
   172 }
   174 }
   173 
   175 
   174 bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
   176 bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
   175 {
   177 {
   176   switch(e->type)
   178   switch(e->type)
   177     {
   179     {
   178 
   180 
   179       //draw the new node in red at the clicked place
   181       //draw the new node in red at the clicked place
       
   182     case GDK_2BUTTON_PRESS:
       
   183       std::cout << "double click" << std::endl;
       
   184       break;
   180     case GDK_BUTTON_PRESS:
   185     case GDK_BUTTON_PRESS:
   181       isbutton=1;
   186       isbutton=1;
   182 
   187 
   183       active_node=NodeIt(g,g.addNode());
   188       active_node=NodeIt(g,g.addNode());
   184 
   189 
   185       //initiating values corresponding to new node in maps
   190       //initiating values corresponding to new node in maps
   186       
       
   187 
   191 
   188       window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
   192       window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
       
   193 
       
   194       target_item=NULL;
       
   195       target_item=get_item_at(clicked_x, clicked_y);
   189 
   196 
   190       nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
   197       nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
   191       active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
   198       active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
   192       *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
   199       *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
   193       *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
   200       *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
   203       //move the new node
   210       //move the new node
   204     case GDK_MOTION_NOTIFY:
   211     case GDK_MOTION_NOTIFY:
   205       {
   212       {
   206 	double world_motion_x, world_motion_y;
   213 	double world_motion_x, world_motion_y;
   207 	GdkEvent * generated=new GdkEvent();
   214 	GdkEvent * generated=new GdkEvent();
   208 	window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y);
   215 	generated->motion.x=e->motion.x;
   209 	generated->motion.x=world_motion_x;
   216 	generated->motion.y=e->motion.y;
   210 	generated->motion.y=world_motion_y;
       
   211 	generated->type=GDK_MOTION_NOTIFY;
   217 	generated->type=GDK_MOTION_NOTIFY;
   212 	moveEventHandler(generated);      
   218 	moveEventHandler(generated);      
   213 	break;
   219 	break;
   214       }
   220       }
   215 
   221 
   216       //finalize the new node
   222       //finalize the new node
   217     case GDK_BUTTON_RELEASE:
   223     case GDK_BUTTON_RELEASE:
   218       isbutton=0;
   224       isbutton=0;
   219       *active_item << Gnome::Canvas::Properties::fill_color("blue");
   225       if(!target_item)
       
   226 	{
       
   227 	  //Its appropriate color is given by update.
       
   228 	  //*active_item << Gnome::Canvas::Properties::fill_color("blue");
       
   229 	}
       
   230       else
       
   231 	{
       
   232 	  //In this case the given color has to be overwritten, because the noe covers an other item.
       
   233 	  *active_item << Gnome::Canvas::Properties::fill_color("lightblue");
       
   234 	}
       
   235       target_item=NULL;
   220       active_item=NULL;
   236       active_item=NULL;
   221       active_node=INVALID;
   237       active_node=INVALID;
   222       updateScrollRegion();
       
   223       break;
   238       break;
   224     default:
   239     default:
   225       break;
   240       break;
   226     }
   241     }
   227   return false;
   242   return false;
   237 	{
   252 	{
   238 	  //there is not yet selected node
   253 	  //there is not yet selected node
   239 	  if(active_node==INVALID)
   254 	  if(active_node==INVALID)
   240 	    {
   255 	    {
   241 	      //we mark the location of the event to be able to calculate parameters of dragging
   256 	      //we mark the location of the event to be able to calculate parameters of dragging
   242 	      clicked_x=e->button.x;
   257 
   243 	      clicked_y=e->button.y;
   258 	      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
   244 	      active_item=(get_item_at(e->button.x, e->button.y));
   259 
       
   260 	      active_item=(get_item_at(clicked_x, clicked_y));
   245 	      active_node=INVALID;
   261 	      active_node=INVALID;
   246 	      for (NodeIt i(g); i!=INVALID; ++i)
   262 	      for (NodeIt i(g); i!=INVALID; ++i)
   247 		{
   263 		{
   248 		  if(nodesmap[i]==active_item)
   264 		  if(nodesmap[i]==active_item)
   249 		    {
   265 		    {
   267 	  // on a node that was found in the set of 
   283 	  // on a node that was found in the set of 
   268 	  //nodes, and now we only search for the second 
   284 	  //nodes, and now we only search for the second 
   269 	  //node
   285 	  //node
   270 	  else
   286 	  else
   271 	    {
   287 	    {
   272 	      target_item=(get_item_at(e->button.x, e->button.y));
   288 	      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
       
   289 	      target_item=(get_item_at(clicked_x, clicked_y));
   273 	      Graph::NodeIt target_node=INVALID;
   290 	      Graph::NodeIt target_node=INVALID;
   274 	      for (NodeIt i(g); i!=INVALID; ++i)
   291 	      for (NodeIt i(g); i!=INVALID; ++i)
   275 		{
   292 		{
   276 		  if(nodesmap[i]==target_item)
   293 		  if(nodesmap[i]==target_item)
   277 		    {
   294 		    {
   364 bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e)
   381 bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e)
   365 {
   382 {
   366   switch(e->type)
   383   switch(e->type)
   367     {
   384     {
   368     case GDK_BUTTON_PRESS:
   385     case GDK_BUTTON_PRESS:
   369       active_item=(get_item_at(e->button.x, e->button.y));
   386       window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
       
   387       active_item=(get_item_at(clicked_x, clicked_y));
   370       active_node=INVALID;
   388       active_node=INVALID;
   371       active_edge=INVALID;
   389       active_edge=INVALID;
   372       for (NodeIt i(g); i!=INVALID; ++i)
   390       for (NodeIt i(g); i!=INVALID; ++i)
   373 	{
   391 	{
   374 	  if(nodesmap[i]==active_item)
   392 	  if(nodesmap[i]==active_item)
   384 		{
   402 		{
   385 		  active_edge=i;
   403 		  active_edge=i;
   386 		}
   404 		}
   387 	    }
   405 	    }
   388 	}
   406 	}
   389     *active_item << Gnome::Canvas::Properties::fill_color("red");
   407       if(active_item)
       
   408 	{
       
   409 	  *active_item << Gnome::Canvas::Properties::fill_color("red");
       
   410 	}
   390       break;
   411       break;
   391 
   412 
   392     case GDK_BUTTON_RELEASE:
   413     case GDK_BUTTON_RELEASE:
   393       if(active_item==(get_item_at(e->button.x, e->button.y)))
   414       window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
   394 	{
   415       if(active_item)
   395 	  if(active_node!=INVALID)
   416 	{
   396 	    {
   417 	  if( active_item == ( get_item_at (clicked_x, clicked_y) ) )
   397 
   418 	    {
   398 	      //collecting edges to delete
   419 	      if(active_node!=INVALID)
   399 	      EdgeIt e;
   420 		{
   400 	      std::set<Graph::Edge> edges_to_delete;
   421 
   401 
   422 		  //collecting edges to delete
   402 	      g.firstOut(e,active_node);
   423 		  EdgeIt e;
   403 	      for(;e!=INVALID;g.nextOut(e))
   424 		  std::set<Graph::Edge> edges_to_delete;
   404 		{
   425 
       
   426 		  g.firstOut(e,active_node);
       
   427 		  for(;e!=INVALID;g.nextOut(e))
       
   428 		    {
   405 		      edges_to_delete.insert(e);
   429 		      edges_to_delete.insert(e);
   406 		}
   430 		    }
   407 
   431 
   408 	      g.firstIn(e,active_node);
   432 		  g.firstIn(e,active_node);
   409 	      for(;e!=INVALID;g.nextIn(e))
   433 		  for(;e!=INVALID;g.nextIn(e))
   410 		{
   434 		    {
   411 		      edges_to_delete.insert(e);
   435 		      edges_to_delete.insert(e);
   412 		}
   436 		    }
   413 
   437 
   414 	      //deleting collected edges
   438 		  //deleting collected edges
   415 	      for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
   439 		  for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
   416 		{
   440 		    {
   417 		  deleteItem(*edge_set_it);
   441 		      deleteItem(*edge_set_it);
   418 		}
   442 		    }
   419 	      deleteItem(active_node);
   443 		  deleteItem(active_node);
   420 	    }
   444 		}
   421 	  //a simple edge was chosen
   445 	      //a simple edge was chosen
       
   446 	      else
       
   447 		{
       
   448 		  deleteItem(active_edge);
       
   449 		}
       
   450 	    }
       
   451 	  //pointer was moved, deletion is cancelled
   422 	  else
   452 	  else
   423 	    {
   453 	    {
   424 	      deleteItem(active_edge);
   454 	      if(active_node!=INVALID)
   425 	    }
   455 		{
   426 
   456 		  *active_item << Gnome::Canvas::Properties::fill_color("blue");
   427 	  
   457 		}
   428 	}
   458 	      else
   429       //pointer was moved, deletion is cancelled
   459 		{
   430       else
   460 		  *active_item << Gnome::Canvas::Properties::fill_color("green");
   431 	{
   461 		}
   432 	  if(active_node!=INVALID)
       
   433 	    {
       
   434 	      *active_item << Gnome::Canvas::Properties::fill_color("blue");
       
   435 	    }
       
   436 	  else
       
   437 	    {
       
   438 	      *active_item << Gnome::Canvas::Properties::fill_color("green");
       
   439 	    }
   462 	    }
   440 	}
   463 	}
   441       //reseting datas
   464       //reseting datas
   442       active_item=NULL;
   465       active_item=NULL;
   443       active_edge=INVALID;
   466       active_edge=INVALID;
   448       break;
   471       break;
   449 
   472 
   450     default:
   473     default:
   451       break;
   474       break;
   452     }
   475     }
   453   return true;
   476   return false;
   454 }
   477 }
   455 
   478 
   456 void GraphDisplayerCanvas::deleteItem(NodeIt node_to_delete)
   479 void GraphDisplayerCanvas::deleteItem(NodeIt node_to_delete)
   457 {
   480 {
   458   delete(nodetextmap[node_to_delete]);
   481   delete(nodetextmap[node_to_delete]);