ladanyi@1442: #include ladanyi@1442: #include ladanyi@1442: ladanyi@1442: 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@1442: { ladanyi@1442: //set_center_scroll_region(true); ladanyi@1442: ladanyi@1442: //first edges are drawn, to hide joining with nodes later ladanyi@1442: ladanyi@1442: for (EdgeIt i(g); i!=INVALID; ++i) ladanyi@1442: { ladanyi@1442: ladanyi@1442: //drawing green lines, coordinates are from cm ladanyi@1442: ladanyi@1442: Gnome::Canvas::Points coos; ladanyi@1442: coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y)); ladanyi@1442: coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y)); ladanyi@1442: ladanyi@1442: edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos); ladanyi@1442: *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green"); ladanyi@1442: edgesmap[i]->property_width_pixels().set_value(10); ladanyi@1442: ladanyi@1442: //initializing edge-text as well, to empty string ladanyi@1442: ladanyi@1442: double x1, x2, y1, y2; ladanyi@1442: edgesmap[i]->get_bounds(x1, y1, x2, y2); ladanyi@1442: ladanyi@1442: edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, ""); ladanyi@1442: edgetextmap[i]->property_fill_color().set_value("black"); ladanyi@1442: } ladanyi@1442: ladanyi@1442: //afterwards nodes come to be drawn ladanyi@1442: ladanyi@1442: NodeIt i(g); ladanyi@1442: int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y; ladanyi@1442: ladanyi@1442: for (; i!=INVALID; ++i) ladanyi@1442: { ladanyi@1442: //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen) ladanyi@1442: ladanyi@1442: if(cm[i].x>maxx)maxx=(int)cm[i].x; ladanyi@1442: if(cm[i].y>maxy)maxy=(int)cm[i].y; ladanyi@1442: if(cm[i].xsignal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i)); ladanyi@1442: } ladanyi@1442: ladanyi@1442: /* ladanyi@1442: //setting zoom to be able to see the whole graph on the canvas ladanyi@1442: ladanyi@1442: double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80); ladanyi@1442: double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80); ladanyi@1442: ladanyi@1442: set_pixels_per_unit((biggest_x>biggest_y)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2)); ladanyi@1442: std::cout< id(g); ladanyi@1442: Graph::NodeMap xc(g); ladanyi@1442: Graph::NodeMap yc(g); ladanyi@1442: ladanyi@1442: int j=1; ladanyi@1442: ladanyi@1442: for (NodeIt i(g); i!=INVALID; ++i) ladanyi@1442: { ladanyi@1442: double x1,y1,x2,y2; ladanyi@1442: nodesmap[i]->get_bounds(x1, y1, x2, y2); ladanyi@1442: ladanyi@1442: id[i]=j++; ladanyi@1442: xc[i]=(x1+x2)/2; ladanyi@1442: yc[i]=(y1+y2)/2; ladanyi@1442: } ladanyi@1442: ladanyi@1442: GraphWriter writer(std::cout,g); ladanyi@1442: ladanyi@1442: writer.writeNodeMap("id", id); ladanyi@1442: writer.writeNodeMap("coordinates_x", xc); ladanyi@1442: writer.writeNodeMap("coordinates_y", yc); ladanyi@1442: writer.run(); ladanyi@1442: } ladanyi@1442: ladanyi@1442: int GraphDisplayerCanvas::changeLineWidth (std::string mapname) ladanyi@1442: { ladanyi@1442: for (EdgeIt i(g); i!=INVALID; ++i) ladanyi@1442: { ladanyi@1442: int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i]; ladanyi@1442: edgesmap[i]->property_width_pixels().set_value(w); ladanyi@1442: } ladanyi@1442: return 0; ladanyi@1442: }; ladanyi@1442: ladanyi@1442: int GraphDisplayerCanvas::changeColor (std::string mapname) ladanyi@1442: { ladanyi@1442: ladanyi@1442: //function maps the range of the maximum and ladanyi@1442: //the minimum of the nodemap to the range of ladanyi@1442: //green in RGB ladanyi@1442: ladanyi@1442: for (EdgeIt i(g); i!=INVALID; ++i) ladanyi@1442: { ladanyi@1442: double w=(*(mapstorage.edgemap_storage)[mapname])[i]; ladanyi@1442: double max=mapstorage.maxOfEdgeMap(mapname); ladanyi@1442: double min=mapstorage.minOfEdgeMap(mapname); ladanyi@1442: ladanyi@1442: //std::cout<property_fill_color_gdk().set_value(color); ladanyi@1442: } ladanyi@1442: return 0; ladanyi@1442: }; ladanyi@1442: ladanyi@1442: int GraphDisplayerCanvas::changeText (std::string mapname) ladanyi@1442: { ladanyi@1442: ladanyi@1442: //the number in the map will be written on the edge ladanyi@1442: //EXCEPT when the name of the map is Text, because ladanyi@1442: //in that case empty string will be written, because ladanyi@1442: //that is the deleter map ladanyi@1442: //\todo isn't it a bit woodcutter? ladanyi@1442: ladanyi@1442: for (EdgeIt i(g); i!=INVALID; ++i) ladanyi@1442: { ladanyi@1442: if(mapname!="Text") ladanyi@1442: { ladanyi@1442: double number=(*(mapstorage.edgemap_storage)[mapname])[i]; ladanyi@1442: int length=(int)(floor(log(number)/log(10)))+1; ladanyi@1442: int maxpos=(int)(pow(10,length-1)); ladanyi@1442: int strl=length+1+RANGE; ladanyi@1442: char * str=new char[strl]; ladanyi@1442: str[length]='.'; ladanyi@1442: str[strl]='\0'; ladanyi@1442: ladanyi@1442: for(int j=0;jproperty_text().set_value(str); ladanyi@1442: } ladanyi@1442: else ladanyi@1442: { ladanyi@1442: edgetextmap[i]->property_text().set_value(""); ladanyi@1442: } ladanyi@1442: } ladanyi@1442: return 0; ladanyi@1442: }; ladanyi@1442: ladanyi@1442: bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n) ladanyi@1442: { ladanyi@1442: switch(e->type) ladanyi@1442: { ladanyi@1442: case GDK_BUTTON_PRESS: ladanyi@1442: //we mark the location of the event to be able to calculate parameters of dragging ladanyi@1442: clicked_x=e->button.x; ladanyi@1442: clicked_y=e->button.y; ladanyi@1442: active_item=(get_item_at(e->button.x, e->button.y)); ladanyi@1442: isbutton=true; ladanyi@1442: break; ladanyi@1442: case GDK_BUTTON_RELEASE: ladanyi@1442: isbutton=false; ladanyi@1442: active_item=NULL; ladanyi@1444: updateScrollRegion(); ladanyi@1442: break; ladanyi@1442: case GDK_MOTION_NOTIFY: ladanyi@1442: //we only have to do sg. if the mouse button is pressed ladanyi@1442: if(isbutton) ladanyi@1442: { ladanyi@1442: //new coordinates will be the old values, ladanyi@1442: //because the item will be moved to the ladanyi@1442: //new coordinate therefore the new movement ladanyi@1442: //has to be calculated from here ladanyi@1442: ladanyi@1442: double dx=e->motion.x-clicked_x; ladanyi@1442: double dy=e->motion.y-clicked_y; ladanyi@1442: active_item->move(dx, dy); ladanyi@1442: clicked_x=e->motion.x; ladanyi@1442: clicked_y=e->motion.y; ladanyi@1442: ladanyi@1442: //all the edges connected to the moved point has to be redrawn ladanyi@1442: ladanyi@1442: EdgeIt e; ladanyi@1442: g.firstOut(e,n); ladanyi@1442: for(;e!=INVALID;g.nextOut(e)) ladanyi@1442: { ladanyi@1442: Gnome::Canvas::Points coos; ladanyi@1442: double x1, x2, y1, y2; ladanyi@1442: ladanyi@1442: nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); ladanyi@1442: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); ladanyi@1442: ladanyi@1442: nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); ladanyi@1442: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); ladanyi@1442: ladanyi@1442: edgesmap[e]->property_points().set_value(coos); ladanyi@1442: ladanyi@1442: edgesmap[e]->get_bounds(x1, y1, x2, y2); ladanyi@1442: ladanyi@1442: edgetextmap[e]->property_x().set_value((x1+x2)/2); ladanyi@1442: edgetextmap[e]->property_y().set_value((y1+y2)/2); ladanyi@1442: } ladanyi@1442: ladanyi@1442: g.firstIn(e,n); ladanyi@1442: for(;e!=INVALID;g.nextIn(e)) ladanyi@1442: { ladanyi@1442: Gnome::Canvas::Points coos; ladanyi@1442: double x1, x2, y1, y2; ladanyi@1442: ladanyi@1442: nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); ladanyi@1442: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); ladanyi@1442: ladanyi@1442: nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); ladanyi@1442: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); ladanyi@1442: ladanyi@1442: edgesmap[e]->property_points().set_value(coos); ladanyi@1442: ladanyi@1442: edgesmap[e]->get_bounds(x1, y1, x2, y2); ladanyi@1442: ladanyi@1442: edgetextmap[e]->property_x().set_value((x1+x2)/2); ladanyi@1442: edgetextmap[e]->property_y().set_value((y1+y2)/2); ladanyi@1442: } ladanyi@1442: } ladanyi@1442: default: break; ladanyi@1442: } ladanyi@1442: return true; ladanyi@1442: } ladanyi@1442: ladanyi@1442: bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event) ladanyi@1442: { ladanyi@1442: Gnome::Canvas::CanvasAA::on_expose_event(event); ladanyi@1442: //usleep(10000); ladanyi@1442: //rezoom(); ladanyi@1442: return true; ladanyi@1442: } ladanyi@1442: ladanyi@1442: void GraphDisplayerCanvas::zoomIn() ladanyi@1442: { ladanyi@1442: set_pixels_per_unit( ladanyi@1442: (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit()); ladanyi@1442: } ladanyi@1442: ladanyi@1442: void GraphDisplayerCanvas::zoomOut() ladanyi@1442: { ladanyi@1442: set_pixels_per_unit( ladanyi@1442: (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit()); ladanyi@1442: } ladanyi@1442: ladanyi@1442: void GraphDisplayerCanvas::zoomFit() ladanyi@1442: { ladanyi@1442: // get the height and width of the canvas ladanyi@1442: Gtk::Allocation a = get_allocation(); ladanyi@1442: int aw = a.get_width(); ladanyi@1442: int ah = a.get_height(); ladanyi@1442: // add some space ladanyi@1442: aw -= 5; if (aw < 0) aw = 0; ladanyi@1442: ah -= 5; if (ah < 0) ah = 0; ladanyi@1442: ladanyi@1442: // get the bounding box of the graph ladanyi@1442: double wx1, wy1, wx2, wy2; ladanyi@1442: Gnome::Canvas::Item* pCanvasItem = root(); ladanyi@1442: pCanvasItem->get_bounds(wx1, wy1, wx2, wy2); ladanyi@1442: ladanyi@1442: // fit the graph to the window ladanyi@1444: double ppu1 = (double) aw / fabs(wx2 - wx1); ladanyi@1444: double ppu2 = (double) ah / fabs(wy2 - wy1); ladanyi@1444: set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2); ladanyi@1442: } ladanyi@1442: ladanyi@1442: void GraphDisplayerCanvas::zoom100() ladanyi@1442: { ladanyi@1442: set_pixels_per_unit(1.0); ladanyi@1442: } ladanyi@1442: ladanyi@1442: void GraphDisplayerCanvas::updateScrollRegion() ladanyi@1442: { ladanyi@1442: double wx1, wy1, wx2, wy2; ladanyi@1442: Gnome::Canvas::Item* pCanvasItem = root(); ladanyi@1442: pCanvasItem->get_bounds(wx1, wy1, wx2, wy2); ladanyi@1444: set_scroll_region(wx1, wy1, wx2, wy2); ladanyi@1442: }