Removed this extra widget thing, because it is now developed in my private branch.
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 //base event handler is move tool
32 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
42 void GraphDisplayerCanvas::setBackground()
48 if (mytab.mapstorage.isBackgroundSet())
50 background_set = true;
51 refBackground = Gdk::Pixbuf::create_from_file(
52 mytab.mapstorage.getBackgroundFilename());
53 background = new Gnome::Canvas::Pixbuf(
55 0.0 - refBackground->get_width() / 2.0,
56 0.0 - refBackground->get_height() / 2.0,
58 background->lower_to_bottom();
62 background_set = false;
66 GraphDisplayerCanvas::~GraphDisplayerCanvas()
68 for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
71 delete nodetextmap[n];
74 for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
77 delete edgetextmap[e];
81 void GraphDisplayerCanvas::propertyChange(bool itisedge, int prop)
85 propertyUpdate(Edge(INVALID), prop);
89 propertyUpdate(Node(INVALID), prop);
93 void GraphDisplayerCanvas::propertyUpdate(Edge edge)
95 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
97 propertyUpdate(edge, i);
101 void GraphDisplayerCanvas::propertyUpdate(Node node)
103 for(int i=0;i<NODE_PROPERTY_NUM;i++)
105 propertyUpdate(node, i);
109 void GraphDisplayerCanvas::propertyUpdate(Node node, int prop)
111 std::string mapname=mytab.getActiveNodeMap(prop);
117 if( ( ((mytab.mapstorage).nodemap_storage).find(mapname) != ((mytab.mapstorage).nodemap_storage).end() ) )
122 changeNodeRadius(mapname, node);
125 changeNodeColor(mapname, node);
128 changeNodeText(mapname, node);
131 std::cerr<<"Error\n";
141 resetNodeRadius(node);
144 resetNodeColor(node);
150 std::cerr<<"Error\n";
156 void GraphDisplayerCanvas::propertyUpdate(Edge edge, int prop)
158 std::string mapname=mytab.getActiveEdgeMap(prop);
164 if( ( ((mytab.mapstorage).edgemap_storage).find(mapname) != ((mytab.mapstorage).edgemap_storage).end() ) )
169 changeEdgeWidth(mapname, edge);
172 changeEdgeColor(mapname, edge);
175 changeEdgeText(mapname, edge);
178 std::cerr<<"Error\n";
187 resetEdgeWidth(edge);
190 resetEdgeColor(edge);
196 std::cerr<<"Error\n";
202 void GraphDisplayerCanvas::drawGraph()
204 //first edges are drawn, to hide joining with nodes later
206 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
208 if (mytab.mapstorage.graph.source(i) == mytab.mapstorage.graph.target(i))
210 edgesmap[i]=new LoopEdge(displayed_graph, i, *this);
214 edgesmap[i]=new BrokenEdge(displayed_graph, i, *this);
216 //initializing edge-text as well, to empty string
218 XY text_pos=mytab.mapstorage.arrow_pos[i];
219 text_pos+=(XY(10,10));
221 edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
222 edgetextmap[i]->property_fill_color().set_value("darkgreen");
223 edgetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
224 edgetextmap[i]->raise_to_top();
227 //afterwards nodes come to be drawn
229 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
231 //drawing bule nodes, with black line around them
233 nodesmap[i]=new Gnome::Canvas::Ellipse(
235 (mytab.mapstorage).coords[i].x-20,
236 (mytab.mapstorage).coords[i].y-20,
237 (mytab.mapstorage).coords[i].x+20,
238 (mytab.mapstorage).coords[i].y+20);
239 *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
240 *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
241 nodesmap[i]->raise_to_top();
243 //initializing edge-text as well, to empty string
246 ((mytab.mapstorage).coords[i].x+node_property_defaults[N_RADIUS]+5),
247 ((mytab.mapstorage).coords[i].y+node_property_defaults[N_RADIUS]+5));
249 nodetextmap[i]=new Gnome::Canvas::Text(displayed_graph,
250 text_pos.x, text_pos.y, "");
251 nodetextmap[i]->property_fill_color().set_value("darkblue");
252 nodetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
253 nodetextmap[i]->raise_to_top();
261 for(int i=0;i<NODE_PROPERTY_NUM;i++)
263 propertyUpdate(Node(INVALID), i);
266 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
268 propertyUpdate(Edge(INVALID), i);
271 updateScrollRegion();
274 void GraphDisplayerCanvas::clear()
278 forming_edge=INVALID;
280 for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
283 delete nodetextmap[n];
286 for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
289 delete edgetextmap[e];
295 void GraphDisplayerCanvas::setView(bool autoscale_p, bool zoomtrack_p, double width_p, double radius_p)
297 autoscale=autoscale_p;
299 radius_size=radius_p;
301 if((!zoomtrack) && zoomtrack_p)
303 fixed_zoom_factor=get_pixels_per_unit();
306 zoomtrack=zoomtrack_p;
308 propertyChange(false, N_RADIUS);
309 propertyChange(true, E_WIDTH);
312 void GraphDisplayerCanvas::getView(bool & autoscale_p, bool & zoomtrack_p, double& width_p, double& radius_p)
314 autoscale_p=autoscale;
315 zoomtrack_p=zoomtrack;
317 radius_p=radius_size;
320 void GraphDisplayerCanvas::reDesignGraph()
323 double init_vector_length=25;
327 NodeIt i((mytab.mapstorage).graph);
329 dim2::Point<double> init(init_vector_length*rnd(),
330 init_vector_length*rnd());
331 moveNode(init.x, init.y, nodesmap[i], i);
339 (mytab.mapstorage).get_design_data(attraction, propulsation, iterations);
342 for(int l=0;l<iterations;l++)
344 Graph::NodeMap<double> x(mytab.mapstorage.graph);
345 Graph::NodeMap<double> y(mytab.mapstorage.graph);
346 XYMap<Graph::NodeMap<double> > actual_forces;
347 actual_forces.setXMap(x);
348 actual_forces.setYMap(y);
350 //count actual force for each nodes
351 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
353 //propulsation of nodes
354 for (NodeIt j((mytab.mapstorage).graph); j!=INVALID; ++j)
358 lemon::dim2::Point<double> delta =
359 ((mytab.mapstorage).coords[i]-
360 (mytab.mapstorage).coords[j]);
362 const double length_sqr=std::max(delta.normSquare(),min_dist);
365 delta/=sqrt(length_sqr);
367 //calculating propulsation strength
368 //greater distance menas smaller propulsation strength
369 delta*=propulsation/length_sqr;
371 actual_forces.set(i,(actual_forces[i]+delta));
374 //attraction of nodes, to which actual node is bound
375 for(OutEdgeIt ei((mytab.mapstorage).graph,i);ei!=INVALID;++ei)
377 lemon::dim2::Point<double> delta =
378 ((mytab.mapstorage).coords[i]-
379 (mytab.mapstorage).coords[mytab.mapstorage.
382 //calculating attraction strength
383 //greater distance means greater strength
386 actual_forces.set(i,actual_forces[i]-delta);
388 for(InEdgeIt ei((mytab.mapstorage).graph,i);ei!=INVALID;++ei)
390 lemon::dim2::Point<double> delta =
391 ((mytab.mapstorage).coords[i]-
392 (mytab.mapstorage).coords[mytab.mapstorage.
395 //calculating attraction strength
396 //greater distance means greater strength
399 actual_forces.set(i,actual_forces[i]-delta);
402 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
404 moveNode(actual_forces[i].x, actual_forces[i].y, nodesmap[i], i);