graph_displayer_canvas-event.cc
branchgui
changeset 98 f60f89147531
parent 96 e664d8aa3f72
child 109 9f8dc346ac6e
equal deleted inserted replaced
32:c7dde3eca8ca 33:5656c4e559d7
   145 
   145 
   146         // the new coordinates of the centre of the node 
   146         // the new coordinates of the centre of the node 
   147         double coord_x = new_x - (clicked_x - (mytab.mapstorage).coords[active_node].x);
   147         double coord_x = new_x - (clicked_x - (mytab.mapstorage).coords[active_node].x);
   148         double coord_y = new_y - (clicked_y - (mytab.mapstorage).coords[active_node].y);
   148         double coord_y = new_y - (clicked_y - (mytab.mapstorage).coords[active_node].y);
   149 
   149 
       
   150         // write back the new coordinates to the coords map
       
   151         (mytab.mapstorage).coords.set(active_node, xy<double>(coord_x, coord_y));
       
   152 
   150         clicked_x=new_x;
   153         clicked_x=new_x;
   151         clicked_y=new_y;
   154         clicked_y=new_y;
   152 
       
   153         // write back the new coordinates to the coords map
       
   154         (mytab.mapstorage).coords.set(active_node, xy<double>(coord_x, coord_y));
       
   155 
   155 
   156         // reposition the coordinates text
   156         // reposition the coordinates text
   157         std::ostringstream ostr;
   157         std::ostringstream ostr;
   158         ostr << "(" <<
   158         ostr << "(" <<
   159           (mytab.mapstorage).coords[active_node].x << ", " <<
   159           (mytab.mapstorage).coords[active_node].x << ", " <<
   181         }
   181         }
   182 
   182 
   183 	//all the edges connected to the moved point has to be redrawn
   183 	//all the edges connected to the moved point has to be redrawn
   184         for(OutEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei)
   184         for(OutEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei)
   185         {
   185         {
   186             Gnome::Canvas::Points coos;
   186             XY moved_node_1(coord_x - dx, coord_y - dy);
   187             double x1, x2, y1, y2;
   187             XY moved_node_2(coord_x, coord_y);
   188 
   188             Node target = mytab.mapstorage.graph.target(ei);
   189             nodesmap[(mytab.mapstorage).graph.source(ei)]->get_bounds(x1, y1, x2, y2);
   189             XY fix_node(mytab.mapstorage.coords[target].x,
   190             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   190                         mytab.mapstorage.coords[target].y);
   191 
   191             XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
   192             nodesmap[(mytab.mapstorage).graph.target(ei)]->get_bounds(x1, y1, x2, y2);
   192 
   193             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   193             XY arrow_pos;
   194 
       
   195 	    if(isbutton==3)
   194 	    if(isbutton==3)
   196 	      {
   195               arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, false);
   197 		edgesmap[ei]->setPoints(coos);
       
   198 	      }
       
   199 	    else
   196 	    else
   200 	      {
   197               arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, true);
   201 		edgesmap[ei]->setPoints(coos,true);
   198 
   202 	      }
   199             mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
       
   200             edgesmap[ei]->draw();
   203 
   201 
   204 	    //reposition of edgetext
   202 	    //reposition of edgetext
   205 	    xy<double> text_pos=edgesmap[ei]->getArrowPos();
   203 	    XY text_pos=mytab.mapstorage.arrow_pos[ei];
   206 	    text_pos+=(xy<double>(10,10));
   204 	    text_pos+=(XY(10,10));
   207 	    edgetextmap[ei]->property_x().set_value(text_pos.x);
   205 	    edgetextmap[ei]->property_x().set_value(text_pos.x);
   208 	    edgetextmap[ei]->property_y().set_value(text_pos.y);
   206 	    edgetextmap[ei]->property_y().set_value(text_pos.y);
   209         }
   207         }
   210 
   208 
   211         for(InEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei)
   209         for(InEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei)
   212         {
   210         {
   213             Gnome::Canvas::Points coos;
   211             XY moved_node_1(coord_x - dx, coord_y - dy);
   214             double x1, x2, y1, y2;
   212             XY moved_node_2(coord_x, coord_y);
   215 
   213             Node source = mytab.mapstorage.graph.source(ei);
   216             nodesmap[(mytab.mapstorage).graph.source(ei)]->get_bounds(x1, y1, x2, y2);
   214             XY fix_node(mytab.mapstorage.coords[source].x,
   217             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   215                         mytab.mapstorage.coords[source].y);
   218 
   216             XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
   219             nodesmap[(mytab.mapstorage).graph.target(ei)]->get_bounds(x1, y1, x2, y2);
   217 
   220             coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   218             XY arrow_pos;
   221 
       
   222 	    if(isbutton==3)
   219 	    if(isbutton==3)
   223 	      {
   220               arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, false);
   224 		edgesmap[ei]->setPoints(coos);
       
   225 	      }
       
   226 	    else
   221 	    else
   227 	      {
   222               arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, true);
   228 		edgesmap[ei]->setPoints(coos,true);
   223 
   229 	      }
   224             mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
   230 
   225             edgesmap[ei]->draw();
   231 	    xy<double> text_pos=edgesmap[ei]->getArrowPos();
   226 
   232 	    text_pos+=(xy<double>(10,10));
   227 	    //reposition of edgetext
       
   228 	    XY text_pos=mytab.mapstorage.arrow_pos[ei];
       
   229 	    text_pos+=(XY(10,10));
   233 	    edgetextmap[ei]->property_x().set_value(text_pos.x);
   230 	    edgetextmap[ei]->property_x().set_value(text_pos.x);
   234 	    edgetextmap[ei]->property_y().set_value(text_pos.y);
   231 	    edgetextmap[ei]->property_y().set_value(text_pos.y);
   235         }
   232         }
   236       }
   233       }
   237     default: break;
   234     default: break;
   238   }
   235   }
   239 
   236 
   240   return false;
   237   return false;
       
   238 }
       
   239 
       
   240 XY GraphDisplayerCanvas::calcArrowPos(XY moved_node_1, XY moved_node_2, XY fix_node, XY old_arrow_pos, bool move)
       
   241 {
       
   242   if(!move)
       
   243   {
       
   244     return XY((moved_node_2.x + fix_node.x) / 2.0, (moved_node_2.y + fix_node.y) / 2.0);
       
   245   }
       
   246   else
       
   247   {
       
   248     //////////////////////////////////////////////////////////////////////////////////////////////////////
       
   249     /////////// keeps shape-with scalar multiplication - version 2.
       
   250     //////////////////////////////////////////////////////////////////////////////////////////////////////
       
   251 
       
   252     //old vector from one to the other node - a
       
   253     xy<double> a_v(moved_node_1.x-fix_node.x,moved_node_1.y-fix_node.y);
       
   254     //new vector from one to the other node - b
       
   255     xy<double> b_v(moved_node_2.x-fix_node.x,moved_node_2.y-fix_node.y);
       
   256 
       
   257     double absa=sqrt(a_v.normSquare());
       
   258     double absb=sqrt(b_v.normSquare());
       
   259 
       
   260     if ((absa == 0.0) || (absb == 0.0))
       
   261     {
       
   262       return old_arrow_pos;
       
   263     }
       
   264     else
       
   265     {
       
   266       //old vector from one node to the breakpoint - c
       
   267       xy<double> c_v(old_arrow_pos.x-fix_node.x,old_arrow_pos.y-fix_node.y);
       
   268 
       
   269       //unit vector with the same direction to a_v
       
   270       xy<double> a_v_u(a_v.x/absa,a_v.y/absa);
       
   271 
       
   272       //normal vector of unit vector with the same direction to a_v
       
   273       xy<double> a_v_u_n(((-1)*a_v_u.y),a_v_u.x);
       
   274 
       
   275       //unit vector with the same direction to b_v
       
   276       xy<double> b_v_u(b_v.x/absb,b_v.y/absb);
       
   277 
       
   278       //normal vector of unit vector with the same direction to b_v
       
   279       xy<double> b_v_u_n(((-1)*b_v_u.y),b_v_u.x);
       
   280 
       
   281       //vector c in a_v_u and a_v_u_n co-ordinate system
       
   282       xy<double> c_a(c_v*a_v_u,c_v*a_v_u_n);
       
   283 
       
   284       //new vector from one node to the breakpoint - d - we have to calculate this one
       
   285       xy<double> d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n);
       
   286 
       
   287       return XY(d_v.x+fix_node.x,d_v.y+fix_node.y);
       
   288     }
       
   289   }
   241 }
   290 }
   242 
   291 
   243 bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
   292 bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
   244 {
   293 {
   245   switch(e->type)
   294   switch(e->type)
   403 
   452 
   404               target_item->get_bounds(x1, y1, x2, y2);
   453               target_item->get_bounds(x1, y1, x2, y2);
   405               coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   454               coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   406 
   455 
   407               //drawing new edge
   456               //drawing new edge
   408               edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos,
   457               edgesmap[active_edge]=new BrokenEdge(displayed_graph, active_edge,
   409                   *this);
   458                   *this);
   410               *(edgesmap[active_edge]) <<
   459               *(edgesmap[active_edge]) <<
   411                 Gnome::Canvas::Properties::fill_color("green");
   460                 Gnome::Canvas::Properties::fill_color("green");
   412               edgesmap[active_edge]->property_width_pixels().set_value(10);
   461               edgesmap[active_edge]->property_width_pixels().set_value(10);
   413 
   462 
   414               edgesmap[active_edge]->lower_to_bottom();
   463               edgesmap[active_edge]->lower_to_bottom();
   415 
   464 
   416               //initializing edge-text as well, to empty string
   465               //initializing edge-text as well, to empty string
   417               xy<double> text_pos=edgesmap[active_edge]->getArrowPos();
   466               XY text_pos=mytab.mapstorage.arrow_pos[active_edge];
   418               text_pos+=(xy<double>(10,10));
   467               text_pos+=(XY(10,10));
   419 
   468 
   420               edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,
   469               edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,
   421                   text_pos.x, text_pos.y, "");
   470                   text_pos.x, text_pos.y, "");
   422               edgetextmap[active_edge]->property_fill_color().set_value(
   471               edgetextmap[active_edge]->property_fill_color().set_value(
   423                   "darkgreen");
   472                   "darkgreen");