ladanyi@1: #include ladanyi@1: ladanyi@1: 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) ladanyi@1: { ladanyi@1: ladanyi@1: for (EdgeIt i(g); i!=INVALID; ++i) ladanyi@1: { ladanyi@1: Gnome::Canvas::Points coos; ladanyi@1: coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y)); ladanyi@1: coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y)); ladanyi@1: ladanyi@1: edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos); ladanyi@1: *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green"); ladanyi@1: edgesmap[i]->property_width_pixels().set_value(10); ladanyi@1: ladanyi@1: ladanyi@1: double x1, x2, y1, y2; ladanyi@1: edgesmap[i]->get_bounds(x1, y1, x2, y2); ladanyi@1: ladanyi@1: edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, ""); ladanyi@1: edgetextmap[i]->property_fill_color().set_value("black"); ladanyi@1: } ladanyi@1: ladanyi@1: NodeIt i(g); ladanyi@1: int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y; ladanyi@1: ladanyi@1: for (; i!=INVALID; ++i) ladanyi@1: { ladanyi@1: if(cm[i].x>maxx)maxx=(int)cm[i].x; ladanyi@1: if(cm[i].y>maxy)maxy=(int)cm[i].y; ladanyi@1: if(cm[i].xsignal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i)); ladanyi@1: } ladanyi@1: ladanyi@1: double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80); ladanyi@1: double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80); ladanyi@1: ladanyi@1: set_pixels_per_unit((biggest_x>biggest_y)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2)); ladanyi@1: std::cout< id(g); ladanyi@1: Graph::NodeMap xc(g); ladanyi@1: Graph::NodeMap yc(g); ladanyi@1: ladanyi@1: int j=1; ladanyi@1: ladanyi@1: for (NodeIt i(g); i!=INVALID; ++i) ladanyi@1: { ladanyi@1: double x1,y1,x2,y2; ladanyi@1: nodesmap[i]->get_bounds(x1, y1, x2, y2); ladanyi@1: ladanyi@1: id[i]=j++; ladanyi@1: xc[i]=(x1+x2)/2; ladanyi@1: yc[i]=(y1+y2)/2; ladanyi@1: } ladanyi@1: ladanyi@1: GraphWriter writer(std::cout,g); ladanyi@1: ladanyi@1: writer.writeNodeMap("id", id); ladanyi@1: writer.writeNodeMap("coordinates_x", xc); ladanyi@1: writer.writeNodeMap("coordinates_y", yc); ladanyi@1: writer.run(); ladanyi@1: } ladanyi@1: ladanyi@1: int GraphDisplayerCanvas::changeLineWidth (std::string mapname) ladanyi@1: { ladanyi@1: for (EdgeIt i(g); i!=INVALID; ++i) ladanyi@1: { ladanyi@1: int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i]; ladanyi@1: edgesmap[i]->property_width_pixels().set_value(w); ladanyi@1: } ladanyi@1: return 0; ladanyi@1: }; ladanyi@1: ladanyi@1: int GraphDisplayerCanvas::changeColor (std::string mapname) ladanyi@1: { ladanyi@1: for (EdgeIt i(g); i!=INVALID; ++i) ladanyi@1: { ladanyi@1: double w=(*(mapstorage.edgemap_storage)[mapname])[i]; ladanyi@1: double max=mapstorage.maxOfEdgeMap(mapname); ladanyi@1: double min=mapstorage.minOfEdgeMap(mapname); ladanyi@1: ladanyi@1: //std::cout<property_fill_color_gdk().set_value(color); ladanyi@1: } ladanyi@1: return 0; ladanyi@1: }; ladanyi@1: ladanyi@1: int GraphDisplayerCanvas::changeText (std::string mapname) ladanyi@1: { ladanyi@1: for (EdgeIt i(g); i!=INVALID; ++i) ladanyi@1: { ladanyi@1: if(mapname!="Text") ladanyi@1: { ladanyi@1: double number=(*(mapstorage.edgemap_storage)[mapname])[i]; ladanyi@1: int length=(int)(floor(log(number)/log(10)))+1; ladanyi@1: int maxpos=(int)(pow(10,length-1)); ladanyi@1: int strl=length+1+RANGE; ladanyi@1: char * str=new char[strl]; ladanyi@1: str[length]='.'; ladanyi@1: str[strl]='\0'; ladanyi@1: ladanyi@1: for(int j=0;jproperty_text().set_value(str); ladanyi@1: } ladanyi@1: else ladanyi@1: { ladanyi@1: edgetextmap[i]->property_text().set_value(""); ladanyi@1: } ladanyi@1: } ladanyi@1: return 0; ladanyi@1: }; ladanyi@1: ladanyi@1: ladanyi@1: int GraphDisplayerCanvas::rezoom () ladanyi@1: { ladanyi@1: double x1, x2, y1, y2; ladanyi@1: int x,y; ladanyi@1: ladanyi@1: NodeIt i(g); ladanyi@1: nodesmap[i]->get_bounds(x1, y1, x2, y2); ladanyi@1: ladanyi@1: x=(int)((x1+x2)/2); ladanyi@1: y=(int)((y1+y2)/2); ladanyi@1: ladanyi@1: int maxx=0, maxy=0, minx=(int)x, miny=(int)y; ladanyi@1: ladanyi@1: for (; i!=INVALID; ++i) ladanyi@1: { ladanyi@1: nodesmap[i]->get_bounds(x1, y1, x2, y2); ladanyi@1: ladanyi@1: x=(int)((x1+x2)/2); ladanyi@1: y=(int)((y1+y2)/2); ladanyi@1: ladanyi@1: if(x>maxx)maxx=x; ladanyi@1: if(y>maxy)maxy=y; ladanyi@1: if(xabs(minx))?(abs(maxx)+80):(abs(minx)+80); ladanyi@1: double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80); ladanyi@1: ladanyi@1: set_pixels_per_unit((biggest_x-WIN_WIDTH>biggest_y-WIN_HEIGHT)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2)); ladanyi@1: return 0; ladanyi@1: }; ladanyi@1: ladanyi@1: ladanyi@1: ///This function moves only one node of displayed_graph, ladanyi@1: ///but recalculate the location of weight point, ladanyi@1: ///and also redraw the sides of the planefigure. ladanyi@1: bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n) ladanyi@1: { ladanyi@1: switch(e->type) ladanyi@1: { ladanyi@1: case GDK_BUTTON_PRESS: ladanyi@1: clicked_x=e->button.x; ladanyi@1: clicked_y=e->button.y; ladanyi@1: active_item=(get_item_at(e->button.x, e->button.y)); ladanyi@1: isbutton=true; ladanyi@1: break; ladanyi@1: case GDK_BUTTON_RELEASE: ladanyi@1: isbutton=false; ladanyi@1: active_item=NULL; ladanyi@1: break; ladanyi@1: case GDK_MOTION_NOTIFY: ladanyi@1: if(isbutton) ladanyi@1: { ladanyi@1: double dx=e->motion.x-clicked_x; ladanyi@1: double dy=e->motion.y-clicked_y; ladanyi@1: active_item->move(dx, dy); ladanyi@1: clicked_x=e->motion.x; ladanyi@1: clicked_y=e->motion.y; ladanyi@1: ladanyi@1: EdgeIt e; ladanyi@1: ladanyi@1: g.firstOut(e,n); ladanyi@1: for(;e!=INVALID;g.nextOut(e)) ladanyi@1: { ladanyi@1: Gnome::Canvas::Points coos; ladanyi@1: double x1, x2, y1, y2; ladanyi@1: ladanyi@1: nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); ladanyi@1: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); ladanyi@1: ladanyi@1: nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); ladanyi@1: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); ladanyi@1: ladanyi@1: edgesmap[e]->property_points().set_value(coos); ladanyi@1: ladanyi@1: edgesmap[e]->get_bounds(x1, y1, x2, y2); ladanyi@1: ladanyi@1: edgetextmap[e]->property_x().set_value((x1+x2)/2); ladanyi@1: edgetextmap[e]->property_y().set_value((y1+y2)/2); ladanyi@1: } ladanyi@1: ladanyi@1: g.firstIn(e,n); ladanyi@1: for(;e!=INVALID;g.nextIn(e)) ladanyi@1: { ladanyi@1: Gnome::Canvas::Points coos; ladanyi@1: double x1, x2, y1, y2; ladanyi@1: ladanyi@1: nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); ladanyi@1: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); ladanyi@1: ladanyi@1: nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); ladanyi@1: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); ladanyi@1: ladanyi@1: edgesmap[e]->property_points().set_value(coos); ladanyi@1: ladanyi@1: edgesmap[e]->get_bounds(x1, y1, x2, y2); ladanyi@1: ladanyi@1: edgetextmap[e]->property_x().set_value((x1+x2)/2); ladanyi@1: edgetextmap[e]->property_y().set_value((y1+y2)/2); ladanyi@1: } ladanyi@1: } ladanyi@1: default: break; ladanyi@1: } ladanyi@1: return true; ladanyi@1: } ladanyi@1: ladanyi@1: bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event) ladanyi@1: { ladanyi@1: Gnome::Canvas::CanvasAA::on_expose_event(event); ladanyi@1: //usleep(10000); ladanyi@1: //rezoom(); ladanyi@1: return true; ladanyi@1: }