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