1 #include "graph_displayer_canvas.h"
2 #include <lemon/random.h>
5 GraphDisplayerCanvas::GraphDisplayerCanvas(NoteBookTab & mainw) :
6 nodesmap(mainw.mapstorage.graph), edgesmap(mainw.mapstorage.graph), edgetextmap(mainw.mapstorage.graph),
7 nodetextmap(mainw.mapstorage.graph), displayed_graph(*(root()), 0, 0),
8 isbutton(0), active_item(NULL), target_item(NULL), nodemap_to_edit(""),
9 edgemap_to_edit(""), autoscale(true), zoomtrack(false), radius_size(20), edge_width(10),
10 iterations(20), attraction(0.05), propulsation(40000), was_redesigned(false), mytab(mainw)
12 //base event handler is move tool
13 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
21 GraphDisplayerCanvas::~GraphDisplayerCanvas()
23 for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
26 delete nodetextmap[n];
29 for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
32 delete edgetextmap[e];
36 void GraphDisplayerCanvas::propertyChange(bool itisedge, int prop)
40 propertyUpdate(Edge(INVALID), prop);
44 propertyUpdate(Node(INVALID), prop);
48 void GraphDisplayerCanvas::propertyUpdate(Edge edge)
50 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
52 propertyUpdate(edge, i);
56 void GraphDisplayerCanvas::propertyUpdate(Node node)
58 for(int i=0;i<NODE_PROPERTY_NUM;i++)
60 propertyUpdate(node, i);
64 void GraphDisplayerCanvas::propertyUpdate(Node node, int prop)
68 std::string mapname=mytab.getActiveNodeMap(prop);
72 if( ( ((mytab.mapstorage).nodemap_storage).find(mapname) != ((mytab.mapstorage).nodemap_storage).end() ) )
77 changeNodeRadius(mapname, node);
80 changeNodeColor(mapname, node);
83 changeNodeText(mapname, node);
96 resetNodeRadius(node);
105 std::cerr<<"Error\n";
111 void GraphDisplayerCanvas::propertyUpdate(Edge edge, int prop)
115 std::string mapname=mytab.getActiveEdgeMap(prop);
119 if( ( ((mytab.mapstorage).edgemap_storage).find(mapname) != ((mytab.mapstorage).edgemap_storage).end() ) )
124 changeEdgeWidth(mapname, edge);
127 changeEdgeColor(mapname, edge);
130 changeEdgeText(mapname, edge);
133 std::cerr<<"Error\n";
142 resetEdgeWidth(edge);
145 resetEdgeColor(edge);
151 std::cerr<<"Error\n";
156 void GraphDisplayerCanvas::drawGraph()
158 //first edges are drawn, to hide joining with nodes later
160 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
162 if (mytab.mapstorage.graph.source(i) == mytab.mapstorage.graph.target(i))
164 edgesmap[i]=new LoopEdge(displayed_graph, i, *this);
168 edgesmap[i]=new BrokenEdge(displayed_graph, i, *this);
170 //initializing edge-text as well, to empty string
172 XY text_pos=mytab.mapstorage.arrow_pos[i];
173 text_pos+=(XY(10,10));
175 edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
176 edgetextmap[i]->property_fill_color().set_value("darkgreen");
177 edgetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
178 edgetextmap[i]->raise_to_top();
181 //afterwards nodes come to be drawn
183 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
185 //drawing bule nodes, with black line around them
187 nodesmap[i]=new Gnome::Canvas::Ellipse(
189 (mytab.mapstorage).coords[i].x-20,
190 (mytab.mapstorage).coords[i].y-20,
191 (mytab.mapstorage).coords[i].x+20,
192 (mytab.mapstorage).coords[i].y+20);
193 *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
194 *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
195 nodesmap[i]->raise_to_top();
197 //initializing edge-text as well, to empty string
200 ((mytab.mapstorage).coords[i].x+node_property_defaults[N_RADIUS]+5),
201 ((mytab.mapstorage).coords[i].y+node_property_defaults[N_RADIUS]+5));
203 nodetextmap[i]=new Gnome::Canvas::Text(displayed_graph,
204 text_pos.x, text_pos.y, "");
205 nodetextmap[i]->property_fill_color().set_value("darkblue");
206 nodetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
207 nodetextmap[i]->raise_to_top();
210 updateScrollRegion();
213 void GraphDisplayerCanvas::clear()
217 forming_edge=INVALID;
219 for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
222 delete nodetextmap[n];
225 for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
228 delete edgetextmap[e];
232 void GraphDisplayerCanvas::setView(bool autoscale_p, bool zoomtrack_p, double width_p, double radius_p)
234 autoscale=autoscale_p;
236 radius_size=radius_p;
238 if((!zoomtrack) && zoomtrack_p)
240 fixed_zoom_factor=get_pixels_per_unit();
243 zoomtrack=zoomtrack_p;
245 propertyChange(false, N_RADIUS);
246 propertyChange(true, E_WIDTH);
249 void GraphDisplayerCanvas::getView(bool & autoscale_p, bool & zoomtrack_p, double& width_p, double& radius_p)
251 autoscale_p=autoscale;
252 zoomtrack_p=zoomtrack;
254 radius_p=radius_size;
257 void GraphDisplayerCanvas::reDesignGraph()
260 double init_vector_length=25;
264 NodeIt i((mytab.mapstorage).graph);
266 dim2::Point<double> init(init_vector_length*rnd(),
267 init_vector_length*rnd());
268 moveNode(init.x, init.y, nodesmap[i], i);
274 for(int l=0;l<iterations;l++)
276 Graph::NodeMap<double> x(mytab.mapstorage.graph);
277 Graph::NodeMap<double> y(mytab.mapstorage.graph);
278 XYMap<Graph::NodeMap<double> > actual_forces;
279 actual_forces.setXMap(x);
280 actual_forces.setYMap(y);
282 lemon::dim2::Point<double> delta;
284 //count actual force for each nodes
285 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
287 //propulsation of nodes
288 for (NodeIt j((mytab.mapstorage).graph); j!=INVALID; ++j)
292 delta=((mytab.mapstorage).coords[i]-(mytab.mapstorage).coords[j]);
294 double length_sqr=delta.normSquare();
295 double length=sqrt(length_sqr);
296 if(length_sqr<min_dist)
304 //calculating propulsation strength
305 //greater distance menas smaller propulsation strength
306 delta*=propulsation/length_sqr;
308 actual_forces.set(i,(actual_forces[i]+delta));
311 //attraction of nodes, to which actual node is bound
312 for(OutEdgeIt ei((mytab.mapstorage).graph,i);ei!=INVALID;++ei)
314 delta=((mytab.mapstorage).coords[i]-(mytab.mapstorage).coords[mytab.mapstorage.graph.target(ei)]);
316 double length_sqr=delta.normSquare();
317 double length=sqrt(length_sqr);
318 if(length_sqr<min_dist)
326 //calculating attraction strength
327 //greater distance means greater strength
328 delta*=attraction*length;
330 actual_forces.set(i,actual_forces[i]-delta);
332 for(InEdgeIt ei((mytab.mapstorage).graph,i);ei!=INVALID;++ei)
334 delta=((mytab.mapstorage).coords[i]-(mytab.mapstorage).coords[mytab.mapstorage.graph.source(ei)]);
336 double length_sqr=delta.normSquare();
337 double length=sqrt(length_sqr);
338 if(length_sqr<min_dist)
346 //calculating attraction strength
347 //greater distance means greater strength
348 delta*=attraction*length;
350 actual_forces.set(i,actual_forces[i]-delta);
353 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
355 moveNode(actual_forces[i].x, actual_forces[i].y, nodesmap[i], i);
360 void GraphDisplayerCanvas::get_design_data(double & attraction_p, double & propulsation_p, int & iterations_p)
362 attraction_p=attraction;
363 propulsation_p=propulsation;
364 iterations_p=iterations;
367 void GraphDisplayerCanvas::set_attraction(double attraction_p)
369 attraction=attraction_p;
372 void GraphDisplayerCanvas::set_propulsation(double propulsation_p)
374 propulsation=propulsation_p;
377 void GraphDisplayerCanvas::set_iteration(int iterations_p)
379 iterations=iterations_p;