Version magic.
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)
30 //base event handler is move tool
31 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
39 GraphDisplayerCanvas::~GraphDisplayerCanvas()
41 for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
44 delete nodetextmap[n];
47 for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
50 delete edgetextmap[e];
54 void GraphDisplayerCanvas::propertyChange(bool itisedge, int prop)
58 propertyUpdate(Edge(INVALID), prop);
62 propertyUpdate(Node(INVALID), prop);
66 void GraphDisplayerCanvas::propertyUpdate(Edge edge)
68 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
70 propertyUpdate(edge, i);
74 void GraphDisplayerCanvas::propertyUpdate(Node node)
76 for(int i=0;i<NODE_PROPERTY_NUM;i++)
78 propertyUpdate(node, i);
82 void GraphDisplayerCanvas::propertyUpdate(Node node, int prop)
84 std::string mapname=mytab.getActiveNodeMap(prop);
90 if( ( ((mytab.mapstorage).nodemap_storage).find(mapname) != ((mytab.mapstorage).nodemap_storage).end() ) )
95 changeNodeRadius(mapname, node);
98 changeNodeColor(mapname, node);
101 changeNodeText(mapname, node);
104 std::cerr<<"Error\n";
114 resetNodeRadius(node);
117 resetNodeColor(node);
123 std::cerr<<"Error\n";
129 void GraphDisplayerCanvas::propertyUpdate(Edge edge, int prop)
131 std::string mapname=mytab.getActiveEdgeMap(prop);
137 if( ( ((mytab.mapstorage).edgemap_storage).find(mapname) != ((mytab.mapstorage).edgemap_storage).end() ) )
142 changeEdgeWidth(mapname, edge);
145 changeEdgeColor(mapname, edge);
148 changeEdgeText(mapname, edge);
151 std::cerr<<"Error\n";
160 resetEdgeWidth(edge);
163 resetEdgeColor(edge);
169 std::cerr<<"Error\n";
175 void GraphDisplayerCanvas::drawGraph()
177 //first edges are drawn, to hide joining with nodes later
179 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
181 if (mytab.mapstorage.graph.source(i) == mytab.mapstorage.graph.target(i))
183 edgesmap[i]=new LoopEdge(displayed_graph, i, *this);
187 edgesmap[i]=new BrokenEdge(displayed_graph, i, *this);
189 //initializing edge-text as well, to empty string
191 XY text_pos=mytab.mapstorage.arrow_pos[i];
192 text_pos+=(XY(10,10));
194 edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
195 edgetextmap[i]->property_fill_color().set_value("darkgreen");
196 edgetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
197 edgetextmap[i]->raise_to_top();
200 //afterwards nodes come to be drawn
202 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
204 //drawing bule nodes, with black line around them
206 nodesmap[i]=new Gnome::Canvas::Ellipse(
208 (mytab.mapstorage).coords[i].x-20,
209 (mytab.mapstorage).coords[i].y-20,
210 (mytab.mapstorage).coords[i].x+20,
211 (mytab.mapstorage).coords[i].y+20);
212 *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
213 *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
214 nodesmap[i]->raise_to_top();
216 //initializing edge-text as well, to empty string
219 ((mytab.mapstorage).coords[i].x+node_property_defaults[N_RADIUS]+5),
220 ((mytab.mapstorage).coords[i].y+node_property_defaults[N_RADIUS]+5));
222 nodetextmap[i]=new Gnome::Canvas::Text(displayed_graph,
223 text_pos.x, text_pos.y, "");
224 nodetextmap[i]->property_fill_color().set_value("darkblue");
225 nodetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
226 nodetextmap[i]->raise_to_top();
234 for(int i=0;i<NODE_PROPERTY_NUM;i++)
236 propertyUpdate(Node(INVALID), i);
239 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
241 propertyUpdate(Edge(INVALID), i);
244 updateScrollRegion();
247 void GraphDisplayerCanvas::clear()
251 forming_edge=INVALID;
253 for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
256 delete nodetextmap[n];
259 for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
262 delete edgetextmap[e];
268 void GraphDisplayerCanvas::setView(bool autoscale_p, bool zoomtrack_p, double width_p, double radius_p)
270 autoscale=autoscale_p;
272 radius_size=radius_p;
274 if((!zoomtrack) && zoomtrack_p)
276 fixed_zoom_factor=get_pixels_per_unit();
279 zoomtrack=zoomtrack_p;
281 propertyChange(false, N_RADIUS);
282 propertyChange(true, E_WIDTH);
285 void GraphDisplayerCanvas::getView(bool & autoscale_p, bool & zoomtrack_p, double& width_p, double& radius_p)
287 autoscale_p=autoscale;
288 zoomtrack_p=zoomtrack;
290 radius_p=radius_size;
293 void GraphDisplayerCanvas::reDesignGraph()
296 double init_vector_length=25;
300 NodeIt i((mytab.mapstorage).graph);
302 dim2::Point<double> init(init_vector_length*rnd(),
303 init_vector_length*rnd());
304 moveNode(init.x, init.y, nodesmap[i], i);
312 (mytab.mapstorage).get_design_data(attraction, propulsation, iterations);
315 for(int l=0;l<iterations;l++)
317 Graph::NodeMap<double> x(mytab.mapstorage.graph);
318 Graph::NodeMap<double> y(mytab.mapstorage.graph);
319 XYMap<Graph::NodeMap<double> > actual_forces;
320 actual_forces.setXMap(x);
321 actual_forces.setYMap(y);
323 //count actual force for each nodes
324 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
326 //propulsation of nodes
327 for (NodeIt j((mytab.mapstorage).graph); j!=INVALID; ++j)
331 lemon::dim2::Point<double> delta =
332 ((mytab.mapstorage).coords[i]-
333 (mytab.mapstorage).coords[j]);
335 const double length_sqr=std::max(delta.normSquare(),min_dist);
338 delta/=sqrt(length_sqr);
340 //calculating propulsation strength
341 //greater distance menas smaller propulsation strength
342 delta*=propulsation/length_sqr;
344 actual_forces.set(i,(actual_forces[i]+delta));
347 //attraction of nodes, to which actual node is bound
348 for(OutEdgeIt ei((mytab.mapstorage).graph,i);ei!=INVALID;++ei)
350 lemon::dim2::Point<double> delta =
351 ((mytab.mapstorage).coords[i]-
352 (mytab.mapstorage).coords[mytab.mapstorage.
355 //calculating attraction strength
356 //greater distance means greater strength
359 actual_forces.set(i,actual_forces[i]-delta);
361 for(InEdgeIt ei((mytab.mapstorage).graph,i);ei!=INVALID;++ei)
363 lemon::dim2::Point<double> delta =
364 ((mytab.mapstorage).coords[i]-
365 (mytab.mapstorage).coords[mytab.mapstorage.
368 //calculating attraction strength
369 //greater distance means greater strength
372 actual_forces.set(i,actual_forces[i]-delta);
375 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
377 moveNode(actual_forces[i].x, actual_forces[i].y, nodesmap[i], i);