graph_displayer_canvas.cc
branchgui
changeset 12 1db7a6dd17f8
parent 11 09b2a893fc9d
child 13 0b2281cc1e32
     1.1 --- a/graph_displayer_canvas.cc	Mon Jun 13 10:30:08 2005 +0000
     1.2 +++ b/graph_displayer_canvas.cc	Mon Jun 13 19:49:33 2005 +0000
     1.3 @@ -6,6 +6,9 @@
     1.4    
     1.5    actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
     1.6  
     1.7 +  active_node=INVALID;
     1.8 +  active_edge=INVALID;
     1.9 +
    1.10    //set_center_scroll_region(true);
    1.11  
    1.12    //first edges are drawn, to hide joining with nodes later
    1.13 @@ -310,9 +313,12 @@
    1.14      case MOVE:
    1.15        actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false);
    1.16        break;
    1.17 +
    1.18 +      //it has to assigned to canvas, because all the canvas has to be monitored, not only the elements of the already drawn group
    1.19      case CREATE_NODE:
    1.20        actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
    1.21        break;
    1.22 +
    1.23      case CREATE_EDGE:
    1.24        actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
    1.25        break;
    1.26 @@ -343,11 +349,12 @@
    1.27      case GDK_BUTTON_RELEASE:
    1.28        isbutton=false;
    1.29        active_item=NULL;
    1.30 +      active_node=INVALID;
    1.31        updateScrollRegion();
    1.32        break;
    1.33      case GDK_MOTION_NOTIFY:
    1.34 -      //we only have to do sg. if the mouse button is pressed
    1.35 -      if(isbutton)
    1.36 +      //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
    1.37 +      if(active_node!=INVALID)
    1.38        {
    1.39  	//new coordinates will be the old values,
    1.40  	//because the item will be moved to the
    1.41 @@ -416,6 +423,8 @@
    1.42  {
    1.43    switch(e->type)
    1.44      {
    1.45 +
    1.46 +      //draw the new node in red at the clicked place
    1.47      case GDK_BUTTON_PRESS:
    1.48        isbutton=true;
    1.49  
    1.50 @@ -429,6 +438,8 @@
    1.51        *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
    1.52        (nodesmap[active_node])->show();
    1.53        break;
    1.54 +
    1.55 +      //move the new node
    1.56      case GDK_MOTION_NOTIFY:
    1.57        {
    1.58  	double world_motion_x, world_motion_y;
    1.59 @@ -440,10 +451,13 @@
    1.60  	move_event_handler(generated);      
    1.61  	break;
    1.62        }
    1.63 +
    1.64 +      //finalize the new node
    1.65      case GDK_BUTTON_RELEASE:
    1.66        isbutton=false;
    1.67        *active_item << Gnome::Canvas::Properties::fill_color("blue");
    1.68        active_item=NULL;
    1.69 +      active_node=INVALID;
    1.70        updateScrollRegion();
    1.71        break;
    1.72      default:
    1.73 @@ -457,73 +471,111 @@
    1.74    switch(e->type)
    1.75      {
    1.76      case GDK_BUTTON_PRESS:
    1.77 -      if(!active_item)
    1.78 +      //in edge creatino right button has special meaning
    1.79 +      if(e->button.button!=3)
    1.80  	{
    1.81 -	  //we mark the location of the event to be able to calculate parameters of dragging
    1.82 -	  clicked_x=e->button.x;
    1.83 -	  clicked_y=e->button.y;
    1.84 -	  active_item=(get_item_at(e->button.x, e->button.y));
    1.85 -	  active_node=INVALID;
    1.86 -	  for (NodeIt i(g); i!=INVALID; ++i)
    1.87 +	  //there is not yet selected node
    1.88 +	  if(active_node==INVALID)
    1.89  	    {
    1.90 -	      if(nodesmap[i]==active_item)
    1.91 +	      //we mark the location of the event to be able to calculate parameters of dragging
    1.92 +	      clicked_x=e->button.x;
    1.93 +	      clicked_y=e->button.y;
    1.94 +	      active_item=(get_item_at(e->button.x, e->button.y));
    1.95 +	      active_node=INVALID;
    1.96 +	      for (NodeIt i(g); i!=INVALID; ++i)
    1.97  		{
    1.98 -		  active_node=i;
    1.99 +		  if(nodesmap[i]==active_item)
   1.100 +		    {
   1.101 +		      active_node=i;
   1.102 +		    }
   1.103 +		}
   1.104 +	      //the clicked item is really a node
   1.105 +	      if(active_node!=INVALID)
   1.106 +		{
   1.107 +		  *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
   1.108 +		  isbutton=true;
   1.109 +		}
   1.110 +	      //clicked item was not a node. It could be e.g. edge.
   1.111 +	      else
   1.112 +		{
   1.113 +		  active_item=NULL;
   1.114  		}
   1.115  	    }
   1.116 -	  *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
   1.117 -	  isbutton=true;
   1.118 -	}
   1.119 -      else
   1.120 -	{
   1.121 -	  target_item=(get_item_at(e->button.x, e->button.y));
   1.122 -	  Graph::NodeIt target_node=INVALID;
   1.123 -	  for (NodeIt i(g); i!=INVALID; ++i)
   1.124 +	  //we only have to do sg. if the mouse button
   1.125 +	  // is pressed already once AND the click was
   1.126 +	  // on a node that was found in the set of 
   1.127 +	  //nodes, and now we only search for the second 
   1.128 +	  //node
   1.129 +	  else
   1.130  	    {
   1.131 -	      if(nodesmap[i]==target_item)
   1.132 +	      target_item=(get_item_at(e->button.x, e->button.y));
   1.133 +	      Graph::NodeIt target_node=INVALID;
   1.134 +	      for (NodeIt i(g); i!=INVALID; ++i)
   1.135  		{
   1.136 -		  target_node=i;
   1.137 +		  if(nodesmap[i]==target_item)
   1.138 +		    {
   1.139 +		      target_node=i;
   1.140 +		    }
   1.141 +		}
   1.142 +	      //the clicked item is a node, the edge can be drawn
   1.143 +	      if(target_node!=INVALID)
   1.144 +		{
   1.145 +		  *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
   1.146 +
   1.147 +		  //creating new edge
   1.148 +		  active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
   1.149 +	  
   1.150 +		  //calculating coordinates of new edge
   1.151 +		  Gnome::Canvas::Points coos;
   1.152 +		  double x1, x2, y1, y2;
   1.153 +	  
   1.154 +		  active_item->get_bounds(x1, y1, x2, y2);
   1.155 +		  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   1.156 +
   1.157 +		  target_item->get_bounds(x1, y1, x2, y2);
   1.158 +		  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   1.159 +
   1.160 +		  //drawing new edge
   1.161 +		  edgesmap[active_edge]=new Gnome::Canvas::Line(displayed_graph, coos);
   1.162 +		  *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
   1.163 +		  edgesmap[active_edge]->property_width_pixels().set_value(10);
   1.164 +
   1.165 +		  //redraw nodes to blank terminations of the new edge
   1.166 +		  target_item->raise_to_top();
   1.167 +		  active_item->raise_to_top();
   1.168 +
   1.169 +		  //initializing edge-text as well, to empty string
   1.170 +		  edgesmap[active_edge]->get_bounds(x1, y1, x2, y2);
   1.171 +		  edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
   1.172 +		  edgetextmap[active_edge]->property_fill_color().set_value("black");
   1.173 +		}
   1.174 +	      //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
   1.175 +	      else
   1.176 +		{
   1.177 +		  target_item=NULL;
   1.178  		}
   1.179  	    }
   1.180 -	  *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
   1.181 -
   1.182 -	  //creating new edge
   1.183 -	  //	  Graph::Edge new_edge=g.addEdge(active_node, target_node);
   1.184 -	  active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
   1.185 -	  
   1.186 -	  //calculating coordinates of new edge
   1.187 -	  Gnome::Canvas::Points coos;
   1.188 -	  double x1, x2, y1, y2;
   1.189 -	  
   1.190 -	  active_item->get_bounds(x1, y1, x2, y2);
   1.191 -	  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   1.192 -
   1.193 -	  target_item->get_bounds(x1, y1, x2, y2);
   1.194 -	  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
   1.195 -
   1.196 -	  //drawing new edge
   1.197 -	  edgesmap[active_edge]=new Gnome::Canvas::Line(displayed_graph, coos);
   1.198 -	  *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
   1.199 -	  edgesmap[active_edge]->property_width_pixels().set_value(10);
   1.200 -
   1.201 -	  //redraw nodes to blank terminations of the new edge
   1.202 -	  target_item->raise_to_top();
   1.203 -	  active_item->raise_to_top();
   1.204 -
   1.205 -	  //initializing edge-text as well, to empty string
   1.206 -	  edgesmap[active_edge]->get_bounds(x1, y1, x2, y2);
   1.207 -	  edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
   1.208 -	  edgetextmap[active_edge]->property_fill_color().set_value("black");
   1.209  	}
   1.210        break;
   1.211      case GDK_BUTTON_RELEASE:
   1.212        isbutton=false;
   1.213 -      if(target_item)
   1.214 +      //we clear settings in two cases
   1.215 +      //1: the edge is ready (target_item has valid value)
   1.216 +      //2: the edge creation is cancelled with right button
   1.217 +      if((target_item)||(e->button.button==3))
   1.218  	{
   1.219 -	  *active_item << Gnome::Canvas::Properties::fill_color("blue");
   1.220 -	  *target_item << Gnome::Canvas::Properties::fill_color("blue");
   1.221 -	  active_item=NULL;
   1.222 -	  target_item=NULL;
   1.223 +	  if(active_item)
   1.224 +	    {
   1.225 +	      *active_item << Gnome::Canvas::Properties::fill_color("blue");
   1.226 +	      active_item=NULL;
   1.227 +	    }
   1.228 +	  if(target_item)
   1.229 +	    {
   1.230 +	      *target_item << Gnome::Canvas::Properties::fill_color("blue");
   1.231 +	      target_item=NULL;
   1.232 +	    }
   1.233 +	  active_node=INVALID;
   1.234 +	  active_edge=INVALID;
   1.235  	}
   1.236        break;
   1.237      default: