No segmentation fault will be occured if two nodes are exactly overlap each other, AND they are connected.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #include "graph_displayer_canvas.h"
20 #include <lemon/random.h>
23 GraphDisplayerCanvas::GraphDisplayerCanvas(NoteBookTab & mainw) :
24 nodesmap(mainw.mapstorage.graph), edgesmap(mainw.mapstorage.graph), edgetextmap(mainw.mapstorage.graph),
25 nodetextmap(mainw.mapstorage.graph), displayed_graph(*(root()), 0, 0),
26 isbutton(0), active_item(NULL), target_item(NULL), nodemap_to_edit(""),
27 edgemap_to_edit(""), autoscale(true), zoomtrack(false), radius_size(20), edge_width(10),
28 was_redesigned(false), is_drawn(false), mytab(mainw),
31 //add mouse scroll event handler - it won't be changed, it handles zoom
32 signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::scrollEventHandler), false);
34 //base event handler is move tool
35 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
46 void GraphDisplayerCanvas::setBackground()
52 if (mytab.mapstorage.isBackgroundSet())
54 background_set = true;
55 refBackground = Gdk::Pixbuf::create_from_file(
56 mytab.mapstorage.getBackgroundFilename());
57 background = new Gnome::Canvas::Pixbuf(
59 0.0 - refBackground->get_width() / 2.0,
60 0.0 - refBackground->get_height() / 2.0,
62 background->lower_to_bottom();
66 background_set = false;
70 GraphDisplayerCanvas::~GraphDisplayerCanvas()
72 for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
75 delete nodetextmap[n];
78 for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
81 delete edgetextmap[e];
85 void GraphDisplayerCanvas::propertyChange(bool itisedge, int prop)
89 propertyUpdate(Edge(INVALID), prop);
93 propertyUpdate(Node(INVALID), prop);
97 void GraphDisplayerCanvas::propertyUpdate(Edge edge)
99 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
101 propertyUpdate(edge, i);
105 void GraphDisplayerCanvas::propertyUpdate(Node node)
107 for(int i=0;i<NODE_PROPERTY_NUM;i++)
109 propertyUpdate(node, i);
113 void GraphDisplayerCanvas::propertyUpdate(Node node, int prop)
115 std::string mapname=mytab.getActiveNodeMap(prop);
121 if( ( ((mytab.mapstorage).nodemap_storage).find(mapname) != ((mytab.mapstorage).nodemap_storage).end() ) )
126 changeNodeRadius(mapname, node);
129 changeNodeColor(mapname, node);
132 changeNodeText(mapname, node);
135 std::cerr<<"Error\n";
145 resetNodeRadius(node);
148 resetNodeColor(node);
154 std::cerr<<"Error\n";
160 void GraphDisplayerCanvas::propertyUpdate(Edge edge, int prop)
162 std::string mapname=mytab.getActiveEdgeMap(prop);
168 if( ( ((mytab.mapstorage).edgemap_storage).find(mapname) != ((mytab.mapstorage).edgemap_storage).end() ) )
173 changeEdgeWidth(mapname, edge);
176 changeEdgeColor(mapname, edge);
179 changeEdgeText(mapname, edge);
182 std::cerr<<"Error\n";
191 resetEdgeWidth(edge);
194 resetEdgeColor(edge);
200 std::cerr<<"Error\n";
206 void GraphDisplayerCanvas::drawGraph()
208 //first edges are drawn, to hide joining with nodes later
210 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
212 if (mytab.mapstorage.graph.source(i) == mytab.mapstorage.graph.target(i))
214 edgesmap[i]=new LoopEdge(displayed_graph, i, *this);
218 edgesmap[i]=new BrokenEdge(displayed_graph, i, *this);
220 //initializing edge-text as well, to empty string
222 XY text_pos=mytab.mapstorage.arrow_pos[i];
223 text_pos+=(XY(10,10));
225 edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
226 edgetextmap[i]->property_fill_color().set_value("darkgreen");
227 edgetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
228 edgetextmap[i]->raise_to_top();
231 //afterwards nodes come to be drawn
233 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
235 //drawing bule nodes, with black line around them
237 nodesmap[i]=new Gnome::Canvas::Ellipse(
239 (mytab.mapstorage).coords[i].x-20,
240 (mytab.mapstorage).coords[i].y-20,
241 (mytab.mapstorage).coords[i].x+20,
242 (mytab.mapstorage).coords[i].y+20);
243 *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
244 *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
245 nodesmap[i]->raise_to_top();
247 //initializing edge-text as well, to empty string
250 ((mytab.mapstorage).coords[i].x+node_property_defaults[N_RADIUS]+5),
251 ((mytab.mapstorage).coords[i].y+node_property_defaults[N_RADIUS]+5));
253 nodetextmap[i]=new Gnome::Canvas::Text(displayed_graph,
254 text_pos.x, text_pos.y, "");
255 nodetextmap[i]->property_fill_color().set_value("darkblue");
256 nodetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
257 nodetextmap[i]->raise_to_top();
265 for(int i=0;i<NODE_PROPERTY_NUM;i++)
267 propertyUpdate(Node(INVALID), i);
270 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
272 propertyUpdate(Edge(INVALID), i);
275 updateScrollRegion();
278 void GraphDisplayerCanvas::clear()
282 forming_edge=INVALID;
284 for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
287 delete nodetextmap[n];
290 for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
293 delete edgetextmap[e];
299 void GraphDisplayerCanvas::setView(bool autoscale_p, bool zoomtrack_p, double width_p, double radius_p)
301 autoscale=autoscale_p;
303 radius_size=radius_p;
305 if((!zoomtrack) && zoomtrack_p)
307 fixed_zoom_factor=get_pixels_per_unit();
310 zoomtrack=zoomtrack_p;
312 propertyChange(false, N_RADIUS);
313 propertyChange(true, E_WIDTH);
316 void GraphDisplayerCanvas::getView(bool & autoscale_p, bool & zoomtrack_p, double& width_p, double& radius_p)
318 autoscale_p=autoscale;
319 zoomtrack_p=zoomtrack;
321 radius_p=radius_size;
324 void GraphDisplayerCanvas::reDesignGraph()
326 double max_coord=50000;
328 double init_vector_length=25;
332 NodeIt i((mytab.mapstorage).graph);
334 dim2::Point<double> init(init_vector_length*rnd(),
335 init_vector_length*rnd());
336 moveNode(init.x, init.y, nodesmap[i], i);
344 (mytab.mapstorage).get_design_data(attraction, propulsation, iterations);
347 for(int l=0;l<iterations;l++)
349 Graph::NodeMap<double> x(mytab.mapstorage.graph);
350 Graph::NodeMap<double> y(mytab.mapstorage.graph);
351 XYMap<Graph::NodeMap<double> > actual_forces;
352 actual_forces.setXMap(x);
353 actual_forces.setYMap(y);
355 //count actual force for each nodes
356 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
358 //propulsation of nodes
359 for (NodeIt j((mytab.mapstorage).graph); j!=INVALID; ++j)
363 lemon::dim2::Point<double> delta =
364 ((mytab.mapstorage).coords[i]-
365 (mytab.mapstorage).coords[j]);
367 const double length_sqr=std::max(delta.normSquare(),min_dist);
370 delta/=sqrt(length_sqr);
372 //calculating propulsation strength
373 //greater distance menas smaller propulsation strength
374 delta*=propulsation/length_sqr;
376 actual_forces.set(i,(actual_forces[i]+delta));
379 //attraction of nodes, to which actual node is bound
380 for(OutEdgeIt ei((mytab.mapstorage).graph,i);ei!=INVALID;++ei)
382 lemon::dim2::Point<double> delta =
383 ((mytab.mapstorage).coords[i]-
384 (mytab.mapstorage).coords[mytab.mapstorage.
387 //calculating attraction strength
388 //greater distance means greater strength
391 actual_forces.set(i,actual_forces[i]-delta);
393 for(InEdgeIt ei((mytab.mapstorage).graph,i);ei!=INVALID;++ei)
395 lemon::dim2::Point<double> delta =
396 ((mytab.mapstorage).coords[i]-
397 (mytab.mapstorage).coords[mytab.mapstorage.
400 //calculating attraction strength
401 //greater distance means greater strength
404 actual_forces.set(i,actual_forces[i]-delta);
407 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
409 if(((mytab.mapstorage).coords[i].x)+actual_forces[i].x>max_coord)
411 actual_forces[i].x=max_coord-((mytab.mapstorage).coords[i].x);
412 std::cout << "Correction! " << (((mytab.mapstorage).coords[i].x)+actual_forces[i].x) << std::endl;
414 else if(((mytab.mapstorage).coords[i].x)+actual_forces[i].x<(0-max_coord))
416 actual_forces[i].x=0-max_coord-((mytab.mapstorage).coords[i].x);
417 std::cout << "Correction! " << (((mytab.mapstorage).coords[i].x)+actual_forces[i].x) << std::endl;
419 if(((mytab.mapstorage).coords[i].y)+actual_forces[i].y>max_coord)
421 actual_forces[i].y=max_coord-((mytab.mapstorage).coords[i].y);
422 std::cout << "Correction! " << (((mytab.mapstorage).coords[i].y)+actual_forces[i].y) << std::endl;
424 else if(((mytab.mapstorage).coords[i].y)+actual_forces[i].y<(0-max_coord))
426 actual_forces[i].y=0-max_coord-((mytab.mapstorage).coords[i].y);
427 std::cout << "Correction! " << (((mytab.mapstorage).coords[i].y)+actual_forces[i].y) << std::endl;
429 moveNode(actual_forces[i].x, actual_forces[i].y, nodesmap[i], i);