COIN-OR::LEMON - Graph Library

source: glemon-0.x/broken_edge.cc @ 63:59768817442a

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