COIN-OR::LEMON - Graph Library

source: glemon-0.x/graph_displayer_canvas-event.cc @ 197:c1084e2bff10

Last change on this file since 197:c1084e2bff10 was 194:6b2b718420eb, checked in by Hegyi Péter, 17 years ago

Header reorganising

  • Property exe set to *
File size: 26.4 KB
Line 
1/* -*- C++ -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library
4 *
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
19#include <graph_displayer_canvas.h>
20#include <mapstorage.h>
21#include <nbtab.h>
22#include <cmath>
23
24
25bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
26{
27  Gnome::Canvas::CanvasAA::on_expose_event(event);
28  //usleep(10000);
29  //rezoom();
30  return true;
31}
32
33void GraphDisplayerCanvas::changeEditorialTool(int newtool)
34{
35  if(actual_tool!=newtool)
36    {
37
38      actual_handler.disconnect();
39
40      switch(actual_tool)
41        {
42        case CREATE_EDGE:
43          {
44            GdkEvent * generated=new GdkEvent();
45            generated->type=GDK_BUTTON_RELEASE;
46            generated->button.button=3;
47            createEdgeEventHandler(generated);     
48            break;
49          }
50        case MAP_EDIT:
51          {
52            break;
53          }
54        default:
55          break;
56        }
57
58      active_item=NULL;
59      target_item=NULL;
60      active_edge=INVALID;     
61      active_node=INVALID;     
62
63
64      actual_tool=newtool;
65 
66      switch(newtool)
67        {
68        case MOVE:
69          actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
70          break;
71
72        case CREATE_NODE:
73          actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false);
74          break;
75
76        case CREATE_EDGE:
77          actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false);
78          break;
79
80        case ERASER:
81          actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false);
82          break;
83
84        case MAP_EDIT:
85          grab_focus();
86          actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
87          break;
88
89        default:
90          break;
91        }
92    }
93}
94
95int GraphDisplayerCanvas::getActualTool()
96{
97  return actual_tool;
98}
99
100bool GraphDisplayerCanvas::scrollEventHandler(GdkEvent* e)
101{
102  bool handled=false;
103  if(e->type==GDK_SCROLL)
104    {
105
106      //pointer shows this win point before zoom
107      XY win_coord(((GdkEventScroll*)e)->x, ((GdkEventScroll*)e)->y);
108
109      //the original scroll settings
110      int scroll_offset_x, scroll_offset_y;
111      get_scroll_offsets(scroll_offset_x, scroll_offset_y);
112
113      //pointer shows this canvas point before zoom
114      XY canvas_coord;
115      window_to_world(win_coord.x, win_coord.y, canvas_coord.x, canvas_coord.y);
116
117      if(((GdkEventScroll*)e)->direction) //IN
118        {
119          zoomIn();
120        }
121      else
122        {
123          zoomOut();
124        }
125
126      //pointer shows this window point after zoom
127      XY post_win_coord;
128      world_to_window(canvas_coord.x, canvas_coord.y, post_win_coord.x, post_win_coord.y);
129
130      //we have to add the difference between new and old window point to original scroll offset
131      scroll_to(scroll_offset_x+(int)(post_win_coord.x-win_coord.x),scroll_offset_y+(int)(post_win_coord.y-win_coord.y));
132     
133      //no other eventhandler is needed
134      handled=true;
135    }
136  return handled;
137}
138
139bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e)
140{
141  static Gnome::Canvas::Text *coord_text = 0;
142  switch(e->type)
143    {
144    case GDK_BUTTON_PRESS:
145      //we mark the location of the event to be able to calculate parameters of dragging
146      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
147     
148      active_item=(get_item_at(clicked_x, clicked_y));
149      active_node=INVALID;
150      for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
151        {
152          if(nodesmap[i]==active_item)
153            {
154              active_node=i;
155            }
156        }
157      isbutton=e->button.button;
158      break;
159    case GDK_BUTTON_RELEASE:
160      if (coord_text)
161        {
162          delete coord_text;
163          coord_text = 0;
164        }
165      isbutton=0;
166      active_item=NULL;
167      active_node=INVALID;
168      break;
169    case GDK_MOTION_NOTIFY:
170      //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
171      if(active_node!=INVALID)
172        {
173          (mytab.mapstorage)->modified = true;
174         
175          //new coordinates will be the old values,
176          //because the item will be moved to the
177          //new coordinate therefore the new movement
178          //has to be calculated from here
179         
180          double new_x, new_y;
181         
182          window_to_world (e->motion.x, e->motion.y, new_x, new_y);
183         
184          double dx=new_x-clicked_x;
185          double dy=new_y-clicked_y;
186         
187          moveNode(dx, dy);
188
189          clicked_x=new_x;
190          clicked_y=new_y;
191
192          // reposition the coordinates text
193          std::ostringstream ostr;
194          ostr << "(" <<
195            (mytab.mapstorage)->coords[active_node].x << ", " <<
196            (mytab.mapstorage)->coords[active_node].y << ")";
197          double radius =
198            (nodesmap[active_node]->property_x2().get_value() -
199             nodesmap[active_node]->property_x1().get_value()) / 2.0;
200          if (coord_text)
201            {
202              coord_text->property_text().set_value(ostr.str());
203              coord_text->property_x().set_value((mytab.mapstorage)->coords[active_node].x +
204                                                 radius);
205              coord_text->property_y().set_value((mytab.mapstorage)->coords[active_node].y -
206                                                 radius);
207            }
208          else
209            {
210              coord_text = new Gnome::Canvas::Text(
211                                                   displayed_graph,
212                                                   (mytab.mapstorage)->coords[active_node].x + radius,
213                                                   (mytab.mapstorage)->coords[active_node].y - radius,
214                                                   ostr.str());
215              coord_text->property_fill_color().set_value("black");
216              coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST);
217            }
218
219
220        }
221    default: break;
222    }
223
224return false;
225}
226
227XY GraphDisplayerCanvas::calcArrowPos(XY moved_node_1, XY moved_node_2, XY fix_node, XY old_arrow_pos, int move_code)
228{
229  switch(move_code)
230    {
231    case 1:
232      return XY((moved_node_2.x + fix_node.x) / 2.0, (moved_node_2.y + fix_node.y) / 2.0);
233      break;
234    case 2:
235      return old_arrow_pos;
236      break;
237    case 3:
238      {
239        //////////////////////////////////////////////////////////////////////////////////////////////////////
240        /////////// keeps shape-with scalar multiplication - version 2.
241        //////////////////////////////////////////////////////////////////////////////////////////////////////
242
243        //old vector from one to the other node - a
244        XY a_v(moved_node_1.x-fix_node.x,moved_node_1.y-fix_node.y);
245        //new vector from one to the other node - b
246        XY b_v(moved_node_2.x-fix_node.x,moved_node_2.y-fix_node.y);
247
248        double absa=sqrt(a_v.normSquare());
249        double absb=sqrt(b_v.normSquare());
250
251        if ((absa == 0.0) || (absb == 0.0))
252          {
253            return old_arrow_pos;
254          }
255        else
256          {
257            //old vector from one node to the breakpoint - c
258            XY c_v(old_arrow_pos.x-fix_node.x,old_arrow_pos.y-fix_node.y);
259
260            //unit vector with the same direction to a_v
261            XY a_v_u(a_v.x/absa,a_v.y/absa);
262
263            //normal vector of unit vector with the same direction to a_v
264            XY a_v_u_n(((-1)*a_v_u.y),a_v_u.x);
265
266            //unit vector with the same direction to b_v
267            XY b_v_u(b_v.x/absb,b_v.y/absb);
268
269            //normal vector of unit vector with the same direction to b_v
270            XY b_v_u_n(((-1)*b_v_u.y),b_v_u.x);
271
272            //vector c in a_v_u and a_v_u_n co-ordinate system
273            XY c_a(c_v*a_v_u,c_v*a_v_u_n);
274
275            //new vector from one node to the breakpoint - d - we have to calculate this one
276            XY d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n);
277
278            return XY(d_v.x+fix_node.x,d_v.y+fix_node.y);
279          }
280        break;
281      }
282    default:
283      break;
284    }
285}
286
287
288bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
289{
290  switch(e->type)
291  {
292    //move the new node
293    case GDK_MOTION_NOTIFY:
294      {
295        GdkEvent * generated=new GdkEvent();
296        generated->motion.x=e->motion.x;
297        generated->motion.y=e->motion.y;
298        generated->type=GDK_MOTION_NOTIFY;
299        moveEventHandler(generated);     
300        break;
301      }
302
303    case GDK_BUTTON_RELEASE:
304      (mytab.mapstorage)->modified = true;
305
306      is_drawn=true;
307
308      isbutton=1;
309
310      active_node=(mytab.mapstorage)->graph.addNode();
311
312      //initiating values corresponding to new node in maps
313
314      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
315
316      // update coordinates
317      (mytab.mapstorage)->coords.set(active_node, XY(clicked_x, clicked_y));
318
319      // update all other maps
320      for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
321          (mytab.mapstorage)->nodemap_storage.begin(); it !=
322          (mytab.mapstorage)->nodemap_storage.end(); ++it)
323      {
324        if ((it->first != "coordinates_x") &&
325            (it->first != "coordinates_y"))
326        {
327          (*(it->second))[active_node] =
328            (mytab.mapstorage)->nodemap_default[it->first];
329        }
330      }
331      // increment the id map's default value
332      (mytab.mapstorage)->nodemap_default["label"] += 1.0;
333
334      nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph,
335          clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
336      active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
337      *(nodesmap[active_node]) <<
338        Gnome::Canvas::Properties::fill_color("blue");
339      *(nodesmap[active_node]) <<
340        Gnome::Canvas::Properties::outline_color("black");
341      active_item->raise_to_top();
342
343      (nodesmap[active_node])->show();
344
345      nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph,
346          clicked_x+node_property_defaults[N_RADIUS]+5,
347          clicked_y+node_property_defaults[N_RADIUS]+5, "");
348      nodetextmap[active_node]->property_fill_color().set_value("darkblue");
349      nodetextmap[active_node]->raise_to_top();
350
351//       mapwin.updateNode(active_node);
352      propertyUpdate(active_node);
353
354      isbutton=0;
355      target_item=NULL;
356      active_item=NULL;
357      active_node=INVALID;
358      break;
359    default:
360      break;
361  }
362  return false;
363}
364
365bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e)
366{
367  switch(e->type)
368  {
369    case GDK_BUTTON_PRESS:
370      //in edge creation right button has special meaning
371      if(e->button.button!=3)
372      {
373        //there is not yet selected node
374        if(active_node==INVALID)
375        {
376          //we mark the location of the event to be able to calculate parameters of dragging
377
378          window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
379
380          active_item=(get_item_at(clicked_x, clicked_y));
381          active_node=INVALID;
382          for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
383          {
384            if(nodesmap[i]==active_item)
385            {
386              active_node=i;
387            }
388          }
389          //the clicked item is really a node
390          if(active_node!=INVALID)
391          {
392            *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
393            isbutton=1;
394          }
395          //clicked item was not a node. It could be e.g. edge.
396          else
397          {
398            active_item=NULL;
399          }
400        }
401        //we only have to do sg. if the mouse button
402        // is pressed already once AND the click was
403        // on a node that was found in the set of
404        //nodes, and now we only search for the second
405        //node
406        else
407        {
408          window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
409          target_item=(get_item_at(clicked_x, clicked_y));
410          Node target_node=INVALID;
411          for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
412          {
413            if(nodesmap[i]==target_item)
414            {
415              target_node=i;
416            }
417          }
418          //the clicked item is a node, the edge can be drawn
419          if(target_node!=INVALID)
420          {
421            (mytab.mapstorage)->modified = true;
422
423            *(nodesmap[target_node]) <<
424              Gnome::Canvas::Properties::fill_color("red");
425
426            //creating new edge
427            active_edge=(mytab.mapstorage)->graph.addEdge(active_node,
428                target_node);
429
430            // update maps
431            for (std::map<std::string,
432                Graph::EdgeMap<double>*>::const_iterator it =
433                (mytab.mapstorage)->edgemap_storage.begin(); it !=
434                (mytab.mapstorage)->edgemap_storage.end(); ++it)
435            {
436              (*(it->second))[active_edge] =
437                (mytab.mapstorage)->edgemap_default[it->first];
438            }
439            // increment the id map's default value
440            (mytab.mapstorage)->edgemap_default["label"] += 1.0;
441
442            if(target_node!=active_node)               
443            {
444              // set the coordinates of the arrow on the new edge
445              MapStorage& ms = *mytab.mapstorage;
446              ms.arrow_pos.set(active_edge,
447                  (ms.coords[ms.graph.source(active_edge)] +
448                   ms.coords[ms.graph.target(active_edge)])/ 2.0);
449
450              //drawing new edge
451              edgesmap[active_edge]=new BrokenEdge(displayed_graph, active_edge,
452                  *this);
453            }
454            else
455            {
456              // set the coordinates of the arrow on the new edge
457              MapStorage& ms = *mytab.mapstorage;
458              ms.arrow_pos.set(active_edge,
459                  (ms.coords[ms.graph.source(active_edge)] +
460                   XY(0.0, 80.0)));
461
462              //drawing new edge
463              edgesmap[active_edge]=new LoopEdge(displayed_graph, active_edge,
464                  *this);
465            }
466
467            //initializing edge-text as well, to empty string
468            XY text_pos=mytab.mapstorage->arrow_pos[active_edge];
469            text_pos+=(XY(10,10));
470
471            edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,
472                text_pos.x, text_pos.y, "");
473            edgetextmap[active_edge]->property_fill_color().set_value(
474                "darkgreen");
475            edgetextmap[active_edge]->raise_to_top();
476
477            propertyUpdate(active_edge);
478          }
479          //clicked item was not a node. it could be an e.g. edge. we do not
480          //deal with it furthermore.
481          else
482          {
483            target_item=NULL;
484          }
485        }
486      }
487      break;
488    case GDK_BUTTON_RELEASE:
489      isbutton=0;
490      //we clear settings in two cases
491      //1: the edge is ready (target_item has valid value)
492      //2: the edge creation is cancelled with right button
493      if((target_item)||(e->button.button==3))
494      {
495        if(active_item)
496        {
497          propertyUpdate(active_node,N_COLOR);
498          active_item=NULL;
499        }
500        if(target_item)
501        {
502          propertyUpdate((mytab.mapstorage)->graph.target(active_edge),N_COLOR);
503          target_item=NULL;
504        }
505        active_node=INVALID;
506        active_edge=INVALID;
507      }
508      break;
509    default:
510      break;
511  }
512  return false;
513}
514
515bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e)
516{
517  switch(e->type)
518    {
519    case GDK_BUTTON_PRESS:
520      //finding the clicked items
521      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
522      active_item=(get_item_at(clicked_x, clicked_y));
523      active_node=INVALID;
524      active_edge=INVALID;
525      //was it a node?
526      for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
527        {
528          if(nodesmap[i]==active_item)
529            {
530              active_node=i;
531            }
532        }
533      //or was it an edge?
534      if(active_node==INVALID)
535        {
536          for (EdgeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
537            {
538              if(edgesmap[i]->getLine()==active_item)
539                {
540                  active_edge=i;
541                }
542            }
543        }
544
545      // return if the clicked object is neither an edge nor a node
546      if (active_edge == INVALID) return false;
547     
548      //recolor activated item
549      if(active_item)
550        {
551          *active_item << Gnome::Canvas::Properties::fill_color("red");
552        }
553      break;
554
555    case GDK_BUTTON_RELEASE:
556      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
557      if(active_item)
558        {
559          //the cursor was not moved since pressing it
560          if( active_item == ( get_item_at (clicked_x, clicked_y) ) )
561            {
562              //a node was found
563              if(active_node!=INVALID)
564                {
565                  (mytab.mapstorage)->modified = true;
566
567                  std::set<Graph::Edge> edges_to_delete;
568
569                  for(OutEdgeIt e((mytab.mapstorage)->graph,active_node);e!=INVALID;++e)
570                    {
571                      edges_to_delete.insert(e);
572                    }
573                 
574                  for(InEdgeIt e((mytab.mapstorage)->graph,active_node);e!=INVALID;++e)
575                    {
576                      edges_to_delete.insert(e);
577                    }
578                 
579                  //deleting collected edges
580                  for(std::set<Graph::Edge>::iterator
581                        edge_set_it=edges_to_delete.begin();
582                      edge_set_it!=edges_to_delete.end();
583                      ++edge_set_it)
584                    {
585                      deleteItem(*edge_set_it);
586                    }
587                  deleteItem(active_node);
588                }
589              //a simple edge was chosen
590              else if (active_edge != INVALID)
591                {
592                  deleteItem(active_edge);
593                }
594            }
595          //pointer was moved, deletion is cancelled
596          else
597            {
598              if(active_node!=INVALID)
599                {
600                  *active_item << Gnome::Canvas::Properties::fill_color("blue");
601                }
602              else if (active_edge != INVALID)
603                {
604                  *active_item << Gnome::Canvas::Properties::fill_color("green");
605                }
606            }
607        }
608      //reseting datas
609      active_item=NULL;
610      active_edge=INVALID;
611      active_node=INVALID;
612      break;
613
614    case GDK_MOTION_NOTIFY:
615      break;
616
617    default:
618      break;
619    }
620  return false;
621}
622
623bool GraphDisplayerCanvas::mapEditEventHandler(GdkEvent* e)
624{
625  if(actual_tool==MAP_EDIT)
626    {
627      switch(e->type)
628        {
629        case GDK_BUTTON_PRESS:
630          {
631            //for determine, whether it was an edge
632            Edge clicked_edge=INVALID;
633            //for determine, whether it was a node
634            Node clicked_node=INVALID;
635
636            window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
637            active_item=(get_item_at(clicked_x, clicked_y));
638
639            //find the activated item between text of nodes
640            for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
641              {
642                //at the same time only one can be active
643                if(nodetextmap[i]==active_item)
644                  {
645                    clicked_node=i;
646                  }
647              }
648
649            //if there was not, search for it between nodes
650            if(clicked_node==INVALID)
651              {
652                for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
653                  {
654                    //at the same time only one can be active
655                    if(nodesmap[i]==active_item)
656                      {
657                        clicked_node=i;
658                      }
659                  }
660              }
661
662            if(clicked_node==INVALID)
663              {
664                //find the activated item between texts
665                for (EdgeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
666                  {
667                    //at the same time only one can be active
668                    if(edgetextmap[i]==active_item)
669                      {
670                        clicked_edge=i;
671                      }
672                  }
673
674                //if it was not between texts, search for it between edges
675                if(clicked_edge==INVALID)
676                  {
677                    for (EdgeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
678                      {
679                        //at the same time only one can be active
680                        if((edgesmap[i]->getLine())==active_item)
681                          {
682                            clicked_edge=i;
683                          }
684                      }
685                  }
686              }
687
688            //if it was really a node...
689            if(clicked_node!=INVALID)
690              {
691                // the id map is not editable
692                if (nodemap_to_edit == "label") return 0;
693
694                //and there is activated map
695                if(nodetextmap[clicked_node]->property_text().get_value()!="")
696                  {
697                    //activate the general variable for it
698                    active_node=clicked_node;
699
700                    //create a dialog
701                    Gtk::Dialog dialog("Edit value", true);
702                    dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
703                    dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
704                    Gtk::VBox* vbox = dialog.get_vbox();
705                    Gtk::SpinButton spin(0.0, 4);
706                    spin.set_increments(1.0, 10.0);
707                    spin.set_range(-1000000.0, 1000000.0);
708                    spin.set_numeric(true);
709                    spin.set_value(atof(nodetextmap[active_node]->property_text().get_value().c_str()));
710                    vbox->add(spin);
711                    spin.show();
712                    switch (dialog.run())
713                      {
714                      case Gtk::RESPONSE_NONE:
715                      case Gtk::RESPONSE_CANCEL:
716                        break;
717                      case Gtk::RESPONSE_ACCEPT:
718                        double new_value = spin.get_value();
719                        (*(mytab.mapstorage)->nodemap_storage[nodemap_to_edit])[active_node] =
720                          new_value;
721                        std::ostringstream ostr;
722                        ostr << new_value;
723                        nodetextmap[active_node]->property_text().set_value(ostr.str());
724                        //mapwin.updateNode(active_node);
725                        //mapwin.updateNode(Node(INVALID));
726                        propertyUpdate(Node(INVALID));
727                      }
728                  }
729              }
730            else
731              //if it was really an edge...
732              if(clicked_edge!=INVALID)
733                {
734                  // the id map is not editable
735                  if (edgemap_to_edit == "label") return 0;
736
737                  //and there is activated map
738                  if(edgetextmap[clicked_edge]->property_text().get_value()!="")
739                    {
740                      //activate the general variable for it
741                      active_edge=clicked_edge;
742
743                      //create a dialog
744                      Gtk::Dialog dialog("Edit value", true);
745                      dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
746                      dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
747                      Gtk::VBox* vbox = dialog.get_vbox();
748                      Gtk::SpinButton spin(0.0, 4);
749                      spin.set_increments(1.0, 10.0);
750                      spin.set_range(-1000000.0, 1000000.0);
751                      spin.set_numeric(true);
752                      spin.set_value(atof(edgetextmap[active_edge]->property_text().get_value().c_str()));
753                      vbox->add(spin);
754                      spin.show();
755                      switch (dialog.run())
756                        {
757                        case Gtk::RESPONSE_NONE:
758                        case Gtk::RESPONSE_CANCEL:
759                          break;
760                        case Gtk::RESPONSE_ACCEPT:
761                          double new_value = spin.get_value();
762                          (*(mytab.mapstorage)->edgemap_storage[edgemap_to_edit])[active_edge] =
763                            new_value;
764                          std::ostringstream ostr;
765                          ostr << new_value;
766                          edgetextmap[active_edge]->property_text().set_value(
767                                                                              ostr.str());
768                          //mapwin.updateEdge(active_edge);
769                          //                   mapwin.updateEdge(Edge(INVALID));
770                          propertyUpdate(Edge(INVALID));
771                        }
772                    }
773                }
774            break;
775          }
776        default:
777          break;
778        }
779    }
780  return false; 
781}
782
783void GraphDisplayerCanvas::deleteItem(Node node_to_delete)
784{
785  delete(nodetextmap[node_to_delete]);
786  delete(nodesmap[node_to_delete]);
787  (mytab.mapstorage)->graph.erase(node_to_delete);
788}
789
790void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete)
791{
792  delete(edgetextmap[edge_to_delete]);
793  delete(edgesmap[edge_to_delete]);
794  (mytab.mapstorage)->graph.erase(edge_to_delete);
795}
796
797void GraphDisplayerCanvas::textReposition(XY new_place)
798{
799  new_place+=(XY(10,10));
800  edgetextmap[forming_edge]->property_x().set_value(new_place.x);
801  edgetextmap[forming_edge]->property_y().set_value(new_place.y);
802}
803
804void GraphDisplayerCanvas::toggleEdgeActivity(EdgeBase* active_bre, bool on)
805{
806  if(on)
807  {
808    if(forming_edge!=INVALID)
809    {
810      std::cerr << "ERROR!!!! Valid edge found!" << std::endl;
811    }
812    else
813    {
814      for (EdgeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
815      {
816        if(edgesmap[i]==active_bre)
817        {
818          forming_edge=i;
819        }
820      }
821    }
822  }
823  else
824    {
825      if(forming_edge!=INVALID)
826        {
827          forming_edge=INVALID;
828        }
829      else
830        {
831          std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
832        }
833    }
834}
835
836void GraphDisplayerCanvas::moveNode(double dx, double dy, Gnome::Canvas::Item * item, Node node)
837{
838  Gnome::Canvas::Item * moved_item=item;
839  Node moved_node=node;
840
841  if(item==NULL && node==INVALID)
842    {
843      moved_item=active_item;
844      moved_node=active_node;
845    }
846  else
847    {
848      isbutton=1;
849    }
850
851  //repositioning node and its text
852  moved_item->move(dx, dy);
853  nodetextmap[moved_node]->move(dx, dy);
854
855  // the new coordinates of the centre of the node
856  double coord_x = dx + (mytab.mapstorage)->coords[moved_node].x;
857  double coord_y = dy + (mytab.mapstorage)->coords[moved_node].y;
858
859  // write back the new coordinates to the coords map
860  (mytab.mapstorage)->coords.set(moved_node, XY(coord_x, coord_y));
861
862  //all the edges connected to the moved point has to be redrawn
863  for(OutEdgeIt ei((mytab.mapstorage)->graph,moved_node);ei!=INVALID;++ei)
864    {
865      XY arrow_pos;
866
867      if (mytab.mapstorage->graph.source(ei) == mytab.mapstorage->graph.target(ei))
868        {
869          arrow_pos = mytab.mapstorage->arrow_pos[ei] + XY(dx, dy);
870        }
871      else
872        {
873          XY moved_node_1(coord_x - dx, coord_y - dy);
874          XY moved_node_2(coord_x, coord_y);
875          Node target = mytab.mapstorage->graph.target(ei);
876          XY fix_node(mytab.mapstorage->coords[target].x,
877                      mytab.mapstorage->coords[target].y);
878          XY old_arrow_pos(mytab.mapstorage->arrow_pos[ei]);
879
880          arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton);
881        }
882
883      mytab.mapstorage->arrow_pos.set(ei, arrow_pos);
884      edgesmap[ei]->draw();
885
886      //reposition of edgetext
887      XY text_pos=mytab.mapstorage->arrow_pos[ei];
888      text_pos+=(XY(10,10));
889      edgetextmap[ei]->property_x().set_value(text_pos.x);
890      edgetextmap[ei]->property_y().set_value(text_pos.y);
891    }
892
893  for(InEdgeIt ei((mytab.mapstorage)->graph,moved_node);ei!=INVALID;++ei)
894    {
895      if (mytab.mapstorage->graph.source(ei) != mytab.mapstorage->graph.target(ei))
896        {
897          XY moved_node_1(coord_x - dx, coord_y - dy);
898          XY moved_node_2(coord_x, coord_y);
899          Node source = mytab.mapstorage->graph.source(ei);
900          XY fix_node(mytab.mapstorage->coords[source].x,
901                      mytab.mapstorage->coords[source].y);
902          XY old_arrow_pos(mytab.mapstorage->arrow_pos[ei]);
903
904          XY arrow_pos;
905          arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton);
906
907          mytab.mapstorage->arrow_pos.set(ei, arrow_pos);
908          edgesmap[ei]->draw();
909
910          //reposition of edgetext
911          XY text_pos=mytab.mapstorage->arrow_pos[ei];
912          text_pos+=(XY(10,10));
913          edgetextmap[ei]->property_x().set_value(text_pos.x);
914          edgetextmap[ei]->property_y().set_value(text_pos.y);
915        }
916    }
917}
918
919Gdk::Color GraphDisplayerCanvas::rainbowColorCounter(double min, double max, double w)
920{
921  Gdk::Color color;
922
923  double pos=(w-min)/(max-min);
924  int phase=0;
925
926  //rainbow transitions contain 6 phase
927  //in each phase only one color is changed
928  //first we determine the phase, in which
929  //the actual value belongs to
930  for (int i=0;i<=5;i++)
931    {
932      if(((double)i/6<pos)&&(pos<=(double(i+1)/6)))
933        {
934          phase=i;
935        }
936    }
937  if(phase<6)
938    {
939      //within its 1/6 long phase the relativ position
940      //determines the power of the color changed in
941      //that phase
942      //we normalize that to one, to be able to give percentage
943      //value for the function
944      double rel_pos=(pos-(phase/6))*6;
945
946      switch(phase)
947        {
948        case 0:
949          color.set_rgb_p (1, 0, 1-rel_pos);
950          break;
951        case 1:
952          color.set_rgb_p (1, rel_pos, 0);
953          break;
954        case 2:
955          color.set_rgb_p (1-rel_pos, 1, 0);
956          break;
957        case 3:
958          color.set_rgb_p (0, 1, rel_pos);
959          break;
960        case 4:
961          color.set_rgb_p (0, 1-rel_pos, 1);
962          break;
963        case 5:
964          color.set_rgb_p ((rel_pos/3), 0, 1);
965          break;
966        default:
967          std::cout << "Wrong phase: " << phase << " " << pos << std::endl;
968        }
969    }
970  else
971    {
972      std::cout << "Wrong phase: " << phase << " " << pos << std::endl;
973    }
974  return color;
975}
Note: See TracBrowser for help on using the repository browser.