[1606] | 1 | #include "graph_displayer_canvas.h" |
---|
| 2 | #include "broken_edge.h" |
---|
[1632] | 3 | #include <cmath> |
---|
[1510] | 4 | |
---|
| 5 | |
---|
| 6 | bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event) |
---|
| 7 | { |
---|
| 8 | Gnome::Canvas::CanvasAA::on_expose_event(event); |
---|
| 9 | //usleep(10000); |
---|
| 10 | //rezoom(); |
---|
| 11 | return true; |
---|
| 12 | } |
---|
| 13 | |
---|
| 14 | void GraphDisplayerCanvas::changeEditorialTool(int newtool) |
---|
| 15 | { |
---|
[1562] | 16 | if(actual_tool!=newtool) |
---|
| 17 | { |
---|
[1510] | 18 | |
---|
[1562] | 19 | actual_handler.disconnect(); |
---|
[1510] | 20 | |
---|
[1562] | 21 | switch(actual_tool) |
---|
| 22 | { |
---|
| 23 | case CREATE_EDGE: |
---|
| 24 | { |
---|
| 25 | GdkEvent * generated=new GdkEvent(); |
---|
| 26 | generated->type=GDK_BUTTON_RELEASE; |
---|
| 27 | generated->button.button=3; |
---|
| 28 | createEdgeEventHandler(generated); |
---|
| 29 | break; |
---|
| 30 | } |
---|
| 31 | case EDGE_MAP_EDIT: |
---|
[1579] | 32 | //has to do the same thing as in the case of NODE_MAP_EDIT |
---|
| 33 | case NODE_MAP_EDIT: |
---|
| 34 | { |
---|
| 35 | break; |
---|
| 36 | } |
---|
[1562] | 37 | default: |
---|
| 38 | break; |
---|
| 39 | } |
---|
[1510] | 40 | |
---|
[1562] | 41 | active_item=NULL; |
---|
| 42 | target_item=NULL; |
---|
| 43 | active_edge=INVALID; |
---|
| 44 | active_node=INVALID; |
---|
[1551] | 45 | |
---|
[1510] | 46 | |
---|
[1562] | 47 | actual_tool=newtool; |
---|
| 48 | |
---|
| 49 | switch(newtool) |
---|
| 50 | { |
---|
| 51 | case MOVE: |
---|
| 52 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false); |
---|
| 53 | break; |
---|
[1510] | 54 | |
---|
[1562] | 55 | case CREATE_NODE: |
---|
| 56 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false); |
---|
| 57 | break; |
---|
[1510] | 58 | |
---|
[1562] | 59 | case CREATE_EDGE: |
---|
| 60 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false); |
---|
| 61 | break; |
---|
[1510] | 62 | |
---|
[1562] | 63 | case ERASER: |
---|
| 64 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false); |
---|
| 65 | break; |
---|
[1550] | 66 | |
---|
[1562] | 67 | case EDGE_MAP_EDIT: |
---|
| 68 | grab_focus(); |
---|
| 69 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false); |
---|
| 70 | break; |
---|
[1550] | 71 | |
---|
[1562] | 72 | case NODE_MAP_EDIT: |
---|
| 73 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false); |
---|
| 74 | break; |
---|
| 75 | |
---|
| 76 | default: |
---|
| 77 | break; |
---|
| 78 | } |
---|
[1510] | 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
[1524] | 82 | int GraphDisplayerCanvas::getActualTool() |
---|
[1510] | 83 | { |
---|
| 84 | return actual_tool; |
---|
| 85 | } |
---|
| 86 | |
---|
[1524] | 87 | bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e) |
---|
[1510] | 88 | { |
---|
[1652] | 89 | static Gnome::Canvas::Text *coord_text = 0; |
---|
[1510] | 90 | switch(e->type) |
---|
| 91 | { |
---|
| 92 | case GDK_BUTTON_PRESS: |
---|
| 93 | //we mark the location of the event to be able to calculate parameters of dragging |
---|
[1525] | 94 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 95 | |
---|
| 96 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
[1510] | 97 | active_node=INVALID; |
---|
[1606] | 98 | for (NodeIt i(mapstorage.graph); i!=INVALID; ++i) |
---|
[1510] | 99 | { |
---|
| 100 | if(nodesmap[i]==active_item) |
---|
| 101 | { |
---|
| 102 | active_node=i; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | switch(e->button.button) |
---|
| 106 | { |
---|
| 107 | case 3: |
---|
| 108 | isbutton=3; |
---|
| 109 | break; |
---|
| 110 | default: |
---|
| 111 | isbutton=1; |
---|
| 112 | break; |
---|
| 113 | } |
---|
| 114 | break; |
---|
| 115 | case GDK_BUTTON_RELEASE: |
---|
[1652] | 116 | if (coord_text) |
---|
| 117 | { |
---|
| 118 | delete coord_text; |
---|
| 119 | coord_text = 0; |
---|
| 120 | } |
---|
[1510] | 121 | isbutton=0; |
---|
| 122 | active_item=NULL; |
---|
| 123 | active_node=INVALID; |
---|
| 124 | break; |
---|
| 125 | case GDK_MOTION_NOTIFY: |
---|
| 126 | //we only have to do sg. if the mouse button is pressed AND the click was on a node that was found in the set of nodes |
---|
| 127 | if(active_node!=INVALID) |
---|
| 128 | { |
---|
[1606] | 129 | mapstorage.modified = true; |
---|
[1652] | 130 | |
---|
[1510] | 131 | //new coordinates will be the old values, |
---|
| 132 | //because the item will be moved to the |
---|
| 133 | //new coordinate therefore the new movement |
---|
| 134 | //has to be calculated from here |
---|
| 135 | |
---|
[1525] | 136 | double new_x, new_y; |
---|
| 137 | |
---|
| 138 | window_to_world (e->motion.x, e->motion.y, new_x, new_y); |
---|
| 139 | |
---|
| 140 | double dx=new_x-clicked_x; |
---|
| 141 | double dy=new_y-clicked_y; |
---|
[1510] | 142 | |
---|
[1512] | 143 | //repositioning node and its text |
---|
[1510] | 144 | active_item->move(dx, dy); |
---|
[1512] | 145 | nodetextmap[active_node]->move(dx, dy); |
---|
[1510] | 146 | |
---|
[1652] | 147 | // the new coordinates of the centre of the node |
---|
| 148 | double coord_x = new_x - (clicked_x - mapstorage.coords[active_node].x); |
---|
| 149 | double coord_y = new_y - (clicked_y - mapstorage.coords[active_node].y); |
---|
| 150 | |
---|
[1525] | 151 | clicked_x=new_x; |
---|
| 152 | clicked_y=new_y; |
---|
[1510] | 153 | |
---|
[1652] | 154 | // write back the new coordinates to the coords map |
---|
| 155 | mapstorage.coords.set(active_node, xy<double>(coord_x, coord_y)); |
---|
[1510] | 156 | |
---|
[1652] | 157 | // reposition the coordinates text |
---|
| 158 | std::ostringstream ostr; |
---|
| 159 | ostr << "(" << |
---|
| 160 | mapstorage.coords[active_node].x << ", " << |
---|
| 161 | mapstorage.coords[active_node].y << ")"; |
---|
| 162 | if (coord_text) |
---|
| 163 | { |
---|
| 164 | coord_text->property_text().set_value(ostr.str()); |
---|
| 165 | coord_text->property_x().set_value(mapstorage.coords[active_node].x + |
---|
| 166 | node_property_defaults[N_RADIUS] + 40); |
---|
| 167 | coord_text->property_y().set_value(mapstorage.coords[active_node].y + |
---|
| 168 | node_property_defaults[N_RADIUS] - 40); |
---|
| 169 | } |
---|
| 170 | else |
---|
| 171 | { |
---|
| 172 | coord_text = new Gnome::Canvas::Text( |
---|
| 173 | displayed_graph, |
---|
| 174 | mapstorage.coords[active_node].x + |
---|
| 175 | node_property_defaults[N_RADIUS] + 40, |
---|
| 176 | mapstorage.coords[active_node].y + |
---|
| 177 | node_property_defaults[N_RADIUS] - 40, |
---|
| 178 | ostr.str()); |
---|
| 179 | coord_text->property_fill_color().set_value("black"); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | //all the edges connected to the moved point has to be redrawn |
---|
[1651] | 183 | for(OutEdgeIt ei(mapstorage.graph,active_node);ei!=INVALID;++ei) |
---|
[1510] | 184 | { |
---|
| 185 | Gnome::Canvas::Points coos; |
---|
| 186 | double x1, x2, y1, y2; |
---|
| 187 | |
---|
[1606] | 188 | nodesmap[mapstorage.graph.source(ei)]->get_bounds(x1, y1, x2, y2); |
---|
[1510] | 189 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
| 190 | |
---|
[1606] | 191 | nodesmap[mapstorage.graph.target(ei)]->get_bounds(x1, y1, x2, y2); |
---|
[1510] | 192 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
| 193 | |
---|
| 194 | if(isbutton==3) |
---|
| 195 | { |
---|
[1524] | 196 | edgesmap[ei]->setPoints(coos); |
---|
[1510] | 197 | } |
---|
| 198 | else |
---|
| 199 | { |
---|
[1524] | 200 | edgesmap[ei]->setPoints(coos,true); |
---|
[1510] | 201 | } |
---|
| 202 | |
---|
[1512] | 203 | //reposition of edgetext |
---|
[1524] | 204 | xy<double> text_pos=edgesmap[ei]->getArrowPos(); |
---|
[1510] | 205 | text_pos+=(xy<double>(10,10)); |
---|
| 206 | edgetextmap[ei]->property_x().set_value(text_pos.x); |
---|
| 207 | edgetextmap[ei]->property_y().set_value(text_pos.y); |
---|
| 208 | } |
---|
| 209 | |
---|
[1651] | 210 | for(InEdgeIt ei(mapstorage.graph,active_node);ei!=INVALID;++ei) |
---|
[1510] | 211 | { |
---|
| 212 | Gnome::Canvas::Points coos; |
---|
| 213 | double x1, x2, y1, y2; |
---|
| 214 | |
---|
[1606] | 215 | nodesmap[mapstorage.graph.source(ei)]->get_bounds(x1, y1, x2, y2); |
---|
[1510] | 216 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
| 217 | |
---|
[1606] | 218 | nodesmap[mapstorage.graph.target(ei)]->get_bounds(x1, y1, x2, y2); |
---|
[1510] | 219 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
| 220 | |
---|
| 221 | if(isbutton==3) |
---|
| 222 | { |
---|
[1524] | 223 | edgesmap[ei]->setPoints(coos); |
---|
[1510] | 224 | } |
---|
| 225 | else |
---|
| 226 | { |
---|
[1524] | 227 | edgesmap[ei]->setPoints(coos,true); |
---|
[1510] | 228 | } |
---|
| 229 | |
---|
[1524] | 230 | xy<double> text_pos=edgesmap[ei]->getArrowPos(); |
---|
[1510] | 231 | text_pos+=(xy<double>(10,10)); |
---|
| 232 | edgetextmap[ei]->property_x().set_value(text_pos.x); |
---|
| 233 | edgetextmap[ei]->property_y().set_value(text_pos.y); |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | default: break; |
---|
| 237 | } |
---|
| 238 | |
---|
[1525] | 239 | return false; |
---|
[1510] | 240 | } |
---|
| 241 | |
---|
[1524] | 242 | bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e) |
---|
[1510] | 243 | { |
---|
| 244 | switch(e->type) |
---|
[1645] | 245 | { |
---|
| 246 | //move the new node |
---|
| 247 | case GDK_MOTION_NOTIFY: |
---|
| 248 | { |
---|
| 249 | GdkEvent * generated=new GdkEvent(); |
---|
| 250 | generated->motion.x=e->motion.x; |
---|
| 251 | generated->motion.y=e->motion.y; |
---|
| 252 | generated->type=GDK_MOTION_NOTIFY; |
---|
| 253 | moveEventHandler(generated); |
---|
| 254 | break; |
---|
| 255 | } |
---|
[1510] | 256 | |
---|
[1645] | 257 | case GDK_BUTTON_RELEASE: |
---|
[1606] | 258 | mapstorage.modified = true; |
---|
| 259 | |
---|
[1510] | 260 | isbutton=1; |
---|
| 261 | |
---|
[1643] | 262 | active_node=mapstorage.graph.addNode(); |
---|
[1510] | 263 | |
---|
| 264 | //initiating values corresponding to new node in maps |
---|
| 265 | |
---|
| 266 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 267 | |
---|
[1645] | 268 | // update coordinates |
---|
[1606] | 269 | mapstorage.coords.set(active_node, xy<double>(clicked_x, clicked_y)); |
---|
[1645] | 270 | |
---|
| 271 | // update all other maps |
---|
| 272 | for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
---|
| 273 | mapstorage.nodemap_storage.begin(); it != |
---|
| 274 | mapstorage.nodemap_storage.end(); ++it) |
---|
| 275 | { |
---|
[1646] | 276 | if ((it->first != "coordinates_x") && |
---|
| 277 | (it->first != "coordinates_y")) |
---|
[1645] | 278 | { |
---|
| 279 | (*(it->second))[active_node] = |
---|
| 280 | mapstorage.nodemap_default[it->first]; |
---|
| 281 | } |
---|
| 282 | } |
---|
[1646] | 283 | // increment the id map's default value |
---|
| 284 | mapstorage.nodemap_default["id"] += 1.0; |
---|
[1645] | 285 | |
---|
[1606] | 286 | nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, |
---|
[1645] | 287 | clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20); |
---|
[1510] | 288 | active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]); |
---|
[1645] | 289 | *(nodesmap[active_node]) << |
---|
| 290 | Gnome::Canvas::Properties::fill_color("blue"); |
---|
| 291 | *(nodesmap[active_node]) << |
---|
| 292 | Gnome::Canvas::Properties::outline_color("black"); |
---|
| 293 | active_item->raise_to_top(); |
---|
| 294 | |
---|
[1510] | 295 | (nodesmap[active_node])->show(); |
---|
[1512] | 296 | |
---|
[1606] | 297 | nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph, |
---|
[1645] | 298 | clicked_x+node_property_defaults[N_RADIUS]+5, |
---|
| 299 | clicked_y+node_property_defaults[N_RADIUS]+5, ""); |
---|
[1512] | 300 | nodetextmap[active_node]->property_fill_color().set_value("darkblue"); |
---|
[1645] | 301 | nodetextmap[active_node]->raise_to_top(); |
---|
[1512] | 302 | |
---|
[1606] | 303 | mapwin.updateNode(active_node); |
---|
[1512] | 304 | |
---|
[1510] | 305 | isbutton=0; |
---|
[1525] | 306 | target_item=NULL; |
---|
[1510] | 307 | active_item=NULL; |
---|
| 308 | active_node=INVALID; |
---|
[1645] | 309 | |
---|
[1510] | 310 | break; |
---|
| 311 | default: |
---|
| 312 | break; |
---|
[1645] | 313 | } |
---|
[1510] | 314 | return false; |
---|
| 315 | } |
---|
| 316 | |
---|
[1524] | 317 | bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e) |
---|
[1510] | 318 | { |
---|
| 319 | switch(e->type) |
---|
[1645] | 320 | { |
---|
[1510] | 321 | case GDK_BUTTON_PRESS: |
---|
| 322 | //in edge creation right button has special meaning |
---|
| 323 | if(e->button.button!=3) |
---|
[1645] | 324 | { |
---|
| 325 | //there is not yet selected node |
---|
| 326 | if(active_node==INVALID) |
---|
| 327 | { |
---|
| 328 | //we mark the location of the event to be able to calculate parameters of dragging |
---|
[1525] | 329 | |
---|
[1645] | 330 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
[1525] | 331 | |
---|
[1645] | 332 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
| 333 | active_node=INVALID; |
---|
| 334 | for (NodeIt i(mapstorage.graph); i!=INVALID; ++i) |
---|
| 335 | { |
---|
| 336 | if(nodesmap[i]==active_item) |
---|
| 337 | { |
---|
| 338 | active_node=i; |
---|
| 339 | } |
---|
| 340 | } |
---|
| 341 | //the clicked item is really a node |
---|
| 342 | if(active_node!=INVALID) |
---|
| 343 | { |
---|
| 344 | *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); |
---|
| 345 | isbutton=1; |
---|
| 346 | } |
---|
| 347 | //clicked item was not a node. It could be e.g. edge. |
---|
| 348 | else |
---|
| 349 | { |
---|
| 350 | active_item=NULL; |
---|
| 351 | } |
---|
| 352 | } |
---|
| 353 | //we only have to do sg. if the mouse button |
---|
| 354 | // is pressed already once AND the click was |
---|
| 355 | // on a node that was found in the set of |
---|
| 356 | //nodes, and now we only search for the second |
---|
| 357 | //node |
---|
| 358 | else |
---|
| 359 | { |
---|
| 360 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 361 | target_item=(get_item_at(clicked_x, clicked_y)); |
---|
| 362 | Node target_node=INVALID; |
---|
| 363 | for (NodeIt i(mapstorage.graph); i!=INVALID; ++i) |
---|
| 364 | { |
---|
| 365 | if(nodesmap[i]==target_item) |
---|
| 366 | { |
---|
| 367 | target_node=i; |
---|
| 368 | } |
---|
| 369 | } |
---|
| 370 | //the clicked item is a node, the edge can be drawn |
---|
| 371 | if(target_node!=INVALID) |
---|
| 372 | { |
---|
| 373 | if(target_node!=active_node) |
---|
| 374 | { |
---|
| 375 | mapstorage.modified = true; |
---|
[1606] | 376 | |
---|
[1645] | 377 | *(nodesmap[target_node]) << |
---|
| 378 | Gnome::Canvas::Properties::fill_color("red"); |
---|
[1510] | 379 | |
---|
[1645] | 380 | //creating new edge |
---|
| 381 | active_edge=mapstorage.graph.addEdge(active_node, |
---|
| 382 | target_node); |
---|
[1510] | 383 | |
---|
[1646] | 384 | // update maps |
---|
[1645] | 385 | for (std::map<std::string, |
---|
| 386 | Graph::EdgeMap<double>*>::const_iterator it = |
---|
| 387 | mapstorage.edgemap_storage.begin(); it != |
---|
| 388 | mapstorage.edgemap_storage.end(); ++it) |
---|
| 389 | { |
---|
[1646] | 390 | (*(it->second))[active_edge] = |
---|
| 391 | mapstorage.edgemap_default[it->first]; |
---|
[1645] | 392 | } |
---|
[1646] | 393 | // increment the id map's default value |
---|
| 394 | mapstorage.edgemap_default["id"] += 1.0; |
---|
[1510] | 395 | |
---|
[1645] | 396 | //calculating coordinates of new edge |
---|
| 397 | Gnome::Canvas::Points coos; |
---|
| 398 | double x1, x2, y1, y2; |
---|
[1510] | 399 | |
---|
[1645] | 400 | active_item->get_bounds(x1, y1, x2, y2); |
---|
| 401 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
[1510] | 402 | |
---|
[1645] | 403 | target_item->get_bounds(x1, y1, x2, y2); |
---|
| 404 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
[1510] | 405 | |
---|
[1645] | 406 | //drawing new edge |
---|
| 407 | edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, |
---|
| 408 | *this); |
---|
| 409 | *(edgesmap[active_edge]) << |
---|
| 410 | Gnome::Canvas::Properties::fill_color("green"); |
---|
| 411 | edgesmap[active_edge]->property_width_pixels().set_value(10); |
---|
[1510] | 412 | |
---|
[1645] | 413 | edgesmap[active_edge]->lower_to_bottom(); |
---|
[1512] | 414 | |
---|
[1645] | 415 | //initializing edge-text as well, to empty string |
---|
| 416 | xy<double> text_pos=edgesmap[active_edge]->getArrowPos(); |
---|
| 417 | text_pos+=(xy<double>(10,10)); |
---|
| 418 | |
---|
| 419 | edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, |
---|
| 420 | text_pos.x, text_pos.y, ""); |
---|
| 421 | edgetextmap[active_edge]->property_fill_color().set_value( |
---|
| 422 | "darkgreen"); |
---|
| 423 | edgetextmap[active_edge]->raise_to_top(); |
---|
| 424 | |
---|
| 425 | //updating its properties |
---|
| 426 | mapwin.updateEdge(active_edge); |
---|
| 427 | } |
---|
| 428 | else |
---|
| 429 | { |
---|
| 430 | target_node=INVALID; |
---|
| 431 | std::cerr << "Loop edge is not yet implemented!" << std::endl; |
---|
| 432 | } |
---|
| 433 | } |
---|
| 434 | //clicked item was not a node. it could be an e.g. edge. we do not |
---|
| 435 | //deal with it furthermore. |
---|
| 436 | else |
---|
| 437 | { |
---|
| 438 | target_item=NULL; |
---|
| 439 | } |
---|
| 440 | } |
---|
| 441 | } |
---|
[1510] | 442 | break; |
---|
| 443 | case GDK_BUTTON_RELEASE: |
---|
| 444 | isbutton=0; |
---|
| 445 | //we clear settings in two cases |
---|
| 446 | //1: the edge is ready (target_item has valid value) |
---|
| 447 | //2: the edge creation is cancelled with right button |
---|
| 448 | if((target_item)||(e->button.button==3)) |
---|
[1645] | 449 | { |
---|
| 450 | if(active_item) |
---|
| 451 | { |
---|
| 452 | *active_item << Gnome::Canvas::Properties::fill_color("blue"); |
---|
| 453 | active_item=NULL; |
---|
| 454 | } |
---|
| 455 | if(target_item) |
---|
| 456 | { |
---|
| 457 | *target_item << Gnome::Canvas::Properties::fill_color("blue"); |
---|
| 458 | target_item=NULL; |
---|
| 459 | } |
---|
| 460 | active_node=INVALID; |
---|
| 461 | active_edge=INVALID; |
---|
| 462 | } |
---|
[1510] | 463 | break; |
---|
| 464 | default: |
---|
| 465 | break; |
---|
[1645] | 466 | } |
---|
[1510] | 467 | return false; |
---|
| 468 | } |
---|
| 469 | |
---|
[1524] | 470 | bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e) |
---|
[1510] | 471 | { |
---|
| 472 | switch(e->type) |
---|
| 473 | { |
---|
| 474 | case GDK_BUTTON_PRESS: |
---|
[1594] | 475 | //finding the clicked items |
---|
[1525] | 476 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 477 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
[1510] | 478 | active_node=INVALID; |
---|
| 479 | active_edge=INVALID; |
---|
[1594] | 480 | //was it a node? |
---|
[1606] | 481 | for (NodeIt i(mapstorage.graph); i!=INVALID; ++i) |
---|
[1510] | 482 | { |
---|
| 483 | if(nodesmap[i]==active_item) |
---|
| 484 | { |
---|
| 485 | active_node=i; |
---|
| 486 | } |
---|
| 487 | } |
---|
[1594] | 488 | //or was it an edge? |
---|
[1510] | 489 | if(active_node==INVALID) |
---|
| 490 | { |
---|
[1606] | 491 | for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i) |
---|
[1510] | 492 | { |
---|
| 493 | if(edgesmap[i]==active_item) |
---|
| 494 | { |
---|
| 495 | active_edge=i; |
---|
| 496 | } |
---|
| 497 | } |
---|
| 498 | } |
---|
[1594] | 499 | |
---|
| 500 | //recolor activated item |
---|
[1525] | 501 | if(active_item) |
---|
| 502 | { |
---|
| 503 | *active_item << Gnome::Canvas::Properties::fill_color("red"); |
---|
| 504 | } |
---|
[1510] | 505 | break; |
---|
| 506 | |
---|
| 507 | case GDK_BUTTON_RELEASE: |
---|
[1525] | 508 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 509 | if(active_item) |
---|
[1510] | 510 | { |
---|
[1594] | 511 | //the cursor was not moved since pressing it |
---|
[1525] | 512 | if( active_item == ( get_item_at (clicked_x, clicked_y) ) ) |
---|
[1510] | 513 | { |
---|
[1594] | 514 | //a node was found |
---|
[1525] | 515 | if(active_node!=INVALID) |
---|
| 516 | { |
---|
[1606] | 517 | mapstorage.modified = true; |
---|
[1510] | 518 | |
---|
[1525] | 519 | std::set<Graph::Edge> edges_to_delete; |
---|
[1510] | 520 | |
---|
[1651] | 521 | for(OutEdgeIt e(mapstorage.graph,active_node);e!=INVALID;++e) |
---|
[1525] | 522 | { |
---|
| 523 | edges_to_delete.insert(e); |
---|
| 524 | } |
---|
[1651] | 525 | |
---|
| 526 | for(InEdgeIt e(mapstorage.graph,active_node);e!=INVALID;++e) |
---|
[1525] | 527 | { |
---|
| 528 | edges_to_delete.insert(e); |
---|
| 529 | } |
---|
[1651] | 530 | |
---|
[1525] | 531 | //deleting collected edges |
---|
[1651] | 532 | for(std::set<Graph::Edge>::iterator |
---|
| 533 | edge_set_it=edges_to_delete.begin(); |
---|
| 534 | edge_set_it!=edges_to_delete.end(); |
---|
| 535 | ++edge_set_it) |
---|
[1525] | 536 | { |
---|
| 537 | deleteItem(*edge_set_it); |
---|
| 538 | } |
---|
| 539 | deleteItem(active_node); |
---|
| 540 | } |
---|
| 541 | //a simple edge was chosen |
---|
| 542 | else |
---|
[1510] | 543 | { |
---|
[1525] | 544 | deleteItem(active_edge); |
---|
[1510] | 545 | } |
---|
| 546 | } |
---|
[1525] | 547 | //pointer was moved, deletion is cancelled |
---|
[1510] | 548 | else |
---|
| 549 | { |
---|
[1525] | 550 | if(active_node!=INVALID) |
---|
| 551 | { |
---|
| 552 | *active_item << Gnome::Canvas::Properties::fill_color("blue"); |
---|
| 553 | } |
---|
| 554 | else |
---|
| 555 | { |
---|
| 556 | *active_item << Gnome::Canvas::Properties::fill_color("green"); |
---|
| 557 | } |
---|
[1510] | 558 | } |
---|
| 559 | } |
---|
| 560 | //reseting datas |
---|
| 561 | active_item=NULL; |
---|
| 562 | active_edge=INVALID; |
---|
| 563 | active_node=INVALID; |
---|
| 564 | break; |
---|
| 565 | |
---|
| 566 | case GDK_MOTION_NOTIFY: |
---|
| 567 | break; |
---|
| 568 | |
---|
| 569 | default: |
---|
| 570 | break; |
---|
| 571 | } |
---|
[1525] | 572 | return false; |
---|
[1510] | 573 | } |
---|
| 574 | |
---|
[1550] | 575 | bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e) |
---|
| 576 | { |
---|
[1599] | 577 | if(actual_tool==EDGE_MAP_EDIT) |
---|
[1648] | 578 | { |
---|
| 579 | switch(e->type) |
---|
[1550] | 580 | { |
---|
[1648] | 581 | case GDK_BUTTON_PRESS: |
---|
| 582 | { |
---|
| 583 | //for determine, whether it was an edge |
---|
| 584 | Edge clicked_edge=INVALID; |
---|
[1594] | 585 | |
---|
[1648] | 586 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 587 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
[1599] | 588 | |
---|
[1648] | 589 | //find the activated item between texts |
---|
| 590 | for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i) |
---|
| 591 | { |
---|
| 592 | //at the same time only one can be active |
---|
| 593 | if(edgetextmap[i]==active_item) |
---|
| 594 | { |
---|
| 595 | clicked_edge=i; |
---|
| 596 | } |
---|
| 597 | } |
---|
[1647] | 598 | |
---|
[1648] | 599 | //if it was not between texts, search for it between edges |
---|
| 600 | if(clicked_edge==INVALID) |
---|
| 601 | { |
---|
| 602 | for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i) |
---|
| 603 | { |
---|
| 604 | //at the same time only one can be active |
---|
| 605 | if((edgesmap[i]==active_item)||(edgetextmap[i]==active_item)) |
---|
| 606 | { |
---|
| 607 | clicked_edge=i; |
---|
| 608 | } |
---|
| 609 | } |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | //if it was really an edge... |
---|
| 613 | if(clicked_edge!=INVALID) |
---|
| 614 | { |
---|
| 615 | // the id map is not editable |
---|
| 616 | if (edgemap_to_edit == "id") return 0; |
---|
[1599] | 617 | |
---|
[1648] | 618 | //and there is activated map |
---|
| 619 | if(edgetextmap[clicked_edge]->property_text().get_value()!="") |
---|
| 620 | { |
---|
| 621 | //activate the general variable for it |
---|
| 622 | active_edge=clicked_edge; |
---|
[1599] | 623 | |
---|
[1648] | 624 | //create a dialog |
---|
| 625 | Gtk::Dialog dialog("Edit value", *parentwin, true); |
---|
| 626 | dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); |
---|
| 627 | dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); |
---|
| 628 | Gtk::VBox* vbox = dialog.get_vbox(); |
---|
| 629 | Gtk::Adjustment adj( |
---|
| 630 | (*mapstorage.edgemap_storage[edgemap_to_edit])[active_edge], |
---|
| 631 | -1000000.0, |
---|
| 632 | 1000000.0, |
---|
| 633 | 1.0, 5.0, 0.0); |
---|
| 634 | //TODO: find out why doesn't it work with |
---|
| 635 | //numeric_limits<double>::min/max |
---|
| 636 | Gtk::SpinButton spin(adj); |
---|
| 637 | spin.set_numeric(true); |
---|
| 638 | spin.set_digits(4); |
---|
| 639 | vbox->add(spin); |
---|
| 640 | spin.show(); |
---|
| 641 | switch (dialog.run()) |
---|
| 642 | { |
---|
| 643 | case Gtk::RESPONSE_NONE: |
---|
| 644 | case Gtk::RESPONSE_CANCEL: |
---|
| 645 | break; |
---|
| 646 | case Gtk::RESPONSE_ACCEPT: |
---|
| 647 | double new_value = spin.get_value(); |
---|
| 648 | (*mapstorage.edgemap_storage[edgemap_to_edit])[active_edge] = |
---|
| 649 | new_value; |
---|
| 650 | std::ostringstream ostr; |
---|
| 651 | ostr << new_value; |
---|
| 652 | edgetextmap[active_edge]->property_text().set_value( |
---|
| 653 | ostr.str()); |
---|
| 654 | //mapwin.updateEdge(active_edge); |
---|
| 655 | mapwin.updateEdge(Edge(INVALID)); |
---|
| 656 | } |
---|
| 657 | } |
---|
| 658 | } |
---|
| 659 | break; |
---|
| 660 | } |
---|
| 661 | default: |
---|
| 662 | break; |
---|
[1550] | 663 | } |
---|
[1648] | 664 | } |
---|
[1550] | 665 | return false; |
---|
| 666 | } |
---|
| 667 | |
---|
| 668 | bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e) |
---|
| 669 | { |
---|
[1599] | 670 | if(actual_tool==NODE_MAP_EDIT) |
---|
[1648] | 671 | { |
---|
| 672 | switch(e->type) |
---|
[1579] | 673 | { |
---|
[1648] | 674 | case GDK_BUTTON_PRESS: |
---|
| 675 | { |
---|
| 676 | //for determine, whether it was a node |
---|
| 677 | Node clicked_node=INVALID; |
---|
[1594] | 678 | |
---|
[1648] | 679 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 680 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
[1594] | 681 | |
---|
[1648] | 682 | //find the activated item between texts |
---|
| 683 | for (NodeIt i(mapstorage.graph); i!=INVALID; ++i) |
---|
| 684 | { |
---|
| 685 | //at the same time only one can be active |
---|
| 686 | if(nodetextmap[i]==active_item) |
---|
| 687 | { |
---|
| 688 | clicked_node=i; |
---|
| 689 | } |
---|
| 690 | } |
---|
[1599] | 691 | |
---|
[1648] | 692 | //if there was not, search for it between nodes |
---|
| 693 | if(clicked_node==INVALID) |
---|
| 694 | { |
---|
| 695 | for (NodeIt i(mapstorage.graph); i!=INVALID; ++i) |
---|
| 696 | { |
---|
| 697 | //at the same time only one can be active |
---|
| 698 | if(nodesmap[i]==active_item) |
---|
| 699 | { |
---|
| 700 | clicked_node=i; |
---|
| 701 | } |
---|
| 702 | } |
---|
| 703 | } |
---|
[1599] | 704 | |
---|
[1648] | 705 | //if it was really a node... |
---|
| 706 | if(clicked_node!=INVALID) |
---|
| 707 | { |
---|
| 708 | // the id map is not editable |
---|
| 709 | if (nodemap_to_edit == "id") return 0; |
---|
[1599] | 710 | |
---|
[1648] | 711 | //and there is activated map |
---|
| 712 | if(nodetextmap[clicked_node]->property_text().get_value()!="") |
---|
| 713 | { |
---|
| 714 | //activate the general variable for it |
---|
| 715 | active_node=clicked_node; |
---|
[1599] | 716 | |
---|
[1648] | 717 | //create a dialog |
---|
| 718 | Gtk::Dialog dialog("Edit value", *parentwin, true); |
---|
| 719 | dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); |
---|
| 720 | dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); |
---|
| 721 | Gtk::VBox* vbox = dialog.get_vbox(); |
---|
| 722 | Gtk::Adjustment adj( |
---|
| 723 | (*mapstorage.nodemap_storage[nodemap_to_edit])[active_node], |
---|
| 724 | -1000000.0, |
---|
| 725 | 1000000.0, |
---|
| 726 | 1.0, 5.0, 0.0); |
---|
| 727 | //TODO: find out why doesn't it work with |
---|
| 728 | //numeric_limits<double>::min/max |
---|
| 729 | Gtk::SpinButton spin(adj); |
---|
| 730 | spin.set_numeric(true); |
---|
| 731 | spin.set_digits(4); |
---|
| 732 | vbox->add(spin); |
---|
| 733 | spin.show(); |
---|
| 734 | switch (dialog.run()) |
---|
| 735 | { |
---|
| 736 | case Gtk::RESPONSE_NONE: |
---|
| 737 | case Gtk::RESPONSE_CANCEL: |
---|
| 738 | break; |
---|
| 739 | case Gtk::RESPONSE_ACCEPT: |
---|
| 740 | double new_value = spin.get_value(); |
---|
| 741 | (*mapstorage.nodemap_storage[nodemap_to_edit])[active_node] = |
---|
| 742 | new_value; |
---|
| 743 | std::ostringstream ostr; |
---|
| 744 | ostr << new_value; |
---|
| 745 | nodetextmap[active_node]->property_text().set_value( |
---|
| 746 | ostr.str()); |
---|
| 747 | //mapwin.updateNode(active_node); |
---|
| 748 | mapwin.updateNode(Node(INVALID)); |
---|
| 749 | } |
---|
| 750 | } |
---|
| 751 | } |
---|
| 752 | break; |
---|
| 753 | } |
---|
| 754 | default: |
---|
| 755 | break; |
---|
[1579] | 756 | } |
---|
[1648] | 757 | } |
---|
[1579] | 758 | return false; |
---|
[1550] | 759 | } |
---|
| 760 | |
---|
[1643] | 761 | void GraphDisplayerCanvas::deleteItem(Node node_to_delete) |
---|
[1510] | 762 | { |
---|
[1512] | 763 | delete(nodetextmap[node_to_delete]); |
---|
[1510] | 764 | delete(nodesmap[node_to_delete]); |
---|
[1606] | 765 | mapstorage.graph.erase(node_to_delete); |
---|
[1510] | 766 | } |
---|
| 767 | |
---|
[1643] | 768 | void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete) |
---|
[1510] | 769 | { |
---|
[1512] | 770 | delete(edgetextmap[edge_to_delete]); |
---|
[1510] | 771 | delete(edgesmap[edge_to_delete]); |
---|
[1606] | 772 | mapstorage.graph.erase(edge_to_delete); |
---|
[1510] | 773 | } |
---|
| 774 | |
---|
[1524] | 775 | void GraphDisplayerCanvas::textReposition(xy<double> new_place) |
---|
[1510] | 776 | { |
---|
| 777 | new_place+=(xy<double>(10,10)); |
---|
[1579] | 778 | edgetextmap[forming_edge]->property_x().set_value(new_place.x); |
---|
| 779 | edgetextmap[forming_edge]->property_y().set_value(new_place.y); |
---|
[1510] | 780 | } |
---|
| 781 | |
---|
[1524] | 782 | void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on) |
---|
[1510] | 783 | { |
---|
| 784 | if(on) |
---|
| 785 | { |
---|
[1579] | 786 | if(forming_edge!=INVALID) |
---|
[1510] | 787 | { |
---|
[1599] | 788 | std::cerr << "ERROR!!!! Valid edge found!" << std::endl; |
---|
[1510] | 789 | } |
---|
| 790 | else |
---|
| 791 | { |
---|
[1606] | 792 | for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i) |
---|
[1510] | 793 | { |
---|
| 794 | if(edgesmap[i]==active_bre) |
---|
| 795 | { |
---|
[1579] | 796 | forming_edge=i; |
---|
[1510] | 797 | } |
---|
| 798 | } |
---|
| 799 | } |
---|
| 800 | } |
---|
| 801 | else |
---|
| 802 | { |
---|
[1579] | 803 | if(forming_edge!=INVALID) |
---|
[1510] | 804 | { |
---|
[1579] | 805 | forming_edge=INVALID; |
---|
[1510] | 806 | } |
---|
| 807 | else |
---|
| 808 | { |
---|
[1599] | 809 | std::cerr << "ERROR!!!! Invalid edge found!" << std::endl; |
---|
[1510] | 810 | } |
---|
| 811 | } |
---|
| 812 | |
---|
| 813 | } |
---|
[1550] | 814 | |
---|
[1597] | 815 | int GraphDisplayerCanvas::addNewEdgeMap(double default_value, std::string mapname) |
---|
[1585] | 816 | { |
---|
[1594] | 817 | //create the new map |
---|
[1606] | 818 | Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (mapstorage.graph, default_value); |
---|
[1597] | 819 | |
---|
| 820 | //if addition was not successful addEdgeMap returns one. |
---|
| 821 | //cause can be that there is already a map named like the new one |
---|
[1645] | 822 | if(mapstorage.addEdgeMap(mapname,emptr, default_value)) |
---|
[1597] | 823 | { |
---|
| 824 | return 1; |
---|
| 825 | } |
---|
| 826 | |
---|
[1594] | 827 | |
---|
| 828 | //add it to the list of the displayable maps |
---|
[1606] | 829 | mapwin.registerNewEdgeMap(mapname); |
---|
[1594] | 830 | |
---|
| 831 | //display it |
---|
[1592] | 832 | changeEdgeText(mapname); |
---|
[1597] | 833 | |
---|
| 834 | return 0; |
---|
[1585] | 835 | } |
---|
| 836 | |
---|
[1597] | 837 | int GraphDisplayerCanvas::addNewNodeMap(double default_value, std::string mapname) |
---|
[1585] | 838 | { |
---|
[1594] | 839 | //create the new map |
---|
[1606] | 840 | Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (mapstorage.graph,default_value); |
---|
[1597] | 841 | |
---|
| 842 | //if addition was not successful addNodeMap returns one. |
---|
| 843 | //cause can be that there is already a map named like the new one |
---|
[1645] | 844 | if(mapstorage.addNodeMap(mapname,emptr, default_value)) |
---|
[1597] | 845 | { |
---|
| 846 | return 1; |
---|
| 847 | } |
---|
[1594] | 848 | |
---|
| 849 | //add it to the list of the displayable maps |
---|
[1606] | 850 | mapwin.registerNewNodeMap(mapname); |
---|
[1594] | 851 | |
---|
| 852 | //display it |
---|
[1592] | 853 | changeNodeText(mapname); |
---|
[1597] | 854 | |
---|
| 855 | return 0; |
---|
[1585] | 856 | } |
---|
| 857 | |
---|