COIN-OR::LEMON - Graph Library

source: glemon-0.x/broken_edge.cc @ 50:eedecee61922

gui
Last change on this file since 50:eedecee61922 was 50:eedecee61922, checked in by Hegyi Péter, 15 years ago

Tooltip should be turn up on CreateNode? tool.

File size: 11.3 KB
Line 
1#include <broken_edge.h>
2#include <math.h>
3
4BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p, GraphDisplayerCanvas & gc) : Line(g), gdc(gc), isbutton(false)
5{
6  my_points=new Gnome::Art::Point[3];
7
8  arrow=new Gnome::Canvas::Polygon(g);
9  *arrow << Gnome::Canvas::Properties::fill_color("red");
10  arrow->signal_event().connect(sigc::mem_fun(*this, &BrokenEdge::edgeFormerEventHandler));
11  setPoints(p);
12}
13
14BrokenEdge::~BrokenEdge()
15{
16  if(arrow)delete(arrow);
17}
18
19void BrokenEdge::setPoints(Gnome::Canvas::Points p, bool move)
20{
21  bool set_arrow=false;
22  //red arrow losts its position-right button
23  if(!move)
24    {
25      if(p.size()==2)
26        {
27          set_arrow=true;
28          Gnome::Canvas::Points points_with_center;
29          points_with_center.push_back(my_points[0]=p[0]);
30          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 );
31          points_with_center.push_back(my_points[2]=p[1]);
32          property_points().set_value(points_with_center);
33        } 
34      if(p.size()==3)
35        {
36          set_arrow=true;
37          property_points().set_value(p);
38          for(int i=0;i<3;i++)
39            {
40              my_points[i]=p[i];
41            }
42        }
43    }
44  else
45    {
46      //arrow keeps its position-left button
47
48      //       if(p.size()==2)
49      //        {
50      //          Gnome::Canvas::Points points;
51      //          my_points[0]=p[0];
52      //          my_points[2]=p[1];
53      //          for(int i=0;i<3;i++)
54      //            {
55      //              points.push_back(my_points[i]);
56      //            }
57      //          property_points().set_value(points);
58      //        }
59
60      //////////////////////////////////////////////////////////////////////////////////////////////////////
61      /////////// kepps shape-with angles
62      //////////////////////////////////////////////////////////////////////////////////////////////////////
63
64
65//       //old vector from one to the other node
66//       xy<double> o_p2p(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
67//       //projection of the old vector to positive x axis
68//       xy<double> o_x_p2p(fabs(o_p2p.x),0);
69//       //length of p2p vector
70//       double o_l_p2p=sqrt( o_p2p.normSquare() );
71//       if(o_p2p.x<0)
72//      {
73//        o_l_p2p*=-1;
74//      }
75//       //length of projection of p2p vector
76//       double o_l_x_p2p=sqrt( o_x_p2p.normSquare() );
77//       //old angle of p2p vector to the x axis
78//       double o_a_p2p=acos(o_l_x_p2p/o_l_p2p);
79//       if(o_p2p.y>0)
80//      {
81//        o_a_p2p=2*M_PI-o_a_p2p;
82//      }
83
84//       //old vector from first node to the breakpoint
85//       xy<double> o_1b((my_points[1].get_x()-my_points[0].get_x()),(my_points[1].get_y()-my_points[0].get_y()));
86//       //projection of the old node-breakpoint vector to positive x axis
87//       xy<double> o_x_1b(fabs(o_1b.x),0);
88//       //length of 1b vector
89//       double o_l_1b=sqrt( o_1b.normSquare() );
90//       if(o_1b.x<0)
91//      {
92//        o_l_1b*=-1;
93//      }
94//       //length of projection of 1b vector
95//       double o_l_x_1b=sqrt( o_x_1b.normSquare() );
96//       //old angle of 1b vector to the x axis
97//       double o_a_1b=acos(o_l_x_1b/o_l_1b);
98//       if(o_1b.y>0)
99//      {
100//        o_a_1b=2*M_PI-o_a_1b;
101//      }
102
103//       if(p.size()==2)
104//              {
105//        set_arrow=true;
106
107//                my_points[0]=p[0];
108//                my_points[2]=p[1];
109
110//        //new vector from one to the other node
111//        xy<double> n_p2p(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
112//        //projection of the new vector to positive x axis
113//        xy<double> n_x_p2p(fabs(n_p2p.x),0);
114//        //length of p2p vector
115//        double n_l_p2p=sqrt( n_p2p.normSquare() );
116//        if(n_p2p.x<0)
117//          {
118//            n_l_p2p*=-1;
119//          }
120//        //length of projection of p2p vector
121//        double n_l_x_p2p=sqrt( n_x_p2p.normSquare() );
122//        //new angle of p2p vector to the x axis
123//        double n_a_p2p=acos(n_l_x_p2p/n_l_p2p);
124//        if(n_p2p.y>0)
125//          {
126//            n_a_p2p=2*M_PI-n_a_p2p;
127//          }
128
129//        //new angle of 1b vector to the x axis
130//        double n_a_1b=o_a_1b+n_a_p2p-o_a_p2p;
131
132//        std::cout << " p2p regi: " << o_a_p2p/M_PI*180 << " uj: " << n_a_p2p/M_PI*180-(int)n_a_p2p/M_PI*180 << std::endl;
133//        std::cout << " 1b regi: " << o_a_1b/M_PI*180 << " uj: " << n_a_1b/M_PI*180-(int)n_a_1b/M_PI*180 << std::endl;
134
135// //     std::cout << o_p2p << " " << n_p2p << std::endl;
136
137//        if((n_a_1b>M_PI*3/2)||(n_a_1b<M_PI/2))
138//          {
139//            std::cout << "jobb terfel" << std::endl;
140//            my_points[1]=Gnome::Art::Point(p[0].get_x()+cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()-sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
141//          }
142//        else if((n_a_1b<M_PI*3/2)&&(n_a_1b>M_PI/2))
143//          {
144//            std::cout << "bal terfel" << std::endl;
145//            my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()+sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
146//          }
147//        else
148//          {
149//            std::cout << "y tengely" << std::endl;
150//            double new_y=my_points[1].get_y();
151//            my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,new_y);
152//          }
153
154//        std::cout << "alap: " << p[0] << " eredeti hossz: " << o_l_1b << " nagy uj: " << n_l_p2p << " nagy regi: " << o_l_p2p << " a*b/c " << o_l_1b*n_l_p2p/o_l_p2p << " eredmeny: " << my_points[1] << std::endl;
155
156
157// //     if(o_1b.x*o_1b.y>0)
158// //       {
159// //         if(n_p2p.x>0)
160// //           {
161// //             my_points[1]=Gnome::Art::Point(p[0].get_x()+cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()+sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
162// //           }
163// //         else
164// //           {
165// //             my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()-sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
166// //           }
167// //       }
168// //     else if(o_1b.x*o_1b.y<0)
169// //       {
170// //         if(n_p2p.x>0)
171// //           {
172// //             my_points[1]=Gnome::Art::Point(p[0].get_x()+cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()-sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
173// //           }
174// //         else
175// //           {
176// //             my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()+sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
177// //           }
178// //       }
179// //     else
180// //       {
181// //       }
182
183      //////////////////////////////////////////////////////////////////////////////////////////////////////
184      /////////// kepps shape-with scalar multiplication
185      //////////////////////////////////////////////////////////////////////////////////////////////////////
186
187      if(p.size()==2)
188        {
189          //old vector from one to the other node - a
190          xy<double> a_v(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
191          //new vector from one to the other node - b
192          xy<double> b_v(p[1].get_x()-p[0].get_x(),p[1].get_y()-p[0].get_y());
193
194          //old vector from one node to the breakpoint - c
195          xy<double> c_v(my_points[1].get_x()-my_points[0].get_x(),my_points[1].get_y()-my_points[0].get_y());
196
197          //new vector from one node to the breakpoint - d - we have to calculate this one
198          xy<double> d_v;
199
200          //scalar product of a and b (old and new vector from first point to the other)
201          double sab=a_v*b_v;
202          //scalar product of c and d (old and new vector from first point to breakpoint)
203          double scd=sab*c_v.normSquare()/a_v.normSquare();
204
205          std::cout<<" a " << a_v<<" b " <<b_v<<" c " <<c_v<<" sab " <<sab<<" scd "<<scd<<std::endl;
206
207          double a=c_v.normSquare();
208          double b=2*scd*c_v.y;
209          double c=scd*scd-b_v.normSquare()/a_v.normSquare()*c_v.normSquare()*c_v.x*c_v.x;
210
211          std::cout<<" a " << a<<" b " <<b<<" c " <<c<<std::endl;
212
213          d_v.y=(-b-sqrt(b*b-4*a*c))/2/a;
214
215          if(c_v.x!=0)
216            {
217              d_v.x=(scd-c_v.y*d_v.y)/c_v.x;
218            }
219          else
220            {
221              d_v.x=my_points[1].get_x();
222            }
223
224          std::cout<<" d " << d_v<<std::endl;
225
226          my_points[1]=Gnome::Art::Point(d_v.x+p[0].get_x(),d_v.y+p[0].get_y());
227
228          Gnome::Canvas::Points points;
229          for(int i=0;i<3;i++)
230            {
231              points.push_back(my_points[i]);
232            }
233          property_points().set_value(points);
234        }
235    }
236  if(set_arrow)
237    {
238      //calculating coordinates of the direction indicator arrow
239
240      xy<gdouble> target( my_points[2].get_x(), my_points[2].get_y() );
241      xy<gdouble> center( my_points[1].get_x(), my_points[1].get_y() );
242
243      xy<gdouble> unit_vector_in_dir(target-center);
244      //       std::cout << target << " - " << center << " = " << unit_vector_in_dir << "    / " <<unit_vector_in_dir.normSquare() ;
245      unit_vector_in_dir/=sqrt( unit_vector_in_dir.normSquare() );
246      //       std::cout << " = " << unit_vector_in_dir << std::endl;
247
248      xy<gdouble> unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
249      //       std::cout << unit_norm_vector << std::endl;
250
251      {     
252        //       /\       // top
253        //      /  \      //
254        //      -  -      // c(enter)l(eft), ccl, ccr, cr
255        //       ||       //
256        //       ||       // b(ottom)l, br
257      }
258
259      double size=3;
260
261      xy<gdouble> bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
262      xy<gdouble> br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
263      xy<gdouble> ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
264      xy<gdouble> ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
265      xy<gdouble> cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
266      xy<gdouble> cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
267      xy<gdouble> top(center + unit_vector_in_dir * 3 * size);
268         
269      //       std::cout << bl << " " << br << " " << ccl << " "  << ccr << " " << cl << " " << cr << " " << top << std::endl;
270
271      Gnome::Canvas::Points arrow_points;
272      arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
273      arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
274      arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
275      arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
276      arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
277      arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
278      arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
279
280      arrow->property_points().set_value(arrow_points);
281    }
282}
283
284bool BrokenEdge::edgeFormerEventHandler(GdkEvent* e)
285{
286  switch(e->type)
287    {
288    case GDK_BUTTON_PRESS:
289      //we mark the location of the event to be able to calculate parameters of dragging
290      if(gdc.getActualTool()!=CREATE_NODE)
291        {
292          gdc.toggleEdgeActivity(this, true);
293          clicked_x=e->button.x;
294          clicked_y=e->button.y;
295          isbutton=true;
296        }
297      break;
298    case GDK_BUTTON_RELEASE:
299      if(gdc.getActualTool()!=CREATE_NODE)
300        {
301          gdc.toggleEdgeActivity(this, false);
302          isbutton=false;
303        }
304      break;
305    case GDK_MOTION_NOTIFY:
306      //we only have to do sg. if the mouse button is pressed
307      if(isbutton)
308        {
309          //new coordinates will be the old values,
310          //because the item will be moved to the
311          //new coordinate therefore the new movement
312          //has to be calculated from here
313
314          double dx=e->motion.x-clicked_x;
315          double dy=e->motion.y-clicked_y;
316
317          Gnome::Canvas::Points points_new;
318
319          points_new.push_back(my_points[0]);
320          points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy));
321          points_new.push_back(my_points[2]);
322
323          setPoints(points_new);
324          gdc.textReposition(xy<double>(my_points[1].get_x(),my_points[1].get_y()));
325
326          clicked_x=e->motion.x;
327          clicked_y=e->motion.y;
328
329        }
330    default: break;
331    }
332
333  return true;
334}
335
336xy<double> BrokenEdge::getArrowPos()
337{
338  xy<double> ret_val(my_points[1].get_x(),my_points[1].get_y());
339  return ret_val;
340}
Note: See TracBrowser for help on using the repository browser.