graph_displayer_canvas-event.cc
changeset 160 14a76109b561
parent 153 d79a71382836
child 174 95872af46fc4
equal deleted inserted replaced
43:1daa8aabaf9f 44:bd925071d884
    79 
    79 
    80 bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e)
    80 bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e)
    81 {
    81 {
    82   static Gnome::Canvas::Text *coord_text = 0;
    82   static Gnome::Canvas::Text *coord_text = 0;
    83   switch(e->type)
    83   switch(e->type)
    84   {
    84     {
    85     case GDK_BUTTON_PRESS:
    85     case GDK_BUTTON_PRESS:
    86       //we mark the location of the event to be able to calculate parameters of dragging
    86       //we mark the location of the event to be able to calculate parameters of dragging
    87       window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
    87       window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
    88 
    88       
    89       active_item=(get_item_at(clicked_x, clicked_y));
    89       active_item=(get_item_at(clicked_x, clicked_y));
    90       active_node=INVALID;
    90       active_node=INVALID;
    91       for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
    91       for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
    92 	{
    92 	{
    93 	  if(nodesmap[i]==active_item)
    93 	  if(nodesmap[i]==active_item)
    97 	}
    97 	}
    98       isbutton=e->button.button;
    98       isbutton=e->button.button;
    99       break;
    99       break;
   100     case GDK_BUTTON_RELEASE:
   100     case GDK_BUTTON_RELEASE:
   101       if (coord_text)
   101       if (coord_text)
   102       {
   102 	{
   103         delete coord_text;
   103 	  delete coord_text;
   104         coord_text = 0;
   104 	  coord_text = 0;
   105       }
   105 	}
   106       isbutton=0;
   106       isbutton=0;
   107       active_item=NULL;
   107       active_item=NULL;
   108       active_node=INVALID;
   108       active_node=INVALID;
   109       break;
   109       break;
   110     case GDK_MOTION_NOTIFY:
   110     case GDK_MOTION_NOTIFY:
   111       //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
   111       //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
   112       if(active_node!=INVALID)
   112       if(active_node!=INVALID)
   113       {
   113 	{
   114         (mytab.mapstorage).modified = true;
   114 	  (mytab.mapstorage).modified = true;
   115 
   115 	  
   116 	//new coordinates will be the old values,
   116 	  //new coordinates will be the old values,
   117 	//because the item will be moved to the
   117 	  //because the item will be moved to the
   118 	//new coordinate therefore the new movement
   118 	  //new coordinate therefore the new movement
   119 	//has to be calculated from here
   119 	  //has to be calculated from here
   120 
   120 	  
   121 	double new_x, new_y;
   121 	  double new_x, new_y;
   122 
   122 	  
   123 	window_to_world (e->motion.x, e->motion.y, new_x, new_y);
   123 	  window_to_world (e->motion.x, e->motion.y, new_x, new_y);
   124 
   124 	  
   125         double dx=new_x-clicked_x;
   125 	  double dx=new_x-clicked_x;
   126         double dy=new_y-clicked_y;
   126 	  double dy=new_y-clicked_y;
   127 
   127 	  
   128 	//repositioning node and its text
   128 	  moveNode(dx, dy);
   129         active_item->move(dx, dy);
   129 
   130 	nodetextmap[active_node]->move(dx, dy);
   130 	  clicked_x=new_x;
   131 
   131 	  clicked_y=new_y;
   132         // the new coordinates of the centre of the node 
   132 
   133         double coord_x = new_x - (clicked_x - (mytab.mapstorage).coords[active_node].x);
   133 	  // reposition the coordinates text
   134         double coord_y = new_y - (clicked_y - (mytab.mapstorage).coords[active_node].y);
   134 	  std::ostringstream ostr;
   135 
   135 	  ostr << "(" <<
   136         // write back the new coordinates to the coords map
   136 	    (mytab.mapstorage).coords[active_node].x << ", " <<
   137         (mytab.mapstorage).coords.set(active_node, XY(coord_x, coord_y));
   137 	    (mytab.mapstorage).coords[active_node].y << ")";
   138 
   138 	  double radius =
   139         clicked_x=new_x;
   139 	    (nodesmap[active_node]->property_x2().get_value() -
   140         clicked_y=new_y;
   140 	     nodesmap[active_node]->property_x1().get_value()) / 2.0;
   141 
   141 	  if (coord_text)
   142         // reposition the coordinates text
   142 	    {
   143         std::ostringstream ostr;
   143 	      coord_text->property_text().set_value(ostr.str());
   144         ostr << "(" <<
   144 	      coord_text->property_x().set_value((mytab.mapstorage).coords[active_node].x +
   145           (mytab.mapstorage).coords[active_node].x << ", " <<
   145 						 radius);
   146           (mytab.mapstorage).coords[active_node].y << ")";
   146 	      coord_text->property_y().set_value((mytab.mapstorage).coords[active_node].y -
   147         double radius =
   147 						 radius);
   148           (nodesmap[active_node]->property_x2().get_value() -
   148 	    }
   149           nodesmap[active_node]->property_x1().get_value()) / 2.0;
   149 	  else
   150         if (coord_text)
   150 	    {
   151         {
   151 	      coord_text = new Gnome::Canvas::Text(
   152           coord_text->property_text().set_value(ostr.str());
   152 						   displayed_graph,
   153           coord_text->property_x().set_value((mytab.mapstorage).coords[active_node].x +
   153 						   (mytab.mapstorage).coords[active_node].x + radius,
   154               radius);
   154 						   (mytab.mapstorage).coords[active_node].y - radius,
   155           coord_text->property_y().set_value((mytab.mapstorage).coords[active_node].y -
   155 						   ostr.str());
   156               radius);
   156 	      coord_text->property_fill_color().set_value("black");
   157         }
   157 	      coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST);
   158         else
   158 	    }
   159         {
   159 
   160           coord_text = new Gnome::Canvas::Text(
   160 
   161               displayed_graph,
   161 	}
   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       }
       
   225     default: break;
   162     default: break;
   226   }
   163     }
   227 
   164 
   228   return false;
   165 return false;
   229 }
   166 }
   230 
   167 
   231 XY GraphDisplayerCanvas::calcArrowPos(XY moved_node_1, XY moved_node_2, XY fix_node, XY old_arrow_pos, int move_code)
   168 XY GraphDisplayerCanvas::calcArrowPos(XY moved_node_1, XY moved_node_2, XY fix_node, XY old_arrow_pos, int move_code)
   232 {
   169 {
   233   switch(move_code)
   170   switch(move_code)
   821         }
   758         }
   822       }
   759       }
   823     }
   760     }
   824   }
   761   }
   825   else
   762   else
   826   {
   763     {
   827     if(forming_edge!=INVALID)
   764       if(forming_edge!=INVALID)
   828     {
   765 	{
   829       forming_edge=INVALID;
   766 	  forming_edge=INVALID;
   830     }
   767 	}
   831     else
   768       else
   832     {
   769 	{
   833       std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
   770 	  std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
   834     }
   771 	}
   835   }
   772     }
   836 }
   773 }
       
   774 
       
   775 void 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 }