graph_displayer_canvas.cc
branchgui
changeset 20 a3bd39d50930
parent 19 164783ceb9be
child 21 44bb92014108
equal deleted inserted replaced
12:6181d3029e6d 13:922f1d2a70c4
     1 #include <graph_displayer_canvas.h>
     1 #include <graph_displayer_canvas.h>
     2 #include <broken_edge.h>
     2 #include <broken_edge.h>
     3 #include <math.h>
     3 #include <math.h>
     4 
     4 
     5 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 GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm, MapStorage & ms):g(gr),nodesmap(g),edgesmap(g),edgetextmap(g),displayed_graph(*(root()), 0, 0),mapstorage(ms),isbutton(0),active_item(NULL),target_item(NULL)
     6 {
     6 {
     7   
     7   
     8   actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
     8   actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
     9 
     9 
    10   active_node=INVALID;
    10   active_node=INVALID;
   179     case GDK_BUTTON_PRESS:
   179     case GDK_BUTTON_PRESS:
   180       //we mark the location of the event to be able to calculate parameters of dragging
   180       //we mark the location of the event to be able to calculate parameters of dragging
   181       clicked_x=e->button.x;
   181       clicked_x=e->button.x;
   182       clicked_y=e->button.y;
   182       clicked_y=e->button.y;
   183       active_item=(get_item_at(e->button.x, e->button.y));
   183       active_item=(get_item_at(e->button.x, e->button.y));
   184       isbutton=true;
   184       isbutton=1;
   185       break;
   185       break;
   186     case GDK_BUTTON_RELEASE:
   186     case GDK_BUTTON_RELEASE:
   187       isbutton=false;
   187       isbutton=0;
   188       active_item=NULL;
   188       active_item=NULL;
   189       updateScrollRegion();
   189       updateScrollRegion();
   190       break;
   190       break;
   191     case GDK_MOTION_NOTIFY:
   191     case GDK_MOTION_NOTIFY:
   192       //we only have to do sg. if the mouse button is pressed
   192       //we only have to do sg. if the mouse button is pressed
   348 	  if(nodesmap[i]==active_item)
   348 	  if(nodesmap[i]==active_item)
   349 	    {
   349 	    {
   350 	      active_node=i;
   350 	      active_node=i;
   351 	    }
   351 	    }
   352 	}
   352 	}
   353       isbutton=true;
   353       switch(e->button.button)
       
   354 	{
       
   355 	case 3:      
       
   356 	  isbutton=3;
       
   357 	  break;
       
   358 	default:
       
   359 	  isbutton=1;
       
   360 	  break;
       
   361 	}
   354       break;
   362       break;
   355     case GDK_BUTTON_RELEASE:
   363     case GDK_BUTTON_RELEASE:
   356       isbutton=false;
   364       isbutton=0;
   357       active_item=NULL;
   365       active_item=NULL;
   358       active_node=INVALID;
   366       active_node=INVALID;
   359       updateScrollRegion();
   367       updateScrollRegion();
   360       break;
   368       break;
   361     case GDK_MOTION_NOTIFY:
   369     case GDK_MOTION_NOTIFY:
   374 
   382 
   375         clicked_x=e->motion.x;
   383         clicked_x=e->motion.x;
   376         clicked_y=e->motion.y;
   384         clicked_y=e->motion.y;
   377 
   385 
   378 	//all the edges connected to the moved point has to be redrawn
   386 	//all the edges connected to the moved point has to be redrawn
   379         EdgeIt e;
   387         EdgeIt ei;
   380 
   388 
   381         g.firstOut(e,active_node);
   389         g.firstOut(ei,active_node);
   382 
   390 
   383         for(;e!=INVALID;g.nextOut(e))
   391         for(;ei!=INVALID;g.nextOut(ei))
   384         {
   392         {
   385             Gnome::Canvas::Points coos;
   393             Gnome::Canvas::Points coos;
   386             double x1, x2, y1, y2;
   394             double x1, x2, y1, y2;
   387 
   395 
   388             nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
   396             nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
   389             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   397             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   390 
   398 
   391             nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
   399             nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
   392             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   400             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   393 
   401 
   394             edgesmap[e]->set_points(coos);
   402 	    if(isbutton==3)
   395 
   403 	      {
   396 	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
   404 		edgesmap[ei]->set_points(coos);
   397 
   405 	      }
   398 	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
   406 	    else
   399 	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
   407 	      {
       
   408 		edgesmap[ei]->set_points(coos,true);
       
   409 	      }
       
   410 
       
   411 	    edgesmap[ei]->get_bounds(x1, y1, x2, y2);
       
   412 
       
   413 	    edgetextmap[ei]->property_x().set_value((x1+x2)/2);
       
   414 	    edgetextmap[ei]->property_y().set_value((y1+y2)/2);
   400         }
   415         }
   401 
   416 
   402         g.firstIn(e,active_node);
   417         g.firstIn(ei,active_node);
   403         for(;e!=INVALID;g.nextIn(e))
   418         for(;ei!=INVALID;g.nextIn(ei))
   404         {
   419         {
   405             Gnome::Canvas::Points coos;
   420             Gnome::Canvas::Points coos;
   406             double x1, x2, y1, y2;
   421             double x1, x2, y1, y2;
   407 
   422 
   408             nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
   423             nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
   409             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   424             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   410 
   425 
   411             nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
   426             nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
   412             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   427             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   413 
   428 
   414             edgesmap[e]->set_points(coos);
   429 	    if(isbutton==3)
   415 
   430 	      {
   416 	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
   431 		edgesmap[ei]->set_points(coos);
   417 
   432 	      }
   418 	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
   433 	    else
   419 	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
   434 	      {
       
   435 		edgesmap[ei]->set_points(coos,true);
       
   436 	      }
       
   437 
       
   438 	    edgesmap[ei]->get_bounds(x1, y1, x2, y2);
       
   439 
       
   440 	    edgetextmap[ei]->property_x().set_value((x1+x2)/2);
       
   441 	    edgetextmap[ei]->property_y().set_value((y1+y2)/2);
   420         }
   442         }
   421       }
   443       }
   422     default: break;
   444     default: break;
   423   }
   445   }
   424 
   446 
   430   switch(e->type)
   452   switch(e->type)
   431     {
   453     {
   432 
   454 
   433       //draw the new node in red at the clicked place
   455       //draw the new node in red at the clicked place
   434     case GDK_BUTTON_PRESS:
   456     case GDK_BUTTON_PRESS:
   435       isbutton=true;
   457       isbutton=1;
   436 
   458 
   437       active_node=NodeIt(g,g.addNode());
   459       active_node=NodeIt(g,g.addNode());
   438 
   460 
   439       window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
   461       window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
   440 
   462 
   458 	break;
   480 	break;
   459       }
   481       }
   460 
   482 
   461       //finalize the new node
   483       //finalize the new node
   462     case GDK_BUTTON_RELEASE:
   484     case GDK_BUTTON_RELEASE:
   463       isbutton=false;
   485       isbutton=0;
   464       *active_item << Gnome::Canvas::Properties::fill_color("blue");
   486       *active_item << Gnome::Canvas::Properties::fill_color("blue");
   465       active_item=NULL;
   487       active_item=NULL;
   466       active_node=INVALID;
   488       active_node=INVALID;
   467       updateScrollRegion();
   489       updateScrollRegion();
   468       break;
   490       break;
   497 		}
   519 		}
   498 	      //the clicked item is really a node
   520 	      //the clicked item is really a node
   499 	      if(active_node!=INVALID)
   521 	      if(active_node!=INVALID)
   500 		{
   522 		{
   501 		  *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
   523 		  *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
   502 		  isbutton=true;
   524 		  isbutton=1;
   503 		}
   525 		}
   504 	      //clicked item was not a node. It could be e.g. edge.
   526 	      //clicked item was not a node. It could be e.g. edge.
   505 	      else
   527 	      else
   506 		{
   528 		{
   507 		  active_item=NULL;
   529 		  active_item=NULL;
   562 		}
   584 		}
   563 	    }
   585 	    }
   564 	}
   586 	}
   565       break;
   587       break;
   566     case GDK_BUTTON_RELEASE:
   588     case GDK_BUTTON_RELEASE:
   567       isbutton=false;
   589       isbutton=0;
   568       //we clear settings in two cases
   590       //we clear settings in two cases
   569       //1: the edge is ready (target_item has valid value)
   591       //1: the edge is ready (target_item has valid value)
   570       //2: the edge creation is cancelled with right button
   592       //2: the edge creation is cancelled with right button
   571       if((target_item)||(e->button.button==3))
   593       if((target_item)||(e->button.button==3))
   572 	{
   594 	{