COIN-OR::LEMON - Graph Library

Ignore:
Timestamp:
10/12/06 13:39:29 (18 years ago)
Author:
Hegyi Péter
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/glemon/trunk@2982
Message:

Node antigravity and edge elasticity based graph layout redesigner.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • graph_displayer_canvas-event.cc

    r153 r160  
    8282  static Gnome::Canvas::Text *coord_text = 0;
    8383  switch(e->type)
    84   {
     84    {
    8585    case GDK_BUTTON_PRESS:
    8686      //we mark the location of the event to be able to calculate parameters of dragging
    8787      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
    88 
     88     
    8989      active_item=(get_item_at(clicked_x, clicked_y));
    9090      active_node=INVALID;
     
    100100    case GDK_BUTTON_RELEASE:
    101101      if (coord_text)
    102       {
    103         delete coord_text;
    104         coord_text = 0;
    105       }
     102        {
     103          delete coord_text;
     104          coord_text = 0;
     105        }
    106106      isbutton=0;
    107107      active_item=NULL;
     
    111111      //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
    112112      if(active_node!=INVALID)
    113       {
    114         (mytab.mapstorage).modified = true;
    115 
    116         //new coordinates will be the old values,
    117         //because the item will be moved to the
    118         //new coordinate therefore the new movement
    119         //has to be calculated from here
    120 
    121         double new_x, new_y;
    122 
    123         window_to_world (e->motion.x, e->motion.y, new_x, new_y);
    124 
    125         double dx=new_x-clicked_x;
    126         double dy=new_y-clicked_y;
    127 
    128         //repositioning node and its text
    129         active_item->move(dx, dy);
    130         nodetextmap[active_node]->move(dx, dy);
    131 
    132         // the new coordinates of the centre of the node
    133         double coord_x = new_x - (clicked_x - (mytab.mapstorage).coords[active_node].x);
    134         double coord_y = new_y - (clicked_y - (mytab.mapstorage).coords[active_node].y);
    135 
    136         // write back the new coordinates to the coords map
    137         (mytab.mapstorage).coords.set(active_node, XY(coord_x, coord_y));
    138 
    139         clicked_x=new_x;
    140         clicked_y=new_y;
    141 
    142         // reposition the coordinates text
    143         std::ostringstream ostr;
    144         ostr << "(" <<
    145           (mytab.mapstorage).coords[active_node].x << ", " <<
    146           (mytab.mapstorage).coords[active_node].y << ")";
    147         double radius =
    148           (nodesmap[active_node]->property_x2().get_value() -
    149           nodesmap[active_node]->property_x1().get_value()) / 2.0;
    150         if (coord_text)
    151         {
    152           coord_text->property_text().set_value(ostr.str());
    153           coord_text->property_x().set_value((mytab.mapstorage).coords[active_node].x +
    154               radius);
    155           coord_text->property_y().set_value((mytab.mapstorage).coords[active_node].y -
    156               radius);
    157         }
    158         else
    159         {
    160           coord_text = new Gnome::Canvas::Text(
    161               displayed_graph,
    162               (mytab.mapstorage).coords[active_node].x + radius,
    163               (mytab.mapstorage).coords[active_node].y - radius,
    164               ostr.str());
    165           coord_text->property_fill_color().set_value("black");
    166           coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST);
    167         }
    168 
    169         //all the edges connected to the moved point has to be redrawn
    170         for(OutEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei)
    171         {
    172           XY arrow_pos;
    173 
    174           if (mytab.mapstorage.graph.source(ei) == mytab.mapstorage.graph.target(ei))
    175           {
    176             arrow_pos = mytab.mapstorage.arrow_pos[ei] + XY(dx, dy);
    177           }
    178           else
    179           {
    180             XY moved_node_1(coord_x - dx, coord_y - dy);
    181             XY moved_node_2(coord_x, coord_y);
    182             Node target = mytab.mapstorage.graph.target(ei);
    183             XY fix_node(mytab.mapstorage.coords[target].x,
    184                 mytab.mapstorage.coords[target].y);
    185             XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
    186 
    187             arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton);
    188           }
    189 
    190           mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
    191           edgesmap[ei]->draw();
    192 
    193           //reposition of edgetext
    194           XY text_pos=mytab.mapstorage.arrow_pos[ei];
    195           text_pos+=(XY(10,10));
    196           edgetextmap[ei]->property_x().set_value(text_pos.x);
    197           edgetextmap[ei]->property_y().set_value(text_pos.y);
    198         }
    199 
    200         for(InEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei)
    201         {
    202           if (mytab.mapstorage.graph.source(ei) != mytab.mapstorage.graph.target(ei))
    203           {
    204             XY moved_node_1(coord_x - dx, coord_y - dy);
    205             XY moved_node_2(coord_x, coord_y);
    206             Node source = mytab.mapstorage.graph.source(ei);
    207             XY fix_node(mytab.mapstorage.coords[source].x,
    208                 mytab.mapstorage.coords[source].y);
    209             XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
    210 
    211             XY arrow_pos;
    212             arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton);
    213 
    214             mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
    215             edgesmap[ei]->draw();
    216 
    217             //reposition of edgetext
    218             XY text_pos=mytab.mapstorage.arrow_pos[ei];
    219             text_pos+=(XY(10,10));
    220             edgetextmap[ei]->property_x().set_value(text_pos.x);
    221             edgetextmap[ei]->property_y().set_value(text_pos.y);
    222           }
    223         }
    224       }
     113        {
     114          (mytab.mapstorage).modified = true;
     115         
     116          //new coordinates will be the old values,
     117          //because the item will be moved to the
     118          //new coordinate therefore the new movement
     119          //has to be calculated from here
     120         
     121          double new_x, new_y;
     122         
     123          window_to_world (e->motion.x, e->motion.y, new_x, new_y);
     124         
     125          double dx=new_x-clicked_x;
     126          double dy=new_y-clicked_y;
     127         
     128          moveNode(dx, dy);
     129
     130          clicked_x=new_x;
     131          clicked_y=new_y;
     132
     133          // reposition the coordinates text
     134          std::ostringstream ostr;
     135          ostr << "(" <<
     136            (mytab.mapstorage).coords[active_node].x << ", " <<
     137            (mytab.mapstorage).coords[active_node].y << ")";
     138          double radius =
     139            (nodesmap[active_node]->property_x2().get_value() -
     140             nodesmap[active_node]->property_x1().get_value()) / 2.0;
     141          if (coord_text)
     142            {
     143              coord_text->property_text().set_value(ostr.str());
     144              coord_text->property_x().set_value((mytab.mapstorage).coords[active_node].x +
     145                                                 radius);
     146              coord_text->property_y().set_value((mytab.mapstorage).coords[active_node].y -
     147                                                 radius);
     148            }
     149          else
     150            {
     151              coord_text = new Gnome::Canvas::Text(
     152                                                   displayed_graph,
     153                                                   (mytab.mapstorage).coords[active_node].x + radius,
     154                                                   (mytab.mapstorage).coords[active_node].y - radius,
     155                                                   ostr.str());
     156              coord_text->property_fill_color().set_value("black");
     157              coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST);
     158            }
     159
     160
     161        }
    225162    default: break;
    226   }
    227 
    228   return false;
     163    }
     164
     165return false;
    229166}
    230167
     
    824761  }
    825762  else
    826   {
    827     if(forming_edge!=INVALID)
    828     {
    829       forming_edge=INVALID;
    830     }
    831     else
    832     {
    833       std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
    834     }
    835   }
    836 }
     763    {
     764      if(forming_edge!=INVALID)
     765        {
     766          forming_edge=INVALID;
     767        }
     768      else
     769        {
     770          std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
     771        }
     772    }
     773}
     774
     775void GraphDisplayerCanvas::moveNode(double dx, double dy, Gnome::Canvas::Item * item, Node node)
     776{
     777  Gnome::Canvas::Item * moved_item=item;
     778  Node moved_node=node;
     779
     780  if(item==NULL && node==INVALID)
     781    {
     782      moved_item=active_item;
     783      moved_node=active_node;
     784    }
     785  else
     786    {
     787      isbutton=1;
     788    }
     789
     790  //repositioning node and its text
     791  moved_item->move(dx, dy);
     792  nodetextmap[moved_node]->move(dx, dy);
     793
     794  // the new coordinates of the centre of the node
     795  double coord_x = dx + (mytab.mapstorage).coords[moved_node].x;
     796  double coord_y = dy + (mytab.mapstorage).coords[moved_node].y;
     797
     798  // write back the new coordinates to the coords map
     799  (mytab.mapstorage).coords.set(moved_node, XY(coord_x, coord_y));
     800
     801  //all the edges connected to the moved point has to be redrawn
     802  for(OutEdgeIt ei((mytab.mapstorage).graph,moved_node);ei!=INVALID;++ei)
     803    {
     804      XY arrow_pos;
     805
     806      if (mytab.mapstorage.graph.source(ei) == mytab.mapstorage.graph.target(ei))
     807        {
     808          arrow_pos = mytab.mapstorage.arrow_pos[ei] + XY(dx, dy);
     809        }
     810      else
     811        {
     812          XY moved_node_1(coord_x - dx, coord_y - dy);
     813          XY moved_node_2(coord_x, coord_y);
     814          Node target = mytab.mapstorage.graph.target(ei);
     815          XY fix_node(mytab.mapstorage.coords[target].x,
     816                      mytab.mapstorage.coords[target].y);
     817          XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
     818
     819          arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton);
     820        }
     821
     822      mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
     823      edgesmap[ei]->draw();
     824
     825      //reposition of edgetext
     826      XY text_pos=mytab.mapstorage.arrow_pos[ei];
     827      text_pos+=(XY(10,10));
     828      edgetextmap[ei]->property_x().set_value(text_pos.x);
     829      edgetextmap[ei]->property_y().set_value(text_pos.y);
     830    }
     831
     832  for(InEdgeIt ei((mytab.mapstorage).graph,moved_node);ei!=INVALID;++ei)
     833    {
     834      if (mytab.mapstorage.graph.source(ei) != mytab.mapstorage.graph.target(ei))
     835        {
     836          XY moved_node_1(coord_x - dx, coord_y - dy);
     837          XY moved_node_2(coord_x, coord_y);
     838          Node source = mytab.mapstorage.graph.source(ei);
     839          XY fix_node(mytab.mapstorage.coords[source].x,
     840                      mytab.mapstorage.coords[source].y);
     841          XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
     842
     843          XY arrow_pos;
     844          arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton);
     845
     846          mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
     847          edgesmap[ei]->draw();
     848
     849          //reposition of edgetext
     850          XY text_pos=mytab.mapstorage.arrow_pos[ei];
     851          text_pos+=(XY(10,10));
     852          edgetextmap[ei]->property_x().set_value(text_pos.x);
     853          edgetextmap[ei]->property_y().set_value(text_pos.y);
     854        }
     855    }
     856}
Note: See TracChangeset for help on using the changeset viewer.