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 <mapstorage.h>
21 #include <graph_displayer_canvas.h>
22 #include <lemon/random.h>
25 GraphDisplayerCanvas::GraphDisplayerCanvas(NoteBookTab & mainw) :
26 nodesmap(mainw.mapstorage->graph), edgesmap(mainw.mapstorage->graph), edgetextmap(mainw.mapstorage->graph),
27 nodetextmap(mainw.mapstorage->graph), displayed_graph(*(root()), 0, 0),
28 isbutton(0), active_item(NULL), target_item(NULL), nodemap_to_edit(""),
29 edgemap_to_edit(""), autoscale(true), zoomtrack(false), radius_size(20), edge_width(10),
30 was_redesigned(false), is_drawn(false), mytab(mainw),
33 //add mouse scroll event handler - it won't be changed, it handles zoom
34 signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::scrollEventHandler), false);
36 //base event handler is move tool
37 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
48 void GraphDisplayerCanvas::setBackground()
54 if (mytab.mapstorage->isBackgroundSet())
56 background_set = true;
57 refBackground = Gdk::Pixbuf::create_from_file(
58 mytab.mapstorage->getBackgroundFilename());
59 background = new Gnome::Canvas::Pixbuf(
61 0.0 - refBackground->get_width() / 2.0,
62 0.0 - refBackground->get_height() / 2.0,
64 background->lower_to_bottom();
68 background_set = false;
72 GraphDisplayerCanvas::~GraphDisplayerCanvas()
74 for (NodeIt n((mytab.mapstorage)->graph); n != INVALID; ++n)
77 delete nodetextmap[n];
80 for (EdgeIt e((mytab.mapstorage)->graph); e != INVALID; ++e)
83 delete edgetextmap[e];
87 void GraphDisplayerCanvas::propertyChange(bool itisedge, int prop)
91 propertyUpdate(Edge(INVALID), prop);
95 propertyUpdate(Node(INVALID), prop);
99 void GraphDisplayerCanvas::propertyUpdate(Edge edge)
101 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
103 propertyUpdate(edge, i);
107 void GraphDisplayerCanvas::propertyUpdate(Node node)
109 for(int i=0;i<NODE_PROPERTY_NUM;i++)
111 propertyUpdate(node, i);
115 void GraphDisplayerCanvas::propertyUpdate(Node node, int prop)
117 std::string mapname=mytab.getActiveNodeMap(prop);
123 if( ( ((mytab.mapstorage)->nodemap_storage).find(mapname) != ((mytab.mapstorage)->nodemap_storage).end() ) )
128 changeNodeRadius(mapname, node);
131 changeNodeColor(mapname, node);
134 changeNodeText(mapname, node);
137 std::cerr<<"Error\n";
147 resetNodeRadius(node);
150 resetNodeColor(node);
156 std::cerr<<"Error\n";
162 void GraphDisplayerCanvas::propertyUpdate(Edge edge, int prop)
164 std::string mapname=mytab.getActiveEdgeMap(prop);
170 if( ( ((mytab.mapstorage)->edgemap_storage).find(mapname) != ((mytab.mapstorage)->edgemap_storage).end() ) )
175 changeEdgeWidth(mapname, edge);
178 changeEdgeColor(mapname, edge);
181 changeEdgeText(mapname, edge);
184 std::cerr<<"Error\n";
193 resetEdgeWidth(edge);
196 resetEdgeColor(edge);
202 std::cerr<<"Error\n";
208 void GraphDisplayerCanvas::drawGraph()
210 //first edges are drawn, to hide joining with nodes later
212 for (EdgeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
214 if (mytab.mapstorage->graph.source(i) == mytab.mapstorage->graph.target(i))
216 edgesmap[i]=new LoopEdge(displayed_graph, i, *this);
220 edgesmap[i]=new BrokenEdge(displayed_graph, i, *this);
222 //initializing edge-text as well, to empty string
224 XY text_pos=mytab.mapstorage->arrow_pos[i];
225 text_pos+=(XY(10,10));
227 edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
228 edgetextmap[i]->property_fill_color().set_value("darkgreen");
229 edgetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
230 edgetextmap[i]->raise_to_top();
233 //afterwards nodes come to be drawn
235 for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
237 //drawing bule nodes, with black line around them
239 nodesmap[i]=new Gnome::Canvas::Ellipse(
241 (mytab.mapstorage)->coords[i].x-20,
242 (mytab.mapstorage)->coords[i].y-20,
243 (mytab.mapstorage)->coords[i].x+20,
244 (mytab.mapstorage)->coords[i].y+20);
245 *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
246 *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
247 nodesmap[i]->raise_to_top();
249 //initializing edge-text as well, to empty string
252 ((mytab.mapstorage)->coords[i].x+node_property_defaults[N_RADIUS]+5),
253 ((mytab.mapstorage)->coords[i].y+node_property_defaults[N_RADIUS]+5));
255 nodetextmap[i]=new Gnome::Canvas::Text(displayed_graph,
256 text_pos.x, text_pos.y, "");
257 nodetextmap[i]->property_fill_color().set_value("darkblue");
258 nodetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
259 nodetextmap[i]->raise_to_top();
267 for(int i=0;i<NODE_PROPERTY_NUM;i++)
269 propertyUpdate(Node(INVALID), i);
272 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
274 propertyUpdate(Edge(INVALID), i);
277 updateScrollRegion();
280 void GraphDisplayerCanvas::clear()
284 forming_edge=INVALID;
286 for (NodeIt n((mytab.mapstorage)->graph); n != INVALID; ++n)
289 delete nodetextmap[n];
292 for (EdgeIt e((mytab.mapstorage)->graph); e != INVALID; ++e)
295 delete edgetextmap[e];
301 void GraphDisplayerCanvas::setView(bool autoscale_p, bool zoomtrack_p, double width_p, double radius_p)
303 autoscale=autoscale_p;
305 radius_size=radius_p;
307 if((!zoomtrack) && zoomtrack_p)
309 fixed_zoom_factor=get_pixels_per_unit();
312 zoomtrack=zoomtrack_p;
314 propertyChange(false, N_RADIUS);
315 propertyChange(true, E_WIDTH);
318 void GraphDisplayerCanvas::getView(bool & autoscale_p, bool & zoomtrack_p, double& width_p, double& radius_p)
320 autoscale_p=autoscale;
321 zoomtrack_p=zoomtrack;
323 radius_p=radius_size;
326 void GraphDisplayerCanvas::reDesignGraph()
328 NodeIt firstnode((mytab.mapstorage)->graph);
329 //is it not an empty graph?
330 if(firstnode!=INVALID)
332 double max_coord=50000;
334 double init_vector_length=25;
338 NodeIt i((mytab.mapstorage)->graph);
340 dim2::Point<double> init(init_vector_length*rnd(),
341 init_vector_length*rnd());
342 moveNode(init.x, init.y, nodesmap[i], i);
350 (mytab.mapstorage)->get_design_data(attraction, propulsation, iterations);
353 for(int l=0;l<iterations;l++)
355 Graph::NodeMap<double> x(mytab.mapstorage->graph);
356 Graph::NodeMap<double> y(mytab.mapstorage->graph);
357 XYMap<Graph::NodeMap<double> > actual_forces;
358 actual_forces.setXMap(x);
359 actual_forces.setYMap(y);
361 //count actual force for each nodes
362 for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
364 //propulsation of nodes
365 for (NodeIt j((mytab.mapstorage)->graph); j!=INVALID; ++j)
369 lemon::dim2::Point<double> delta =
370 ((mytab.mapstorage)->coords[i]-
371 (mytab.mapstorage)->coords[j]);
373 const double length_sqr=std::max(delta.normSquare(),min_dist);
376 delta/=sqrt(length_sqr);
378 //calculating propulsation strength
379 //greater distance menas smaller propulsation strength
380 delta*=propulsation/length_sqr;
382 actual_forces.set(i,(actual_forces[i]+delta));
385 //attraction of nodes, to which actual node is bound
386 for(OutEdgeIt ei((mytab.mapstorage)->graph,i);ei!=INVALID;++ei)
388 lemon::dim2::Point<double> delta =
389 ((mytab.mapstorage)->coords[i]-
390 (mytab.mapstorage)->coords[mytab.mapstorage->
393 //calculating attraction strength
394 //greater distance means greater strength
397 actual_forces.set(i,actual_forces[i]-delta);
399 for(InEdgeIt ei((mytab.mapstorage)->graph,i);ei!=INVALID;++ei)
401 lemon::dim2::Point<double> delta =
402 ((mytab.mapstorage)->coords[i]-
403 (mytab.mapstorage)->coords[mytab.mapstorage->
406 //calculating attraction strength
407 //greater distance means greater strength
410 actual_forces.set(i,actual_forces[i]-delta);
413 for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i)
415 if(((mytab.mapstorage)->coords[i].x)+actual_forces[i].x>max_coord)
417 actual_forces[i].x=max_coord-((mytab.mapstorage)->coords[i].x);
418 std::cout << "Correction! " << (((mytab.mapstorage)->coords[i].x)+actual_forces[i].x) << std::endl;
420 else if(((mytab.mapstorage)->coords[i].x)+actual_forces[i].x<(0-max_coord))
422 actual_forces[i].x=0-max_coord-((mytab.mapstorage)->coords[i].x);
423 std::cout << "Correction! " << (((mytab.mapstorage)->coords[i].x)+actual_forces[i].x) << std::endl;
425 if(((mytab.mapstorage)->coords[i].y)+actual_forces[i].y>max_coord)
427 actual_forces[i].y=max_coord-((mytab.mapstorage)->coords[i].y);
428 std::cout << "Correction! " << (((mytab.mapstorage)->coords[i].y)+actual_forces[i].y) << std::endl;
430 else if(((mytab.mapstorage)->coords[i].y)+actual_forces[i].y<(0-max_coord))
432 actual_forces[i].y=0-max_coord-((mytab.mapstorage)->coords[i].y);
433 std::cout << "Correction! " << (((mytab.mapstorage)->coords[i].y)+actual_forces[i].y) << std::endl;
435 moveNode(actual_forces[i].x, actual_forces[i].y, nodesmap[i], i);