gui/graph_displayer_canvas-event.cc
changeset 1860 27a9a75b957b
parent 1849 a4d1362397fe
child 1879 01d41844ef46
     1.1 --- a/gui/graph_displayer_canvas-event.cc	Wed Dec 14 18:11:03 2005 +0000
     1.2 +++ b/gui/graph_displayer_canvas-event.cc	Sat Dec 17 20:55:41 2005 +0000
     1.3 @@ -147,12 +147,12 @@
     1.4          double coord_x = new_x - (clicked_x - (mytab.mapstorage).coords[active_node].x);
     1.5          double coord_y = new_y - (clicked_y - (mytab.mapstorage).coords[active_node].y);
     1.6  
     1.7 +        // write back the new coordinates to the coords map
     1.8 +        (mytab.mapstorage).coords.set(active_node, xy<double>(coord_x, coord_y));
     1.9 +
    1.10          clicked_x=new_x;
    1.11          clicked_y=new_y;
    1.12  
    1.13 -        // write back the new coordinates to the coords map
    1.14 -        (mytab.mapstorage).coords.set(active_node, xy<double>(coord_x, coord_y));
    1.15 -
    1.16          // reposition the coordinates text
    1.17          std::ostringstream ostr;
    1.18          ostr << "(" <<
    1.19 @@ -183,53 +183,50 @@
    1.20  	//all the edges connected to the moved point has to be redrawn
    1.21          for(OutEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei)
    1.22          {
    1.23 -            Gnome::Canvas::Points coos;
    1.24 -            double x1, x2, y1, y2;
    1.25 +            XY moved_node_1(coord_x - dx, coord_y - dy);
    1.26 +            XY moved_node_2(coord_x, coord_y);
    1.27 +            Node target = mytab.mapstorage.graph.target(ei);
    1.28 +            XY fix_node(mytab.mapstorage.coords[target].x,
    1.29 +                        mytab.mapstorage.coords[target].y);
    1.30 +            XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
    1.31  
    1.32 -            nodesmap[(mytab.mapstorage).graph.source(ei)]->get_bounds(x1, y1, x2, y2);
    1.33 -            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
    1.34 +            XY arrow_pos;
    1.35 +	    if(isbutton==3)
    1.36 +              arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, false);
    1.37 +	    else
    1.38 +              arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, true);
    1.39  
    1.40 -            nodesmap[(mytab.mapstorage).graph.target(ei)]->get_bounds(x1, y1, x2, y2);
    1.41 -            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
    1.42 -
    1.43 -	    if(isbutton==3)
    1.44 -	      {
    1.45 -		edgesmap[ei]->setPoints(coos);
    1.46 -	      }
    1.47 -	    else
    1.48 -	      {
    1.49 -		edgesmap[ei]->setPoints(coos,true);
    1.50 -	      }
    1.51 +            mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
    1.52 +            edgesmap[ei]->draw();
    1.53  
    1.54  	    //reposition of edgetext
    1.55 -	    xy<double> text_pos=edgesmap[ei]->getArrowPos();
    1.56 -	    text_pos+=(xy<double>(10,10));
    1.57 +	    XY text_pos=mytab.mapstorage.arrow_pos[ei];
    1.58 +	    text_pos+=(XY(10,10));
    1.59  	    edgetextmap[ei]->property_x().set_value(text_pos.x);
    1.60  	    edgetextmap[ei]->property_y().set_value(text_pos.y);
    1.61          }
    1.62  
    1.63          for(InEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei)
    1.64          {
    1.65 -            Gnome::Canvas::Points coos;
    1.66 -            double x1, x2, y1, y2;
    1.67 +            XY moved_node_1(coord_x - dx, coord_y - dy);
    1.68 +            XY moved_node_2(coord_x, coord_y);
    1.69 +            Node source = mytab.mapstorage.graph.source(ei);
    1.70 +            XY fix_node(mytab.mapstorage.coords[source].x,
    1.71 +                        mytab.mapstorage.coords[source].y);
    1.72 +            XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
    1.73  
    1.74 -            nodesmap[(mytab.mapstorage).graph.source(ei)]->get_bounds(x1, y1, x2, y2);
    1.75 -            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
    1.76 +            XY arrow_pos;
    1.77 +	    if(isbutton==3)
    1.78 +              arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, false);
    1.79 +	    else
    1.80 +              arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, true);
    1.81  
    1.82 -            nodesmap[(mytab.mapstorage).graph.target(ei)]->get_bounds(x1, y1, x2, y2);
    1.83 -            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
    1.84 +            mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
    1.85 +            edgesmap[ei]->draw();
    1.86  
    1.87 -	    if(isbutton==3)
    1.88 -	      {
    1.89 -		edgesmap[ei]->setPoints(coos);
    1.90 -	      }
    1.91 -	    else
    1.92 -	      {
    1.93 -		edgesmap[ei]->setPoints(coos,true);
    1.94 -	      }
    1.95 -
    1.96 -	    xy<double> text_pos=edgesmap[ei]->getArrowPos();
    1.97 -	    text_pos+=(xy<double>(10,10));
    1.98 +	    //reposition of edgetext
    1.99 +	    XY text_pos=mytab.mapstorage.arrow_pos[ei];
   1.100 +	    text_pos+=(XY(10,10));
   1.101  	    edgetextmap[ei]->property_x().set_value(text_pos.x);
   1.102  	    edgetextmap[ei]->property_y().set_value(text_pos.y);
   1.103          }
   1.104 @@ -240,6 +237,58 @@
   1.105    return false;
   1.106  }
   1.107  
   1.108 +XY GraphDisplayerCanvas::calcArrowPos(XY moved_node_1, XY moved_node_2, XY fix_node, XY old_arrow_pos, bool move)
   1.109 +{
   1.110 +  if(!move)
   1.111 +  {
   1.112 +    return XY((moved_node_2.x + fix_node.x) / 2.0, (moved_node_2.y + fix_node.y) / 2.0);
   1.113 +  }
   1.114 +  else
   1.115 +  {
   1.116 +    //////////////////////////////////////////////////////////////////////////////////////////////////////
   1.117 +    /////////// keeps shape-with scalar multiplication - version 2.
   1.118 +    //////////////////////////////////////////////////////////////////////////////////////////////////////
   1.119 +
   1.120 +    //old vector from one to the other node - a
   1.121 +    xy<double> a_v(moved_node_1.x-fix_node.x,moved_node_1.y-fix_node.y);
   1.122 +    //new vector from one to the other node - b
   1.123 +    xy<double> b_v(moved_node_2.x-fix_node.x,moved_node_2.y-fix_node.y);
   1.124 +
   1.125 +    double absa=sqrt(a_v.normSquare());
   1.126 +    double absb=sqrt(b_v.normSquare());
   1.127 +
   1.128 +    if ((absa == 0.0) || (absb == 0.0))
   1.129 +    {
   1.130 +      return old_arrow_pos;
   1.131 +    }
   1.132 +    else
   1.133 +    {
   1.134 +      //old vector from one node to the breakpoint - c
   1.135 +      xy<double> c_v(old_arrow_pos.x-fix_node.x,old_arrow_pos.y-fix_node.y);
   1.136 +
   1.137 +      //unit vector with the same direction to a_v
   1.138 +      xy<double> a_v_u(a_v.x/absa,a_v.y/absa);
   1.139 +
   1.140 +      //normal vector of unit vector with the same direction to a_v
   1.141 +      xy<double> a_v_u_n(((-1)*a_v_u.y),a_v_u.x);
   1.142 +
   1.143 +      //unit vector with the same direction to b_v
   1.144 +      xy<double> b_v_u(b_v.x/absb,b_v.y/absb);
   1.145 +
   1.146 +      //normal vector of unit vector with the same direction to b_v
   1.147 +      xy<double> b_v_u_n(((-1)*b_v_u.y),b_v_u.x);
   1.148 +
   1.149 +      //vector c in a_v_u and a_v_u_n co-ordinate system
   1.150 +      xy<double> c_a(c_v*a_v_u,c_v*a_v_u_n);
   1.151 +
   1.152 +      //new vector from one node to the breakpoint - d - we have to calculate this one
   1.153 +      xy<double> d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n);
   1.154 +
   1.155 +      return XY(d_v.x+fix_node.x,d_v.y+fix_node.y);
   1.156 +    }
   1.157 +  }
   1.158 +}
   1.159 +
   1.160  bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
   1.161  {
   1.162    switch(e->type)
   1.163 @@ -405,7 +454,7 @@
   1.164                coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   1.165  
   1.166                //drawing new edge
   1.167 -              edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos,
   1.168 +              edgesmap[active_edge]=new BrokenEdge(displayed_graph, active_edge,
   1.169                    *this);
   1.170                *(edgesmap[active_edge]) <<
   1.171                  Gnome::Canvas::Properties::fill_color("green");
   1.172 @@ -414,8 +463,8 @@
   1.173                edgesmap[active_edge]->lower_to_bottom();
   1.174  
   1.175                //initializing edge-text as well, to empty string
   1.176 -              xy<double> text_pos=edgesmap[active_edge]->getArrowPos();
   1.177 -              text_pos+=(xy<double>(10,10));
   1.178 +              XY text_pos=mytab.mapstorage.arrow_pos[active_edge];
   1.179 +              text_pos+=(XY(10,10));
   1.180  
   1.181                edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,
   1.182                    text_pos.x, text_pos.y, "");