Little beauty fault is corrected.
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 double x1, x2, y1, y2;
34 edgesmap[i]->get_bounds(x1, y1, x2, y2);
36 edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
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("");
176 bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
180 case GDK_BUTTON_PRESS:
181 //we mark the location of the event to be able to calculate parameters of dragging
182 clicked_x=e->button.x;
183 clicked_y=e->button.y;
184 active_item=(get_item_at(e->button.x, e->button.y));
187 case GDK_BUTTON_RELEASE:
190 updateScrollRegion();
192 case GDK_MOTION_NOTIFY:
193 //we only have to do sg. if the mouse button is pressed
196 //new coordinates will be the old values,
197 //because the item will be moved to the
198 //new coordinate therefore the new movement
199 //has to be calculated from here
201 double dx=e->motion.x-clicked_x;
202 double dy=e->motion.y-clicked_y;
203 active_item->move(dx, dy);
204 clicked_x=e->motion.x;
205 clicked_y=e->motion.y;
207 //all the edges connected to the moved point has to be redrawn
211 for(;e!=INVALID;g.nextOut(e))
213 Gnome::Canvas::Points coos;
214 double x1, x2, y1, y2;
216 nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
217 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
219 nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
220 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
222 edgesmap[e]->property_points().set_value(coos);
224 edgesmap[e]->get_bounds(x1, y1, x2, y2);
226 edgetextmap[e]->property_x().set_value((x1+x2)/2);
227 edgetextmap[e]->property_y().set_value((y1+y2)/2);
231 for(;e!=INVALID;g.nextIn(e))
233 Gnome::Canvas::Points coos;
234 double x1, x2, y1, y2;
236 nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
237 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
239 nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
240 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
242 edgesmap[e]->property_points().set_value(coos);
244 edgesmap[e]->get_bounds(x1, y1, x2, y2);
246 edgetextmap[e]->property_x().set_value((x1+x2)/2);
247 edgetextmap[e]->property_y().set_value((y1+y2)/2);
255 bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
257 Gnome::Canvas::CanvasAA::on_expose_event(event);
263 void GraphDisplayerCanvas::zoomIn()
266 (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
269 void GraphDisplayerCanvas::zoomOut()
272 (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
275 void GraphDisplayerCanvas::zoomFit()
277 // get the height and width of the canvas
278 Gtk::Allocation a = get_allocation();
279 int aw = a.get_width();
280 int ah = a.get_height();
282 aw -= 5; if (aw < 0) aw = 0;
283 ah -= 5; if (ah < 0) ah = 0;
285 // get the bounding box of the graph
286 double wx1, wy1, wx2, wy2;
287 Gnome::Canvas::Item* pCanvasItem = root();
288 pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
290 // fit the graph to the window
291 double ppu1 = (double) aw / fabs(wx2 - wx1);
292 double ppu2 = (double) ah / fabs(wy2 - wy1);
293 set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2);
296 void GraphDisplayerCanvas::zoom100()
298 set_pixels_per_unit(1.0);
301 void GraphDisplayerCanvas::updateScrollRegion()
303 double wx1, wy1, wx2, wy2;
304 Gnome::Canvas::Item* pCanvasItem = root();
305 pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
306 set_scroll_region(wx1, wy1, wx2, wy2);
309 void GraphDisplayerCanvas::changeEditorialTool(int newtool)
311 actual_handler.disconnect();
313 if(actual_tool==CREATE_EDGE)
315 GdkEvent * generated=new GdkEvent();
316 generated->type=GDK_BUTTON_RELEASE;
317 generated->button.button=3;
318 create_edge_event_handler(generated);
326 actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false);
329 //it has to assigned to canvas, because all the canvas has to be monitored, not only the elements of the already drawn group
331 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
335 actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
339 actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraser_event_handler), false);
347 int GraphDisplayerCanvas::get_actual_tool()
352 bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e)
356 case GDK_BUTTON_PRESS:
357 //we mark the location of the event to be able to calculate parameters of dragging
358 clicked_x=e->button.x;
359 clicked_y=e->button.y;
360 active_item=(get_item_at(e->button.x, e->button.y));
362 for (NodeIt i(g); i!=INVALID; ++i)
364 if(nodesmap[i]==active_item)
369 switch(e->button.button)
379 case GDK_BUTTON_RELEASE:
383 updateScrollRegion();
385 case GDK_MOTION_NOTIFY:
386 //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
387 if(active_node!=INVALID)
389 //new coordinates will be the old values,
390 //because the item will be moved to the
391 //new coordinate therefore the new movement
392 //has to be calculated from here
394 double dx=e->motion.x-clicked_x;
395 double dy=e->motion.y-clicked_y;
397 active_item->move(dx, dy);
399 clicked_x=e->motion.x;
400 clicked_y=e->motion.y;
402 //all the edges connected to the moved point has to be redrawn
405 g.firstOut(ei,active_node);
407 for(;ei!=INVALID;g.nextOut(ei))
409 Gnome::Canvas::Points coos;
410 double x1, x2, y1, y2;
412 nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
413 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
415 nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
416 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
420 edgesmap[ei]->set_points(coos);
424 edgesmap[ei]->set_points(coos,true);
427 edgesmap[ei]->get_bounds(x1, y1, x2, y2);
429 edgetextmap[ei]->property_x().set_value((x1+x2)/2);
430 edgetextmap[ei]->property_y().set_value((y1+y2)/2);
433 g.firstIn(ei,active_node);
434 for(;ei!=INVALID;g.nextIn(ei))
436 Gnome::Canvas::Points coos;
437 double x1, x2, y1, y2;
439 nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
440 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
442 nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
443 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
447 edgesmap[ei]->set_points(coos);
451 edgesmap[ei]->set_points(coos,true);
454 edgesmap[ei]->get_bounds(x1, y1, x2, y2);
456 edgetextmap[ei]->property_x().set_value((x1+x2)/2);
457 edgetextmap[ei]->property_y().set_value((y1+y2)/2);
466 bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e)
471 //draw the new node in red at the clicked place
472 case GDK_BUTTON_PRESS:
475 active_node=NodeIt(g,g.addNode());
477 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
479 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
480 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
481 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
482 *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
483 (nodesmap[active_node])->show();
487 case GDK_MOTION_NOTIFY:
489 double world_motion_x, world_motion_y;
490 GdkEvent * generated=new GdkEvent();
491 window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y);
492 generated->motion.x=world_motion_x;
493 generated->motion.y=world_motion_y;
494 generated->type=GDK_MOTION_NOTIFY;
495 move_event_handler(generated);
499 //finalize the new node
500 case GDK_BUTTON_RELEASE:
502 *active_item << Gnome::Canvas::Properties::fill_color("blue");
505 updateScrollRegion();
513 bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e)
517 case GDK_BUTTON_PRESS:
518 //in edge creatino right button has special meaning
519 if(e->button.button!=3)
521 //there is not yet selected node
522 if(active_node==INVALID)
524 //we mark the location of the event to be able to calculate parameters of dragging
525 clicked_x=e->button.x;
526 clicked_y=e->button.y;
527 active_item=(get_item_at(e->button.x, e->button.y));
529 for (NodeIt i(g); i!=INVALID; ++i)
531 if(nodesmap[i]==active_item)
536 //the clicked item is really a node
537 if(active_node!=INVALID)
539 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
542 //clicked item was not a node. It could be e.g. edge.
548 //we only have to do sg. if the mouse button
549 // is pressed already once AND the click was
550 // on a node that was found in the set of
551 //nodes, and now we only search for the second
555 target_item=(get_item_at(e->button.x, e->button.y));
556 Graph::NodeIt target_node=INVALID;
557 for (NodeIt i(g); i!=INVALID; ++i)
559 if(nodesmap[i]==target_item)
564 //the clicked item is a node, the edge can be drawn
565 if(target_node!=INVALID)
567 *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
570 active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
572 //calculating coordinates of new edge
573 Gnome::Canvas::Points coos;
574 double x1, x2, y1, y2;
576 active_item->get_bounds(x1, y1, x2, y2);
577 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
579 target_item->get_bounds(x1, y1, x2, y2);
580 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
583 edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this);
584 *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
585 edgesmap[active_edge]->property_width_pixels().set_value(10);
587 //redraw nodes to blank terminations of the new edge
588 target_item->raise_to_top();
589 active_item->raise_to_top();
591 //initializing edge-text as well, to empty string
592 edgesmap[active_edge]->get_bounds(x1, y1, x2, y2);
593 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
594 edgetextmap[active_edge]->property_fill_color().set_value("black");
596 //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
604 case GDK_BUTTON_RELEASE:
606 //we clear settings in two cases
607 //1: the edge is ready (target_item has valid value)
608 //2: the edge creation is cancelled with right button
609 if((target_item)||(e->button.button==3))
613 *active_item << Gnome::Canvas::Properties::fill_color("blue");
618 *target_item << Gnome::Canvas::Properties::fill_color("blue");
631 bool GraphDisplayerCanvas::eraser_event_handler(GdkEvent* e)
635 case GDK_BUTTON_PRESS:
636 active_item=(get_item_at(e->button.x, e->button.y));
639 for (NodeIt i(g); i!=INVALID; ++i)
641 if(nodesmap[i]==active_item)
646 if(active_node==INVALID)
648 for (EdgeIt i(g); i!=INVALID; ++i)
650 if(edgesmap[i]==active_item)
656 *active_item << Gnome::Canvas::Properties::fill_color("red");
659 case GDK_BUTTON_RELEASE:
660 if(active_item==(get_item_at(e->button.x, e->button.y)))
662 if(active_node!=INVALID)
665 //collecting edges to delete
667 std::set<Graph::Edge> edges_to_delete;
669 g.firstOut(e,active_node);
670 for(;e!=INVALID;g.nextOut(e))
672 edges_to_delete.insert(e);
675 g.firstIn(e,active_node);
676 for(;e!=INVALID;g.nextIn(e))
678 edges_to_delete.insert(e);
681 //deleting collected edges
682 for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
684 delete_item(*edge_set_it);
686 delete_item(active_node);
688 //a simple edge was chosen
691 delete_item(active_edge);
696 //pointer was moved, deletion is cancelled
699 if(active_node!=INVALID)
701 *active_item << Gnome::Canvas::Properties::fill_color("blue");
705 *active_item << Gnome::Canvas::Properties::fill_color("green");
714 case GDK_MOTION_NOTIFY:
723 void GraphDisplayerCanvas::delete_item(NodeIt node_to_delete)
725 delete(nodesmap[node_to_delete]);
726 g.erase(node_to_delete);
729 void GraphDisplayerCanvas::delete_item(EdgeIt edge_to_delete)
731 delete(edgesmap[edge_to_delete]);
732 g.erase(edge_to_delete);
735 void GraphDisplayerCanvas::delete_item(Graph::Edge edge_to_delete)
737 delete(edgesmap[edge_to_delete]);
738 g.erase(edge_to_delete);