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