#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),target_item(NULL) { actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false); //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)); } updateScrollRegion(); } GraphDisplayerCanvas::~GraphDisplayerCanvas() { //writing out the end state of the graph //\todo all the maps has to be write out! Graph::NodeMap 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; updateScrollRegion(); 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; // get the bounding box of the graph double wx1, wy1, wx2, wy2; Gnome::Canvas::Item* pCanvasItem = root(); pCanvasItem->get_bounds(wx1, wy1, wx2, wy2); // fit the graph to the window double ppu1 = (double) aw / fabs(wx2 - wx1); double ppu2 = (double) ah / fabs(wy2 - wy1); set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2); } void GraphDisplayerCanvas::zoom100() { set_pixels_per_unit(1.0); } void GraphDisplayerCanvas::updateScrollRegion() { double wx1, wy1, wx2, wy2; Gnome::Canvas::Item* pCanvasItem = root(); pCanvasItem->get_bounds(wx1, wy1, wx2, wy2); set_scroll_region(wx1, wy1, wx2, wy2); } void GraphDisplayerCanvas::changeEditorialTool(int newtool) { actual_handler.disconnect(); switch(newtool) { case MOVE: actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false); break; case CREATE_NODE: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false); break; case CREATE_EDGE: actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false); break; default: break; } } bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e) { 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)); active_node=INVALID; for (NodeIt i(g); i!=INVALID; ++i) { if(nodesmap[i]==active_item) { active_node=i; } } isbutton=true; break; case GDK_BUTTON_RELEASE: isbutton=false; active_item=NULL; updateScrollRegion(); 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,active_node); 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,active_node); 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::create_node_event_handler(GdkEvent* e) { switch(e->type) { case GDK_BUTTON_PRESS: isbutton=true; active_node=NodeIt(g,g.addNode()); window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20); active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]); *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black"); (nodesmap[active_node])->show(); break; case GDK_MOTION_NOTIFY: { double world_motion_x, world_motion_y; GdkEvent * generated=new GdkEvent(); window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y); generated->motion.x=world_motion_x; generated->motion.y=world_motion_y; generated->type=GDK_MOTION_NOTIFY; move_event_handler(generated); break; } case GDK_BUTTON_RELEASE: isbutton=false; *active_item << Gnome::Canvas::Properties::fill_color("blue"); active_item=NULL; updateScrollRegion(); break; default: break; } return false; } bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e) { switch(e->type) { case GDK_BUTTON_PRESS: if(!active_item) { //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)); active_node=INVALID; for (NodeIt i(g); i!=INVALID; ++i) { if(nodesmap[i]==active_item) { active_node=i; } } *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); isbutton=true; } else { target_item=(get_item_at(e->button.x, e->button.y)); Graph::NodeIt target_node=INVALID; for (NodeIt i(g); i!=INVALID; ++i) { if(nodesmap[i]==target_item) { target_node=i; } } *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red"); //creating new edge // Graph::Edge new_edge=g.addEdge(active_node, target_node); active_edge=EdgeIt(g,g.addEdge(active_node, target_node)); //calculating coordinates of new edge Gnome::Canvas::Points coos; double x1, x2, y1, y2; active_item->get_bounds(x1, y1, x2, y2); coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); target_item->get_bounds(x1, y1, x2, y2); coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); //drawing new edge edgesmap[active_edge]=new Gnome::Canvas::Line(displayed_graph, coos); *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green"); edgesmap[active_edge]->property_width_pixels().set_value(10); //redraw nodes to blank terminations of the new edge target_item->raise_to_top(); active_item->raise_to_top(); //initializing edge-text as well, to empty string edgesmap[active_edge]->get_bounds(x1, y1, x2, y2); edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, ""); edgetextmap[active_edge]->property_fill_color().set_value("black"); } break; case GDK_BUTTON_RELEASE: isbutton=false; if(target_item) { *active_item << Gnome::Canvas::Properties::fill_color("blue"); *target_item << Gnome::Canvas::Properties::fill_color("blue"); active_item=NULL; target_item=NULL; } break; default: break; } return false; }