gui/graph_displayer_canvas.cc
changeset 1435 8e85e6bbefdf
parent 1412 c7fab5a1174a
child 1440 3d2e3cfb2a6c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gui/graph_displayer_canvas.cc	Mon May 23 04:48:14 2005 +0000
     1.3 @@ -0,0 +1,259 @@
     1.4 +#include <graph_displayer_canvas.h>
     1.5 +
     1.6 +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)
     1.7 +{
     1.8 +
     1.9 +  for (EdgeIt i(g); i!=INVALID; ++i)
    1.10 +  {
    1.11 +    Gnome::Canvas::Points coos;
    1.12 +    coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
    1.13 +    coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
    1.14 +    
    1.15 +    edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
    1.16 +    *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
    1.17 +    edgesmap[i]->property_width_pixels().set_value(10);
    1.18 +    
    1.19 +    
    1.20 +    double x1, x2, y1, y2;
    1.21 +    edgesmap[i]->get_bounds(x1, y1, x2, y2);
    1.22 +    
    1.23 +    edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
    1.24 +    edgetextmap[i]->property_fill_color().set_value("black");
    1.25 +  }
    1.26 +
    1.27 +  NodeIt i(g);
    1.28 +  int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
    1.29 +
    1.30 +  for (; i!=INVALID; ++i)
    1.31 +  {
    1.32 +    if(cm[i].x>maxx)maxx=(int)cm[i].x;
    1.33 +    if(cm[i].y>maxy)maxy=(int)cm[i].y;
    1.34 +    if(cm[i].x<minx)minx=(int)cm[i].x;
    1.35 +    if(cm[i].y<miny)miny=(int)cm[i].y;
    1.36 +
    1.37 +    nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
    1.38 +    *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
    1.39 +    *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
    1.40 +    (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
    1.41 +  }
    1.42 +
    1.43 +  double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
    1.44 +  double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
    1.45 +
    1.46 +  set_pixels_per_unit((biggest_x>biggest_y)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
    1.47 +  std::cout<<abs(maxx)<<" "<<abs(minx)<<" big x "<<biggest_x<<" "<<abs(maxy)<<" "<<abs(miny)<<" big y "<<biggest_y<<std::endl;
    1.48 +  std::cout<<maxx<<" "<<minx<<" big x "<<biggest_x<<" "<<maxy<<" "<<miny<<" big y "<<biggest_y<<std::endl;
    1.49 +  std::cout<<"dx "<<(maxx-minx)<<" dy "<<(maxy-miny)<<" xrate "<<((maxx-minx)/WIN_WIDTH)<<" yrate "<<((maxy-miny)/WIN_HEIGHT)<<std::endl;
    1.50 +
    1.51 +}
    1.52 +
    1.53 +GraphDisplayerCanvas::~GraphDisplayerCanvas()
    1.54 +{
    1.55 +    Graph::NodeMap <int> id(g);
    1.56 +    Graph::NodeMap <double> xc(g);
    1.57 +    Graph::NodeMap <double> yc(g);
    1.58 +
    1.59 +    int j=1;
    1.60 +
    1.61 +    for (NodeIt i(g); i!=INVALID; ++i)
    1.62 +    {
    1.63 +        double x1,y1,x2,y2;
    1.64 +        nodesmap[i]->get_bounds(x1, y1, x2, y2);
    1.65 +
    1.66 +        id[i]=j++;
    1.67 +        xc[i]=(x1+x2)/2;
    1.68 +        yc[i]=(y1+y2)/2;
    1.69 +    }
    1.70 +
    1.71 +    GraphWriter<Graph> writer(std::cout,g);
    1.72 +
    1.73 +    writer.writeNodeMap("id", id);
    1.74 +    writer.writeNodeMap("coordinates_x", xc);
    1.75 +    writer.writeNodeMap("coordinates_y", yc);
    1.76 +    writer.run();
    1.77 +}
    1.78 +
    1.79 +int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
    1.80 +{
    1.81 +  for (EdgeIt i(g); i!=INVALID; ++i)
    1.82 +  {
    1.83 +    int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
    1.84 +    edgesmap[i]->property_width_pixels().set_value(w);
    1.85 +  }
    1.86 +  return 0;
    1.87 +};
    1.88 +
    1.89 +int GraphDisplayerCanvas::changeColor (std::string mapname)
    1.90 +{  
    1.91 +  for (EdgeIt i(g); i!=INVALID; ++i)
    1.92 +  {
    1.93 +    double w=(*(mapstorage.edgemap_storage)[mapname])[i];
    1.94 +    double max=mapstorage.maxOfEdgeMap(mapname);
    1.95 +    double min=mapstorage.minOfEdgeMap(mapname);
    1.96 +      
    1.97 +    //std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
    1.98 +    Gdk::Color color;
    1.99 +    if(max!=min)
   1.100 +    {
   1.101 +      color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
   1.102 +    }
   1.103 +    else
   1.104 +    {
   1.105 +      color.set_rgb_p (0, 100, 0);
   1.106 +    }
   1.107 +
   1.108 +    edgesmap[i]->property_fill_color_gdk().set_value(color);
   1.109 +  }
   1.110 +  return 0;
   1.111 +};
   1.112 +
   1.113 +int GraphDisplayerCanvas::changeText (std::string mapname)
   1.114 +{
   1.115 +  for (EdgeIt i(g); i!=INVALID; ++i)
   1.116 +  {
   1.117 +    if(mapname!="Text")
   1.118 +    {
   1.119 +      double number=(*(mapstorage.edgemap_storage)[mapname])[i];
   1.120 +      int length=(int)(floor(log(number)/log(10)))+1;
   1.121 +      int maxpos=(int)(pow(10,length-1));
   1.122 +      int strl=length+1+RANGE;
   1.123 +      char * str=new char[strl];
   1.124 +      str[length]='.';
   1.125 +      str[strl]='\0';
   1.126 +      
   1.127 +      for(int j=0;j<strl;j++)
   1.128 +      {
   1.129 +	if(j!=length)
   1.130 +        {
   1.131 +	  int digit=(int)(number/maxpos);
   1.132 +	  str[j]=(digit+'0');
   1.133 +	  number-=digit*maxpos;
   1.134 +	  number*=10;
   1.135 +        }
   1.136 +      }
   1.137 +      
   1.138 +      edgetextmap[i]->property_text().set_value(str);
   1.139 +    }
   1.140 +    else
   1.141 +    {
   1.142 +      edgetextmap[i]->property_text().set_value("");
   1.143 +    }
   1.144 +  }
   1.145 +  return 0;
   1.146 +};
   1.147 +
   1.148 +
   1.149 +int GraphDisplayerCanvas::rezoom ()
   1.150 +{
   1.151 +  double x1, x2, y1, y2;
   1.152 +  int x,y;
   1.153 +
   1.154 +  NodeIt i(g);
   1.155 +  nodesmap[i]->get_bounds(x1, y1, x2, y2);
   1.156 +
   1.157 +  x=(int)((x1+x2)/2);
   1.158 +  y=(int)((y1+y2)/2);
   1.159 +  
   1.160 +  int maxx=0, maxy=0, minx=(int)x, miny=(int)y;
   1.161 +
   1.162 +  for (; i!=INVALID; ++i)
   1.163 +  {
   1.164 +    nodesmap[i]->get_bounds(x1, y1, x2, y2);
   1.165 +
   1.166 +    x=(int)((x1+x2)/2);
   1.167 +    y=(int)((y1+y2)/2);
   1.168 +
   1.169 +    if(x>maxx)maxx=x;
   1.170 +    if(y>maxy)maxy=y;
   1.171 +    if(x<minx)minx=x;
   1.172 +    if(y<miny)miny=y;
   1.173 +  }
   1.174 +
   1.175 +  double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
   1.176 +  double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
   1.177 +
   1.178 +  set_pixels_per_unit((biggest_x-WIN_WIDTH>biggest_y-WIN_HEIGHT)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
   1.179 +  return 0;
   1.180 +};
   1.181 +
   1.182 +
   1.183 +///This function moves only one node of displayed_graph,
   1.184 +///but recalculate the location of weight point,
   1.185 +///and also redraw the sides of the planefigure.
   1.186 +bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
   1.187 +{
   1.188 +  switch(e->type)
   1.189 +  {
   1.190 +    case GDK_BUTTON_PRESS:
   1.191 +      clicked_x=e->button.x;
   1.192 +      clicked_y=e->button.y;
   1.193 +      active_item=(get_item_at(e->button.x, e->button.y));
   1.194 +      isbutton=true;
   1.195 +      break;
   1.196 +    case GDK_BUTTON_RELEASE:
   1.197 +      isbutton=false;
   1.198 +      active_item=NULL;
   1.199 +      break;
   1.200 +    case GDK_MOTION_NOTIFY:
   1.201 +      if(isbutton)
   1.202 +      {
   1.203 +        double dx=e->motion.x-clicked_x;
   1.204 +        double dy=e->motion.y-clicked_y;
   1.205 +        active_item->move(dx, dy);
   1.206 +        clicked_x=e->motion.x;
   1.207 +        clicked_y=e->motion.y;
   1.208 +
   1.209 +        EdgeIt e;
   1.210 +
   1.211 +        g.firstOut(e,n);
   1.212 +        for(;e!=INVALID;g.nextOut(e))
   1.213 +        {
   1.214 +            Gnome::Canvas::Points coos;
   1.215 +            double x1, x2, y1, y2;
   1.216 +
   1.217 +            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
   1.218 +            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   1.219 +
   1.220 +            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
   1.221 +            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   1.222 +
   1.223 +            edgesmap[e]->property_points().set_value(coos);
   1.224 +
   1.225 +	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
   1.226 +
   1.227 +	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
   1.228 +	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
   1.229 +        }
   1.230 +
   1.231 +        g.firstIn(e,n);
   1.232 +        for(;e!=INVALID;g.nextIn(e))
   1.233 +        {
   1.234 +            Gnome::Canvas::Points coos;
   1.235 +            double x1, x2, y1, y2;
   1.236 +
   1.237 +            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
   1.238 +            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   1.239 +
   1.240 +            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
   1.241 +            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   1.242 +
   1.243 +            edgesmap[e]->property_points().set_value(coos);
   1.244 +
   1.245 +	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
   1.246 +
   1.247 +	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
   1.248 +	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
   1.249 +        }
   1.250 +      }
   1.251 +    default: break;
   1.252 +  }
   1.253 +  return true;
   1.254 +}
   1.255 +
   1.256 +bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
   1.257 +{
   1.258 +  Gnome::Canvas::CanvasAA::on_expose_event(event);
   1.259 +  //usleep(10000);
   1.260 +  //rezoom();
   1.261 +  return true;
   1.262 +}