graph_displayer_canvas.cc
branchgui
changeset 9 0bb1675306cb
parent 7 6a9399f56813
child 11 09b2a893fc9d
equal deleted inserted replaced
4:141bbe037636 5:ebbbe507322e
     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)
     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   
       
    10   actual_handler=/*displayed_graph.*/signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
       
    11 
     6   //set_center_scroll_region(true);
    12   //set_center_scroll_region(true);
     7 
    13 
     8   //first edges are drawn, to hide joining with nodes later
    14   //first edges are drawn, to hide joining with nodes later
     9 
    15 
    10   for (EdgeIt i(g); i!=INVALID; ++i)
    16   for (EdgeIt i(g); i!=INVALID; ++i)
    46     //drawing bule nodes, with black line around them
    52     //drawing bule nodes, with black line around them
    47 
    53 
    48     nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
    54     nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
    49     *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
    55     *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
    50     *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
    56     *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
    51     (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
    57     //!!!!!!! (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
    52   }
    58   }
    53 
    59 
    54 /*
       
    55   //setting zoom to be able to see the whole graph on the canvas
       
    56 
       
    57   double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
       
    58   double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
       
    59 
       
    60   set_pixels_per_unit((biggest_x>biggest_y)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
       
    61   std::cout<<abs(maxx)<<" "<<abs(minx)<<" big x "<<biggest_x<<" "<<abs(maxy)<<" "<<abs(miny)<<" big y "<<biggest_y<<std::endl;
       
    62   std::cout<<maxx<<" "<<minx<<" big x "<<biggest_x<<" "<<maxy<<" "<<miny<<" big y "<<biggest_y<<std::endl;
       
    63   std::cout<<"dx "<<(maxx-minx)<<" dy "<<(maxy-miny)<<" xrate "<<((maxx-minx)/WIN_WIDTH)<<" yrate "<<((maxy-miny)/WIN_HEIGHT)<<std::endl;
       
    64 */
       
    65   updateScrollRegion();
    60   updateScrollRegion();
    66 }
    61 }
    67 
    62 
    68 GraphDisplayerCanvas::~GraphDisplayerCanvas()
    63 GraphDisplayerCanvas::~GraphDisplayerCanvas()
    69 {
    64 {
   306   double wx1, wy1, wx2, wy2;
   301   double wx1, wy1, wx2, wy2;
   307   Gnome::Canvas::Item* pCanvasItem = root();
   302   Gnome::Canvas::Item* pCanvasItem = root();
   308   pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
   303   pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
   309   set_scroll_region(wx1, wy1, wx2, wy2);
   304   set_scroll_region(wx1, wy1, wx2, wy2);
   310 }
   305 }
       
   306 
       
   307 void GraphDisplayerCanvas::changeEditorialTool(int newtool)
       
   308 {
       
   309   actual_handler.disconnect();
       
   310 
       
   311   switch(newtool)
       
   312     {
       
   313     case MOVE:
       
   314       actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false);
       
   315       break;
       
   316     case CREATE_NODE:
       
   317       actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
       
   318       break;
       
   319     case CREATE_EDGE:
       
   320       actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
       
   321       break;
       
   322     default:
       
   323       break;
       
   324     }
       
   325 }
       
   326 
       
   327 bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e)
       
   328 {
       
   329   switch(e->type)
       
   330   {
       
   331     case GDK_BUTTON_PRESS:
       
   332       //we mark the location of the event to be able to calculate parameters of dragging
       
   333       clicked_x=e->button.x;
       
   334       clicked_y=e->button.y;
       
   335       active_item=(get_item_at(e->button.x, e->button.y));
       
   336       active_node=INVALID;
       
   337       for (NodeIt i(g); i!=INVALID; ++i)
       
   338 	{
       
   339 	  if(nodesmap[i]==active_item)
       
   340 	    {
       
   341 	      active_node=i;
       
   342 	    }
       
   343 	}
       
   344       isbutton=true;
       
   345       break;
       
   346     case GDK_BUTTON_RELEASE:
       
   347       isbutton=false;
       
   348       active_item=NULL;
       
   349       updateScrollRegion();
       
   350       break;
       
   351     case GDK_MOTION_NOTIFY:
       
   352       //we only have to do sg. if the mouse button is pressed
       
   353       if(isbutton)
       
   354       {
       
   355 	//new coordinates will be the old values,
       
   356 	//because the item will be moved to the
       
   357 	//new coordinate therefore the new movement
       
   358 	//has to be calculated from here
       
   359 
       
   360         double dx=e->motion.x-clicked_x;
       
   361         double dy=e->motion.y-clicked_y;
       
   362 
       
   363         active_item->move(dx, dy);
       
   364 
       
   365         clicked_x=e->motion.x;
       
   366         clicked_y=e->motion.y;
       
   367 
       
   368 	//all the edges connected to the moved point has to be redrawn
       
   369 
       
   370         EdgeIt e;
       
   371 
       
   372         g.firstOut(e,active_node);
       
   373 
       
   374         for(;e!=INVALID;g.nextOut(e))
       
   375         {
       
   376             Gnome::Canvas::Points coos;
       
   377             double x1, x2, y1, y2;
       
   378 
       
   379             nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
       
   380             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
       
   381 
       
   382             nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
       
   383             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
       
   384 
       
   385             edgesmap[e]->property_points().set_value(coos);
       
   386 
       
   387 	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
       
   388 
       
   389 	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
       
   390 	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
       
   391         }
       
   392 
       
   393         g.firstIn(e,active_node);
       
   394         for(;e!=INVALID;g.nextIn(e))
       
   395         {
       
   396             Gnome::Canvas::Points coos;
       
   397             double x1, x2, y1, y2;
       
   398 
       
   399             nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
       
   400             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
       
   401 
       
   402             nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
       
   403             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
       
   404 
       
   405             edgesmap[e]->property_points().set_value(coos);
       
   406 
       
   407 	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
       
   408 
       
   409 	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
       
   410 	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
       
   411         }
       
   412       }
       
   413     default: break;
       
   414   }
       
   415 
       
   416   return true;
       
   417 }
       
   418 
       
   419 bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e)
       
   420 {
       
   421   switch(e->type)
       
   422     {
       
   423     case GDK_BUTTON_PRESS:
       
   424       isbutton=true;
       
   425 
       
   426       active_node=NodeIt(g,g.addNode());
       
   427 
       
   428       window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
       
   429 
       
   430       nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
       
   431       active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
       
   432       *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
       
   433       *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
       
   434       (nodesmap[active_node])->show();
       
   435       break;
       
   436     case GDK_MOTION_NOTIFY:
       
   437       {
       
   438 	double world_motion_x, world_motion_y;
       
   439 	GdkEvent * generated=new GdkEvent();
       
   440 	window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y);
       
   441 	generated->motion.x=world_motion_x;
       
   442 	generated->motion.y=world_motion_y;
       
   443 	generated->type=GDK_MOTION_NOTIFY;
       
   444 	move_event_handler(generated);      
       
   445 	break;
       
   446       }
       
   447     case GDK_BUTTON_RELEASE:
       
   448       isbutton=false;
       
   449       *active_item << Gnome::Canvas::Properties::fill_color("blue");
       
   450       active_item=NULL;
       
   451       updateScrollRegion();
       
   452       break;
       
   453     default:
       
   454       break;
       
   455     }
       
   456   return false;
       
   457 }
       
   458 
       
   459 bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e)
       
   460 {
       
   461   e=e;
       
   462   return false;
       
   463 }
       
   464