Maps are now selectable through ComboBoxes, and Escape makes MapWin disappeared.
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:
193 updateScrollRegion();
195 case GDK_MOTION_NOTIFY:
196 //we only have to do sg. if the mouse button is pressed
199 //new coordinates will be the old values,
200 //because the item will be moved to the
201 //new coordinate therefore the new movement
202 //has to be calculated from here
204 double dx=e->motion.x-clicked_x;
205 double dy=e->motion.y-clicked_y;
206 active_item->move(dx, dy);
207 clicked_x=e->motion.x;
208 clicked_y=e->motion.y;
210 //all the edges connected to the moved point has to be redrawn
214 for(;e!=INVALID;g.nextOut(e))
216 Gnome::Canvas::Points coos;
217 double x1, x2, y1, y2;
219 nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
220 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
222 nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
223 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
225 edgesmap[e]->property_points().set_value(coos);
227 edgesmap[e]->get_bounds(x1, y1, x2, y2);
229 edgetextmap[e]->property_x().set_value((x1+x2)/2);
230 edgetextmap[e]->property_y().set_value((y1+y2)/2);
234 for(;e!=INVALID;g.nextIn(e))
236 Gnome::Canvas::Points coos;
237 double x1, x2, y1, y2;
239 nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
240 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
242 nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
243 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
245 edgesmap[e]->property_points().set_value(coos);
247 edgesmap[e]->get_bounds(x1, y1, x2, y2);
249 edgetextmap[e]->property_x().set_value((x1+x2)/2);
250 edgetextmap[e]->property_y().set_value((y1+y2)/2);
258 bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
260 Gnome::Canvas::CanvasAA::on_expose_event(event);
266 void GraphDisplayerCanvas::zoomIn()
269 (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
272 void GraphDisplayerCanvas::zoomOut()
275 (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
278 void GraphDisplayerCanvas::zoomFit()
280 // get the height and width of the canvas
281 Gtk::Allocation a = get_allocation();
282 int aw = a.get_width();
283 int ah = a.get_height();
285 aw -= 5; if (aw < 0) aw = 0;
286 ah -= 5; if (ah < 0) ah = 0;
288 // get the bounding box of the graph
289 double wx1, wy1, wx2, wy2;
290 Gnome::Canvas::Item* pCanvasItem = root();
291 pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
293 // fit the graph to the window
294 double ppu1 = (double) aw / fabs(wx2 - wx1);
295 double ppu2 = (double) ah / fabs(wy2 - wy1);
296 set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2);
299 void GraphDisplayerCanvas::zoom100()
301 set_pixels_per_unit(1.0);
304 void GraphDisplayerCanvas::updateScrollRegion()
306 double wx1, wy1, wx2, wy2;
307 Gnome::Canvas::Item* pCanvasItem = root();
308 pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
309 set_scroll_region(wx1, wy1, wx2, wy2);