COIN-OR::LEMON - Graph Library

source: lemon-0.x/gui/graph_displayer_canvas.cc @ 1499:9316dcc0a355

Last change on this file since 1499:9316dcc0a355 was 1499:9316dcc0a355, checked in by Hegyi Péter, 15 years ago

Little red arrows appear in breakpoints of edges.

  • Property exe set to *
File size: 18.6 KB
Line 
1#include <graph_displayer_canvas.h>
2#include <broken_edge.h>
3#include <math.h>
4
5GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm, MapStorage & ms):g(gr),nodesmap(g),edgesmap(g),edgetextmap(g),displayed_graph(*(root()), 0, 0),mapstorage(ms),isbutton(false),active_item(NULL),target_item(NULL)
6{
7 
8  actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
9
10  active_node=INVALID;
11  active_edge=INVALID;
12
13  //set_center_scroll_region(true);
14
15  //first edges are drawn, to hide joining with nodes later
16
17  for (EdgeIt i(g); i!=INVALID; ++i)
18  {
19
20    //drawing green lines, coordinates are from cm
21
22    Gnome::Canvas::Points coos;
23    coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
24    coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
25   
26    edgesmap[i]=new BrokenEdge(displayed_graph, coos);
27    *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
28    edgesmap[i]->property_width_pixels().set_value(10);   
29   
30    //initializing edge-text as well, to empty string
31
32    double x1, x2, y1, y2;
33    edgesmap[i]->get_bounds(x1, y1, x2, y2);
34   
35    edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
36    edgetextmap[i]->property_fill_color().set_value("black");
37  }
38
39  //afterwards nodes come to be drawn
40
41  NodeIt i(g);
42  int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
43
44  for (; i!=INVALID; ++i)
45  {
46    //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
47
48    if(cm[i].x>maxx)maxx=(int)cm[i].x;
49    if(cm[i].y>maxy)maxy=(int)cm[i].y;
50    if(cm[i].x<minx)minx=(int)cm[i].x;
51    if(cm[i].y<miny)miny=(int)cm[i].y;
52
53    //drawing bule nodes, with black line around them
54
55    nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
56    *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
57    *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
58    //!!!!!!! (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
59  }
60
61  updateScrollRegion();
62}
63
64GraphDisplayerCanvas::~GraphDisplayerCanvas()
65{
66
67  //writing out the end state of the graph
68  //\todo all the maps has to be write out!
69
70  Graph::NodeMap <int> id(g);
71  Graph::NodeMap <double> xc(g);
72  Graph::NodeMap <double> yc(g);
73 
74  int j=1;
75 
76  for (NodeIt i(g); i!=INVALID; ++i)
77  {
78    double x1,y1,x2,y2;
79    nodesmap[i]->get_bounds(x1, y1, x2, y2);
80   
81    id[i]=j++;
82    xc[i]=(x1+x2)/2;
83    yc[i]=(y1+y2)/2;
84  }
85
86  GraphWriter<Graph> writer(std::cout,g);
87 
88  writer.writeNodeMap("id", id);
89  writer.writeNodeMap("coordinates_x", xc);
90  writer.writeNodeMap("coordinates_y", yc);
91  writer.run();
92}
93
94int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
95{
96  for (EdgeIt i(g); i!=INVALID; ++i)
97  {
98    int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
99    edgesmap[i]->property_width_pixels().set_value(w);
100  }
101  return 0;
102};
103
104int GraphDisplayerCanvas::changeColor (std::string mapname)
105
106
107  //function maps the range of the maximum and
108  //the minimum of the nodemap to the range of
109  //green in RGB
110
111  for (EdgeIt i(g); i!=INVALID; ++i)
112  {
113    double w=(*(mapstorage.edgemap_storage)[mapname])[i];
114    double max=mapstorage.maxOfEdgeMap(mapname);
115    double min=mapstorage.minOfEdgeMap(mapname);
116     
117    //std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
118    Gdk::Color color;
119    if(max!=min)
120    {
121      color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
122    }
123    else
124    {
125      color.set_rgb_p (0, 100, 0);
126    }
127
128    edgesmap[i]->property_fill_color_gdk().set_value(color);
129  }
130  return 0;
131};
132
133int GraphDisplayerCanvas::changeText (std::string mapname)
134{
135
136  //the number in the map will be written on the edge
137  //EXCEPT when the name of the map is Text, because
138  //in that case empty string will be written, because
139  //that is the deleter map
140  //\todo isn't it a bit woodcutter?
141
142  for (EdgeIt i(g); i!=INVALID; ++i)
143  {
144    if(mapname!="Text")
145    {
146      double number=(*(mapstorage.edgemap_storage)[mapname])[i];
147      int length=(int)(floor(log(number)/log(10)))+1;
148      int maxpos=(int)(pow(10,length-1));
149      int strl=length+1+RANGE;
150      char * str=new char[strl];
151      str[length]='.';
152      str[strl]='\0';
153     
154      for(int j=0;j<strl;j++)
155      {
156        if(j!=length)
157        {
158          int digit=(int)(number/maxpos);
159          str[j]=(digit+'0');
160          number-=digit*maxpos;
161          number*=10;
162        }
163      }
164     
165      edgetextmap[i]->property_text().set_value(str);
166    }
167    else
168    {
169      edgetextmap[i]->property_text().set_value("");
170    }
171  }
172  return 0;
173};
174
175bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
176{
177  switch(e->type)
178  {
179    case GDK_BUTTON_PRESS:
180      //we mark the location of the event to be able to calculate parameters of dragging
181      clicked_x=e->button.x;
182      clicked_y=e->button.y;
183      active_item=(get_item_at(e->button.x, e->button.y));
184      isbutton=true;
185      break;
186    case GDK_BUTTON_RELEASE:
187      isbutton=false;
188      active_item=NULL;
189      updateScrollRegion();
190      break;
191    case GDK_MOTION_NOTIFY:
192      //we only have to do sg. if the mouse button is pressed
193      if(isbutton)
194      {
195        //new coordinates will be the old values,
196        //because the item will be moved to the
197        //new coordinate therefore the new movement
198        //has to be calculated from here
199
200        double dx=e->motion.x-clicked_x;
201        double dy=e->motion.y-clicked_y;
202        active_item->move(dx, dy);
203        clicked_x=e->motion.x;
204        clicked_y=e->motion.y;
205
206        //all the edges connected to the moved point has to be redrawn
207
208        EdgeIt e;
209        g.firstOut(e,n);
210        for(;e!=INVALID;g.nextOut(e))
211        {
212            Gnome::Canvas::Points coos;
213            double x1, x2, y1, y2;
214
215            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
216            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
217
218            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
219            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
220
221            edgesmap[e]->property_points().set_value(coos);
222
223            edgesmap[e]->get_bounds(x1, y1, x2, y2);
224
225            edgetextmap[e]->property_x().set_value((x1+x2)/2);
226            edgetextmap[e]->property_y().set_value((y1+y2)/2);
227        }
228
229        g.firstIn(e,n);
230        for(;e!=INVALID;g.nextIn(e))
231        {
232            Gnome::Canvas::Points coos;
233            double x1, x2, y1, y2;
234
235            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
236            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
237
238            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
239            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
240
241            edgesmap[e]->property_points().set_value(coos);
242
243            edgesmap[e]->get_bounds(x1, y1, x2, y2);
244
245            edgetextmap[e]->property_x().set_value((x1+x2)/2);
246            edgetextmap[e]->property_y().set_value((y1+y2)/2);
247        }
248      }
249    default: break;
250  }
251  return true;
252}
253
254bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
255{
256  Gnome::Canvas::CanvasAA::on_expose_event(event);
257  //usleep(10000);
258  //rezoom();
259  return true;
260}
261
262void GraphDisplayerCanvas::zoomIn()
263{
264  set_pixels_per_unit(
265      (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
266}
267
268void GraphDisplayerCanvas::zoomOut()
269{
270  set_pixels_per_unit(
271      (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
272}
273
274void GraphDisplayerCanvas::zoomFit()
275{
276  // get the height and width of the canvas
277  Gtk::Allocation a = get_allocation();
278  int aw = a.get_width();
279  int ah = a.get_height();
280  // add some space
281  aw -= 5; if (aw < 0) aw = 0;
282  ah -= 5; if (ah < 0) ah = 0;
283
284  // get the bounding box of the graph
285  double wx1, wy1, wx2, wy2;
286  Gnome::Canvas::Item* pCanvasItem = root();
287  pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
288
289  // fit the graph to the window
290  double ppu1 = (double) aw / fabs(wx2 - wx1);
291  double ppu2 = (double) ah / fabs(wy2 - wy1);
292  set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2);
293}
294
295void GraphDisplayerCanvas::zoom100()
296{
297  set_pixels_per_unit(1.0);
298}
299
300void GraphDisplayerCanvas::updateScrollRegion()
301{
302  double wx1, wy1, wx2, wy2;
303  Gnome::Canvas::Item* pCanvasItem = root();
304  pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
305  set_scroll_region(wx1, wy1, wx2, wy2);
306}
307
308void GraphDisplayerCanvas::changeEditorialTool(int newtool)
309{
310  actual_handler.disconnect();
311
312  switch(newtool)
313    {
314    case MOVE:
315      actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false);
316      break;
317
318      //it has to assigned to canvas, because all the canvas has to be monitored, not only the elements of the already drawn group
319    case CREATE_NODE:
320      actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
321      break;
322
323    case CREATE_EDGE:
324      actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
325      break;
326
327    case ERASER:
328      actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraser_event_handler), false);
329      break;
330
331    default:
332      break;
333    }
334}
335
336bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e)
337{
338  switch(e->type)
339  {
340    case GDK_BUTTON_PRESS:
341      //we mark the location of the event to be able to calculate parameters of dragging
342      clicked_x=e->button.x;
343      clicked_y=e->button.y;
344      active_item=(get_item_at(e->button.x, e->button.y));
345      active_node=INVALID;
346      for (NodeIt i(g); i!=INVALID; ++i)
347        {
348          if(nodesmap[i]==active_item)
349            {
350              active_node=i;
351            }
352        }
353      isbutton=true;
354      break;
355    case GDK_BUTTON_RELEASE:
356      isbutton=false;
357      active_item=NULL;
358      active_node=INVALID;
359      updateScrollRegion();
360      break;
361    case GDK_MOTION_NOTIFY:
362      //we only have to do sg. if the mouse button is pressed AND the click was on a node that was found in the set of nodes
363      if(active_node!=INVALID)
364      {
365        //new coordinates will be the old values,
366        //because the item will be moved to the
367        //new coordinate therefore the new movement
368        //has to be calculated from here
369
370        double dx=e->motion.x-clicked_x;
371        double dy=e->motion.y-clicked_y;
372
373        active_item->move(dx, dy);
374
375        clicked_x=e->motion.x;
376        clicked_y=e->motion.y;
377
378        //all the edges connected to the moved point has to be redrawn
379        EdgeIt e;
380
381        g.firstOut(e,active_node);
382
383        for(;e!=INVALID;g.nextOut(e))
384        {
385            Gnome::Canvas::Points coos;
386            double x1, x2, y1, y2;
387
388            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
389            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
390
391            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
392            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
393
394            edgesmap[e]->set_points(coos);
395
396            edgesmap[e]->get_bounds(x1, y1, x2, y2);
397
398            edgetextmap[e]->property_x().set_value((x1+x2)/2);
399            edgetextmap[e]->property_y().set_value((y1+y2)/2);
400        }
401
402        g.firstIn(e,active_node);
403        for(;e!=INVALID;g.nextIn(e))
404        {
405            Gnome::Canvas::Points coos;
406            double x1, x2, y1, y2;
407
408            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
409            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
410
411            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
412            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
413
414            edgesmap[e]->set_points(coos);
415
416            edgesmap[e]->get_bounds(x1, y1, x2, y2);
417
418            edgetextmap[e]->property_x().set_value((x1+x2)/2);
419            edgetextmap[e]->property_y().set_value((y1+y2)/2);
420        }
421      }
422    default: break;
423  }
424
425  return true;
426}
427
428bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e)
429{
430  switch(e->type)
431    {
432
433      //draw the new node in red at the clicked place
434    case GDK_BUTTON_PRESS:
435      isbutton=true;
436
437      active_node=NodeIt(g,g.addNode());
438
439      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
440
441      nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
442      active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
443      *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
444      *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
445      (nodesmap[active_node])->show();
446      break;
447
448      //move the new node
449    case GDK_MOTION_NOTIFY:
450      {
451        double world_motion_x, world_motion_y;
452        GdkEvent * generated=new GdkEvent();
453        window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y);
454        generated->motion.x=world_motion_x;
455        generated->motion.y=world_motion_y;
456        generated->type=GDK_MOTION_NOTIFY;
457        move_event_handler(generated);     
458        break;
459      }
460
461      //finalize the new node
462    case GDK_BUTTON_RELEASE:
463      isbutton=false;
464      *active_item << Gnome::Canvas::Properties::fill_color("blue");
465      active_item=NULL;
466      active_node=INVALID;
467      updateScrollRegion();
468      break;
469    default:
470      break;
471    }
472  return false;
473}
474
475bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e)
476{
477  switch(e->type)
478    {
479    case GDK_BUTTON_PRESS:
480      //in edge creatino right button has special meaning
481      if(e->button.button!=3)
482        {
483          //there is not yet selected node
484          if(active_node==INVALID)
485            {
486              //we mark the location of the event to be able to calculate parameters of dragging
487              clicked_x=e->button.x;
488              clicked_y=e->button.y;
489              active_item=(get_item_at(e->button.x, e->button.y));
490              active_node=INVALID;
491              for (NodeIt i(g); i!=INVALID; ++i)
492                {
493                  if(nodesmap[i]==active_item)
494                    {
495                      active_node=i;
496                    }
497                }
498              //the clicked item is really a node
499              if(active_node!=INVALID)
500                {
501                  *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
502                  isbutton=true;
503                }
504              //clicked item was not a node. It could be e.g. edge.
505              else
506                {
507                  active_item=NULL;
508                }
509            }
510          //we only have to do sg. if the mouse button
511          // is pressed already once AND the click was
512          // on a node that was found in the set of
513          //nodes, and now we only search for the second
514          //node
515          else
516            {
517              target_item=(get_item_at(e->button.x, e->button.y));
518              Graph::NodeIt target_node=INVALID;
519              for (NodeIt i(g); i!=INVALID; ++i)
520                {
521                  if(nodesmap[i]==target_item)
522                    {
523                      target_node=i;
524                    }
525                }
526              //the clicked item is a node, the edge can be drawn
527              if(target_node!=INVALID)
528                {
529                  *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
530
531                  //creating new edge
532                  active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
533         
534                  //calculating coordinates of new edge
535                  Gnome::Canvas::Points coos;
536                  double x1, x2, y1, y2;
537         
538                  active_item->get_bounds(x1, y1, x2, y2);
539                  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
540
541                  target_item->get_bounds(x1, y1, x2, y2);
542                  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
543
544                  //drawing new edge
545                  edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos);
546                  *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
547                  edgesmap[active_edge]->property_width_pixels().set_value(10);
548
549                  //redraw nodes to blank terminations of the new edge
550                  target_item->raise_to_top();
551                  active_item->raise_to_top();
552
553                  //initializing edge-text as well, to empty string
554                  edgesmap[active_edge]->get_bounds(x1, y1, x2, y2);
555                  edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
556                  edgetextmap[active_edge]->property_fill_color().set_value("black");
557                }
558              //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
559              else
560                {
561                  target_item=NULL;
562                }
563            }
564        }
565      break;
566    case GDK_BUTTON_RELEASE:
567      isbutton=false;
568      //we clear settings in two cases
569      //1: the edge is ready (target_item has valid value)
570      //2: the edge creation is cancelled with right button
571      if((target_item)||(e->button.button==3))
572        {
573          if(active_item)
574            {
575              *active_item << Gnome::Canvas::Properties::fill_color("blue");
576              active_item=NULL;
577            }
578          if(target_item)
579            {
580              *target_item << Gnome::Canvas::Properties::fill_color("blue");
581              target_item=NULL;
582            }
583          active_node=INVALID;
584          active_edge=INVALID;
585        }
586      break;
587    default:
588      break;
589    }
590  return false;
591}
592
593bool GraphDisplayerCanvas::eraser_event_handler(GdkEvent* e)
594{
595  switch(e->type)
596    {
597    case GDK_BUTTON_PRESS:
598      active_item=(get_item_at(e->button.x, e->button.y));
599      active_node=INVALID;
600      active_edge=INVALID;
601      for (NodeIt i(g); i!=INVALID; ++i)
602        {
603          if(nodesmap[i]==active_item)
604            {
605              active_node=i;
606            }
607        }
608      if(active_node==INVALID)
609        {
610          for (EdgeIt i(g); i!=INVALID; ++i)
611            {
612              if(edgesmap[i]==active_item)
613                {
614                  active_edge=i;
615                }
616            }
617        }
618    *active_item << Gnome::Canvas::Properties::fill_color("red");
619      break;
620
621    case GDK_BUTTON_RELEASE:
622      if(active_item==(get_item_at(e->button.x, e->button.y)))
623        {
624          if(active_node!=INVALID)
625            {
626
627              //collecting edges to delete
628              EdgeIt e;
629              std::set<Graph::Edge> edges_to_delete;
630
631              g.firstOut(e,active_node);
632              for(;e!=INVALID;g.nextOut(e))
633                {
634                      edges_to_delete.insert(e);
635                }
636
637              g.firstIn(e,active_node);
638              for(;e!=INVALID;g.nextIn(e))
639                {
640                      edges_to_delete.insert(e);
641                }
642
643              //deleting collected edges
644              for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
645                {
646                  delete_item(*edge_set_it);
647                }
648              delete_item(active_node);
649            }
650          //a simple edge was chosen
651          else
652            {
653              delete_item(active_edge);
654            }
655
656         
657        }
658      //pointer was moved, deletion is cancelled
659      else
660        {
661          if(active_node!=INVALID)
662            {
663              *active_item << Gnome::Canvas::Properties::fill_color("blue");
664            }
665          else
666            {
667              *active_item << Gnome::Canvas::Properties::fill_color("green");
668            }
669        }
670      //reseting datas
671      active_item=NULL;
672      active_edge=INVALID;
673      active_node=INVALID;
674      break;
675
676    case GDK_MOTION_NOTIFY:
677      break;
678
679    default:
680      break;
681    }
682  return true;
683}
684
685void GraphDisplayerCanvas::delete_item(NodeIt node_to_delete)
686{
687  delete(nodesmap[node_to_delete]);
688  g.erase(node_to_delete);
689}
690
691void GraphDisplayerCanvas::delete_item(EdgeIt edge_to_delete)
692{
693  delete(edgesmap[edge_to_delete]);
694  g.erase(edge_to_delete);
695}
696
697void GraphDisplayerCanvas::delete_item(Graph::Edge edge_to_delete)
698{
699  delete(edgesmap[edge_to_delete]);
700  g.erase(edge_to_delete);
701}
702
Note: See TracBrowser for help on using the repository browser.