#include "graph_displayer_canvas.h" #include GraphDisplayerCanvas::BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p, GraphDisplayerCanvas & gc) : Line(g), gdc(gc), isbutton(false) { my_points=new Gnome::Art::Point[3]; arrow=new Gnome::Canvas::Polygon(g); *arrow << Gnome::Canvas::Properties::fill_color("red"); arrow->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler)); arrow->lower_to_bottom(); setPoints(p); } GraphDisplayerCanvas::BrokenEdge::~BrokenEdge() { if(arrow)delete(arrow); } void GraphDisplayerCanvas::BrokenEdge::setPoints(Gnome::Canvas::Points p, bool move) { bool set_arrow=false; //red arrow losts its position-right button if(!move) { if(p.size()==2) { set_arrow=true; Gnome::Canvas::Points points_with_center; points_with_center.push_back(my_points[0]=p[0]); points_with_center.push_back(my_points[1]=Gnome::Art::Point( (p[0].get_x()+p[1].get_x())/2+0 , (p[0].get_y()+p[1].get_y())/2 )+0 ); points_with_center.push_back(my_points[2]=p[1]); property_points().set_value(points_with_center); } if(p.size()==3) { set_arrow=true; property_points().set_value(p); for(int i=0;i<3;i++) { my_points[i]=p[i]; } } } else { //arrow keeps its position-left button // if(p.size()==2) // { // Gnome::Canvas::Points points; // my_points[0]=p[0]; // my_points[2]=p[1]; // for(int i=0;i<3;i++) // { // points.push_back(my_points[i]); // } // property_points().set_value(points); // } set_arrow=true; ////////////////////////////////////////////////////////////////////////////////////////////////////// /////////// keeps shape-with scalar multiplication - version 2. ////////////////////////////////////////////////////////////////////////////////////////////////////// if(p.size()==2) { //old vector from one to the other node - a xy a_v(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y()); //new vector from one to the other node - b xy b_v(p[1].get_x()-p[0].get_x(),p[1].get_y()-p[0].get_y()); double absa=sqrt(a_v.normSquare()); double absb=sqrt(b_v.normSquare()); if((absa!=0)&&(absb!=0)) { //old vector from one node to the breakpoint - c xy c_v(my_points[1].get_x()-my_points[0].get_x(),my_points[1].get_y()-my_points[0].get_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); my_points[1]=Gnome::Art::Point(d_v.x+p[0].get_x(),d_v.y+p[0].get_y()); my_points[0]=p[0]; my_points[2]=p[1]; Gnome::Canvas::Points points; for(int i=0;i<3;i++) { points.push_back(my_points[i]); } property_points().set_value(points); } else { //if distance is 0, segmentation would be occured //in calculations, because of division by zero //But we have luck: the edge cannot be seen in //this case, so no update needed set_arrow=false; } } } if(set_arrow) { //calculating coordinates of the direction indicator arrow xy target( my_points[2].get_x(), my_points[2].get_y() ); xy center( my_points[1].get_x(), my_points[1].get_y() ); xy unit_vector_in_dir(target-center); double length=sqrt( unit_vector_in_dir.normSquare() ); // std::cout << target << " - " << center << " = " << unit_vector_in_dir << " / " <property_points().set_value(arrow_points); } } bool GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler(GdkEvent* e) { switch(e->type) { case GDK_BUTTON_PRESS: //we mark the location of the event to be able to calculate parameters of dragging if(gdc.getActualTool()!=CREATE_NODE) { gdc.toggleEdgeActivity(this, true); clicked_x=e->button.x; clicked_y=e->button.y; isbutton=true; } break; case GDK_BUTTON_RELEASE: if(gdc.getActualTool()!=CREATE_NODE) { gdc.toggleEdgeActivity(this, false); isbutton=false; } 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; Gnome::Canvas::Points points_new; points_new.push_back(my_points[0]); points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy)); points_new.push_back(my_points[2]); setPoints(points_new); gdc.textReposition(xy(my_points[1].get_x(),my_points[1].get_y())); clicked_x=e->motion.x; clicked_y=e->motion.y; } default: break; } return true; } xy GraphDisplayerCanvas::BrokenEdge::getArrowPos() { xy ret_val(my_points[1].get_x(),my_points[1].get_y()); return ret_val; }