1 #include <graph_displayer_canvas.h>
4 GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm, MapStorage & ms):g(gr),nodesmap(g),edgesmap(g),edgetextmap(g),displayed_graph(*(root()), 0, 0),mapstorage(ms),isbutton(false),active_item(NULL)
6 //set_center_scroll_region(true);
8 //first edges are drawn, to hide joining with nodes later
10 for (EdgeIt i(g); i!=INVALID; ++i)
13 //drawing green lines, coordinates are from cm
15 Gnome::Canvas::Points coos;
16 coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
17 coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
19 edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
20 *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
21 edgesmap[i]->property_width_pixels().set_value(10);
23 //initializing edge-text as well, to empty string
25 double x1, x2, y1, y2;
26 edgesmap[i]->get_bounds(x1, y1, x2, y2);
28 edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
29 edgetextmap[i]->property_fill_color().set_value("black");
32 //afterwards nodes come to be drawn
35 int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
37 for (; i!=INVALID; ++i)
39 //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
41 if(cm[i].x>maxx)maxx=(int)cm[i].x;
42 if(cm[i].y>maxy)maxy=(int)cm[i].y;
43 if(cm[i].x<minx)minx=(int)cm[i].x;
44 if(cm[i].y<miny)miny=(int)cm[i].y;
46 //drawing bule nodes, with black line around them
48 nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
49 *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
50 *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
51 (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
55 //setting zoom to be able to see the whole graph on the canvas
57 double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
58 double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
60 set_pixels_per_unit((biggest_x>biggest_y)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
61 std::cout<<abs(maxx)<<" "<<abs(minx)<<" big x "<<biggest_x<<" "<<abs(maxy)<<" "<<abs(miny)<<" big y "<<biggest_y<<std::endl;
62 std::cout<<maxx<<" "<<minx<<" big x "<<biggest_x<<" "<<maxy<<" "<<miny<<" big y "<<biggest_y<<std::endl;
63 std::cout<<"dx "<<(maxx-minx)<<" dy "<<(maxy-miny)<<" xrate "<<((maxx-minx)/WIN_WIDTH)<<" yrate "<<((maxy-miny)/WIN_HEIGHT)<<std::endl;
68 GraphDisplayerCanvas::~GraphDisplayerCanvas()
71 //writing out the end state of the graph
72 //\todo all the maps has to be write out!
74 Graph::NodeMap <int> id(g);
75 Graph::NodeMap <double> xc(g);
76 Graph::NodeMap <double> yc(g);
80 for (NodeIt i(g); i!=INVALID; ++i)
83 nodesmap[i]->get_bounds(x1, y1, x2, y2);
90 GraphWriter<Graph> writer(std::cout,g);
92 writer.writeNodeMap("id", id);
93 writer.writeNodeMap("coordinates_x", xc);
94 writer.writeNodeMap("coordinates_y", yc);
98 int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
100 for (EdgeIt i(g); i!=INVALID; ++i)
102 int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
103 edgesmap[i]->property_width_pixels().set_value(w);
108 int GraphDisplayerCanvas::changeColor (std::string mapname)
111 //function maps the range of the maximum and
112 //the minimum of the nodemap to the range of
115 for (EdgeIt i(g); i!=INVALID; ++i)
117 double w=(*(mapstorage.edgemap_storage)[mapname])[i];
118 double max=mapstorage.maxOfEdgeMap(mapname);
119 double min=mapstorage.minOfEdgeMap(mapname);
121 //std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
125 color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
129 color.set_rgb_p (0, 100, 0);
132 edgesmap[i]->property_fill_color_gdk().set_value(color);
137 int GraphDisplayerCanvas::changeText (std::string mapname)
140 //the number in the map will be written on the edge
141 //EXCEPT when the name of the map is Text, because
142 //in that case empty string will be written, because
143 //that is the deleter map
144 //\todo isn't it a bit woodcutter?
146 for (EdgeIt i(g); i!=INVALID; ++i)
150 double number=(*(mapstorage.edgemap_storage)[mapname])[i];
151 int length=(int)(floor(log(number)/log(10)))+1;
152 int maxpos=(int)(pow(10,length-1));
153 int strl=length+1+RANGE;
154 char * str=new char[strl];
158 for(int j=0;j<strl;j++)
162 int digit=(int)(number/maxpos);
164 number-=digit*maxpos;
169 edgetextmap[i]->property_text().set_value(str);
173 edgetextmap[i]->property_text().set_value("");
179 bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
183 case GDK_BUTTON_PRESS:
184 //we mark the location of the event to be able to calculate parameters of dragging
185 clicked_x=e->button.x;
186 clicked_y=e->button.y;
187 active_item=(get_item_at(e->button.x, e->button.y));
190 case GDK_BUTTON_RELEASE:
194 case GDK_MOTION_NOTIFY:
195 //we only have to do sg. if the mouse button is pressed
198 //new coordinates will be the old values,
199 //because the item will be moved to the
200 //new coordinate therefore the new movement
201 //has to be calculated from here
203 double dx=e->motion.x-clicked_x;
204 double dy=e->motion.y-clicked_y;
205 active_item->move(dx, dy);
206 clicked_x=e->motion.x;
207 clicked_y=e->motion.y;
209 //all the edges connected to the moved point has to be redrawn
213 for(;e!=INVALID;g.nextOut(e))
215 Gnome::Canvas::Points coos;
216 double x1, x2, y1, y2;
218 nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
219 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
221 nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
222 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
224 edgesmap[e]->property_points().set_value(coos);
226 edgesmap[e]->get_bounds(x1, y1, x2, y2);
228 edgetextmap[e]->property_x().set_value((x1+x2)/2);
229 edgetextmap[e]->property_y().set_value((y1+y2)/2);
233 for(;e!=INVALID;g.nextIn(e))
235 Gnome::Canvas::Points coos;
236 double x1, x2, y1, y2;
238 nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
239 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
241 nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
242 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
244 edgesmap[e]->property_points().set_value(coos);
246 edgesmap[e]->get_bounds(x1, y1, x2, y2);
248 edgetextmap[e]->property_x().set_value((x1+x2)/2);
249 edgetextmap[e]->property_y().set_value((y1+y2)/2);
257 bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
259 Gnome::Canvas::CanvasAA::on_expose_event(event);
265 void GraphDisplayerCanvas::zoomIn()
268 (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
271 void GraphDisplayerCanvas::zoomOut()
274 (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
277 void GraphDisplayerCanvas::zoomFit()
279 // get the height and width of the canvas
280 Gtk::Allocation a = get_allocation();
281 int aw = a.get_width();
282 int ah = a.get_height();
284 aw -= 5; if (aw < 0) aw = 0;
285 ah -= 5; if (ah < 0) ah = 0;
286 //std::cout << "aw=" << aw << " ah=" << ah << std::endl;
288 // get the bounding box of the graph
289 set_pixels_per_unit(1.0); // I don't really understand why this is necessary
290 double wx1, wy1, wx2, wy2;
291 double cx1, cy1, cx2, cy2;
292 Gnome::Canvas::Item* pCanvasItem = root();
293 pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
294 //std::cout << "root bounds: " << wx1 << " " << wy1 << " " << wx2 << " " << wy2 << std::endl;
295 w2c(wx1, wy1, cx1, cy1);
296 w2c(wx2, wy2, cx2, cy2);
297 //std::cout << "root bounds (c): " << cx1 << " " << cy1 << " " << cx2 << " " << cy2 << std::endl;
298 //std::cout << "cx2 - cx1=" << fabs(cx2 - cx1) << " cy2 - cy1=" << fabs(cy2 - cy1) << std::endl;
300 // fit the graph to the window
301 double ppu1 = (double) aw / fabs(cx2 - cx1);
302 double ppu2 = (double) ah / fabs(cy2 - cy1);
303 //std::cout << "ppu1=" << ppu1 << " ppu2=" << ppu2 << std::endl;
304 (ppu1 < ppu2) ? set_pixels_per_unit(ppu1) : set_pixels_per_unit(ppu2);
307 void GraphDisplayerCanvas::zoom100()
309 set_pixels_per_unit(1.0);
312 void GraphDisplayerCanvas::updateScrollRegion()
314 double wx1, wy1, wx2, wy2;
315 int cx1, cy1, cx2, cy2;
316 Gnome::Canvas::Item* pCanvasItem = root();
317 pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
318 w2c(wx1, wy1, cx1, cy1);
319 w2c(wx2, wy2, cx2, cy2);
320 set_scroll_region(cx1, cy1, cx2, cy2);