gui/graph_displayer_canvas.cc
changeset 1477 0d32f7947a00
parent 1468 d0ccb2fdeeff
child 1478 bc7ae573d77d
equal deleted inserted replaced
5:9de1b9d130e7 6:bb6f10991669
     1 #include <graph_displayer_canvas.h>
     1 #include <graph_displayer_canvas.h>
     2 #include <math.h>
     2 #include <math.h>
     3 
     3 
     4 GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm, MapStorage & ms):g(gr),nodesmap(g),edgesmap(g),edgetextmap(g),displayed_graph(*(root()), 0, 0),mapstorage(ms),isbutton(false),active_item(NULL)
     4 GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm, MapStorage & ms):g(gr),nodesmap(g),edgesmap(g),edgetextmap(g),displayed_graph(*(root()), 0, 0),mapstorage(ms),isbutton(false),active_item(NULL),target_item(NULL)
     5 {
     5 {
     6   Gnome::Canvas::Ellipse * origo=new Gnome::Canvas::Ellipse(displayed_graph, 0-20, 0-20, 0+20, 0+20);
       
     7   *origo << Gnome::Canvas::Properties::fill_color("black");
       
     8   *origo << Gnome::Canvas::Properties::outline_color("black");
       
     9   
     6   
    10   actual_handler=/*displayed_graph.*/signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
     7   actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
    11 
     8 
    12   //set_center_scroll_region(true);
     9   //set_center_scroll_region(true);
    13 
    10 
    14   //first edges are drawn, to hide joining with nodes later
    11   //first edges are drawn, to hide joining with nodes later
    15 
    12 
   364 
   361 
   365         clicked_x=e->motion.x;
   362         clicked_x=e->motion.x;
   366         clicked_y=e->motion.y;
   363         clicked_y=e->motion.y;
   367 
   364 
   368 	//all the edges connected to the moved point has to be redrawn
   365 	//all the edges connected to the moved point has to be redrawn
   369 
       
   370         EdgeIt e;
   366         EdgeIt e;
   371 
   367 
   372         g.firstOut(e,active_node);
   368         g.firstOut(e,active_node);
   373 
   369 
   374         for(;e!=INVALID;g.nextOut(e))
   370         for(;e!=INVALID;g.nextOut(e))
   456   return false;
   452   return false;
   457 }
   453 }
   458 
   454 
   459 bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e)
   455 bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e)
   460 {
   456 {
   461   e=e;
   457   switch(e->type)
       
   458     {
       
   459     case GDK_BUTTON_PRESS:
       
   460       if(!active_item)
       
   461 	{
       
   462 	  //we mark the location of the event to be able to calculate parameters of dragging
       
   463 	  clicked_x=e->button.x;
       
   464 	  clicked_y=e->button.y;
       
   465 	  active_item=(get_item_at(e->button.x, e->button.y));
       
   466 	  active_node=INVALID;
       
   467 	  for (NodeIt i(g); i!=INVALID; ++i)
       
   468 	    {
       
   469 	      if(nodesmap[i]==active_item)
       
   470 		{
       
   471 		  active_node=i;
       
   472 		}
       
   473 	    }
       
   474 	  *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
       
   475 	  isbutton=true;
       
   476 	}
       
   477       else
       
   478 	{
       
   479 	  target_item=(get_item_at(e->button.x, e->button.y));
       
   480 	  Graph::NodeIt target_node=INVALID;
       
   481 	  for (NodeIt i(g); i!=INVALID; ++i)
       
   482 	    {
       
   483 	      if(nodesmap[i]==target_item)
       
   484 		{
       
   485 		  target_node=i;
       
   486 		}
       
   487 	    }
       
   488 	  *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
       
   489 
       
   490 	  //creating new edge
       
   491 	  //	  Graph::Edge new_edge=g.addEdge(active_node, target_node);
       
   492 	  active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
       
   493 	  
       
   494 	  //calculating coordinates of new edge
       
   495 	  Gnome::Canvas::Points coos;
       
   496 	  double x1, x2, y1, y2;
       
   497 	  
       
   498 	  active_item->get_bounds(x1, y1, x2, y2);
       
   499 	  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
       
   500 
       
   501 	  target_item->get_bounds(x1, y1, x2, y2);
       
   502 	  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
       
   503 
       
   504 	  //drawing new edge
       
   505 	  edgesmap[active_edge]=new Gnome::Canvas::Line(displayed_graph, coos);
       
   506 	  *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
       
   507 	  edgesmap[active_edge]->property_width_pixels().set_value(10);
       
   508 
       
   509 	  //redraw nodes to blank terminations of the new edge
       
   510 	  target_item->raise_to_top();
       
   511 	  active_item->raise_to_top();
       
   512 
       
   513 	  //initializing edge-text as well, to empty string
       
   514 	  edgesmap[active_edge]->get_bounds(x1, y1, x2, y2);
       
   515 	  edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
       
   516 	  edgetextmap[active_edge]->property_fill_color().set_value("black");
       
   517 	}
       
   518       break;
       
   519     case GDK_BUTTON_RELEASE:
       
   520       isbutton=false;
       
   521       if(target_item)
       
   522 	{
       
   523 	  *active_item << Gnome::Canvas::Properties::fill_color("blue");
       
   524 	  *target_item << Gnome::Canvas::Properties::fill_color("blue");
       
   525 	  active_item=NULL;
       
   526 	  target_item=NULL;
       
   527 	}
       
   528       break;
       
   529     default:
       
   530       break;
       
   531     }
   462   return false;
   532   return false;
   463 }
   533 }
   464 
   534