# HG changeset patch # User hegyi # Date 1118658608 0 # Node ID 09b2a893fc9dbabdd0601d6af7cc89cbd22c6f60 # Parent 43ddd0c9d8819466f4bac1ef5056b4231a1e06f1 Edge creation is available. diff -r 43ddd0c9d881 -r 09b2a893fc9d graph_displayer_canvas.cc --- a/graph_displayer_canvas.cc Fri Jun 10 12:11:50 2005 +0000 +++ b/graph_displayer_canvas.cc Mon Jun 13 10:30:08 2005 +0000 @@ -1,13 +1,10 @@ #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) +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) { - Gnome::Canvas::Ellipse * origo=new Gnome::Canvas::Ellipse(displayed_graph, 0-20, 0-20, 0+20, 0+20); - *origo << Gnome::Canvas::Properties::fill_color("black"); - *origo << Gnome::Canvas::Properties::outline_color("black"); - actual_handler=/*displayed_graph.*/signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false); + actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false); //set_center_scroll_region(true); @@ -366,7 +363,6 @@ clicked_y=e->motion.y; //all the edges connected to the moved point has to be redrawn - EdgeIt e; g.firstOut(e,active_node); @@ -458,7 +454,81 @@ bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e) { - e=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; } diff -r 43ddd0c9d881 -r 09b2a893fc9d graph_displayer_canvas.h --- a/graph_displayer_canvas.h Fri Jun 10 12:11:50 2005 +0000 +++ b/graph_displayer_canvas.h Mon Jun 13 10:30:08 2005 +0000 @@ -96,8 +96,9 @@ ///this variable is needed, because ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution - Gnome::Canvas::Item * active_item; + Gnome::Canvas::Item * active_item, * target_item; Graph::NodeIt active_node; + Graph::EdgeIt active_edge; static const int zoom_step = 5; };