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 createEdgeEventHandler(generated);
36 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
40 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false);
44 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false);
48 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false);
52 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false);
56 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false);
64 int GraphDisplayerCanvas::getActualTool()
69 bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e)
73 case GDK_BUTTON_PRESS:
74 //we mark the location of the event to be able to calculate parameters of dragging
75 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
77 active_item=(get_item_at(clicked_x, clicked_y));
79 for (NodeIt i(g); i!=INVALID; ++i)
81 if(nodesmap[i]==active_item)
86 switch(e->button.button)
96 case GDK_BUTTON_RELEASE:
101 case GDK_MOTION_NOTIFY:
102 //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
103 if(active_node!=INVALID)
105 //new coordinates will be the old values,
106 //because the item will be moved to the
107 //new coordinate therefore the new movement
108 //has to be calculated from here
112 window_to_world (e->motion.x, e->motion.y, new_x, new_y);
114 double dx=new_x-clicked_x;
115 double dy=new_y-clicked_y;
117 //repositioning node and its text
118 active_item->move(dx, dy);
119 nodetextmap[active_node]->move(dx, dy);
124 //all the edges connected to the moved point has to be redrawn
127 g.firstOut(ei,active_node);
129 for(;ei!=INVALID;g.nextOut(ei))
131 Gnome::Canvas::Points coos;
132 double x1, x2, y1, y2;
134 nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
135 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
137 nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
138 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
142 edgesmap[ei]->setPoints(coos);
146 edgesmap[ei]->setPoints(coos,true);
149 //reposition of edgetext
150 xy<double> text_pos=edgesmap[ei]->getArrowPos();
151 text_pos+=(xy<double>(10,10));
152 edgetextmap[ei]->property_x().set_value(text_pos.x);
153 edgetextmap[ei]->property_y().set_value(text_pos.y);
156 g.firstIn(ei,active_node);
157 for(;ei!=INVALID;g.nextIn(ei))
159 Gnome::Canvas::Points coos;
160 double x1, x2, y1, y2;
162 nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
163 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
165 nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
166 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
170 edgesmap[ei]->setPoints(coos);
174 edgesmap[ei]->setPoints(coos,true);
177 xy<double> text_pos=edgesmap[ei]->getArrowPos();
178 text_pos+=(xy<double>(10,10));
179 edgetextmap[ei]->property_x().set_value(text_pos.x);
180 edgetextmap[ei]->property_y().set_value(text_pos.y);
189 bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
194 //draw the new node in red at the clicked place
195 case GDK_2BUTTON_PRESS:
196 std::cout << "double click" << std::endl;
198 case GDK_BUTTON_PRESS:
201 active_node=NodeIt(g,g.addNode());
203 //initiating values corresponding to new node in maps
205 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
208 target_item=get_item_at(clicked_x, clicked_y);
210 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
211 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
212 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
213 *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
214 (nodesmap[active_node])->show();
216 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, "");
217 nodetextmap[active_node]->property_fill_color().set_value("darkblue");
219 mapwin->updateNode(active_node);
224 case GDK_MOTION_NOTIFY:
226 GdkEvent * generated=new GdkEvent();
227 generated->motion.x=e->motion.x;
228 generated->motion.y=e->motion.y;
229 generated->type=GDK_MOTION_NOTIFY;
230 moveEventHandler(generated);
234 //finalize the new node
235 case GDK_BUTTON_RELEASE:
239 //Its appropriate color is given by update.
240 //*active_item << Gnome::Canvas::Properties::fill_color("blue");
244 //In this case the given color has to be overwritten, because the noe covers an other item.
245 *active_item << Gnome::Canvas::Properties::fill_color("lightblue");
257 bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e)
261 case GDK_BUTTON_PRESS:
262 //in edge creation right button has special meaning
263 if(e->button.button!=3)
265 //there is not yet selected node
266 if(active_node==INVALID)
268 //we mark the location of the event to be able to calculate parameters of dragging
270 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
272 active_item=(get_item_at(clicked_x, clicked_y));
274 for (NodeIt i(g); i!=INVALID; ++i)
276 if(nodesmap[i]==active_item)
281 //the clicked item is really a node
282 if(active_node!=INVALID)
284 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
287 //clicked item was not a node. It could be e.g. edge.
293 //we only have to do sg. if the mouse button
294 // is pressed already once AND the click was
295 // on a node that was found in the set of
296 //nodes, and now we only search for the second
300 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
301 target_item=(get_item_at(clicked_x, clicked_y));
302 Graph::NodeIt target_node=INVALID;
303 for (NodeIt i(g); i!=INVALID; ++i)
305 if(nodesmap[i]==target_item)
310 //the clicked item is a node, the edge can be drawn
311 if(target_node!=INVALID)
313 if(target_node!=active_node)
315 *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
318 active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
320 //initiating values corresponding to new edge in maps
321 mapstorage.initMapsForEdge(active_edge);
323 //calculating coordinates of new edge
324 Gnome::Canvas::Points coos;
325 double x1, x2, y1, y2;
327 active_item->get_bounds(x1, y1, x2, y2);
328 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
330 target_item->get_bounds(x1, y1, x2, y2);
331 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
334 edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this);
335 *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
336 edgesmap[active_edge]->property_width_pixels().set_value(10);
338 //redraw nodes to blank terminations of the new edge
339 target_item->raise_to_top();
340 active_item->raise_to_top();
342 //initializing edge-text as well, to empty string
343 xy<double> text_pos=edgesmap[active_edge]->getArrowPos();
344 text_pos+=(xy<double>(10,10));
346 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
347 edgetextmap[active_edge]->property_fill_color().set_value("darkgreen");
349 //updating its properties
350 mapwin->updateEdge(active_edge);
355 std::cout << "Loop edge is not yet implemented!" << std::endl;
358 //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
366 case GDK_BUTTON_RELEASE:
368 //we clear settings in two cases
369 //1: the edge is ready (target_item has valid value)
370 //2: the edge creation is cancelled with right button
371 if((target_item)||(e->button.button==3))
375 *active_item << Gnome::Canvas::Properties::fill_color("blue");
380 *target_item << Gnome::Canvas::Properties::fill_color("blue");
393 bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e)
397 case GDK_BUTTON_PRESS:
398 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
399 active_item=(get_item_at(clicked_x, clicked_y));
402 for (NodeIt i(g); i!=INVALID; ++i)
404 if(nodesmap[i]==active_item)
409 if(active_node==INVALID)
411 for (EdgeIt i(g); i!=INVALID; ++i)
413 if(edgesmap[i]==active_item)
421 *active_item << Gnome::Canvas::Properties::fill_color("red");
425 case GDK_BUTTON_RELEASE:
426 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
429 if( active_item == ( get_item_at (clicked_x, clicked_y) ) )
431 if(active_node!=INVALID)
434 //collecting edges to delete
436 std::set<Graph::Edge> edges_to_delete;
438 g.firstOut(e,active_node);
439 for(;e!=INVALID;g.nextOut(e))
441 edges_to_delete.insert(e);
444 g.firstIn(e,active_node);
445 for(;e!=INVALID;g.nextIn(e))
447 edges_to_delete.insert(e);
450 //deleting collected edges
451 for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
453 deleteItem(*edge_set_it);
455 deleteItem(active_node);
457 //a simple edge was chosen
460 deleteItem(active_edge);
463 //pointer was moved, deletion is cancelled
466 if(active_node!=INVALID)
468 *active_item << Gnome::Canvas::Properties::fill_color("blue");
472 *active_item << Gnome::Canvas::Properties::fill_color("green");
482 case GDK_MOTION_NOTIFY:
491 bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e)
495 case GDK_BUTTON_PRESS:
496 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
497 active_item=(get_item_at(clicked_x, clicked_y));
499 for (EdgeIt i(g); i!=INVALID; ++i)
501 if(edgesmap[i]==active_item)
506 if((active_edge!=INVALID)&&(edgetextmap[active_edge]->property_text().get_value()!=""))
508 if(canvasentrywidget)
510 delete(canvasentrywidget);
513 entrywidget.set_text(edgetextmap[active_edge]->property_text().get_value());
514 xy<double> entry_coos;
515 entry_coos.x=(edgetextmap[active_edge])->property_x().get_value();
516 entry_coos.x-=edgetextmap[active_edge]->property_text_width().get_value()/2;
517 entry_coos.y=(edgetextmap[active_edge])->property_y().get_value();
518 entry_coos.y-=edgetextmap[active_edge]->property_text_height().get_value()*1.5/2;
519 canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget);
520 canvasentrywidget->property_width().set_value(edgetextmap[active_edge]->property_text_width().get_value()*1.5);
521 canvasentrywidget->property_height().set_value(edgetextmap[active_edge]->property_text_height().get_value()*1.5);
530 bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e)
536 void GraphDisplayerCanvas::deleteItem(NodeIt node_to_delete)
538 delete(nodetextmap[node_to_delete]);
539 delete(nodesmap[node_to_delete]);
540 g.erase(node_to_delete);
543 void GraphDisplayerCanvas::deleteItem(EdgeIt edge_to_delete)
545 delete(edgetextmap[edge_to_delete]);
546 delete(edgesmap[edge_to_delete]);
547 g.erase(edge_to_delete);
550 void GraphDisplayerCanvas::deleteItem(Graph::Edge edge_to_delete)
552 delete(edgetextmap[edge_to_delete]);
553 delete(edgesmap[edge_to_delete]);
554 g.erase(edge_to_delete);
557 void GraphDisplayerCanvas::textReposition(xy<double> new_place)
559 new_place+=(xy<double>(10,10));
560 edgetextmap[active_edge]->property_x().set_value(new_place.x);
561 edgetextmap[active_edge]->property_y().set_value(new_place.y);
564 void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on)
568 if(active_edge!=INVALID)
570 std::cout << "ERROR!!!! Valid edge found!" << std::endl;
574 for (EdgeIt i(g); i!=INVALID; ++i)
576 if(edgesmap[i]==active_bre)
585 if(active_edge!=INVALID)
591 std::cout << "ERROR!!!! Invalid edge found!" << std::endl;