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