1 #include "graph_displayer_canvas.h"
4 GraphDisplayerCanvas::GraphDisplayerCanvas(NoteBookTab & mainw) :
5 nodesmap(mainw.mapstorage.graph), edgesmap(mainw.mapstorage.graph), edgetextmap(mainw.mapstorage.graph),
6 nodetextmap(mainw.mapstorage.graph), displayed_graph(*(root()), 0, 0),
7 isbutton(0), active_item(NULL), target_item(NULL), nodemap_to_edit(""),
8 edgemap_to_edit(""), autoscale(true), zoomtrack(false), radius_size(20), edge_width(10),
9 iterations(20), attraction(0.05), propulsation(40000), mytab(mainw)
11 //base event handler is move tool
12 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
20 GraphDisplayerCanvas::~GraphDisplayerCanvas()
22 for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
25 delete nodetextmap[n];
28 for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
31 delete edgetextmap[e];
35 void GraphDisplayerCanvas::propertyChange(bool itisedge, int prop)
39 propertyUpdate(Edge(INVALID), prop);
43 propertyUpdate(Node(INVALID), prop);
47 void GraphDisplayerCanvas::propertyUpdate(Edge edge)
49 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
51 propertyUpdate(edge, i);
55 void GraphDisplayerCanvas::propertyUpdate(Node node)
57 for(int i=0;i<NODE_PROPERTY_NUM;i++)
59 propertyUpdate(node, i);
63 void GraphDisplayerCanvas::propertyUpdate(Node node, int prop)
67 std::string mapname=mytab.getActiveNodeMap(prop);
71 if( ( ((mytab.mapstorage).nodemap_storage).find(mapname) != ((mytab.mapstorage).nodemap_storage).end() ) )
76 changeNodeRadius(mapname, node);
79 changeNodeColor(mapname, node);
82 changeNodeText(mapname, node);
95 resetNodeRadius(node);
104 std::cerr<<"Error\n";
110 void GraphDisplayerCanvas::propertyUpdate(Edge edge, int prop)
114 std::string mapname=mytab.getActiveEdgeMap(prop);
118 if( ( ((mytab.mapstorage).edgemap_storage).find(mapname) != ((mytab.mapstorage).edgemap_storage).end() ) )
123 changeEdgeWidth(mapname, edge);
126 changeEdgeColor(mapname, edge);
129 changeEdgeText(mapname, edge);
132 std::cerr<<"Error\n";
141 resetEdgeWidth(edge);
144 resetEdgeColor(edge);
150 std::cerr<<"Error\n";
155 void GraphDisplayerCanvas::drawGraph()
157 //first edges are drawn, to hide joining with nodes later
159 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
161 if (mytab.mapstorage.graph.source(i) == mytab.mapstorage.graph.target(i))
163 edgesmap[i]=new LoopEdge(displayed_graph, i, *this);
167 edgesmap[i]=new BrokenEdge(displayed_graph, i, *this);
169 //initializing edge-text as well, to empty string
171 XY text_pos=mytab.mapstorage.arrow_pos[i];
172 text_pos+=(XY(10,10));
174 edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
175 edgetextmap[i]->property_fill_color().set_value("darkgreen");
176 edgetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
177 edgetextmap[i]->raise_to_top();
180 //afterwards nodes come to be drawn
182 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
184 //drawing bule nodes, with black line around them
186 nodesmap[i]=new Gnome::Canvas::Ellipse(
188 (mytab.mapstorage).coords[i].x-20,
189 (mytab.mapstorage).coords[i].y-20,
190 (mytab.mapstorage).coords[i].x+20,
191 (mytab.mapstorage).coords[i].y+20);
192 *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
193 *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
194 nodesmap[i]->raise_to_top();
196 //initializing edge-text as well, to empty string
199 ((mytab.mapstorage).coords[i].x+node_property_defaults[N_RADIUS]+5),
200 ((mytab.mapstorage).coords[i].y+node_property_defaults[N_RADIUS]+5));
202 nodetextmap[i]=new Gnome::Canvas::Text(displayed_graph,
203 text_pos.x, text_pos.y, "");
204 nodetextmap[i]->property_fill_color().set_value("darkblue");
205 nodetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
206 nodetextmap[i]->raise_to_top();
209 updateScrollRegion();
212 void GraphDisplayerCanvas::clear()
216 forming_edge=INVALID;
218 for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
221 delete nodetextmap[n];
224 for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
227 delete edgetextmap[e];
231 void GraphDisplayerCanvas::setView(bool autoscale_p, bool zoomtrack_p, double width_p, double radius_p)
233 autoscale=autoscale_p;
235 radius_size=radius_p;
237 if((!zoomtrack) && zoomtrack_p)
239 fixed_zoom_factor=get_pixels_per_unit();
242 zoomtrack=zoomtrack_p;
244 propertyChange(false, N_RADIUS);
245 propertyChange(true, E_WIDTH);
248 void GraphDisplayerCanvas::getView(bool & autoscale_p, bool & zoomtrack_p, double& width_p, double& radius_p)
250 autoscale_p=autoscale;
251 zoomtrack_p=zoomtrack;
253 radius_p=radius_size;
256 void GraphDisplayerCanvas::reDesignGraph()
261 for(int l=0;l<iterations;l++)
263 Graph::NodeMap<double> x(mytab.mapstorage.graph);
264 Graph::NodeMap<double> y(mytab.mapstorage.graph);
265 XYMap<Graph::NodeMap<double> > actual_forces;
266 actual_forces.setXMap(x);
267 actual_forces.setYMap(y);
269 lemon::dim2::Point<double> delta;
271 //count actual force for each nodes
272 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
274 //propulsation of nodes
275 for (NodeIt j((mytab.mapstorage).graph); j!=INVALID; ++j)
279 delta=((mytab.mapstorage).coords[i]-(mytab.mapstorage).coords[j]);
281 double length_sqr=delta.normSquare();
282 double length=sqrt(length_sqr);
283 if(length_sqr<min_dist)
291 //calculating propulsation strength
292 //greater distance menas smaller propulsation strength
293 delta*=propulsation/length_sqr;
295 actual_forces.set(i,(actual_forces[i]+delta));
298 //attraction of nodes, to which actual node is bound
299 for(OutEdgeIt ei((mytab.mapstorage).graph,i);ei!=INVALID;++ei)
301 delta=((mytab.mapstorage).coords[i]-(mytab.mapstorage).coords[mytab.mapstorage.graph.target(ei)]);
303 double length_sqr=delta.normSquare();
304 double length=sqrt(length_sqr);
305 if(length_sqr<min_dist)
313 //calculating attraction strength
314 //greater distance means greater strength
315 delta*=attraction*length;
317 actual_forces.set(i,actual_forces[i]-delta);
319 for(InEdgeIt ei((mytab.mapstorage).graph,i);ei!=INVALID;++ei)
321 delta=((mytab.mapstorage).coords[i]-(mytab.mapstorage).coords[mytab.mapstorage.graph.source(ei)]);
323 double length_sqr=delta.normSquare();
324 double length=sqrt(length_sqr);
325 if(length_sqr<min_dist)
333 //calculating attraction strength
334 //greater distance means greater strength
335 delta*=attraction*length;
337 actual_forces.set(i,actual_forces[i]-delta);
340 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
342 moveNode(actual_forces[i].x, actual_forces[i].y, nodesmap[i], i);
347 void GraphDisplayerCanvas::get_design_data(double & attraction_p, double & propulsation_p, int & iterations_p)
349 attraction_p=attraction;
350 propulsation_p=propulsation;
351 iterations_p=iterations;
354 void GraphDisplayerCanvas::set_attraction(double attraction_p)
356 attraction=attraction_p;
359 void GraphDisplayerCanvas::set_propulsation(double propulsation_p)
361 propulsation=propulsation_p;
364 void GraphDisplayerCanvas::set_iteration(int iterations_p)
366 iterations=iterations_p;