Edge creation is available.
1 #include <graph_displayer_canvas.h>
4 GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm, MapStorage & ms):g(gr),nodesmap(g),edgesmap(g),edgetextmap(g),displayed_graph(*(root()), 0, 0),mapstorage(ms),isbutton(false),active_item(NULL),target_item(NULL)
7 actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
9 //set_center_scroll_region(true);
11 //first edges are drawn, to hide joining with nodes later
13 for (EdgeIt i(g); i!=INVALID; ++i)
16 //drawing green lines, coordinates are from cm
18 Gnome::Canvas::Points coos;
19 coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
20 coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
22 edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
23 *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
24 edgesmap[i]->property_width_pixels().set_value(10);
26 //initializing edge-text as well, to empty string
28 double x1, x2, y1, y2;
29 edgesmap[i]->get_bounds(x1, y1, x2, y2);
31 edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
32 edgetextmap[i]->property_fill_color().set_value("black");
35 //afterwards nodes come to be drawn
38 int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
40 for (; i!=INVALID; ++i)
42 //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
44 if(cm[i].x>maxx)maxx=(int)cm[i].x;
45 if(cm[i].y>maxy)maxy=(int)cm[i].y;
46 if(cm[i].x<minx)minx=(int)cm[i].x;
47 if(cm[i].y<miny)miny=(int)cm[i].y;
49 //drawing bule nodes, with black line around them
51 nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
52 *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
53 *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
54 //!!!!!!! (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
60 GraphDisplayerCanvas::~GraphDisplayerCanvas()
63 //writing out the end state of the graph
64 //\todo all the maps has to be write out!
66 Graph::NodeMap <int> id(g);
67 Graph::NodeMap <double> xc(g);
68 Graph::NodeMap <double> yc(g);
72 for (NodeIt i(g); i!=INVALID; ++i)
75 nodesmap[i]->get_bounds(x1, y1, x2, y2);
82 GraphWriter<Graph> writer(std::cout,g);
84 writer.writeNodeMap("id", id);
85 writer.writeNodeMap("coordinates_x", xc);
86 writer.writeNodeMap("coordinates_y", yc);
90 int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
92 for (EdgeIt i(g); i!=INVALID; ++i)
94 int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
95 edgesmap[i]->property_width_pixels().set_value(w);
100 int GraphDisplayerCanvas::changeColor (std::string mapname)
103 //function maps the range of the maximum and
104 //the minimum of the nodemap to the range of
107 for (EdgeIt i(g); i!=INVALID; ++i)
109 double w=(*(mapstorage.edgemap_storage)[mapname])[i];
110 double max=mapstorage.maxOfEdgeMap(mapname);
111 double min=mapstorage.minOfEdgeMap(mapname);
113 //std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
117 color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
121 color.set_rgb_p (0, 100, 0);
124 edgesmap[i]->property_fill_color_gdk().set_value(color);
129 int GraphDisplayerCanvas::changeText (std::string mapname)
132 //the number in the map will be written on the edge
133 //EXCEPT when the name of the map is Text, because
134 //in that case empty string will be written, because
135 //that is the deleter map
136 //\todo isn't it a bit woodcutter?
138 for (EdgeIt i(g); i!=INVALID; ++i)
142 double number=(*(mapstorage.edgemap_storage)[mapname])[i];
143 int length=(int)(floor(log(number)/log(10)))+1;
144 int maxpos=(int)(pow(10,length-1));
145 int strl=length+1+RANGE;
146 char * str=new char[strl];
150 for(int j=0;j<strl;j++)
154 int digit=(int)(number/maxpos);
156 number-=digit*maxpos;
161 edgetextmap[i]->property_text().set_value(str);
165 edgetextmap[i]->property_text().set_value("");
171 bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
175 case GDK_BUTTON_PRESS:
176 //we mark the location of the event to be able to calculate parameters of dragging
177 clicked_x=e->button.x;
178 clicked_y=e->button.y;
179 active_item=(get_item_at(e->button.x, e->button.y));
182 case GDK_BUTTON_RELEASE:
185 updateScrollRegion();
187 case GDK_MOTION_NOTIFY:
188 //we only have to do sg. if the mouse button is pressed
191 //new coordinates will be the old values,
192 //because the item will be moved to the
193 //new coordinate therefore the new movement
194 //has to be calculated from here
196 double dx=e->motion.x-clicked_x;
197 double dy=e->motion.y-clicked_y;
198 active_item->move(dx, dy);
199 clicked_x=e->motion.x;
200 clicked_y=e->motion.y;
202 //all the edges connected to the moved point has to be redrawn
206 for(;e!=INVALID;g.nextOut(e))
208 Gnome::Canvas::Points coos;
209 double x1, x2, y1, y2;
211 nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
212 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
214 nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
215 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
217 edgesmap[e]->property_points().set_value(coos);
219 edgesmap[e]->get_bounds(x1, y1, x2, y2);
221 edgetextmap[e]->property_x().set_value((x1+x2)/2);
222 edgetextmap[e]->property_y().set_value((y1+y2)/2);
226 for(;e!=INVALID;g.nextIn(e))
228 Gnome::Canvas::Points coos;
229 double x1, x2, y1, y2;
231 nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
232 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
234 nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
235 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
237 edgesmap[e]->property_points().set_value(coos);
239 edgesmap[e]->get_bounds(x1, y1, x2, y2);
241 edgetextmap[e]->property_x().set_value((x1+x2)/2);
242 edgetextmap[e]->property_y().set_value((y1+y2)/2);
250 bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
252 Gnome::Canvas::CanvasAA::on_expose_event(event);
258 void GraphDisplayerCanvas::zoomIn()
261 (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
264 void GraphDisplayerCanvas::zoomOut()
267 (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
270 void GraphDisplayerCanvas::zoomFit()
272 // get the height and width of the canvas
273 Gtk::Allocation a = get_allocation();
274 int aw = a.get_width();
275 int ah = a.get_height();
277 aw -= 5; if (aw < 0) aw = 0;
278 ah -= 5; if (ah < 0) ah = 0;
280 // get the bounding box of the graph
281 double wx1, wy1, wx2, wy2;
282 Gnome::Canvas::Item* pCanvasItem = root();
283 pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
285 // fit the graph to the window
286 double ppu1 = (double) aw / fabs(wx2 - wx1);
287 double ppu2 = (double) ah / fabs(wy2 - wy1);
288 set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2);
291 void GraphDisplayerCanvas::zoom100()
293 set_pixels_per_unit(1.0);
296 void GraphDisplayerCanvas::updateScrollRegion()
298 double wx1, wy1, wx2, wy2;
299 Gnome::Canvas::Item* pCanvasItem = root();
300 pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
301 set_scroll_region(wx1, wy1, wx2, wy2);
304 void GraphDisplayerCanvas::changeEditorialTool(int newtool)
306 actual_handler.disconnect();
311 actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false);
314 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
317 actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
324 bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e)
328 case GDK_BUTTON_PRESS:
329 //we mark the location of the event to be able to calculate parameters of dragging
330 clicked_x=e->button.x;
331 clicked_y=e->button.y;
332 active_item=(get_item_at(e->button.x, e->button.y));
334 for (NodeIt i(g); i!=INVALID; ++i)
336 if(nodesmap[i]==active_item)
343 case GDK_BUTTON_RELEASE:
346 updateScrollRegion();
348 case GDK_MOTION_NOTIFY:
349 //we only have to do sg. if the mouse button is pressed
352 //new coordinates will be the old values,
353 //because the item will be moved to the
354 //new coordinate therefore the new movement
355 //has to be calculated from here
357 double dx=e->motion.x-clicked_x;
358 double dy=e->motion.y-clicked_y;
360 active_item->move(dx, dy);
362 clicked_x=e->motion.x;
363 clicked_y=e->motion.y;
365 //all the edges connected to the moved point has to be redrawn
368 g.firstOut(e,active_node);
370 for(;e!=INVALID;g.nextOut(e))
372 Gnome::Canvas::Points coos;
373 double x1, x2, y1, y2;
375 nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
376 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
378 nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
379 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
381 edgesmap[e]->property_points().set_value(coos);
383 edgesmap[e]->get_bounds(x1, y1, x2, y2);
385 edgetextmap[e]->property_x().set_value((x1+x2)/2);
386 edgetextmap[e]->property_y().set_value((y1+y2)/2);
389 g.firstIn(e,active_node);
390 for(;e!=INVALID;g.nextIn(e))
392 Gnome::Canvas::Points coos;
393 double x1, x2, y1, y2;
395 nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
396 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
398 nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
399 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
401 edgesmap[e]->property_points().set_value(coos);
403 edgesmap[e]->get_bounds(x1, y1, x2, y2);
405 edgetextmap[e]->property_x().set_value((x1+x2)/2);
406 edgetextmap[e]->property_y().set_value((y1+y2)/2);
415 bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e)
419 case GDK_BUTTON_PRESS:
422 active_node=NodeIt(g,g.addNode());
424 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
426 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
427 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
428 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
429 *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
430 (nodesmap[active_node])->show();
432 case GDK_MOTION_NOTIFY:
434 double world_motion_x, world_motion_y;
435 GdkEvent * generated=new GdkEvent();
436 window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y);
437 generated->motion.x=world_motion_x;
438 generated->motion.y=world_motion_y;
439 generated->type=GDK_MOTION_NOTIFY;
440 move_event_handler(generated);
443 case GDK_BUTTON_RELEASE:
445 *active_item << Gnome::Canvas::Properties::fill_color("blue");
447 updateScrollRegion();
455 bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e)
459 case GDK_BUTTON_PRESS:
462 //we mark the location of the event to be able to calculate parameters of dragging
463 clicked_x=e->button.x;
464 clicked_y=e->button.y;
465 active_item=(get_item_at(e->button.x, e->button.y));
467 for (NodeIt i(g); i!=INVALID; ++i)
469 if(nodesmap[i]==active_item)
474 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
479 target_item=(get_item_at(e->button.x, e->button.y));
480 Graph::NodeIt target_node=INVALID;
481 for (NodeIt i(g); i!=INVALID; ++i)
483 if(nodesmap[i]==target_item)
488 *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
491 // Graph::Edge new_edge=g.addEdge(active_node, target_node);
492 active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
494 //calculating coordinates of new edge
495 Gnome::Canvas::Points coos;
496 double x1, x2, y1, y2;
498 active_item->get_bounds(x1, y1, x2, y2);
499 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
501 target_item->get_bounds(x1, y1, x2, y2);
502 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
505 edgesmap[active_edge]=new Gnome::Canvas::Line(displayed_graph, coos);
506 *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
507 edgesmap[active_edge]->property_width_pixels().set_value(10);
509 //redraw nodes to blank terminations of the new edge
510 target_item->raise_to_top();
511 active_item->raise_to_top();
513 //initializing edge-text as well, to empty string
514 edgesmap[active_edge]->get_bounds(x1, y1, x2, y2);
515 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
516 edgetextmap[active_edge]->property_fill_color().set_value("black");
519 case GDK_BUTTON_RELEASE:
523 *active_item << Gnome::Canvas::Properties::fill_color("blue");
524 *target_item << Gnome::Canvas::Properties::fill_color("blue");