gui/graph_displayer_canvas.cc
author ladanyi
Mon, 23 May 2005 04:48:14 +0000
changeset 1435 8e85e6bbefdf
parent 1412 src/gui/graph_displayer_canvas.cc@c7fab5a1174a
child 1440 3d2e3cfb2a6c
permissions -rwxr-xr-x
trunk/src/* move to trunk/
     1 #include <graph_displayer_canvas.h>
     2 
     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 {
     5 
     6   for (EdgeIt i(g); i!=INVALID; ++i)
     7   {
     8     Gnome::Canvas::Points coos;
     9     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));
    11     
    12     edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
    13     *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
    14     edgesmap[i]->property_width_pixels().set_value(10);
    15     
    16     
    17     double x1, x2, y1, y2;
    18     edgesmap[i]->get_bounds(x1, y1, x2, y2);
    19     
    20     edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
    21     edgetextmap[i]->property_fill_color().set_value("black");
    22   }
    23 
    24   NodeIt i(g);
    25   int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
    26 
    27   for (; i!=INVALID; ++i)
    28   {
    29     if(cm[i].x>maxx)maxx=(int)cm[i].x;
    30     if(cm[i].y>maxy)maxy=(int)cm[i].y;
    31     if(cm[i].x<minx)minx=(int)cm[i].x;
    32     if(cm[i].y<miny)miny=(int)cm[i].y;
    33 
    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);
    35     *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
    36     *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
    37     (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
    38   }
    39 
    40   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);
    42 
    43   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;
    45   std::cout<<maxx<<" "<<minx<<" big x "<<biggest_x<<" "<<maxy<<" "<<miny<<" big y "<<biggest_y<<std::endl;
    46   std::cout<<"dx "<<(maxx-minx)<<" dy "<<(maxy-miny)<<" xrate "<<((maxx-minx)/WIN_WIDTH)<<" yrate "<<((maxy-miny)/WIN_HEIGHT)<<std::endl;
    47 
    48 }
    49 
    50 GraphDisplayerCanvas::~GraphDisplayerCanvas()
    51 {
    52     Graph::NodeMap <int> id(g);
    53     Graph::NodeMap <double> xc(g);
    54     Graph::NodeMap <double> yc(g);
    55 
    56     int j=1;
    57 
    58     for (NodeIt i(g); i!=INVALID; ++i)
    59     {
    60         double x1,y1,x2,y2;
    61         nodesmap[i]->get_bounds(x1, y1, x2, y2);
    62 
    63         id[i]=j++;
    64         xc[i]=(x1+x2)/2;
    65         yc[i]=(y1+y2)/2;
    66     }
    67 
    68     GraphWriter<Graph> writer(std::cout,g);
    69 
    70     writer.writeNodeMap("id", id);
    71     writer.writeNodeMap("coordinates_x", xc);
    72     writer.writeNodeMap("coordinates_y", yc);
    73     writer.run();
    74 }
    75 
    76 int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
    77 {
    78   for (EdgeIt i(g); i!=INVALID; ++i)
    79   {
    80     int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
    81     edgesmap[i]->property_width_pixels().set_value(w);
    82   }
    83   return 0;
    84 };
    85 
    86 int GraphDisplayerCanvas::changeColor (std::string mapname)
    87 {  
    88   for (EdgeIt i(g); i!=INVALID; ++i)
    89   {
    90     double w=(*(mapstorage.edgemap_storage)[mapname])[i];
    91     double max=mapstorage.maxOfEdgeMap(mapname);
    92     double min=mapstorage.minOfEdgeMap(mapname);
    93       
    94     //std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
    95     Gdk::Color color;
    96     if(max!=min)
    97     {
    98       color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
    99     }
   100     else
   101     {
   102       color.set_rgb_p (0, 100, 0);
   103     }
   104 
   105     edgesmap[i]->property_fill_color_gdk().set_value(color);
   106   }
   107   return 0;
   108 };
   109 
   110 int GraphDisplayerCanvas::changeText (std::string mapname)
   111 {
   112   for (EdgeIt i(g); i!=INVALID; ++i)
   113   {
   114     if(mapname!="Text")
   115     {
   116       double number=(*(mapstorage.edgemap_storage)[mapname])[i];
   117       int length=(int)(floor(log(number)/log(10)))+1;
   118       int maxpos=(int)(pow(10,length-1));
   119       int strl=length+1+RANGE;
   120       char * str=new char[strl];
   121       str[length]='.';
   122       str[strl]='\0';
   123       
   124       for(int j=0;j<strl;j++)
   125       {
   126 	if(j!=length)
   127         {
   128 	  int digit=(int)(number/maxpos);
   129 	  str[j]=(digit+'0');
   130 	  number-=digit*maxpos;
   131 	  number*=10;
   132         }
   133       }
   134       
   135       edgetextmap[i]->property_text().set_value(str);
   136     }
   137     else
   138     {
   139       edgetextmap[i]->property_text().set_value("");
   140     }
   141   }
   142   return 0;
   143 };
   144 
   145 
   146 int GraphDisplayerCanvas::rezoom ()
   147 {
   148   double x1, x2, y1, y2;
   149   int x,y;
   150 
   151   NodeIt i(g);
   152   nodesmap[i]->get_bounds(x1, y1, x2, y2);
   153 
   154   x=(int)((x1+x2)/2);
   155   y=(int)((y1+y2)/2);
   156   
   157   int maxx=0, maxy=0, minx=(int)x, miny=(int)y;
   158 
   159   for (; i!=INVALID; ++i)
   160   {
   161     nodesmap[i]->get_bounds(x1, y1, x2, y2);
   162 
   163     x=(int)((x1+x2)/2);
   164     y=(int)((y1+y2)/2);
   165 
   166     if(x>maxx)maxx=x;
   167     if(y>maxy)maxy=y;
   168     if(x<minx)minx=x;
   169     if(y<miny)miny=y;
   170   }
   171 
   172   double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
   173   double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
   174 
   175   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;
   177 };
   178 
   179 
   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)
   184 {
   185   switch(e->type)
   186   {
   187     case GDK_BUTTON_PRESS:
   188       clicked_x=e->button.x;
   189       clicked_y=e->button.y;
   190       active_item=(get_item_at(e->button.x, e->button.y));
   191       isbutton=true;
   192       break;
   193     case GDK_BUTTON_RELEASE:
   194       isbutton=false;
   195       active_item=NULL;
   196       break;
   197     case GDK_MOTION_NOTIFY:
   198       if(isbutton)
   199       {
   200         double dx=e->motion.x-clicked_x;
   201         double dy=e->motion.y-clicked_y;
   202         active_item->move(dx, dy);
   203         clicked_x=e->motion.x;
   204         clicked_y=e->motion.y;
   205 
   206         EdgeIt e;
   207 
   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 }