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