ladanyi@6: #include hegyi@17: #include ladanyi@6: #include ladanyi@6: hegyi@20: GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm, MapStorage & ms):g(gr),nodesmap(g),edgesmap(g),edgetextmap(g),displayed_graph(*(root()), 0, 0),mapstorage(ms),isbutton(0),active_item(NULL),target_item(NULL) ladanyi@6: { hegyi@9: hegyi@21: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false); hegyi@21: actual_tool=CREATE_NODE; hegyi@9: hegyi@12: active_node=INVALID; hegyi@12: active_edge=INVALID; hegyi@12: ladanyi@6: //set_center_scroll_region(true); ladanyi@6: ladanyi@6: //first edges are drawn, to hide joining with nodes later ladanyi@6: ladanyi@6: for (EdgeIt i(g); i!=INVALID; ++i) ladanyi@6: { ladanyi@6: ladanyi@6: //drawing green lines, coordinates are from cm ladanyi@6: ladanyi@6: Gnome::Canvas::Points coos; ladanyi@6: coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y)); ladanyi@6: coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y)); ladanyi@6: hegyi@21: edgesmap[i]=new BrokenEdge(displayed_graph, coos, *this); ladanyi@6: *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green"); ladanyi@6: edgesmap[i]->property_width_pixels().set_value(10); ladanyi@6: ladanyi@6: //initializing edge-text as well, to empty string ladanyi@6: ladanyi@6: double x1, x2, y1, y2; ladanyi@6: edgesmap[i]->get_bounds(x1, y1, x2, y2); ladanyi@6: ladanyi@6: edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, ""); ladanyi@6: edgetextmap[i]->property_fill_color().set_value("black"); ladanyi@6: } ladanyi@6: ladanyi@6: //afterwards nodes come to be drawn ladanyi@6: ladanyi@6: NodeIt i(g); ladanyi@6: int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y; ladanyi@6: ladanyi@6: for (; i!=INVALID; ++i) ladanyi@6: { ladanyi@6: //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen) ladanyi@6: ladanyi@6: if(cm[i].x>maxx)maxx=(int)cm[i].x; ladanyi@6: if(cm[i].y>maxy)maxy=(int)cm[i].y; ladanyi@6: if(cm[i].xsignal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i)); ladanyi@6: } ladanyi@6: ladanyi@6: updateScrollRegion(); ladanyi@6: } ladanyi@6: ladanyi@6: GraphDisplayerCanvas::~GraphDisplayerCanvas() ladanyi@6: { ladanyi@6: ladanyi@6: //writing out the end state of the graph ladanyi@6: //\todo all the maps has to be write out! ladanyi@6: ladanyi@6: Graph::NodeMap id(g); ladanyi@6: Graph::NodeMap xc(g); ladanyi@6: Graph::NodeMap yc(g); ladanyi@6: ladanyi@6: int j=1; ladanyi@6: ladanyi@6: for (NodeIt i(g); i!=INVALID; ++i) ladanyi@6: { ladanyi@6: double x1,y1,x2,y2; ladanyi@6: nodesmap[i]->get_bounds(x1, y1, x2, y2); ladanyi@6: ladanyi@6: id[i]=j++; ladanyi@6: xc[i]=(x1+x2)/2; ladanyi@6: yc[i]=(y1+y2)/2; ladanyi@6: } ladanyi@6: ladanyi@6: GraphWriter writer(std::cout,g); ladanyi@6: ladanyi@6: writer.writeNodeMap("id", id); ladanyi@6: writer.writeNodeMap("coordinates_x", xc); ladanyi@6: writer.writeNodeMap("coordinates_y", yc); ladanyi@6: writer.run(); ladanyi@6: } ladanyi@6: ladanyi@6: int GraphDisplayerCanvas::changeLineWidth (std::string mapname) ladanyi@6: { ladanyi@6: for (EdgeIt i(g); i!=INVALID; ++i) ladanyi@6: { ladanyi@6: int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i]; ladanyi@6: edgesmap[i]->property_width_pixels().set_value(w); ladanyi@6: } ladanyi@6: return 0; ladanyi@6: }; ladanyi@6: ladanyi@6: int GraphDisplayerCanvas::changeColor (std::string mapname) ladanyi@6: { ladanyi@6: ladanyi@6: //function maps the range of the maximum and ladanyi@6: //the minimum of the nodemap to the range of ladanyi@6: //green in RGB ladanyi@6: ladanyi@6: for (EdgeIt i(g); i!=INVALID; ++i) ladanyi@6: { ladanyi@6: double w=(*(mapstorage.edgemap_storage)[mapname])[i]; ladanyi@6: double max=mapstorage.maxOfEdgeMap(mapname); ladanyi@6: double min=mapstorage.minOfEdgeMap(mapname); ladanyi@6: ladanyi@6: //std::cout<property_fill_color_gdk().set_value(color); ladanyi@6: } ladanyi@6: return 0; ladanyi@6: }; ladanyi@6: ladanyi@6: int GraphDisplayerCanvas::changeText (std::string mapname) ladanyi@6: { ladanyi@6: ladanyi@6: //the number in the map will be written on the edge ladanyi@6: //EXCEPT when the name of the map is Text, because ladanyi@6: //in that case empty string will be written, because ladanyi@6: //that is the deleter map ladanyi@6: //\todo isn't it a bit woodcutter? ladanyi@6: ladanyi@6: for (EdgeIt i(g); i!=INVALID; ++i) ladanyi@6: { ladanyi@6: if(mapname!="Text") ladanyi@6: { ladanyi@6: double number=(*(mapstorage.edgemap_storage)[mapname])[i]; ladanyi@6: int length=(int)(floor(log(number)/log(10)))+1; ladanyi@6: int maxpos=(int)(pow(10,length-1)); ladanyi@6: int strl=length+1+RANGE; ladanyi@6: char * str=new char[strl]; ladanyi@6: str[length]='.'; ladanyi@6: str[strl]='\0'; ladanyi@6: ladanyi@6: for(int j=0;jproperty_text().set_value(str); ladanyi@6: } ladanyi@6: else ladanyi@6: { ladanyi@6: edgetextmap[i]->property_text().set_value(""); ladanyi@6: } ladanyi@6: } ladanyi@6: return 0; ladanyi@6: }; ladanyi@6: ladanyi@6: bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n) ladanyi@6: { ladanyi@6: switch(e->type) ladanyi@6: { ladanyi@6: case GDK_BUTTON_PRESS: ladanyi@6: //we mark the location of the event to be able to calculate parameters of dragging ladanyi@6: clicked_x=e->button.x; ladanyi@6: clicked_y=e->button.y; ladanyi@6: active_item=(get_item_at(e->button.x, e->button.y)); hegyi@20: isbutton=1; ladanyi@6: break; ladanyi@6: case GDK_BUTTON_RELEASE: hegyi@20: isbutton=0; ladanyi@6: active_item=NULL; ladanyi@7: updateScrollRegion(); ladanyi@6: break; ladanyi@6: case GDK_MOTION_NOTIFY: ladanyi@6: //we only have to do sg. if the mouse button is pressed ladanyi@6: if(isbutton) ladanyi@6: { ladanyi@6: //new coordinates will be the old values, ladanyi@6: //because the item will be moved to the ladanyi@6: //new coordinate therefore the new movement ladanyi@6: //has to be calculated from here ladanyi@6: ladanyi@6: double dx=e->motion.x-clicked_x; ladanyi@6: double dy=e->motion.y-clicked_y; ladanyi@6: active_item->move(dx, dy); ladanyi@6: clicked_x=e->motion.x; ladanyi@6: clicked_y=e->motion.y; ladanyi@6: ladanyi@6: //all the edges connected to the moved point has to be redrawn ladanyi@6: ladanyi@6: EdgeIt e; ladanyi@6: g.firstOut(e,n); ladanyi@6: for(;e!=INVALID;g.nextOut(e)) ladanyi@6: { ladanyi@6: Gnome::Canvas::Points coos; ladanyi@6: double x1, x2, y1, y2; ladanyi@6: ladanyi@6: nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); ladanyi@6: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); ladanyi@6: ladanyi@6: nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); ladanyi@6: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); ladanyi@6: ladanyi@6: edgesmap[e]->property_points().set_value(coos); ladanyi@6: ladanyi@6: edgesmap[e]->get_bounds(x1, y1, x2, y2); ladanyi@6: ladanyi@6: edgetextmap[e]->property_x().set_value((x1+x2)/2); ladanyi@6: edgetextmap[e]->property_y().set_value((y1+y2)/2); ladanyi@6: } ladanyi@6: ladanyi@6: g.firstIn(e,n); ladanyi@6: for(;e!=INVALID;g.nextIn(e)) ladanyi@6: { ladanyi@6: Gnome::Canvas::Points coos; ladanyi@6: double x1, x2, y1, y2; ladanyi@6: ladanyi@6: nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); ladanyi@6: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); ladanyi@6: ladanyi@6: nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); ladanyi@6: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); ladanyi@6: ladanyi@6: edgesmap[e]->property_points().set_value(coos); ladanyi@6: ladanyi@6: edgesmap[e]->get_bounds(x1, y1, x2, y2); ladanyi@6: ladanyi@6: edgetextmap[e]->property_x().set_value((x1+x2)/2); ladanyi@6: edgetextmap[e]->property_y().set_value((y1+y2)/2); ladanyi@6: } ladanyi@6: } ladanyi@6: default: break; ladanyi@6: } ladanyi@6: return true; ladanyi@6: } ladanyi@6: ladanyi@6: bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event) ladanyi@6: { ladanyi@6: Gnome::Canvas::CanvasAA::on_expose_event(event); ladanyi@6: //usleep(10000); ladanyi@6: //rezoom(); ladanyi@6: return true; ladanyi@6: } ladanyi@6: ladanyi@6: void GraphDisplayerCanvas::zoomIn() ladanyi@6: { ladanyi@6: set_pixels_per_unit( ladanyi@6: (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit()); ladanyi@6: } ladanyi@6: ladanyi@6: void GraphDisplayerCanvas::zoomOut() ladanyi@6: { ladanyi@6: set_pixels_per_unit( ladanyi@6: (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit()); ladanyi@6: } ladanyi@6: ladanyi@6: void GraphDisplayerCanvas::zoomFit() ladanyi@6: { ladanyi@6: // get the height and width of the canvas ladanyi@6: Gtk::Allocation a = get_allocation(); ladanyi@6: int aw = a.get_width(); ladanyi@6: int ah = a.get_height(); ladanyi@6: // add some space ladanyi@6: aw -= 5; if (aw < 0) aw = 0; ladanyi@6: ah -= 5; if (ah < 0) ah = 0; ladanyi@6: ladanyi@6: // get the bounding box of the graph ladanyi@6: double wx1, wy1, wx2, wy2; ladanyi@6: Gnome::Canvas::Item* pCanvasItem = root(); ladanyi@6: pCanvasItem->get_bounds(wx1, wy1, wx2, wy2); ladanyi@6: ladanyi@6: // fit the graph to the window ladanyi@7: double ppu1 = (double) aw / fabs(wx2 - wx1); ladanyi@7: double ppu2 = (double) ah / fabs(wy2 - wy1); ladanyi@7: set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2); ladanyi@6: } ladanyi@6: ladanyi@6: void GraphDisplayerCanvas::zoom100() ladanyi@6: { ladanyi@6: set_pixels_per_unit(1.0); ladanyi@6: } ladanyi@6: ladanyi@6: void GraphDisplayerCanvas::updateScrollRegion() ladanyi@6: { ladanyi@6: double wx1, wy1, wx2, wy2; ladanyi@6: Gnome::Canvas::Item* pCanvasItem = root(); ladanyi@6: pCanvasItem->get_bounds(wx1, wy1, wx2, wy2); ladanyi@7: set_scroll_region(wx1, wy1, wx2, wy2); ladanyi@6: } hegyi@9: hegyi@9: void GraphDisplayerCanvas::changeEditorialTool(int newtool) hegyi@9: { hegyi@9: actual_handler.disconnect(); hegyi@9: hegyi@21: if(actual_tool==CREATE_EDGE) hegyi@21: { hegyi@21: GdkEvent * generated=new GdkEvent(); hegyi@21: generated->type=GDK_BUTTON_RELEASE; hegyi@21: generated->button.button=3; hegyi@21: create_edge_event_handler(generated); hegyi@21: } hegyi@21: hegyi@21: actual_tool=newtool; hegyi@21: hegyi@9: switch(newtool) hegyi@9: { hegyi@9: case MOVE: hegyi@9: actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false); hegyi@9: break; hegyi@12: hegyi@12: //it has to assigned to canvas, because all the canvas has to be monitored, not only the elements of the already drawn group hegyi@9: case CREATE_NODE: hegyi@9: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false); hegyi@9: break; hegyi@12: hegyi@9: case CREATE_EDGE: hegyi@9: actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false); hegyi@9: break; hegyi@13: hegyi@13: case ERASER: hegyi@13: actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraser_event_handler), false); hegyi@13: break; hegyi@13: hegyi@9: default: hegyi@9: break; hegyi@9: } hegyi@9: } hegyi@9: hegyi@21: int GraphDisplayerCanvas::get_actual_tool() hegyi@21: { hegyi@21: return actual_tool; hegyi@21: } hegyi@21: hegyi@9: bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e) hegyi@9: { hegyi@9: switch(e->type) hegyi@9: { hegyi@9: case GDK_BUTTON_PRESS: hegyi@9: //we mark the location of the event to be able to calculate parameters of dragging hegyi@9: clicked_x=e->button.x; hegyi@9: clicked_y=e->button.y; hegyi@9: active_item=(get_item_at(e->button.x, e->button.y)); hegyi@9: active_node=INVALID; hegyi@9: for (NodeIt i(g); i!=INVALID; ++i) hegyi@9: { hegyi@9: if(nodesmap[i]==active_item) hegyi@9: { hegyi@9: active_node=i; hegyi@9: } hegyi@9: } hegyi@20: switch(e->button.button) hegyi@20: { hegyi@20: case 3: hegyi@20: isbutton=3; hegyi@20: break; hegyi@20: default: hegyi@20: isbutton=1; hegyi@20: break; hegyi@20: } hegyi@9: break; hegyi@9: case GDK_BUTTON_RELEASE: hegyi@20: isbutton=0; hegyi@9: active_item=NULL; hegyi@12: active_node=INVALID; hegyi@9: updateScrollRegion(); hegyi@9: break; hegyi@9: case GDK_MOTION_NOTIFY: hegyi@12: //we only have to do sg. if the mouse button is pressed AND the click was on a node that was found in the set of nodes hegyi@12: if(active_node!=INVALID) hegyi@9: { hegyi@9: //new coordinates will be the old values, hegyi@9: //because the item will be moved to the hegyi@9: //new coordinate therefore the new movement hegyi@9: //has to be calculated from here hegyi@9: hegyi@9: double dx=e->motion.x-clicked_x; hegyi@9: double dy=e->motion.y-clicked_y; hegyi@9: hegyi@9: active_item->move(dx, dy); hegyi@9: hegyi@9: clicked_x=e->motion.x; hegyi@9: clicked_y=e->motion.y; hegyi@9: hegyi@9: //all the edges connected to the moved point has to be redrawn hegyi@20: EdgeIt ei; hegyi@9: hegyi@20: g.firstOut(ei,active_node); hegyi@9: hegyi@20: for(;ei!=INVALID;g.nextOut(ei)) hegyi@9: { hegyi@9: Gnome::Canvas::Points coos; hegyi@9: double x1, x2, y1, y2; hegyi@9: hegyi@20: nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2); hegyi@9: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@9: hegyi@20: nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2); hegyi@9: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@9: hegyi@20: if(isbutton==3) hegyi@20: { hegyi@20: edgesmap[ei]->set_points(coos); hegyi@20: } hegyi@20: else hegyi@20: { hegyi@20: edgesmap[ei]->set_points(coos,true); hegyi@20: } hegyi@9: hegyi@20: edgesmap[ei]->get_bounds(x1, y1, x2, y2); hegyi@9: hegyi@20: edgetextmap[ei]->property_x().set_value((x1+x2)/2); hegyi@20: edgetextmap[ei]->property_y().set_value((y1+y2)/2); hegyi@9: } hegyi@9: hegyi@20: g.firstIn(ei,active_node); hegyi@20: for(;ei!=INVALID;g.nextIn(ei)) hegyi@9: { hegyi@9: Gnome::Canvas::Points coos; hegyi@9: double x1, x2, y1, y2; hegyi@9: hegyi@20: nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2); hegyi@9: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@9: hegyi@20: nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2); hegyi@9: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@9: hegyi@20: if(isbutton==3) hegyi@20: { hegyi@20: edgesmap[ei]->set_points(coos); hegyi@20: } hegyi@20: else hegyi@20: { hegyi@20: edgesmap[ei]->set_points(coos,true); hegyi@20: } hegyi@9: hegyi@20: edgesmap[ei]->get_bounds(x1, y1, x2, y2); hegyi@9: hegyi@20: edgetextmap[ei]->property_x().set_value((x1+x2)/2); hegyi@20: edgetextmap[ei]->property_y().set_value((y1+y2)/2); hegyi@9: } hegyi@9: } hegyi@9: default: break; hegyi@9: } hegyi@9: hegyi@9: return true; hegyi@9: } hegyi@9: hegyi@9: bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e) hegyi@9: { hegyi@9: switch(e->type) hegyi@9: { hegyi@12: hegyi@12: //draw the new node in red at the clicked place hegyi@9: case GDK_BUTTON_PRESS: hegyi@20: isbutton=1; hegyi@9: hegyi@9: active_node=NodeIt(g,g.addNode()); hegyi@9: hegyi@9: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@9: hegyi@9: nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20); hegyi@9: active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]); hegyi@9: *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); hegyi@9: *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black"); hegyi@9: (nodesmap[active_node])->show(); hegyi@9: break; hegyi@12: hegyi@12: //move the new node hegyi@9: case GDK_MOTION_NOTIFY: hegyi@9: { hegyi@9: double world_motion_x, world_motion_y; hegyi@9: GdkEvent * generated=new GdkEvent(); hegyi@9: window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y); hegyi@9: generated->motion.x=world_motion_x; hegyi@9: generated->motion.y=world_motion_y; hegyi@9: generated->type=GDK_MOTION_NOTIFY; hegyi@9: move_event_handler(generated); hegyi@9: break; hegyi@9: } hegyi@12: hegyi@12: //finalize the new node hegyi@9: case GDK_BUTTON_RELEASE: hegyi@20: isbutton=0; hegyi@9: *active_item << Gnome::Canvas::Properties::fill_color("blue"); hegyi@9: active_item=NULL; hegyi@12: active_node=INVALID; hegyi@9: updateScrollRegion(); hegyi@9: break; hegyi@9: default: hegyi@9: break; hegyi@9: } hegyi@9: return false; hegyi@9: } hegyi@9: hegyi@9: bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e) hegyi@9: { hegyi@11: switch(e->type) hegyi@11: { hegyi@11: case GDK_BUTTON_PRESS: hegyi@12: //in edge creatino right button has special meaning hegyi@12: if(e->button.button!=3) hegyi@11: { hegyi@12: //there is not yet selected node hegyi@12: if(active_node==INVALID) hegyi@11: { hegyi@12: //we mark the location of the event to be able to calculate parameters of dragging hegyi@12: clicked_x=e->button.x; hegyi@12: clicked_y=e->button.y; hegyi@12: active_item=(get_item_at(e->button.x, e->button.y)); hegyi@12: active_node=INVALID; hegyi@12: for (NodeIt i(g); i!=INVALID; ++i) hegyi@11: { hegyi@12: if(nodesmap[i]==active_item) hegyi@12: { hegyi@12: active_node=i; hegyi@12: } hegyi@12: } hegyi@12: //the clicked item is really a node hegyi@12: if(active_node!=INVALID) hegyi@12: { hegyi@12: *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); hegyi@20: isbutton=1; hegyi@12: } hegyi@12: //clicked item was not a node. It could be e.g. edge. hegyi@12: else hegyi@12: { hegyi@12: active_item=NULL; hegyi@11: } hegyi@11: } hegyi@12: //we only have to do sg. if the mouse button hegyi@12: // is pressed already once AND the click was hegyi@12: // on a node that was found in the set of hegyi@12: //nodes, and now we only search for the second hegyi@12: //node hegyi@12: else hegyi@11: { hegyi@12: target_item=(get_item_at(e->button.x, e->button.y)); hegyi@12: Graph::NodeIt target_node=INVALID; hegyi@12: for (NodeIt i(g); i!=INVALID; ++i) hegyi@11: { hegyi@12: if(nodesmap[i]==target_item) hegyi@12: { hegyi@12: target_node=i; hegyi@12: } hegyi@12: } hegyi@12: //the clicked item is a node, the edge can be drawn hegyi@12: if(target_node!=INVALID) hegyi@12: { hegyi@12: *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red"); hegyi@12: hegyi@12: //creating new edge hegyi@12: active_edge=EdgeIt(g,g.addEdge(active_node, target_node)); hegyi@12: hegyi@12: //calculating coordinates of new edge hegyi@12: Gnome::Canvas::Points coos; hegyi@12: double x1, x2, y1, y2; hegyi@12: hegyi@12: active_item->get_bounds(x1, y1, x2, y2); hegyi@12: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@12: hegyi@12: target_item->get_bounds(x1, y1, x2, y2); hegyi@12: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@12: hegyi@12: //drawing new edge hegyi@21: edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this); hegyi@12: *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green"); hegyi@12: edgesmap[active_edge]->property_width_pixels().set_value(10); hegyi@12: hegyi@12: //redraw nodes to blank terminations of the new edge hegyi@12: target_item->raise_to_top(); hegyi@12: active_item->raise_to_top(); hegyi@12: hegyi@12: //initializing edge-text as well, to empty string hegyi@12: edgesmap[active_edge]->get_bounds(x1, y1, x2, y2); hegyi@12: edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, ""); hegyi@12: edgetextmap[active_edge]->property_fill_color().set_value("black"); hegyi@12: } hegyi@12: //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore. hegyi@12: else hegyi@12: { hegyi@12: target_item=NULL; hegyi@11: } hegyi@11: } hegyi@11: } hegyi@11: break; hegyi@11: case GDK_BUTTON_RELEASE: hegyi@20: isbutton=0; hegyi@12: //we clear settings in two cases hegyi@12: //1: the edge is ready (target_item has valid value) hegyi@12: //2: the edge creation is cancelled with right button hegyi@12: if((target_item)||(e->button.button==3)) hegyi@11: { hegyi@12: if(active_item) hegyi@12: { hegyi@12: *active_item << Gnome::Canvas::Properties::fill_color("blue"); hegyi@12: active_item=NULL; hegyi@12: } hegyi@12: if(target_item) hegyi@12: { hegyi@12: *target_item << Gnome::Canvas::Properties::fill_color("blue"); hegyi@12: target_item=NULL; hegyi@12: } hegyi@12: active_node=INVALID; hegyi@12: active_edge=INVALID; hegyi@11: } hegyi@11: break; hegyi@11: default: hegyi@11: break; hegyi@11: } hegyi@9: return false; hegyi@9: } hegyi@9: hegyi@13: bool GraphDisplayerCanvas::eraser_event_handler(GdkEvent* e) hegyi@13: { hegyi@13: switch(e->type) hegyi@13: { hegyi@13: case GDK_BUTTON_PRESS: hegyi@13: active_item=(get_item_at(e->button.x, e->button.y)); hegyi@13: active_node=INVALID; hegyi@13: active_edge=INVALID; hegyi@13: for (NodeIt i(g); i!=INVALID; ++i) hegyi@13: { hegyi@13: if(nodesmap[i]==active_item) hegyi@13: { hegyi@13: active_node=i; hegyi@13: } hegyi@13: } hegyi@13: if(active_node==INVALID) hegyi@13: { hegyi@13: for (EdgeIt i(g); i!=INVALID; ++i) hegyi@13: { hegyi@13: if(edgesmap[i]==active_item) hegyi@13: { hegyi@13: active_edge=i; hegyi@13: } hegyi@13: } hegyi@13: } hegyi@13: *active_item << Gnome::Canvas::Properties::fill_color("red"); hegyi@13: break; hegyi@13: hegyi@13: case GDK_BUTTON_RELEASE: hegyi@13: if(active_item==(get_item_at(e->button.x, e->button.y))) hegyi@13: { hegyi@13: if(active_node!=INVALID) hegyi@13: { hegyi@14: hegyi@16: //collecting edges to delete hegyi@16: EdgeIt e; hegyi@16: std::set edges_to_delete; hegyi@16: hegyi@16: g.firstOut(e,active_node); hegyi@16: for(;e!=INVALID;g.nextOut(e)) hegyi@13: { hegyi@16: edges_to_delete.insert(e); hegyi@16: } hegyi@16: hegyi@16: g.firstIn(e,active_node); hegyi@16: for(;e!=INVALID;g.nextIn(e)) hegyi@16: { hegyi@16: edges_to_delete.insert(e); hegyi@16: } hegyi@16: hegyi@16: //deleting collected edges hegyi@16: for(std::set::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++) hegyi@16: { hegyi@16: delete_item(*edge_set_it); hegyi@13: } hegyi@14: delete_item(active_node); hegyi@14: } hegyi@16: //a simple edge was chosen hegyi@14: else hegyi@14: { hegyi@14: delete_item(active_edge); hegyi@14: } hegyi@13: hegyi@13: hegyi@13: } hegyi@16: //pointer was moved, deletion is cancelled hegyi@13: else hegyi@13: { hegyi@13: if(active_node!=INVALID) hegyi@13: { hegyi@13: *active_item << Gnome::Canvas::Properties::fill_color("blue"); hegyi@13: } hegyi@13: else hegyi@13: { hegyi@13: *active_item << Gnome::Canvas::Properties::fill_color("green"); hegyi@13: } hegyi@13: } hegyi@16: //reseting datas hegyi@13: active_item=NULL; hegyi@13: active_edge=INVALID; hegyi@13: active_node=INVALID; hegyi@13: break; hegyi@13: hegyi@13: case GDK_MOTION_NOTIFY: hegyi@13: break; hegyi@13: hegyi@13: default: hegyi@13: break; hegyi@13: } hegyi@13: return true; hegyi@13: } hegyi@13: hegyi@14: void GraphDisplayerCanvas::delete_item(NodeIt node_to_delete) hegyi@13: { hegyi@14: delete(nodesmap[node_to_delete]); hegyi@14: g.erase(node_to_delete); hegyi@13: } hegyi@13: hegyi@14: void GraphDisplayerCanvas::delete_item(EdgeIt edge_to_delete) hegyi@14: { hegyi@14: delete(edgesmap[edge_to_delete]); hegyi@14: g.erase(edge_to_delete); hegyi@14: } hegyi@14: hegyi@16: void GraphDisplayerCanvas::delete_item(Graph::Edge edge_to_delete) hegyi@16: { hegyi@16: delete(edgesmap[edge_to_delete]); hegyi@16: g.erase(edge_to_delete); hegyi@16: } hegyi@16: