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