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: { hegyi@1468: Gnome::Canvas::Ellipse * origo=new Gnome::Canvas::Ellipse(displayed_graph, 0-20, 0-20, 0+20, 0+20); hegyi@1468: *origo << Gnome::Canvas::Properties::fill_color("black"); hegyi@1468: *origo << Gnome::Canvas::Properties::outline_color("black"); hegyi@1468: hegyi@1468: actual_handler=/*displayed_graph.*/signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false); hegyi@1468: 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: updateScrollRegion(); ladanyi@1442: } ladanyi@1442: ladanyi@1442: GraphDisplayerCanvas::~GraphDisplayerCanvas() ladanyi@1442: { ladanyi@1442: ladanyi@1442: //writing out the end state of the graph ladanyi@1442: //\todo all the maps has to be write out! ladanyi@1442: ladanyi@1442: Graph::NodeMap 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: } hegyi@1468: hegyi@1468: void GraphDisplayerCanvas::changeEditorialTool(int newtool) hegyi@1468: { hegyi@1468: actual_handler.disconnect(); hegyi@1468: hegyi@1468: switch(newtool) hegyi@1468: { hegyi@1468: case MOVE: hegyi@1468: actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false); hegyi@1468: break; hegyi@1468: case CREATE_NODE: hegyi@1468: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false); hegyi@1468: break; hegyi@1468: case CREATE_EDGE: hegyi@1468: actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false); hegyi@1468: break; hegyi@1468: default: hegyi@1468: break; hegyi@1468: } hegyi@1468: } hegyi@1468: hegyi@1468: bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e) hegyi@1468: { hegyi@1468: switch(e->type) hegyi@1468: { hegyi@1468: case GDK_BUTTON_PRESS: hegyi@1468: //we mark the location of the event to be able to calculate parameters of dragging hegyi@1468: clicked_x=e->button.x; hegyi@1468: clicked_y=e->button.y; hegyi@1468: active_item=(get_item_at(e->button.x, e->button.y)); hegyi@1468: active_node=INVALID; hegyi@1468: for (NodeIt i(g); i!=INVALID; ++i) hegyi@1468: { hegyi@1468: if(nodesmap[i]==active_item) hegyi@1468: { hegyi@1468: active_node=i; hegyi@1468: } hegyi@1468: } hegyi@1468: isbutton=true; hegyi@1468: break; hegyi@1468: case GDK_BUTTON_RELEASE: hegyi@1468: isbutton=false; hegyi@1468: active_item=NULL; hegyi@1468: updateScrollRegion(); hegyi@1468: break; hegyi@1468: case GDK_MOTION_NOTIFY: hegyi@1468: //we only have to do sg. if the mouse button is pressed hegyi@1468: if(isbutton) hegyi@1468: { hegyi@1468: //new coordinates will be the old values, hegyi@1468: //because the item will be moved to the hegyi@1468: //new coordinate therefore the new movement hegyi@1468: //has to be calculated from here hegyi@1468: hegyi@1468: double dx=e->motion.x-clicked_x; hegyi@1468: double dy=e->motion.y-clicked_y; hegyi@1468: hegyi@1468: active_item->move(dx, dy); hegyi@1468: hegyi@1468: clicked_x=e->motion.x; hegyi@1468: clicked_y=e->motion.y; hegyi@1468: hegyi@1468: //all the edges connected to the moved point has to be redrawn hegyi@1468: hegyi@1468: EdgeIt e; hegyi@1468: hegyi@1468: g.firstOut(e,active_node); hegyi@1468: hegyi@1468: for(;e!=INVALID;g.nextOut(e)) hegyi@1468: { hegyi@1468: Gnome::Canvas::Points coos; hegyi@1468: double x1, x2, y1, y2; hegyi@1468: hegyi@1468: nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); hegyi@1468: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@1468: hegyi@1468: nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); hegyi@1468: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@1468: hegyi@1468: edgesmap[e]->property_points().set_value(coos); hegyi@1468: hegyi@1468: edgesmap[e]->get_bounds(x1, y1, x2, y2); hegyi@1468: hegyi@1468: edgetextmap[e]->property_x().set_value((x1+x2)/2); hegyi@1468: edgetextmap[e]->property_y().set_value((y1+y2)/2); hegyi@1468: } hegyi@1468: hegyi@1468: g.firstIn(e,active_node); hegyi@1468: for(;e!=INVALID;g.nextIn(e)) hegyi@1468: { hegyi@1468: Gnome::Canvas::Points coos; hegyi@1468: double x1, x2, y1, y2; hegyi@1468: hegyi@1468: nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); hegyi@1468: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@1468: hegyi@1468: nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); hegyi@1468: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@1468: hegyi@1468: edgesmap[e]->property_points().set_value(coos); hegyi@1468: hegyi@1468: edgesmap[e]->get_bounds(x1, y1, x2, y2); hegyi@1468: hegyi@1468: edgetextmap[e]->property_x().set_value((x1+x2)/2); hegyi@1468: edgetextmap[e]->property_y().set_value((y1+y2)/2); hegyi@1468: } hegyi@1468: } hegyi@1468: default: break; hegyi@1468: } hegyi@1468: hegyi@1468: return true; hegyi@1468: } hegyi@1468: hegyi@1468: bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e) hegyi@1468: { hegyi@1468: switch(e->type) hegyi@1468: { hegyi@1468: case GDK_BUTTON_PRESS: hegyi@1468: isbutton=true; hegyi@1468: hegyi@1468: active_node=NodeIt(g,g.addNode()); hegyi@1468: hegyi@1468: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@1468: hegyi@1468: nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20); hegyi@1468: active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]); hegyi@1468: *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); hegyi@1468: *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black"); hegyi@1468: (nodesmap[active_node])->show(); hegyi@1468: break; hegyi@1468: case GDK_MOTION_NOTIFY: hegyi@1468: { hegyi@1468: double world_motion_x, world_motion_y; hegyi@1468: GdkEvent * generated=new GdkEvent(); hegyi@1468: window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y); hegyi@1468: generated->motion.x=world_motion_x; hegyi@1468: generated->motion.y=world_motion_y; hegyi@1468: generated->type=GDK_MOTION_NOTIFY; hegyi@1468: move_event_handler(generated); hegyi@1468: break; hegyi@1468: } hegyi@1468: case GDK_BUTTON_RELEASE: hegyi@1468: isbutton=false; hegyi@1468: *active_item << Gnome::Canvas::Properties::fill_color("blue"); hegyi@1468: active_item=NULL; hegyi@1468: updateScrollRegion(); hegyi@1468: break; hegyi@1468: default: hegyi@1468: break; hegyi@1468: } hegyi@1468: return false; hegyi@1468: } hegyi@1468: hegyi@1468: bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e) hegyi@1468: { hegyi@1468: e=e; hegyi@1468: return false; hegyi@1468: } hegyi@1468: