diff -r 638124c0ef08 -r d0ccb2fdeeff gui/graph_displayer_canvas.cc --- a/gui/graph_displayer_canvas.cc Thu Jun 09 21:49:48 2005 +0000 +++ b/gui/graph_displayer_canvas.cc Fri Jun 10 11:58:03 2005 +0000 @@ -3,6 +3,12 @@ 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) { + 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); + //set_center_scroll_region(true); //first edges are drawn, to hide joining with nodes later @@ -48,20 +54,9 @@ nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20); *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue"); *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black"); - (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i)); + //!!!!!!! (nodesmap[i])->signal_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<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) +{ + e=e; + return false; +} +