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