1 #include <graph_displayer_canvas.h>
2 #include <broken_edge.h>
5 GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm, MapStorage & ms):g(gr),nodesmap(g),edgesmap(g),edgetextmap(g),displayed_graph(*(root()), 0, 0),mapstorage(ms),isbutton(0),active_item(NULL),target_item(NULL)
8 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
9 actual_tool=CREATE_NODE;
14 //set_center_scroll_region(true);
16 //first edges are drawn, to hide joining with nodes later
18 for (EdgeIt i(g); i!=INVALID; ++i)
21 //drawing green lines, coordinates are from cm
23 Gnome::Canvas::Points coos;
24 coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
25 coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
27 edgesmap[i]=new BrokenEdge(displayed_graph, coos, *this);
28 *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
29 edgesmap[i]->property_width_pixels().set_value(10);
31 //initializing edge-text as well, to empty string
33 xy<double> text_pos=edgesmap[i]->get_arrow_pos();
34 text_pos+=(xy<double>(10,10));
36 edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
37 edgetextmap[i]->property_fill_color().set_value("black");
40 //afterwards nodes come to be drawn
43 int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
45 for (; i!=INVALID; ++i)
47 //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
49 if(cm[i].x>maxx)maxx=(int)cm[i].x;
50 if(cm[i].y>maxy)maxy=(int)cm[i].y;
51 if(cm[i].x<minx)minx=(int)cm[i].x;
52 if(cm[i].y<miny)miny=(int)cm[i].y;
54 //drawing bule nodes, with black line around them
56 nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
57 *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
58 *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
59 //!!!!!!! (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
65 GraphDisplayerCanvas::~GraphDisplayerCanvas()
68 //writing out the end state of the graph
69 //\todo all the maps has to be write out!
71 Graph::NodeMap <int> id(g);
72 Graph::NodeMap <double> xc(g);
73 Graph::NodeMap <double> yc(g);
77 for (NodeIt i(g); i!=INVALID; ++i)
80 nodesmap[i]->get_bounds(x1, y1, x2, y2);
87 GraphWriter<Graph> writer(std::cout,g);
89 writer.writeNodeMap("id", id);
90 writer.writeNodeMap("coordinates_x", xc);
91 writer.writeNodeMap("coordinates_y", yc);
95 int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
97 for (EdgeIt i(g); i!=INVALID; ++i)
99 int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
100 edgesmap[i]->property_width_pixels().set_value(w);
105 int GraphDisplayerCanvas::changeColor (std::string mapname)
108 //function maps the range of the maximum and
109 //the minimum of the nodemap to the range of
112 for (EdgeIt i(g); i!=INVALID; ++i)
114 double w=(*(mapstorage.edgemap_storage)[mapname])[i];
115 double max=mapstorage.maxOfEdgeMap(mapname);
116 double min=mapstorage.minOfEdgeMap(mapname);
118 //std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
122 color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
126 color.set_rgb_p (0, 100, 0);
129 edgesmap[i]->property_fill_color_gdk().set_value(color);
134 int GraphDisplayerCanvas::changeText (std::string mapname)
137 //the number in the map will be written on the edge
138 //EXCEPT when the name of the map is Text, because
139 //in that case empty string will be written, because
140 //that is the deleter map
141 //\todo isn't it a bit woodcutter?
143 for (EdgeIt i(g); i!=INVALID; ++i)
147 double number=(*(mapstorage.edgemap_storage)[mapname])[i];
148 int length=(int)(floor(log(number)/log(10)))+1;
149 int maxpos=(int)(pow(10,length-1));
150 int strl=length+1+RANGE;
151 char * str=new char[strl];
155 for(int j=0;j<strl;j++)
159 int digit=(int)(number/maxpos);
161 number-=digit*maxpos;
166 edgetextmap[i]->property_text().set_value(str);
170 edgetextmap[i]->property_text().set_value("");
177 bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
181 case GDK_BUTTON_PRESS:
182 //we mark the location of the event to be able to calculate parameters of dragging
183 clicked_x=e->button.x;
184 clicked_y=e->button.y;
185 active_item=(get_item_at(e->button.x, e->button.y));
188 case GDK_BUTTON_RELEASE:
191 updateScrollRegion();
193 case GDK_MOTION_NOTIFY:
194 //we only have to do sg. if the mouse button is pressed
197 //new coordinates will be the old values,
198 //because the item will be moved to the
199 //new coordinate therefore the new movement
200 //has to be calculated from here
202 double dx=e->motion.x-clicked_x;
203 double dy=e->motion.y-clicked_y;
204 active_item->move(dx, dy);
205 clicked_x=e->motion.x;
206 clicked_y=e->motion.y;
208 //all the edges connected to the moved point has to be redrawn
212 for(;e!=INVALID;g.nextOut(e))
214 Gnome::Canvas::Points coos;
215 double x1, x2, y1, y2;
217 nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
218 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
220 nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
221 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
223 edgesmap[e]->property_points().set_value(coos);
225 edgesmap[e]->get_bounds(x1, y1, x2, y2);
227 edgetextmap[e]->property_x().set_value((x1+x2)/2);
228 edgetextmap[e]->property_y().set_value((y1+y2)/2);
232 for(;e!=INVALID;g.nextIn(e))
234 Gnome::Canvas::Points coos;
235 double x1, x2, y1, y2;
237 nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
238 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
240 nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
241 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
243 edgesmap[e]->property_points().set_value(coos);
245 edgesmap[e]->get_bounds(x1, y1, x2, y2);
247 edgetextmap[e]->property_x().set_value((x1+x2)/2);
248 edgetextmap[e]->property_y().set_value((y1+y2)/2);
256 bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
258 Gnome::Canvas::CanvasAA::on_expose_event(event);
264 void GraphDisplayerCanvas::zoomIn()
267 (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
270 void GraphDisplayerCanvas::zoomOut()
273 (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
276 void GraphDisplayerCanvas::zoomFit()
278 // get the height and width of the canvas
279 Gtk::Allocation a = get_allocation();
280 int aw = a.get_width();
281 int ah = a.get_height();
283 aw -= 5; if (aw < 0) aw = 0;
284 ah -= 5; if (ah < 0) ah = 0;
286 // get the bounding box of the graph
287 double wx1, wy1, wx2, wy2;
288 Gnome::Canvas::Item* pCanvasItem = root();
289 pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
291 // fit the graph to the window
292 double ppu1 = (double) aw / fabs(wx2 - wx1);
293 double ppu2 = (double) ah / fabs(wy2 - wy1);
294 set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2);
297 void GraphDisplayerCanvas::zoom100()
299 set_pixels_per_unit(1.0);
302 void GraphDisplayerCanvas::updateScrollRegion()
304 double wx1, wy1, wx2, wy2;
305 Gnome::Canvas::Item* pCanvasItem = root();
306 pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
307 set_scroll_region(wx1, wy1, wx2, wy2);
310 void GraphDisplayerCanvas::changeEditorialTool(int newtool)
312 actual_handler.disconnect();
314 if(actual_tool==CREATE_EDGE)
316 GdkEvent * generated=new GdkEvent();
317 generated->type=GDK_BUTTON_RELEASE;
318 generated->button.button=3;
319 create_edge_event_handler(generated);
327 actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false);
330 //it has to assigned to canvas, because all the canvas has to be monitored, not only the elements of the already drawn group
332 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
336 actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
340 actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraser_event_handler), false);
348 int GraphDisplayerCanvas::get_actual_tool()
353 bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e)
357 case GDK_BUTTON_PRESS:
358 //we mark the location of the event to be able to calculate parameters of dragging
359 clicked_x=e->button.x;
360 clicked_y=e->button.y;
361 active_item=(get_item_at(e->button.x, e->button.y));
363 for (NodeIt i(g); i!=INVALID; ++i)
365 if(nodesmap[i]==active_item)
370 switch(e->button.button)
380 case GDK_BUTTON_RELEASE:
384 updateScrollRegion();
386 case GDK_MOTION_NOTIFY:
387 //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
388 if(active_node!=INVALID)
390 //new coordinates will be the old values,
391 //because the item will be moved to the
392 //new coordinate therefore the new movement
393 //has to be calculated from here
395 double dx=e->motion.x-clicked_x;
396 double dy=e->motion.y-clicked_y;
398 active_item->move(dx, dy);
400 clicked_x=e->motion.x;
401 clicked_y=e->motion.y;
403 //all the edges connected to the moved point has to be redrawn
406 g.firstOut(ei,active_node);
408 for(;ei!=INVALID;g.nextOut(ei))
410 Gnome::Canvas::Points coos;
411 double x1, x2, y1, y2;
413 nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
414 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
416 nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
417 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
421 edgesmap[ei]->set_points(coos);
425 edgesmap[ei]->set_points(coos,true);
428 xy<double> text_pos=edgesmap[ei]->get_arrow_pos();
429 text_pos+=(xy<double>(10,10));
430 edgetextmap[ei]->property_x().set_value(text_pos.x);
431 edgetextmap[ei]->property_y().set_value(text_pos.y);
434 g.firstIn(ei,active_node);
435 for(;ei!=INVALID;g.nextIn(ei))
437 Gnome::Canvas::Points coos;
438 double x1, x2, y1, y2;
440 nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
441 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
443 nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
444 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
448 edgesmap[ei]->set_points(coos);
452 edgesmap[ei]->set_points(coos,true);
455 xy<double> text_pos=edgesmap[ei]->get_arrow_pos();
456 text_pos+=(xy<double>(10,10));
457 edgetextmap[ei]->property_x().set_value(text_pos.x);
458 edgetextmap[ei]->property_y().set_value(text_pos.y);
467 bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e)
472 //draw the new node in red at the clicked place
473 case GDK_BUTTON_PRESS:
476 active_node=NodeIt(g,g.addNode());
478 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
480 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
481 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
482 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
483 *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
484 (nodesmap[active_node])->show();
488 case GDK_MOTION_NOTIFY:
490 double world_motion_x, world_motion_y;
491 GdkEvent * generated=new GdkEvent();
492 window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y);
493 generated->motion.x=world_motion_x;
494 generated->motion.y=world_motion_y;
495 generated->type=GDK_MOTION_NOTIFY;
496 move_event_handler(generated);
500 //finalize the new node
501 case GDK_BUTTON_RELEASE:
503 *active_item << Gnome::Canvas::Properties::fill_color("blue");
506 updateScrollRegion();
514 bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e)
518 case GDK_BUTTON_PRESS:
519 //in edge creatino right button has special meaning
520 if(e->button.button!=3)
522 //there is not yet selected node
523 if(active_node==INVALID)
525 //we mark the location of the event to be able to calculate parameters of dragging
526 clicked_x=e->button.x;
527 clicked_y=e->button.y;
528 active_item=(get_item_at(e->button.x, e->button.y));
530 for (NodeIt i(g); i!=INVALID; ++i)
532 if(nodesmap[i]==active_item)
537 //the clicked item is really a node
538 if(active_node!=INVALID)
540 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
543 //clicked item was not a node. It could be e.g. edge.
549 //we only have to do sg. if the mouse button
550 // is pressed already once AND the click was
551 // on a node that was found in the set of
552 //nodes, and now we only search for the second
556 target_item=(get_item_at(e->button.x, e->button.y));
557 Graph::NodeIt target_node=INVALID;
558 for (NodeIt i(g); i!=INVALID; ++i)
560 if(nodesmap[i]==target_item)
565 //the clicked item is a node, the edge can be drawn
566 if(target_node!=INVALID)
568 *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
571 active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
573 //calculating coordinates of new edge
574 Gnome::Canvas::Points coos;
575 double x1, x2, y1, y2;
577 active_item->get_bounds(x1, y1, x2, y2);
578 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
580 target_item->get_bounds(x1, y1, x2, y2);
581 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
584 edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this);
585 *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
586 edgesmap[active_edge]->property_width_pixels().set_value(10);
588 //redraw nodes to blank terminations of the new edge
589 target_item->raise_to_top();
590 active_item->raise_to_top();
592 //initializing edge-text as well, to empty string
593 xy<double> text_pos=edgesmap[active_edge]->get_arrow_pos();
594 text_pos+=(xy<double>(10,10));
596 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
597 edgetextmap[active_edge]->property_fill_color().set_value("black");
599 //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
607 case GDK_BUTTON_RELEASE:
609 //we clear settings in two cases
610 //1: the edge is ready (target_item has valid value)
611 //2: the edge creation is cancelled with right button
612 if((target_item)||(e->button.button==3))
616 *active_item << Gnome::Canvas::Properties::fill_color("blue");
621 *target_item << Gnome::Canvas::Properties::fill_color("blue");
634 bool GraphDisplayerCanvas::eraser_event_handler(GdkEvent* e)
638 case GDK_BUTTON_PRESS:
639 active_item=(get_item_at(e->button.x, e->button.y));
642 for (NodeIt i(g); i!=INVALID; ++i)
644 if(nodesmap[i]==active_item)
649 if(active_node==INVALID)
651 for (EdgeIt i(g); i!=INVALID; ++i)
653 if(edgesmap[i]==active_item)
659 *active_item << Gnome::Canvas::Properties::fill_color("red");
662 case GDK_BUTTON_RELEASE:
663 if(active_item==(get_item_at(e->button.x, e->button.y)))
665 if(active_node!=INVALID)
668 //collecting edges to delete
670 std::set<Graph::Edge> edges_to_delete;
672 g.firstOut(e,active_node);
673 for(;e!=INVALID;g.nextOut(e))
675 edges_to_delete.insert(e);
678 g.firstIn(e,active_node);
679 for(;e!=INVALID;g.nextIn(e))
681 edges_to_delete.insert(e);
684 //deleting collected edges
685 for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
687 delete_item(*edge_set_it);
689 delete_item(active_node);
691 //a simple edge was chosen
694 delete_item(active_edge);
699 //pointer was moved, deletion is cancelled
702 if(active_node!=INVALID)
704 *active_item << Gnome::Canvas::Properties::fill_color("blue");
708 *active_item << Gnome::Canvas::Properties::fill_color("green");
717 case GDK_MOTION_NOTIFY:
726 void GraphDisplayerCanvas::delete_item(NodeIt node_to_delete)
728 delete(nodesmap[node_to_delete]);
729 g.erase(node_to_delete);
732 void GraphDisplayerCanvas::delete_item(EdgeIt edge_to_delete)
734 delete(edgesmap[edge_to_delete]);
735 g.erase(edge_to_delete);
738 void GraphDisplayerCanvas::delete_item(Graph::Edge edge_to_delete)
740 delete(edgesmap[edge_to_delete]);
741 g.erase(edge_to_delete);
744 void GraphDisplayerCanvas::text_reposition(xy<double> new_place)
746 new_place+=(xy<double>(10,10));
747 edgetextmap[active_edge]->property_x().set_value(new_place.x);
748 edgetextmap[active_edge]->property_y().set_value(new_place.y);
751 void GraphDisplayerCanvas::toggle_edge_activity(BrokenEdge* active_bre, bool on)
755 if(active_edge!=INVALID)
757 std::cout << "ERROR!!!! Valid edge found!" << std::endl;
761 for (EdgeIt i(g); i!=INVALID; ++i)
763 if(edgesmap[i]==active_bre)
772 if(active_edge!=INVALID)
778 std::cout << "ERROR!!!! Invalid edge found!" << std::endl;