Improving tests.
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 if(actual_tool!=newtool)
19 actual_handler.disconnect();
25 GdkEvent * generated=new GdkEvent();
26 generated->type=GDK_BUTTON_RELEASE;
27 generated->button.button=3;
28 createEdgeEventHandler(generated);
48 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
52 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false);
56 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false);
60 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false);
65 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false);
69 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false);
78 int GraphDisplayerCanvas::getActualTool()
83 bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e)
87 case GDK_BUTTON_PRESS:
88 //we mark the location of the event to be able to calculate parameters of dragging
89 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
91 active_item=(get_item_at(clicked_x, clicked_y));
93 for (NodeIt i(g); i!=INVALID; ++i)
95 if(nodesmap[i]==active_item)
100 switch(e->button.button)
110 case GDK_BUTTON_RELEASE:
115 case GDK_MOTION_NOTIFY:
116 //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
117 if(active_node!=INVALID)
119 //new coordinates will be the old values,
120 //because the item will be moved to the
121 //new coordinate therefore the new movement
122 //has to be calculated from here
126 window_to_world (e->motion.x, e->motion.y, new_x, new_y);
128 double dx=new_x-clicked_x;
129 double dy=new_y-clicked_y;
131 //repositioning node and its text
132 active_item->move(dx, dy);
133 nodetextmap[active_node]->move(dx, dy);
138 //all the edges connected to the moved point has to be redrawn
141 g.firstOut(ei,active_node);
143 for(;ei!=INVALID;g.nextOut(ei))
145 Gnome::Canvas::Points coos;
146 double x1, x2, y1, y2;
148 nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
149 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
151 nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
152 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
156 edgesmap[ei]->setPoints(coos);
160 edgesmap[ei]->setPoints(coos,true);
163 //reposition of edgetext
164 xy<double> text_pos=edgesmap[ei]->getArrowPos();
165 text_pos+=(xy<double>(10,10));
166 edgetextmap[ei]->property_x().set_value(text_pos.x);
167 edgetextmap[ei]->property_y().set_value(text_pos.y);
170 g.firstIn(ei,active_node);
171 for(;ei!=INVALID;g.nextIn(ei))
173 Gnome::Canvas::Points coos;
174 double x1, x2, y1, y2;
176 nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
177 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
179 nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
180 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
184 edgesmap[ei]->setPoints(coos);
188 edgesmap[ei]->setPoints(coos,true);
191 xy<double> text_pos=edgesmap[ei]->getArrowPos();
192 text_pos+=(xy<double>(10,10));
193 edgetextmap[ei]->property_x().set_value(text_pos.x);
194 edgetextmap[ei]->property_y().set_value(text_pos.y);
203 bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
208 //draw the new node in red at the clicked place
209 case GDK_2BUTTON_PRESS:
210 std::cout << "double click" << std::endl;
212 case GDK_BUTTON_PRESS:
215 active_node=NodeIt(g,g.addNode());
217 //initiating values corresponding to new node in maps
219 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
222 target_item=get_item_at(clicked_x, clicked_y);
224 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
225 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
226 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
227 *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
228 (nodesmap[active_node])->show();
230 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, "");
231 nodetextmap[active_node]->property_fill_color().set_value("darkblue");
233 mapwin->updateNode(active_node);
238 case GDK_MOTION_NOTIFY:
240 GdkEvent * generated=new GdkEvent();
241 generated->motion.x=e->motion.x;
242 generated->motion.y=e->motion.y;
243 generated->type=GDK_MOTION_NOTIFY;
244 moveEventHandler(generated);
248 //finalize the new node
249 case GDK_BUTTON_RELEASE:
253 //Its appropriate color is given by update.
254 //*active_item << Gnome::Canvas::Properties::fill_color("blue");
258 //In this case the given color has to be overwritten, because the noe covers an other item.
259 *active_item << Gnome::Canvas::Properties::fill_color("lightblue");
271 bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e)
275 case GDK_BUTTON_PRESS:
276 //in edge creation right button has special meaning
277 if(e->button.button!=3)
279 //there is not yet selected node
280 if(active_node==INVALID)
282 //we mark the location of the event to be able to calculate parameters of dragging
284 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
286 active_item=(get_item_at(clicked_x, clicked_y));
288 for (NodeIt i(g); i!=INVALID; ++i)
290 if(nodesmap[i]==active_item)
295 //the clicked item is really a node
296 if(active_node!=INVALID)
298 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
301 //clicked item was not a node. It could be e.g. edge.
307 //we only have to do sg. if the mouse button
308 // is pressed already once AND the click was
309 // on a node that was found in the set of
310 //nodes, and now we only search for the second
314 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
315 target_item=(get_item_at(clicked_x, clicked_y));
316 Graph::NodeIt target_node=INVALID;
317 for (NodeIt i(g); i!=INVALID; ++i)
319 if(nodesmap[i]==target_item)
324 //the clicked item is a node, the edge can be drawn
325 if(target_node!=INVALID)
327 if(target_node!=active_node)
329 *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
332 active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
334 //initiating values corresponding to new edge in maps
335 mapstorage.initMapsForEdge(active_edge);
337 //calculating coordinates of new edge
338 Gnome::Canvas::Points coos;
339 double x1, x2, y1, y2;
341 active_item->get_bounds(x1, y1, x2, y2);
342 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
344 target_item->get_bounds(x1, y1, x2, y2);
345 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
348 edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this);
349 *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
350 edgesmap[active_edge]->property_width_pixels().set_value(10);
352 //redraw nodes to blank terminations of the new edge
353 target_item->raise_to_top();
354 active_item->raise_to_top();
356 //initializing edge-text as well, to empty string
357 xy<double> text_pos=edgesmap[active_edge]->getArrowPos();
358 text_pos+=(xy<double>(10,10));
360 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
361 edgetextmap[active_edge]->property_fill_color().set_value("darkgreen");
363 //updating its properties
364 mapwin->updateEdge(active_edge);
369 std::cout << "Loop edge is not yet implemented!" << std::endl;
372 //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
380 case GDK_BUTTON_RELEASE:
382 //we clear settings in two cases
383 //1: the edge is ready (target_item has valid value)
384 //2: the edge creation is cancelled with right button
385 if((target_item)||(e->button.button==3))
389 *active_item << Gnome::Canvas::Properties::fill_color("blue");
394 *target_item << Gnome::Canvas::Properties::fill_color("blue");
407 bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e)
411 case GDK_BUTTON_PRESS:
412 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
413 active_item=(get_item_at(clicked_x, clicked_y));
416 for (NodeIt i(g); i!=INVALID; ++i)
418 if(nodesmap[i]==active_item)
423 if(active_node==INVALID)
425 for (EdgeIt i(g); i!=INVALID; ++i)
427 if(edgesmap[i]==active_item)
435 *active_item << Gnome::Canvas::Properties::fill_color("red");
439 case GDK_BUTTON_RELEASE:
440 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
443 if( active_item == ( get_item_at (clicked_x, clicked_y) ) )
445 if(active_node!=INVALID)
448 //collecting edges to delete
450 std::set<Graph::Edge> edges_to_delete;
452 g.firstOut(e,active_node);
453 for(;e!=INVALID;g.nextOut(e))
455 edges_to_delete.insert(e);
458 g.firstIn(e,active_node);
459 for(;e!=INVALID;g.nextIn(e))
461 edges_to_delete.insert(e);
464 //deleting collected edges
465 for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
467 deleteItem(*edge_set_it);
469 deleteItem(active_node);
471 //a simple edge was chosen
474 deleteItem(active_edge);
477 //pointer was moved, deletion is cancelled
480 if(active_node!=INVALID)
482 *active_item << Gnome::Canvas::Properties::fill_color("blue");
486 *active_item << Gnome::Canvas::Properties::fill_color("green");
496 case GDK_MOTION_NOTIFY:
505 bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e)
511 std::cout << "Any button was pressed" << std::endl;
512 switch(((GdkEventKey*)e)->keyval)
515 std::cout << "A button was pressed" << std::endl;
523 case GDK_BUTTON_PRESS:
525 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
526 active_item=(get_item_at(clicked_x, clicked_y));
527 Graph::EdgeIt clicked_edge=INVALID;
528 for (EdgeIt i(g); i!=INVALID; ++i)
530 if(edgesmap[i]==active_item)
535 if(clicked_edge!=INVALID)
537 if(edgetextmap[clicked_edge]->property_text().get_value()!="")
539 active_edge=clicked_edge;
540 if(canvasentrywidget)
542 delete(canvasentrywidget);
545 entrywidget.set_text(edgetextmap[active_edge]->property_text().get_value());
546 xy<double> entry_coos;
547 entry_coos.x=(edgetextmap[active_edge])->property_x().get_value();
548 entry_coos.x-=edgetextmap[active_edge]->property_text_width().get_value()/2;
549 entry_coos.y=(edgetextmap[active_edge])->property_y().get_value();
550 entry_coos.y-=edgetextmap[active_edge]->property_text_height().get_value()*1.5/2;
551 canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget);
552 canvasentrywidget->property_width().set_value(edgetextmap[active_edge]->property_text_width().get_value()*1.5);
553 canvasentrywidget->property_height().set_value(edgetextmap[active_edge]->property_text_height().get_value()*1.5);
558 //mentse el -> problema, hogy nem tudja melyik map-be
569 bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e)
575 bool GraphDisplayerCanvas::entryWidgetChangeHandler(GdkEvent* e)
577 Glib::ustring mapvalue = entrywidget.get_text();
578 std::cout << mapvalue << std::endl;
583 void GraphDisplayerCanvas::deleteItem(NodeIt node_to_delete)
585 delete(nodetextmap[node_to_delete]);
586 delete(nodesmap[node_to_delete]);
587 g.erase(node_to_delete);
590 void GraphDisplayerCanvas::deleteItem(EdgeIt edge_to_delete)
592 delete(edgetextmap[edge_to_delete]);
593 delete(edgesmap[edge_to_delete]);
594 g.erase(edge_to_delete);
597 void GraphDisplayerCanvas::deleteItem(Graph::Edge edge_to_delete)
599 delete(edgetextmap[edge_to_delete]);
600 delete(edgesmap[edge_to_delete]);
601 g.erase(edge_to_delete);
604 void GraphDisplayerCanvas::textReposition(xy<double> new_place)
606 new_place+=(xy<double>(10,10));
607 edgetextmap[active_edge]->property_x().set_value(new_place.x);
608 edgetextmap[active_edge]->property_y().set_value(new_place.y);
611 void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on)
615 if(active_edge!=INVALID)
617 std::cout << "ERROR!!!! Valid edge found!" << std::endl;
621 for (EdgeIt i(g); i!=INVALID; ++i)
623 if(edgesmap[i]==active_bre)
632 if(active_edge!=INVALID)
638 std::cout << "ERROR!!!! Invalid edge found!" << std::endl;