gui/graph_displayer_canvas.cc
author hegyi
Fri, 10 Jun 2005 11:58:03 +0000
changeset 1468 d0ccb2fdeeff
parent 1444 5fcd69785959
child 1474 75c6d2eb187a
permissions -rwxr-xr-x
Hopefully, node creation works well, after a small structural consideration.
     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)
     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 
    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     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