[6] | 1 | #include <graph_displayer_canvas.h> |
---|
| 2 | #include <math.h> |
---|
| 3 | |
---|
| 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) |
---|
| 5 | { |
---|
| 6 | //set_center_scroll_region(true); |
---|
| 7 | |
---|
| 8 | //first edges are drawn, to hide joining with nodes later |
---|
| 9 | |
---|
| 10 | for (EdgeIt i(g); i!=INVALID; ++i) |
---|
| 11 | { |
---|
| 12 | |
---|
| 13 | //drawing green lines, coordinates are from cm |
---|
| 14 | |
---|
| 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)); |
---|
| 18 | |
---|
| 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); |
---|
| 22 | |
---|
| 23 | //initializing edge-text as well, to empty string |
---|
| 24 | |
---|
| 25 | double x1, x2, y1, y2; |
---|
| 26 | edgesmap[i]->get_bounds(x1, y1, x2, y2); |
---|
| 27 | |
---|
| 28 | edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, ""); |
---|
| 29 | edgetextmap[i]->property_fill_color().set_value("black"); |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | //afterwards nodes come to be drawn |
---|
| 33 | |
---|
| 34 | NodeIt i(g); |
---|
| 35 | int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y; |
---|
| 36 | |
---|
| 37 | for (; i!=INVALID; ++i) |
---|
| 38 | { |
---|
| 39 | //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen) |
---|
| 40 | |
---|
| 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; |
---|
| 45 | |
---|
| 46 | //drawing bule nodes, with black line around them |
---|
| 47 | |
---|
| 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)); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /* |
---|
| 55 | //setting zoom to be able to see the whole graph on the canvas |
---|
| 56 | |
---|
| 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); |
---|
| 59 | |
---|
| 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; |
---|
| 64 | */ |
---|
| 65 | updateScrollRegion(); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | GraphDisplayerCanvas::~GraphDisplayerCanvas() |
---|
| 69 | { |
---|
| 70 | |
---|
| 71 | //writing out the end state of the graph |
---|
| 72 | //\todo all the maps has to be write out! |
---|
| 73 | |
---|
| 74 | Graph::NodeMap <int> id(g); |
---|
| 75 | Graph::NodeMap <double> xc(g); |
---|
| 76 | Graph::NodeMap <double> yc(g); |
---|
| 77 | |
---|
| 78 | int j=1; |
---|
| 79 | |
---|
| 80 | for (NodeIt i(g); i!=INVALID; ++i) |
---|
| 81 | { |
---|
| 82 | double x1,y1,x2,y2; |
---|
| 83 | nodesmap[i]->get_bounds(x1, y1, x2, y2); |
---|
| 84 | |
---|
| 85 | id[i]=j++; |
---|
| 86 | xc[i]=(x1+x2)/2; |
---|
| 87 | yc[i]=(y1+y2)/2; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | GraphWriter<Graph> writer(std::cout,g); |
---|
| 91 | |
---|
| 92 | writer.writeNodeMap("id", id); |
---|
| 93 | writer.writeNodeMap("coordinates_x", xc); |
---|
| 94 | writer.writeNodeMap("coordinates_y", yc); |
---|
| 95 | writer.run(); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | int GraphDisplayerCanvas::changeLineWidth (std::string mapname) |
---|
| 99 | { |
---|
| 100 | for (EdgeIt i(g); i!=INVALID; ++i) |
---|
| 101 | { |
---|
| 102 | int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i]; |
---|
| 103 | edgesmap[i]->property_width_pixels().set_value(w); |
---|
| 104 | } |
---|
| 105 | return 0; |
---|
| 106 | }; |
---|
| 107 | |
---|
| 108 | int GraphDisplayerCanvas::changeColor (std::string mapname) |
---|
| 109 | { |
---|
| 110 | |
---|
| 111 | //function maps the range of the maximum and |
---|
| 112 | //the minimum of the nodemap to the range of |
---|
| 113 | //green in RGB |
---|
| 114 | |
---|
| 115 | for (EdgeIt i(g); i!=INVALID; ++i) |
---|
| 116 | { |
---|
| 117 | double w=(*(mapstorage.edgemap_storage)[mapname])[i]; |
---|
| 118 | double max=mapstorage.maxOfEdgeMap(mapname); |
---|
| 119 | double min=mapstorage.minOfEdgeMap(mapname); |
---|
| 120 | |
---|
| 121 | //std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl; |
---|
| 122 | Gdk::Color color; |
---|
| 123 | if(max!=min) |
---|
| 124 | { |
---|
| 125 | color.set_rgb_p (0, 100*(w-min)/(max-min), 0); |
---|
| 126 | } |
---|
| 127 | else |
---|
| 128 | { |
---|
| 129 | color.set_rgb_p (0, 100, 0); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | edgesmap[i]->property_fill_color_gdk().set_value(color); |
---|
| 133 | } |
---|
| 134 | return 0; |
---|
| 135 | }; |
---|
| 136 | |
---|
| 137 | int GraphDisplayerCanvas::changeText (std::string mapname) |
---|
| 138 | { |
---|
| 139 | |
---|
| 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? |
---|
| 145 | |
---|
| 146 | for (EdgeIt i(g); i!=INVALID; ++i) |
---|
| 147 | { |
---|
| 148 | if(mapname!="Text") |
---|
| 149 | { |
---|
| 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]; |
---|
| 155 | str[length]='.'; |
---|
| 156 | str[strl]='\0'; |
---|
| 157 | |
---|
| 158 | for(int j=0;j<strl;j++) |
---|
| 159 | { |
---|
| 160 | if(j!=length) |
---|
| 161 | { |
---|
| 162 | int digit=(int)(number/maxpos); |
---|
| 163 | str[j]=(digit+'0'); |
---|
| 164 | number-=digit*maxpos; |
---|
| 165 | number*=10; |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | edgetextmap[i]->property_text().set_value(str); |
---|
| 170 | } |
---|
| 171 | else |
---|
| 172 | { |
---|
| 173 | edgetextmap[i]->property_text().set_value(""); |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | return 0; |
---|
| 177 | }; |
---|
| 178 | |
---|
| 179 | bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n) |
---|
| 180 | { |
---|
| 181 | switch(e->type) |
---|
| 182 | { |
---|
| 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)); |
---|
| 188 | isbutton=true; |
---|
| 189 | break; |
---|
| 190 | case GDK_BUTTON_RELEASE: |
---|
| 191 | isbutton=false; |
---|
| 192 | active_item=NULL; |
---|
[7] | 193 | updateScrollRegion(); |
---|
[6] | 194 | break; |
---|
| 195 | case GDK_MOTION_NOTIFY: |
---|
| 196 | //we only have to do sg. if the mouse button is pressed |
---|
| 197 | if(isbutton) |
---|
| 198 | { |
---|
| 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 |
---|
| 203 | |
---|
| 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; |
---|
| 209 | |
---|
| 210 | //all the edges connected to the moved point has to be redrawn |
---|
| 211 | |
---|
| 212 | EdgeIt e; |
---|
| 213 | g.firstOut(e,n); |
---|
| 214 | for(;e!=INVALID;g.nextOut(e)) |
---|
| 215 | { |
---|
| 216 | Gnome::Canvas::Points coos; |
---|
| 217 | double x1, x2, y1, y2; |
---|
| 218 | |
---|
| 219 | nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); |
---|
| 220 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
| 221 | |
---|
| 222 | nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); |
---|
| 223 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
| 224 | |
---|
| 225 | edgesmap[e]->property_points().set_value(coos); |
---|
| 226 | |
---|
| 227 | edgesmap[e]->get_bounds(x1, y1, x2, y2); |
---|
| 228 | |
---|
| 229 | edgetextmap[e]->property_x().set_value((x1+x2)/2); |
---|
| 230 | edgetextmap[e]->property_y().set_value((y1+y2)/2); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | g.firstIn(e,n); |
---|
| 234 | for(;e!=INVALID;g.nextIn(e)) |
---|
| 235 | { |
---|
| 236 | Gnome::Canvas::Points coos; |
---|
| 237 | double x1, x2, y1, y2; |
---|
| 238 | |
---|
| 239 | nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); |
---|
| 240 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
| 241 | |
---|
| 242 | nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); |
---|
| 243 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
| 244 | |
---|
| 245 | edgesmap[e]->property_points().set_value(coos); |
---|
| 246 | |
---|
| 247 | edgesmap[e]->get_bounds(x1, y1, x2, y2); |
---|
| 248 | |
---|
| 249 | edgetextmap[e]->property_x().set_value((x1+x2)/2); |
---|
| 250 | edgetextmap[e]->property_y().set_value((y1+y2)/2); |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | default: break; |
---|
| 254 | } |
---|
| 255 | return true; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event) |
---|
| 259 | { |
---|
| 260 | Gnome::Canvas::CanvasAA::on_expose_event(event); |
---|
| 261 | //usleep(10000); |
---|
| 262 | //rezoom(); |
---|
| 263 | return true; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | void GraphDisplayerCanvas::zoomIn() |
---|
| 267 | { |
---|
| 268 | set_pixels_per_unit( |
---|
| 269 | (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit()); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | void GraphDisplayerCanvas::zoomOut() |
---|
| 273 | { |
---|
| 274 | set_pixels_per_unit( |
---|
| 275 | (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit()); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | void GraphDisplayerCanvas::zoomFit() |
---|
| 279 | { |
---|
| 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(); |
---|
| 284 | // add some space |
---|
| 285 | aw -= 5; if (aw < 0) aw = 0; |
---|
| 286 | ah -= 5; if (ah < 0) ah = 0; |
---|
| 287 | |
---|
| 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); |
---|
| 292 | |
---|
| 293 | // fit the graph to the window |
---|
[7] | 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); |
---|
[6] | 297 | } |
---|
| 298 | |
---|
| 299 | void GraphDisplayerCanvas::zoom100() |
---|
| 300 | { |
---|
| 301 | set_pixels_per_unit(1.0); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | void GraphDisplayerCanvas::updateScrollRegion() |
---|
| 305 | { |
---|
| 306 | double wx1, wy1, wx2, wy2; |
---|
| 307 | Gnome::Canvas::Item* pCanvasItem = root(); |
---|
| 308 | pCanvasItem->get_bounds(wx1, wy1, wx2, wy2); |
---|
[7] | 309 | set_scroll_region(wx1, wy1, wx2, wy2); |
---|
[6] | 310 | } |
---|