diff -r 23f0afd1a323 -r f60f89147531 graph_displayer_canvas-event.cc --- a/graph_displayer_canvas-event.cc Thu Dec 08 14:16:08 2005 +0000 +++ b/graph_displayer_canvas-event.cc Sat Dec 17 20:55:41 2005 +0000 @@ -147,12 +147,12 @@ double coord_x = new_x - (clicked_x - (mytab.mapstorage).coords[active_node].x); double coord_y = new_y - (clicked_y - (mytab.mapstorage).coords[active_node].y); + // write back the new coordinates to the coords map + (mytab.mapstorage).coords.set(active_node, xy(coord_x, coord_y)); + clicked_x=new_x; clicked_y=new_y; - // write back the new coordinates to the coords map - (mytab.mapstorage).coords.set(active_node, xy(coord_x, coord_y)); - // reposition the coordinates text std::ostringstream ostr; ostr << "(" << @@ -183,53 +183,50 @@ //all the edges connected to the moved point has to be redrawn for(OutEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei) { - Gnome::Canvas::Points coos; - double x1, x2, y1, y2; + XY moved_node_1(coord_x - dx, coord_y - dy); + XY moved_node_2(coord_x, coord_y); + Node target = mytab.mapstorage.graph.target(ei); + XY fix_node(mytab.mapstorage.coords[target].x, + mytab.mapstorage.coords[target].y); + XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]); - nodesmap[(mytab.mapstorage).graph.source(ei)]->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); + XY arrow_pos; + if(isbutton==3) + arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, false); + else + arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, true); - nodesmap[(mytab.mapstorage).graph.target(ei)]->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); - - if(isbutton==3) - { - edgesmap[ei]->setPoints(coos); - } - else - { - edgesmap[ei]->setPoints(coos,true); - } + mytab.mapstorage.arrow_pos.set(ei, arrow_pos); + edgesmap[ei]->draw(); //reposition of edgetext - xy text_pos=edgesmap[ei]->getArrowPos(); - text_pos+=(xy(10,10)); + XY text_pos=mytab.mapstorage.arrow_pos[ei]; + text_pos+=(XY(10,10)); edgetextmap[ei]->property_x().set_value(text_pos.x); edgetextmap[ei]->property_y().set_value(text_pos.y); } for(InEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei) { - Gnome::Canvas::Points coos; - double x1, x2, y1, y2; + XY moved_node_1(coord_x - dx, coord_y - dy); + XY moved_node_2(coord_x, coord_y); + Node source = mytab.mapstorage.graph.source(ei); + XY fix_node(mytab.mapstorage.coords[source].x, + mytab.mapstorage.coords[source].y); + XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]); - nodesmap[(mytab.mapstorage).graph.source(ei)]->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); + XY arrow_pos; + if(isbutton==3) + arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, false); + else + arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, true); - nodesmap[(mytab.mapstorage).graph.target(ei)]->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); + mytab.mapstorage.arrow_pos.set(ei, arrow_pos); + edgesmap[ei]->draw(); - if(isbutton==3) - { - edgesmap[ei]->setPoints(coos); - } - else - { - edgesmap[ei]->setPoints(coos,true); - } - - xy text_pos=edgesmap[ei]->getArrowPos(); - text_pos+=(xy(10,10)); + //reposition of edgetext + XY text_pos=mytab.mapstorage.arrow_pos[ei]; + text_pos+=(XY(10,10)); edgetextmap[ei]->property_x().set_value(text_pos.x); edgetextmap[ei]->property_y().set_value(text_pos.y); } @@ -240,6 +237,58 @@ return false; } +XY GraphDisplayerCanvas::calcArrowPos(XY moved_node_1, XY moved_node_2, XY fix_node, XY old_arrow_pos, bool move) +{ + if(!move) + { + return XY((moved_node_2.x + fix_node.x) / 2.0, (moved_node_2.y + fix_node.y) / 2.0); + } + else + { + ////////////////////////////////////////////////////////////////////////////////////////////////////// + /////////// keeps shape-with scalar multiplication - version 2. + ////////////////////////////////////////////////////////////////////////////////////////////////////// + + //old vector from one to the other node - a + xy a_v(moved_node_1.x-fix_node.x,moved_node_1.y-fix_node.y); + //new vector from one to the other node - b + xy b_v(moved_node_2.x-fix_node.x,moved_node_2.y-fix_node.y); + + double absa=sqrt(a_v.normSquare()); + double absb=sqrt(b_v.normSquare()); + + if ((absa == 0.0) || (absb == 0.0)) + { + return old_arrow_pos; + } + else + { + //old vector from one node to the breakpoint - c + xy c_v(old_arrow_pos.x-fix_node.x,old_arrow_pos.y-fix_node.y); + + //unit vector with the same direction to a_v + xy a_v_u(a_v.x/absa,a_v.y/absa); + + //normal vector of unit vector with the same direction to a_v + xy a_v_u_n(((-1)*a_v_u.y),a_v_u.x); + + //unit vector with the same direction to b_v + xy b_v_u(b_v.x/absb,b_v.y/absb); + + //normal vector of unit vector with the same direction to b_v + xy b_v_u_n(((-1)*b_v_u.y),b_v_u.x); + + //vector c in a_v_u and a_v_u_n co-ordinate system + xy c_a(c_v*a_v_u,c_v*a_v_u_n); + + //new vector from one node to the breakpoint - d - we have to calculate this one + xy d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n); + + return XY(d_v.x+fix_node.x,d_v.y+fix_node.y); + } + } +} + bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e) { switch(e->type) @@ -405,7 +454,7 @@ coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); //drawing new edge - edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, + edgesmap[active_edge]=new BrokenEdge(displayed_graph, active_edge, *this); *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green"); @@ -414,8 +463,8 @@ edgesmap[active_edge]->lower_to_bottom(); //initializing edge-text as well, to empty string - xy text_pos=edgesmap[active_edge]->getArrowPos(); - text_pos+=(xy(10,10)); + XY text_pos=mytab.mapstorage.arrow_pos[active_edge]; + text_pos+=(XY(10,10)); edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");