1 #include <graph_displayer_canvas.h>
2 #include <broken_edge.h>
6 bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
8 Gnome::Canvas::CanvasAA::on_expose_event(event);
14 void GraphDisplayerCanvas::changeEditorialTool(int newtool)
16 actual_handler.disconnect();
18 if(actual_tool==CREATE_EDGE)
20 GdkEvent * generated=new GdkEvent();
21 generated->type=GDK_BUTTON_RELEASE;
22 generated->button.button=3;
23 create_edge_event_handler(generated);
31 actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false);
34 //it has to assigned to canvas, because all the canvas has to be monitored, not only the elements of the already drawn group
36 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
40 actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
44 actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraser_event_handler), false);
52 int GraphDisplayerCanvas::get_actual_tool()
57 bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e)
61 case GDK_BUTTON_PRESS:
62 //we mark the location of the event to be able to calculate parameters of dragging
63 clicked_x=e->button.x;
64 clicked_y=e->button.y;
65 active_item=(get_item_at(e->button.x, e->button.y));
67 for (NodeIt i(g); i!=INVALID; ++i)
69 if(nodesmap[i]==active_item)
74 switch(e->button.button)
84 case GDK_BUTTON_RELEASE:
90 case GDK_MOTION_NOTIFY:
91 //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
92 if(active_node!=INVALID)
94 //new coordinates will be the old values,
95 //because the item will be moved to the
96 //new coordinate therefore the new movement
97 //has to be calculated from here
99 double dx=e->motion.x-clicked_x;
100 double dy=e->motion.y-clicked_y;
102 //repositioning node and its text
103 active_item->move(dx, dy);
104 nodetextmap[active_node]->move(dx, dy);
106 clicked_x=e->motion.x;
107 clicked_y=e->motion.y;
109 //all the edges connected to the moved point has to be redrawn
112 g.firstOut(ei,active_node);
114 for(;ei!=INVALID;g.nextOut(ei))
116 Gnome::Canvas::Points coos;
117 double x1, x2, y1, y2;
119 nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
120 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
122 nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
123 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
127 edgesmap[ei]->set_points(coos);
131 edgesmap[ei]->set_points(coos,true);
134 //reposition of edgetext
135 xy<double> text_pos=edgesmap[ei]->get_arrow_pos();
136 text_pos+=(xy<double>(10,10));
137 edgetextmap[ei]->property_x().set_value(text_pos.x);
138 edgetextmap[ei]->property_y().set_value(text_pos.y);
141 g.firstIn(ei,active_node);
142 for(;ei!=INVALID;g.nextIn(ei))
144 Gnome::Canvas::Points coos;
145 double x1, x2, y1, y2;
147 nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
148 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
150 nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
151 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
155 edgesmap[ei]->set_points(coos);
159 edgesmap[ei]->set_points(coos,true);
162 xy<double> text_pos=edgesmap[ei]->get_arrow_pos();
163 text_pos+=(xy<double>(10,10));
164 edgetextmap[ei]->property_x().set_value(text_pos.x);
165 edgetextmap[ei]->property_y().set_value(text_pos.y);
174 bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e)
179 //draw the new node in red at the clicked place
180 case GDK_BUTTON_PRESS:
183 active_node=NodeIt(g,g.addNode());
185 //initiating values corresponding to new node in maps
188 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
190 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
191 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
192 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
193 *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
194 (nodesmap[active_node])->show();
196 nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph, clicked_x+node_property_defaults[N_RADIUS]+5, clicked_y+node_property_defaults[N_RADIUS]+5, "");
197 nodetextmap[active_node]->property_fill_color().set_value("darkblue");
199 mapwin->update_node(active_node);
204 case GDK_MOTION_NOTIFY:
206 double world_motion_x, world_motion_y;
207 GdkEvent * generated=new GdkEvent();
208 window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y);
209 generated->motion.x=world_motion_x;
210 generated->motion.y=world_motion_y;
211 generated->type=GDK_MOTION_NOTIFY;
212 move_event_handler(generated);
216 //finalize the new node
217 case GDK_BUTTON_RELEASE:
219 *active_item << Gnome::Canvas::Properties::fill_color("blue");
222 updateScrollRegion();
230 bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e)
234 case GDK_BUTTON_PRESS:
235 //in edge creation right button has special meaning
236 if(e->button.button!=3)
238 //there is not yet selected node
239 if(active_node==INVALID)
241 //we mark the location of the event to be able to calculate parameters of dragging
242 clicked_x=e->button.x;
243 clicked_y=e->button.y;
244 active_item=(get_item_at(e->button.x, e->button.y));
246 for (NodeIt i(g); i!=INVALID; ++i)
248 if(nodesmap[i]==active_item)
253 //the clicked item is really a node
254 if(active_node!=INVALID)
256 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
259 //clicked item was not a node. It could be e.g. edge.
265 //we only have to do sg. if the mouse button
266 // is pressed already once AND the click was
267 // on a node that was found in the set of
268 //nodes, and now we only search for the second
272 target_item=(get_item_at(e->button.x, e->button.y));
273 Graph::NodeIt target_node=INVALID;
274 for (NodeIt i(g); i!=INVALID; ++i)
276 if(nodesmap[i]==target_item)
281 //the clicked item is a node, the edge can be drawn
282 if(target_node!=INVALID)
284 if(target_node!=active_node)
286 *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
289 active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
291 //initiating values corresponding to new edge in maps
292 mapstorage.init_maps_for_edge(active_edge);
294 //calculating coordinates of new edge
295 Gnome::Canvas::Points coos;
296 double x1, x2, y1, y2;
298 active_item->get_bounds(x1, y1, x2, y2);
299 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
301 target_item->get_bounds(x1, y1, x2, y2);
302 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
305 edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this);
306 *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
307 edgesmap[active_edge]->property_width_pixels().set_value(10);
309 //redraw nodes to blank terminations of the new edge
310 target_item->raise_to_top();
311 active_item->raise_to_top();
313 //initializing edge-text as well, to empty string
314 xy<double> text_pos=edgesmap[active_edge]->get_arrow_pos();
315 text_pos+=(xy<double>(10,10));
317 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
318 edgetextmap[active_edge]->property_fill_color().set_value("darkgreen");
320 //updating its properties
321 mapwin->update_edge(active_edge);
326 std::cout << "Loop edge is not yet implemented!" << std::endl;
329 //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
337 case GDK_BUTTON_RELEASE:
339 //we clear settings in two cases
340 //1: the edge is ready (target_item has valid value)
341 //2: the edge creation is cancelled with right button
342 if((target_item)||(e->button.button==3))
346 *active_item << Gnome::Canvas::Properties::fill_color("blue");
351 *target_item << Gnome::Canvas::Properties::fill_color("blue");
364 bool GraphDisplayerCanvas::eraser_event_handler(GdkEvent* e)
368 case GDK_BUTTON_PRESS:
369 active_item=(get_item_at(e->button.x, e->button.y));
372 for (NodeIt i(g); i!=INVALID; ++i)
374 if(nodesmap[i]==active_item)
379 if(active_node==INVALID)
381 for (EdgeIt i(g); i!=INVALID; ++i)
383 if(edgesmap[i]==active_item)
389 *active_item << Gnome::Canvas::Properties::fill_color("red");
392 case GDK_BUTTON_RELEASE:
393 if(active_item==(get_item_at(e->button.x, e->button.y)))
395 if(active_node!=INVALID)
398 //collecting edges to delete
400 std::set<Graph::Edge> edges_to_delete;
402 g.firstOut(e,active_node);
403 for(;e!=INVALID;g.nextOut(e))
405 edges_to_delete.insert(e);
408 g.firstIn(e,active_node);
409 for(;e!=INVALID;g.nextIn(e))
411 edges_to_delete.insert(e);
414 //deleting collected edges
415 for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
417 delete_item(*edge_set_it);
419 delete_item(active_node);
421 //a simple edge was chosen
424 delete_item(active_edge);
429 //pointer was moved, deletion is cancelled
432 if(active_node!=INVALID)
434 *active_item << Gnome::Canvas::Properties::fill_color("blue");
438 *active_item << Gnome::Canvas::Properties::fill_color("green");
447 case GDK_MOTION_NOTIFY:
456 void GraphDisplayerCanvas::delete_item(NodeIt node_to_delete)
458 delete(nodetextmap[node_to_delete]);
459 delete(nodesmap[node_to_delete]);
460 g.erase(node_to_delete);
463 void GraphDisplayerCanvas::delete_item(EdgeIt edge_to_delete)
465 delete(edgetextmap[edge_to_delete]);
466 delete(edgesmap[edge_to_delete]);
467 g.erase(edge_to_delete);
470 void GraphDisplayerCanvas::delete_item(Graph::Edge edge_to_delete)
472 delete(edgetextmap[edge_to_delete]);
473 delete(edgesmap[edge_to_delete]);
474 g.erase(edge_to_delete);
477 void GraphDisplayerCanvas::text_reposition(xy<double> new_place)
479 new_place+=(xy<double>(10,10));
480 edgetextmap[active_edge]->property_x().set_value(new_place.x);
481 edgetextmap[active_edge]->property_y().set_value(new_place.y);
484 void GraphDisplayerCanvas::toggle_edge_activity(BrokenEdge* active_bre, bool on)
488 if(active_edge!=INVALID)
490 std::cout << "ERROR!!!! Valid edge found!" << std::endl;
494 for (EdgeIt i(g); i!=INVALID; ++i)
496 if(edgesmap[i]==active_bre)
505 if(active_edge!=INVALID)
511 std::cout << "ERROR!!!! Invalid edge found!" << std::endl;