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