gui/graph_displayer_canvas.cc
changeset 1440 3d2e3cfb2a6c
parent 1435 8e85e6bbefdf
child 1441 fd4b6f6d592a
equal deleted inserted replaced
0:104651a2a1d5 1:b232cac441bc
     1 #include <graph_displayer_canvas.h>
     1 #include <graph_displayer_canvas.h>
       
     2 #include <math.h>
     2 
     3 
     3 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)
     4 {
     5 {
     5 
     6 
       
     7   //first edges are drawn, to hide joining with nodes later
       
     8 
     6   for (EdgeIt i(g); i!=INVALID; ++i)
     9   for (EdgeIt i(g); i!=INVALID; ++i)
     7   {
    10   {
       
    11 
       
    12     //drawing green lines, coordinates are from cm
       
    13 
     8     Gnome::Canvas::Points coos;
    14     Gnome::Canvas::Points coos;
     9     coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
    15     coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
    10     coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
    16     coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
    11     
    17     
    12     edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
    18     edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
    13     *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
    19     *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
    14     edgesmap[i]->property_width_pixels().set_value(10);
    20     edgesmap[i]->property_width_pixels().set_value(10);    
    15     
    21     
    16     
    22     //initializing edge-text as well, to empty string
       
    23 
    17     double x1, x2, y1, y2;
    24     double x1, x2, y1, y2;
    18     edgesmap[i]->get_bounds(x1, y1, x2, y2);
    25     edgesmap[i]->get_bounds(x1, y1, x2, y2);
    19     
    26     
    20     edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
    27     edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
    21     edgetextmap[i]->property_fill_color().set_value("black");
    28     edgetextmap[i]->property_fill_color().set_value("black");
    22   }
    29   }
    23 
    30 
       
    31   //afterwards nodes come to be drawn
       
    32 
    24   NodeIt i(g);
    33   NodeIt i(g);
    25   int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
    34   int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
    26 
    35 
    27   for (; i!=INVALID; ++i)
    36   for (; i!=INVALID; ++i)
    28   {
    37   {
       
    38     //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
       
    39 
    29     if(cm[i].x>maxx)maxx=(int)cm[i].x;
    40     if(cm[i].x>maxx)maxx=(int)cm[i].x;
    30     if(cm[i].y>maxy)maxy=(int)cm[i].y;
    41     if(cm[i].y>maxy)maxy=(int)cm[i].y;
    31     if(cm[i].x<minx)minx=(int)cm[i].x;
    42     if(cm[i].x<minx)minx=(int)cm[i].x;
    32     if(cm[i].y<miny)miny=(int)cm[i].y;
    43     if(cm[i].y<miny)miny=(int)cm[i].y;
    33 
    44 
       
    45     //drawing bule nodes, with black line around them
       
    46 
    34     nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
    47     nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
    35     *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
    48     *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
    36     *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
    49     *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
    37     (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
    50     (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
    38   }
    51   }
    39 
    52 
       
    53   //setting zoom to be able to see the whole graph on the canvas
       
    54 
    40   double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
    55   double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
    41   double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
    56   double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
    42 
    57 
    43   set_pixels_per_unit((biggest_x>biggest_y)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
    58   set_pixels_per_unit((biggest_x>biggest_y)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
    44   std::cout<<abs(maxx)<<" "<<abs(minx)<<" big x "<<biggest_x<<" "<<abs(maxy)<<" "<<abs(miny)<<" big y "<<biggest_y<<std::endl;
    59   std::cout<<abs(maxx)<<" "<<abs(minx)<<" big x "<<biggest_x<<" "<<abs(maxy)<<" "<<abs(miny)<<" big y "<<biggest_y<<std::endl;
    47 
    62 
    48 }
    63 }
    49 
    64 
    50 GraphDisplayerCanvas::~GraphDisplayerCanvas()
    65 GraphDisplayerCanvas::~GraphDisplayerCanvas()
    51 {
    66 {
    52     Graph::NodeMap <int> id(g);
    67 
    53     Graph::NodeMap <double> xc(g);
    68   //writing out the end state of the graph
    54     Graph::NodeMap <double> yc(g);
    69   //\todo all the maps has to be write out!
    55 
    70 
    56     int j=1;
    71   Graph::NodeMap <int> id(g);
    57 
    72   Graph::NodeMap <double> xc(g);
    58     for (NodeIt i(g); i!=INVALID; ++i)
    73   Graph::NodeMap <double> yc(g);
    59     {
    74   
    60         double x1,y1,x2,y2;
    75   int j=1;
    61         nodesmap[i]->get_bounds(x1, y1, x2, y2);
    76   
    62 
    77   for (NodeIt i(g); i!=INVALID; ++i)
    63         id[i]=j++;
    78   {
    64         xc[i]=(x1+x2)/2;
    79     double x1,y1,x2,y2;
    65         yc[i]=(y1+y2)/2;
    80     nodesmap[i]->get_bounds(x1, y1, x2, y2);
    66     }
    81     
    67 
    82     id[i]=j++;
    68     GraphWriter<Graph> writer(std::cout,g);
    83     xc[i]=(x1+x2)/2;
    69 
    84     yc[i]=(y1+y2)/2;
    70     writer.writeNodeMap("id", id);
    85   }
    71     writer.writeNodeMap("coordinates_x", xc);
    86 
    72     writer.writeNodeMap("coordinates_y", yc);
    87   GraphWriter<Graph> writer(std::cout,g);
    73     writer.run();
    88   
       
    89   writer.writeNodeMap("id", id);
       
    90   writer.writeNodeMap("coordinates_x", xc);
       
    91   writer.writeNodeMap("coordinates_y", yc);
       
    92   writer.run();
    74 }
    93 }
    75 
    94 
    76 int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
    95 int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
    77 {
    96 {
    78   for (EdgeIt i(g); i!=INVALID; ++i)
    97   for (EdgeIt i(g); i!=INVALID; ++i)
    83   return 0;
   102   return 0;
    84 };
   103 };
    85 
   104 
    86 int GraphDisplayerCanvas::changeColor (std::string mapname)
   105 int GraphDisplayerCanvas::changeColor (std::string mapname)
    87 {  
   106 {  
       
   107 
       
   108   //function maps the range of the maximum and
       
   109   //the minimum of the nodemap to the range of
       
   110   //green in RGB
       
   111 
    88   for (EdgeIt i(g); i!=INVALID; ++i)
   112   for (EdgeIt i(g); i!=INVALID; ++i)
    89   {
   113   {
    90     double w=(*(mapstorage.edgemap_storage)[mapname])[i];
   114     double w=(*(mapstorage.edgemap_storage)[mapname])[i];
    91     double max=mapstorage.maxOfEdgeMap(mapname);
   115     double max=mapstorage.maxOfEdgeMap(mapname);
    92     double min=mapstorage.minOfEdgeMap(mapname);
   116     double min=mapstorage.minOfEdgeMap(mapname);
   107   return 0;
   131   return 0;
   108 };
   132 };
   109 
   133 
   110 int GraphDisplayerCanvas::changeText (std::string mapname)
   134 int GraphDisplayerCanvas::changeText (std::string mapname)
   111 {
   135 {
       
   136 
       
   137   //the number in the map will be written on the edge
       
   138   //EXCEPT when the name of the map is Text, because
       
   139   //in that case empty string will be written, because
       
   140   //that is the deleter map
       
   141   //\todo isn't it a bit woodcutter?
       
   142 
   112   for (EdgeIt i(g); i!=INVALID; ++i)
   143   for (EdgeIt i(g); i!=INVALID; ++i)
   113   {
   144   {
   114     if(mapname!="Text")
   145     if(mapname!="Text")
   115     {
   146     {
   116       double number=(*(mapstorage.edgemap_storage)[mapname])[i];
   147       double number=(*(mapstorage.edgemap_storage)[mapname])[i];
   143 };
   174 };
   144 
   175 
   145 
   176 
   146 int GraphDisplayerCanvas::rezoom ()
   177 int GraphDisplayerCanvas::rezoom ()
   147 {
   178 {
       
   179 
       
   180   //searches for the minimum and the maximum
       
   181   //value of the coordinates of the nodes to
       
   182   //set the pixel rpo unit to a value to be 
       
   183   //able to see the whole graph in the canvas
       
   184   //\todo does not work properly
       
   185 
   148   double x1, x2, y1, y2;
   186   double x1, x2, y1, y2;
   149   int x,y;
   187   int x,y;
   150 
   188 
   151   NodeIt i(g);
   189   NodeIt i(g);
   152   nodesmap[i]->get_bounds(x1, y1, x2, y2);
   190   nodesmap[i]->get_bounds(x1, y1, x2, y2);
   175   set_pixels_per_unit((biggest_x-WIN_WIDTH>biggest_y-WIN_HEIGHT)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
   213   set_pixels_per_unit((biggest_x-WIN_WIDTH>biggest_y-WIN_HEIGHT)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
   176   return 0;
   214   return 0;
   177 };
   215 };
   178 
   216 
   179 
   217 
   180 ///This function moves only one node of displayed_graph,
       
   181 ///but recalculate the location of weight point,
       
   182 ///and also redraw the sides of the planefigure.
       
   183 bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
   218 bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
   184 {
   219 {
   185   switch(e->type)
   220   switch(e->type)
   186   {
   221   {
   187     case GDK_BUTTON_PRESS:
   222     case GDK_BUTTON_PRESS:
       
   223       //we mark the location of the event to be able to calculate parameters of dragging
   188       clicked_x=e->button.x;
   224       clicked_x=e->button.x;
   189       clicked_y=e->button.y;
   225       clicked_y=e->button.y;
   190       active_item=(get_item_at(e->button.x, e->button.y));
   226       active_item=(get_item_at(e->button.x, e->button.y));
   191       isbutton=true;
   227       isbutton=true;
   192       break;
   228       break;
   193     case GDK_BUTTON_RELEASE:
   229     case GDK_BUTTON_RELEASE:
   194       isbutton=false;
   230       isbutton=false;
   195       active_item=NULL;
   231       active_item=NULL;
   196       break;
   232       break;
   197     case GDK_MOTION_NOTIFY:
   233     case GDK_MOTION_NOTIFY:
       
   234       //we only have to do sg. if the mouse button is pressed
   198       if(isbutton)
   235       if(isbutton)
   199       {
   236       {
       
   237 	//new coordinates will be the old values,
       
   238 	//because the item will be moved to the
       
   239 	//new coordinate therefore the new movement
       
   240 	//has to be calculated from here
       
   241 
   200         double dx=e->motion.x-clicked_x;
   242         double dx=e->motion.x-clicked_x;
   201         double dy=e->motion.y-clicked_y;
   243         double dy=e->motion.y-clicked_y;
   202         active_item->move(dx, dy);
   244         active_item->move(dx, dy);
   203         clicked_x=e->motion.x;
   245         clicked_x=e->motion.x;
   204         clicked_y=e->motion.y;
   246         clicked_y=e->motion.y;
   205 
   247 
       
   248 	//all the edges connected to the moved point has to be redrawn
       
   249 
   206         EdgeIt e;
   250         EdgeIt e;
   207 
       
   208         g.firstOut(e,n);
   251         g.firstOut(e,n);
   209         for(;e!=INVALID;g.nextOut(e))
   252         for(;e!=INVALID;g.nextOut(e))
   210         {
   253         {
   211             Gnome::Canvas::Points coos;
   254             Gnome::Canvas::Points coos;
   212             double x1, x2, y1, y2;
   255             double x1, x2, y1, y2;