gui/graph_displayer_canvas.cc
author ladanyi
Tue, 14 Jun 2005 09:42:46 +0000
changeset 1480 268c2b5d8971
parent 1474 75c6d2eb187a
child 1485 3a1c6678fa23
permissions -rwxr-xr-x
distribute the LICENSE file too
     1 #include <graph_displayer_canvas.h>
     2 #include <math.h>
     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),target_item(NULL)
     5 {
     6   
     7   actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
     8 
     9   active_node=INVALID;
    10   active_edge=INVALID;
    11 
    12   //set_center_scroll_region(true);
    13 
    14   //first edges are drawn, to hide joining with nodes later
    15 
    16   for (EdgeIt i(g); i!=INVALID; ++i)
    17   {
    18 
    19     //drawing green lines, coordinates are from cm
    20 
    21     Gnome::Canvas::Points coos;
    22     coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
    23     coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
    24     
    25     edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
    26     *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
    27     edgesmap[i]->property_width_pixels().set_value(10);    
    28     
    29     //initializing edge-text as well, to empty string
    30 
    31     double x1, x2, y1, y2;
    32     edgesmap[i]->get_bounds(x1, y1, x2, y2);
    33     
    34     edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
    35     edgetextmap[i]->property_fill_color().set_value("black");
    36   }
    37 
    38   //afterwards nodes come to be drawn
    39 
    40   NodeIt i(g);
    41   int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
    42 
    43   for (; i!=INVALID; ++i)
    44   {
    45     //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
    46 
    47     if(cm[i].x>maxx)maxx=(int)cm[i].x;
    48     if(cm[i].y>maxy)maxy=(int)cm[i].y;
    49     if(cm[i].x<minx)minx=(int)cm[i].x;
    50     if(cm[i].y<miny)miny=(int)cm[i].y;
    51 
    52     //drawing bule nodes, with black line around them
    53 
    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);
    55     *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
    56     *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
    57     //!!!!!!! (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
    58   }
    59 
    60   updateScrollRegion();
    61 }
    62 
    63 GraphDisplayerCanvas::~GraphDisplayerCanvas()
    64 {
    65 
    66   //writing out the end state of the graph
    67   //\todo all the maps has to be write out!
    68 
    69   Graph::NodeMap <int> id(g);
    70   Graph::NodeMap <double> xc(g);
    71   Graph::NodeMap <double> yc(g);
    72   
    73   int j=1;
    74   
    75   for (NodeIt i(g); i!=INVALID; ++i)
    76   {
    77     double x1,y1,x2,y2;
    78     nodesmap[i]->get_bounds(x1, y1, x2, y2);
    79     
    80     id[i]=j++;
    81     xc[i]=(x1+x2)/2;
    82     yc[i]=(y1+y2)/2;
    83   }
    84 
    85   GraphWriter<Graph> writer(std::cout,g);
    86   
    87   writer.writeNodeMap("id", id);
    88   writer.writeNodeMap("coordinates_x", xc);
    89   writer.writeNodeMap("coordinates_y", yc);
    90   writer.run();
    91 }
    92 
    93 int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
    94 {
    95   for (EdgeIt i(g); i!=INVALID; ++i)
    96   {
    97     int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
    98     edgesmap[i]->property_width_pixels().set_value(w);
    99   }
   100   return 0;
   101 };
   102 
   103 int GraphDisplayerCanvas::changeColor (std::string mapname)
   104 {  
   105 
   106   //function maps the range of the maximum and
   107   //the minimum of the nodemap to the range of
   108   //green in RGB
   109 
   110   for (EdgeIt i(g); i!=INVALID; ++i)
   111   {
   112     double w=(*(mapstorage.edgemap_storage)[mapname])[i];
   113     double max=mapstorage.maxOfEdgeMap(mapname);
   114     double min=mapstorage.minOfEdgeMap(mapname);
   115       
   116     //std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
   117     Gdk::Color color;
   118     if(max!=min)
   119     {
   120       color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
   121     }
   122     else
   123     {
   124       color.set_rgb_p (0, 100, 0);
   125     }
   126 
   127     edgesmap[i]->property_fill_color_gdk().set_value(color);
   128   }
   129   return 0;
   130 };
   131 
   132 int GraphDisplayerCanvas::changeText (std::string mapname)
   133 {
   134 
   135   //the number in the map will be written on the edge
   136   //EXCEPT when the name of the map is Text, because
   137   //in that case empty string will be written, because
   138   //that is the deleter map
   139   //\todo isn't it a bit woodcutter?
   140 
   141   for (EdgeIt i(g); i!=INVALID; ++i)
   142   {
   143     if(mapname!="Text")
   144     {
   145       double number=(*(mapstorage.edgemap_storage)[mapname])[i];
   146       int length=(int)(floor(log(number)/log(10)))+1;
   147       int maxpos=(int)(pow(10,length-1));
   148       int strl=length+1+RANGE;
   149       char * str=new char[strl];
   150       str[length]='.';
   151       str[strl]='\0';
   152       
   153       for(int j=0;j<strl;j++)
   154       {
   155 	if(j!=length)
   156         {
   157 	  int digit=(int)(number/maxpos);
   158 	  str[j]=(digit+'0');
   159 	  number-=digit*maxpos;
   160 	  number*=10;
   161         }
   162       }
   163       
   164       edgetextmap[i]->property_text().set_value(str);
   165     }
   166     else
   167     {
   168       edgetextmap[i]->property_text().set_value("");
   169     }
   170   }
   171   return 0;
   172 };
   173 
   174 bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
   175 {
   176   switch(e->type)
   177   {
   178     case GDK_BUTTON_PRESS:
   179       //we mark the location of the event to be able to calculate parameters of dragging
   180       clicked_x=e->button.x;
   181       clicked_y=e->button.y;
   182       active_item=(get_item_at(e->button.x, e->button.y));
   183       isbutton=true;
   184       break;
   185     case GDK_BUTTON_RELEASE:
   186       isbutton=false;
   187       active_item=NULL;
   188       updateScrollRegion();
   189       break;
   190     case GDK_MOTION_NOTIFY:
   191       //we only have to do sg. if the mouse button is pressed
   192       if(isbutton)
   193       {
   194 	//new coordinates will be the old values,
   195 	//because the item will be moved to the
   196 	//new coordinate therefore the new movement
   197 	//has to be calculated from here
   198 
   199         double dx=e->motion.x-clicked_x;
   200         double dy=e->motion.y-clicked_y;
   201         active_item->move(dx, dy);
   202         clicked_x=e->motion.x;
   203         clicked_y=e->motion.y;
   204 
   205 	//all the edges connected to the moved point has to be redrawn
   206 
   207         EdgeIt e;
   208         g.firstOut(e,n);
   209         for(;e!=INVALID;g.nextOut(e))
   210         {
   211             Gnome::Canvas::Points coos;
   212             double x1, x2, y1, y2;
   213 
   214             nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
   215             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   216 
   217             nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
   218             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   219 
   220             edgesmap[e]->property_points().set_value(coos);
   221 
   222 	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
   223 
   224 	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
   225 	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
   226         }
   227 
   228         g.firstIn(e,n);
   229         for(;e!=INVALID;g.nextIn(e))
   230         {
   231             Gnome::Canvas::Points coos;
   232             double x1, x2, y1, y2;
   233 
   234             nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
   235             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   236 
   237             nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
   238             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   239 
   240             edgesmap[e]->property_points().set_value(coos);
   241 
   242 	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
   243 
   244 	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
   245 	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
   246         }
   247       }
   248     default: break;
   249   }
   250   return true;
   251 }
   252 
   253 bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
   254 {
   255   Gnome::Canvas::CanvasAA::on_expose_event(event);
   256   //usleep(10000);
   257   //rezoom();
   258   return true;
   259 }
   260 
   261 void GraphDisplayerCanvas::zoomIn()
   262 {
   263   set_pixels_per_unit(
   264       (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
   265 }
   266 
   267 void GraphDisplayerCanvas::zoomOut()
   268 {
   269   set_pixels_per_unit(
   270       (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
   271 }
   272 
   273 void GraphDisplayerCanvas::zoomFit()
   274 {
   275   // get the height and width of the canvas
   276   Gtk::Allocation a = get_allocation();
   277   int aw = a.get_width();
   278   int ah = a.get_height();
   279   // add some space
   280   aw -= 5; if (aw < 0) aw = 0;
   281   ah -= 5; if (ah < 0) ah = 0;
   282 
   283   // get the bounding box of the graph
   284   double wx1, wy1, wx2, wy2;
   285   Gnome::Canvas::Item* pCanvasItem = root();
   286   pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
   287 
   288   // fit the graph to the window
   289   double ppu1 = (double) aw / fabs(wx2 - wx1);
   290   double ppu2 = (double) ah / fabs(wy2 - wy1);
   291   set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2);
   292 }
   293 
   294 void GraphDisplayerCanvas::zoom100()
   295 {
   296   set_pixels_per_unit(1.0);
   297 }
   298 
   299 void GraphDisplayerCanvas::updateScrollRegion()
   300 {
   301   double wx1, wy1, wx2, wy2;
   302   Gnome::Canvas::Item* pCanvasItem = root();
   303   pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
   304   set_scroll_region(wx1, wy1, wx2, wy2);
   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 
   317       //it has to assigned to canvas, because all the canvas has to be monitored, not only the elements of the already drawn group
   318     case CREATE_NODE:
   319       actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
   320       break;
   321 
   322     case CREATE_EDGE:
   323       actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
   324       break;
   325     default:
   326       break;
   327     }
   328 }
   329 
   330 bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e)
   331 {
   332   switch(e->type)
   333   {
   334     case GDK_BUTTON_PRESS:
   335       //we mark the location of the event to be able to calculate parameters of dragging
   336       clicked_x=e->button.x;
   337       clicked_y=e->button.y;
   338       active_item=(get_item_at(e->button.x, e->button.y));
   339       active_node=INVALID;
   340       for (NodeIt i(g); i!=INVALID; ++i)
   341 	{
   342 	  if(nodesmap[i]==active_item)
   343 	    {
   344 	      active_node=i;
   345 	    }
   346 	}
   347       isbutton=true;
   348       break;
   349     case GDK_BUTTON_RELEASE:
   350       isbutton=false;
   351       active_item=NULL;
   352       active_node=INVALID;
   353       updateScrollRegion();
   354       break;
   355     case GDK_MOTION_NOTIFY:
   356       //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
   357       if(active_node!=INVALID)
   358       {
   359 	//new coordinates will be the old values,
   360 	//because the item will be moved to the
   361 	//new coordinate therefore the new movement
   362 	//has to be calculated from here
   363 
   364         double dx=e->motion.x-clicked_x;
   365         double dy=e->motion.y-clicked_y;
   366 
   367         active_item->move(dx, dy);
   368 
   369         clicked_x=e->motion.x;
   370         clicked_y=e->motion.y;
   371 
   372 	//all the edges connected to the moved point has to be redrawn
   373         EdgeIt e;
   374 
   375         g.firstOut(e,active_node);
   376 
   377         for(;e!=INVALID;g.nextOut(e))
   378         {
   379             Gnome::Canvas::Points coos;
   380             double x1, x2, y1, y2;
   381 
   382             nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
   383             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   384 
   385             nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
   386             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   387 
   388             edgesmap[e]->property_points().set_value(coos);
   389 
   390 	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
   391 
   392 	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
   393 	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
   394         }
   395 
   396         g.firstIn(e,active_node);
   397         for(;e!=INVALID;g.nextIn(e))
   398         {
   399             Gnome::Canvas::Points coos;
   400             double x1, x2, y1, y2;
   401 
   402             nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
   403             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   404 
   405             nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
   406             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   407 
   408             edgesmap[e]->property_points().set_value(coos);
   409 
   410 	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
   411 
   412 	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
   413 	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
   414         }
   415       }
   416     default: break;
   417   }
   418 
   419   return true;
   420 }
   421 
   422 bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e)
   423 {
   424   switch(e->type)
   425     {
   426 
   427       //draw the new node in red at the clicked place
   428     case GDK_BUTTON_PRESS:
   429       isbutton=true;
   430 
   431       active_node=NodeIt(g,g.addNode());
   432 
   433       window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
   434 
   435       nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
   436       active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
   437       *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
   438       *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
   439       (nodesmap[active_node])->show();
   440       break;
   441 
   442       //move the new node
   443     case GDK_MOTION_NOTIFY:
   444       {
   445 	double world_motion_x, world_motion_y;
   446 	GdkEvent * generated=new GdkEvent();
   447 	window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y);
   448 	generated->motion.x=world_motion_x;
   449 	generated->motion.y=world_motion_y;
   450 	generated->type=GDK_MOTION_NOTIFY;
   451 	move_event_handler(generated);      
   452 	break;
   453       }
   454 
   455       //finalize the new node
   456     case GDK_BUTTON_RELEASE:
   457       isbutton=false;
   458       *active_item << Gnome::Canvas::Properties::fill_color("blue");
   459       active_item=NULL;
   460       active_node=INVALID;
   461       updateScrollRegion();
   462       break;
   463     default:
   464       break;
   465     }
   466   return false;
   467 }
   468 
   469 bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e)
   470 {
   471   switch(e->type)
   472     {
   473     case GDK_BUTTON_PRESS:
   474       //in edge creatino right button has special meaning
   475       if(e->button.button!=3)
   476 	{
   477 	  //there is not yet selected node
   478 	  if(active_node==INVALID)
   479 	    {
   480 	      //we mark the location of the event to be able to calculate parameters of dragging
   481 	      clicked_x=e->button.x;
   482 	      clicked_y=e->button.y;
   483 	      active_item=(get_item_at(e->button.x, e->button.y));
   484 	      active_node=INVALID;
   485 	      for (NodeIt i(g); i!=INVALID; ++i)
   486 		{
   487 		  if(nodesmap[i]==active_item)
   488 		    {
   489 		      active_node=i;
   490 		    }
   491 		}
   492 	      //the clicked item is really a node
   493 	      if(active_node!=INVALID)
   494 		{
   495 		  *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
   496 		  isbutton=true;
   497 		}
   498 	      //clicked item was not a node. It could be e.g. edge.
   499 	      else
   500 		{
   501 		  active_item=NULL;
   502 		}
   503 	    }
   504 	  //we only have to do sg. if the mouse button
   505 	  // is pressed already once AND the click was
   506 	  // on a node that was found in the set of 
   507 	  //nodes, and now we only search for the second 
   508 	  //node
   509 	  else
   510 	    {
   511 	      target_item=(get_item_at(e->button.x, e->button.y));
   512 	      Graph::NodeIt target_node=INVALID;
   513 	      for (NodeIt i(g); i!=INVALID; ++i)
   514 		{
   515 		  if(nodesmap[i]==target_item)
   516 		    {
   517 		      target_node=i;
   518 		    }
   519 		}
   520 	      //the clicked item is a node, the edge can be drawn
   521 	      if(target_node!=INVALID)
   522 		{
   523 		  *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
   524 
   525 		  //creating new edge
   526 		  active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
   527 	  
   528 		  //calculating coordinates of new edge
   529 		  Gnome::Canvas::Points coos;
   530 		  double x1, x2, y1, y2;
   531 	  
   532 		  active_item->get_bounds(x1, y1, x2, y2);
   533 		  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   534 
   535 		  target_item->get_bounds(x1, y1, x2, y2);
   536 		  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   537 
   538 		  //drawing new edge
   539 		  edgesmap[active_edge]=new Gnome::Canvas::Line(displayed_graph, coos);
   540 		  *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
   541 		  edgesmap[active_edge]->property_width_pixels().set_value(10);
   542 
   543 		  //redraw nodes to blank terminations of the new edge
   544 		  target_item->raise_to_top();
   545 		  active_item->raise_to_top();
   546 
   547 		  //initializing edge-text as well, to empty string
   548 		  edgesmap[active_edge]->get_bounds(x1, y1, x2, y2);
   549 		  edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
   550 		  edgetextmap[active_edge]->property_fill_color().set_value("black");
   551 		}
   552 	      //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
   553 	      else
   554 		{
   555 		  target_item=NULL;
   556 		}
   557 	    }
   558 	}
   559       break;
   560     case GDK_BUTTON_RELEASE:
   561       isbutton=false;
   562       //we clear settings in two cases
   563       //1: the edge is ready (target_item has valid value)
   564       //2: the edge creation is cancelled with right button
   565       if((target_item)||(e->button.button==3))
   566 	{
   567 	  if(active_item)
   568 	    {
   569 	      *active_item << Gnome::Canvas::Properties::fill_color("blue");
   570 	      active_item=NULL;
   571 	    }
   572 	  if(target_item)
   573 	    {
   574 	      *target_item << Gnome::Canvas::Properties::fill_color("blue");
   575 	      target_item=NULL;
   576 	    }
   577 	  active_node=INVALID;
   578 	  active_edge=INVALID;
   579 	}
   580       break;
   581     default:
   582       break;
   583     }
   584   return false;
   585 }
   586