[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; |
---|
[1837] | 97 | for (NodeIt i((mainwin.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 | { |
---|
[1837] | 128 | (mainwin.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 |
---|
[1837] | 147 | double coord_x = new_x - (clicked_x - (mainwin.mapstorage).coords[active_node].x); |
---|
| 148 | double coord_y = new_y - (clicked_y - (mainwin.mapstorage).coords[active_node].y); |
---|
[1652] | 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 |
---|
[1837] | 154 | (mainwin.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 << "(" << |
---|
[1837] | 159 | (mainwin.mapstorage).coords[active_node].x << ", " << |
---|
| 160 | (mainwin.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()); |
---|
[1837] | 167 | coord_text->property_x().set_value((mainwin.mapstorage).coords[active_node].x + |
---|
[1654] | 168 | radius); |
---|
[1837] | 169 | coord_text->property_y().set_value((mainwin.mapstorage).coords[active_node].y - |
---|
[1654] | 170 | radius); |
---|
[1652] | 171 | } |
---|
| 172 | else |
---|
| 173 | { |
---|
| 174 | coord_text = new Gnome::Canvas::Text( |
---|
| 175 | displayed_graph, |
---|
[1837] | 176 | (mainwin.mapstorage).coords[active_node].x + radius, |
---|
| 177 | (mainwin.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 |
---|
[1837] | 184 | for(OutEdgeIt ei((mainwin.mapstorage).graph,active_node);ei!=INVALID;++ei) |
---|
[1510] | 185 | { |
---|
| 186 | Gnome::Canvas::Points coos; |
---|
| 187 | double x1, x2, y1, y2; |
---|
| 188 | |
---|
[1837] | 189 | nodesmap[(mainwin.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 | |
---|
[1837] | 192 | nodesmap[(mainwin.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 | |
---|
[1837] | 211 | for(InEdgeIt ei((mainwin.mapstorage).graph,active_node);ei!=INVALID;++ei) |
---|
[1510] | 212 | { |
---|
| 213 | Gnome::Canvas::Points coos; |
---|
| 214 | double x1, x2, y1, y2; |
---|
| 215 | |
---|
[1837] | 216 | nodesmap[(mainwin.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 | |
---|
[1837] | 219 | nodesmap[(mainwin.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: |
---|
[1837] | 259 | (mainwin.mapstorage).modified = true; |
---|
[1606] | 260 | |
---|
[1510] | 261 | isbutton=1; |
---|
| 262 | |
---|
[1837] | 263 | active_node=(mainwin.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 |
---|
[1837] | 270 | (mainwin.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 = |
---|
[1837] | 274 | (mainwin.mapstorage).nodemap_storage.begin(); it != |
---|
| 275 | (mainwin.mapstorage).nodemap_storage.end(); ++it) |
---|
[1645] | 276 | { |
---|
[1646] | 277 | if ((it->first != "coordinates_x") && |
---|
| 278 | (it->first != "coordinates_y")) |
---|
[1645] | 279 | { |
---|
| 280 | (*(it->second))[active_node] = |
---|
[1837] | 281 | (mainwin.mapstorage).nodemap_default[it->first]; |
---|
[1645] | 282 | } |
---|
| 283 | } |
---|
[1646] | 284 | // increment the id map's default value |
---|
[1837] | 285 | (mainwin.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 | |
---|
[1837] | 304 | // mapwin.updateNode(active_node); |
---|
| 305 | propertyUpdate(active_node); |
---|
[1512] | 306 | |
---|
[1510] | 307 | isbutton=0; |
---|
[1525] | 308 | target_item=NULL; |
---|
[1510] | 309 | active_item=NULL; |
---|
| 310 | active_node=INVALID; |
---|
| 311 | break; |
---|
| 312 | default: |
---|
| 313 | break; |
---|
[1645] | 314 | } |
---|
[1510] | 315 | return false; |
---|
| 316 | } |
---|
| 317 | |
---|
[1524] | 318 | bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e) |
---|
[1510] | 319 | { |
---|
| 320 | switch(e->type) |
---|
[1645] | 321 | { |
---|
[1510] | 322 | case GDK_BUTTON_PRESS: |
---|
| 323 | //in edge creation right button has special meaning |
---|
| 324 | if(e->button.button!=3) |
---|
[1645] | 325 | { |
---|
| 326 | //there is not yet selected node |
---|
| 327 | if(active_node==INVALID) |
---|
| 328 | { |
---|
| 329 | //we mark the location of the event to be able to calculate parameters of dragging |
---|
[1525] | 330 | |
---|
[1645] | 331 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
[1525] | 332 | |
---|
[1645] | 333 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
| 334 | active_node=INVALID; |
---|
[1837] | 335 | for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i) |
---|
[1645] | 336 | { |
---|
| 337 | if(nodesmap[i]==active_item) |
---|
| 338 | { |
---|
| 339 | active_node=i; |
---|
| 340 | } |
---|
| 341 | } |
---|
| 342 | //the clicked item is really a node |
---|
| 343 | if(active_node!=INVALID) |
---|
| 344 | { |
---|
| 345 | *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); |
---|
| 346 | isbutton=1; |
---|
| 347 | } |
---|
| 348 | //clicked item was not a node. It could be e.g. edge. |
---|
| 349 | else |
---|
| 350 | { |
---|
| 351 | active_item=NULL; |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | //we only have to do sg. if the mouse button |
---|
| 355 | // is pressed already once AND the click was |
---|
| 356 | // on a node that was found in the set of |
---|
| 357 | //nodes, and now we only search for the second |
---|
| 358 | //node |
---|
| 359 | else |
---|
| 360 | { |
---|
| 361 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 362 | target_item=(get_item_at(clicked_x, clicked_y)); |
---|
| 363 | Node target_node=INVALID; |
---|
[1837] | 364 | for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i) |
---|
[1645] | 365 | { |
---|
| 366 | if(nodesmap[i]==target_item) |
---|
| 367 | { |
---|
| 368 | target_node=i; |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | //the clicked item is a node, the edge can be drawn |
---|
| 372 | if(target_node!=INVALID) |
---|
| 373 | { |
---|
| 374 | if(target_node!=active_node) |
---|
| 375 | { |
---|
[1837] | 376 | (mainwin.mapstorage).modified = true; |
---|
[1606] | 377 | |
---|
[1645] | 378 | *(nodesmap[target_node]) << |
---|
| 379 | Gnome::Canvas::Properties::fill_color("red"); |
---|
[1510] | 380 | |
---|
[1645] | 381 | //creating new edge |
---|
[1837] | 382 | active_edge=(mainwin.mapstorage).graph.addEdge(active_node, |
---|
[1645] | 383 | target_node); |
---|
[1510] | 384 | |
---|
[1646] | 385 | // update maps |
---|
[1645] | 386 | for (std::map<std::string, |
---|
| 387 | Graph::EdgeMap<double>*>::const_iterator it = |
---|
[1837] | 388 | (mainwin.mapstorage).edgemap_storage.begin(); it != |
---|
| 389 | (mainwin.mapstorage).edgemap_storage.end(); ++it) |
---|
[1645] | 390 | { |
---|
[1646] | 391 | (*(it->second))[active_edge] = |
---|
[1837] | 392 | (mainwin.mapstorage).edgemap_default[it->first]; |
---|
[1645] | 393 | } |
---|
[1646] | 394 | // increment the id map's default value |
---|
[1837] | 395 | (mainwin.mapstorage).edgemap_default["id"] += 1.0; |
---|
[1510] | 396 | |
---|
[1645] | 397 | //calculating coordinates of new edge |
---|
| 398 | Gnome::Canvas::Points coos; |
---|
| 399 | double x1, x2, y1, y2; |
---|
[1510] | 400 | |
---|
[1645] | 401 | active_item->get_bounds(x1, y1, x2, y2); |
---|
| 402 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
[1510] | 403 | |
---|
[1645] | 404 | target_item->get_bounds(x1, y1, x2, y2); |
---|
| 405 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
[1510] | 406 | |
---|
[1645] | 407 | //drawing new edge |
---|
| 408 | edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, |
---|
| 409 | *this); |
---|
| 410 | *(edgesmap[active_edge]) << |
---|
| 411 | Gnome::Canvas::Properties::fill_color("green"); |
---|
| 412 | edgesmap[active_edge]->property_width_pixels().set_value(10); |
---|
[1510] | 413 | |
---|
[1645] | 414 | edgesmap[active_edge]->lower_to_bottom(); |
---|
[1512] | 415 | |
---|
[1645] | 416 | //initializing edge-text as well, to empty string |
---|
| 417 | xy<double> text_pos=edgesmap[active_edge]->getArrowPos(); |
---|
| 418 | text_pos+=(xy<double>(10,10)); |
---|
| 419 | |
---|
| 420 | edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, |
---|
| 421 | text_pos.x, text_pos.y, ""); |
---|
| 422 | edgetextmap[active_edge]->property_fill_color().set_value( |
---|
| 423 | "darkgreen"); |
---|
| 424 | edgetextmap[active_edge]->raise_to_top(); |
---|
| 425 | |
---|
| 426 | //updating its properties |
---|
[1837] | 427 | // mapwin.updateEdge(active_edge); |
---|
| 428 | propertyUpdate(active_edge); |
---|
[1645] | 429 | } |
---|
| 430 | else |
---|
| 431 | { |
---|
| 432 | target_node=INVALID; |
---|
| 433 | std::cerr << "Loop edge is not yet implemented!" << std::endl; |
---|
| 434 | } |
---|
| 435 | } |
---|
| 436 | //clicked item was not a node. it could be an e.g. edge. we do not |
---|
| 437 | //deal with it furthermore. |
---|
| 438 | else |
---|
| 439 | { |
---|
| 440 | target_item=NULL; |
---|
| 441 | } |
---|
| 442 | } |
---|
| 443 | } |
---|
[1510] | 444 | break; |
---|
| 445 | case GDK_BUTTON_RELEASE: |
---|
| 446 | isbutton=0; |
---|
| 447 | //we clear settings in two cases |
---|
| 448 | //1: the edge is ready (target_item has valid value) |
---|
| 449 | //2: the edge creation is cancelled with right button |
---|
| 450 | if((target_item)||(e->button.button==3)) |
---|
[1645] | 451 | { |
---|
| 452 | if(active_item) |
---|
| 453 | { |
---|
| 454 | *active_item << Gnome::Canvas::Properties::fill_color("blue"); |
---|
| 455 | active_item=NULL; |
---|
| 456 | } |
---|
| 457 | if(target_item) |
---|
| 458 | { |
---|
| 459 | *target_item << Gnome::Canvas::Properties::fill_color("blue"); |
---|
| 460 | target_item=NULL; |
---|
| 461 | } |
---|
| 462 | active_node=INVALID; |
---|
| 463 | active_edge=INVALID; |
---|
| 464 | } |
---|
[1510] | 465 | break; |
---|
| 466 | default: |
---|
| 467 | break; |
---|
[1645] | 468 | } |
---|
[1510] | 469 | return false; |
---|
| 470 | } |
---|
| 471 | |
---|
[1524] | 472 | bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e) |
---|
[1510] | 473 | { |
---|
| 474 | switch(e->type) |
---|
| 475 | { |
---|
| 476 | case GDK_BUTTON_PRESS: |
---|
[1594] | 477 | //finding the clicked items |
---|
[1525] | 478 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 479 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
[1510] | 480 | active_node=INVALID; |
---|
| 481 | active_edge=INVALID; |
---|
[1594] | 482 | //was it a node? |
---|
[1837] | 483 | for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i) |
---|
[1510] | 484 | { |
---|
| 485 | if(nodesmap[i]==active_item) |
---|
| 486 | { |
---|
| 487 | active_node=i; |
---|
| 488 | } |
---|
| 489 | } |
---|
[1594] | 490 | //or was it an edge? |
---|
[1510] | 491 | if(active_node==INVALID) |
---|
| 492 | { |
---|
[1837] | 493 | for (EdgeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i) |
---|
[1510] | 494 | { |
---|
| 495 | if(edgesmap[i]==active_item) |
---|
| 496 | { |
---|
| 497 | active_edge=i; |
---|
| 498 | } |
---|
| 499 | } |
---|
| 500 | } |
---|
[1594] | 501 | |
---|
| 502 | //recolor activated item |
---|
[1525] | 503 | if(active_item) |
---|
| 504 | { |
---|
| 505 | *active_item << Gnome::Canvas::Properties::fill_color("red"); |
---|
| 506 | } |
---|
[1510] | 507 | break; |
---|
| 508 | |
---|
| 509 | case GDK_BUTTON_RELEASE: |
---|
[1525] | 510 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 511 | if(active_item) |
---|
[1510] | 512 | { |
---|
[1594] | 513 | //the cursor was not moved since pressing it |
---|
[1525] | 514 | if( active_item == ( get_item_at (clicked_x, clicked_y) ) ) |
---|
[1510] | 515 | { |
---|
[1594] | 516 | //a node was found |
---|
[1525] | 517 | if(active_node!=INVALID) |
---|
| 518 | { |
---|
[1837] | 519 | (mainwin.mapstorage).modified = true; |
---|
[1510] | 520 | |
---|
[1525] | 521 | std::set<Graph::Edge> edges_to_delete; |
---|
[1510] | 522 | |
---|
[1837] | 523 | for(OutEdgeIt e((mainwin.mapstorage).graph,active_node);e!=INVALID;++e) |
---|
[1525] | 524 | { |
---|
| 525 | edges_to_delete.insert(e); |
---|
| 526 | } |
---|
[1651] | 527 | |
---|
[1837] | 528 | for(InEdgeIt e((mainwin.mapstorage).graph,active_node);e!=INVALID;++e) |
---|
[1525] | 529 | { |
---|
| 530 | edges_to_delete.insert(e); |
---|
| 531 | } |
---|
[1651] | 532 | |
---|
[1525] | 533 | //deleting collected edges |
---|
[1651] | 534 | for(std::set<Graph::Edge>::iterator |
---|
| 535 | edge_set_it=edges_to_delete.begin(); |
---|
| 536 | edge_set_it!=edges_to_delete.end(); |
---|
| 537 | ++edge_set_it) |
---|
[1525] | 538 | { |
---|
| 539 | deleteItem(*edge_set_it); |
---|
| 540 | } |
---|
| 541 | deleteItem(active_node); |
---|
| 542 | } |
---|
| 543 | //a simple edge was chosen |
---|
| 544 | else |
---|
[1510] | 545 | { |
---|
[1525] | 546 | deleteItem(active_edge); |
---|
[1510] | 547 | } |
---|
| 548 | } |
---|
[1525] | 549 | //pointer was moved, deletion is cancelled |
---|
[1510] | 550 | else |
---|
| 551 | { |
---|
[1525] | 552 | if(active_node!=INVALID) |
---|
| 553 | { |
---|
| 554 | *active_item << Gnome::Canvas::Properties::fill_color("blue"); |
---|
| 555 | } |
---|
| 556 | else |
---|
| 557 | { |
---|
| 558 | *active_item << Gnome::Canvas::Properties::fill_color("green"); |
---|
| 559 | } |
---|
[1510] | 560 | } |
---|
| 561 | } |
---|
| 562 | //reseting datas |
---|
| 563 | active_item=NULL; |
---|
| 564 | active_edge=INVALID; |
---|
| 565 | active_node=INVALID; |
---|
| 566 | break; |
---|
| 567 | |
---|
| 568 | case GDK_MOTION_NOTIFY: |
---|
| 569 | break; |
---|
| 570 | |
---|
| 571 | default: |
---|
| 572 | break; |
---|
| 573 | } |
---|
[1525] | 574 | return false; |
---|
[1510] | 575 | } |
---|
| 576 | |
---|
[1550] | 577 | bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e) |
---|
| 578 | { |
---|
[1599] | 579 | if(actual_tool==EDGE_MAP_EDIT) |
---|
[1648] | 580 | { |
---|
| 581 | switch(e->type) |
---|
[1550] | 582 | { |
---|
[1648] | 583 | case GDK_BUTTON_PRESS: |
---|
| 584 | { |
---|
| 585 | //for determine, whether it was an edge |
---|
| 586 | Edge clicked_edge=INVALID; |
---|
[1594] | 587 | |
---|
[1648] | 588 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 589 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
[1599] | 590 | |
---|
[1648] | 591 | //find the activated item between texts |
---|
[1837] | 592 | for (EdgeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i) |
---|
[1648] | 593 | { |
---|
| 594 | //at the same time only one can be active |
---|
| 595 | if(edgetextmap[i]==active_item) |
---|
| 596 | { |
---|
| 597 | clicked_edge=i; |
---|
| 598 | } |
---|
| 599 | } |
---|
[1647] | 600 | |
---|
[1648] | 601 | //if it was not between texts, search for it between edges |
---|
| 602 | if(clicked_edge==INVALID) |
---|
| 603 | { |
---|
[1837] | 604 | for (EdgeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i) |
---|
[1648] | 605 | { |
---|
| 606 | //at the same time only one can be active |
---|
| 607 | if((edgesmap[i]==active_item)||(edgetextmap[i]==active_item)) |
---|
| 608 | { |
---|
| 609 | clicked_edge=i; |
---|
| 610 | } |
---|
| 611 | } |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | //if it was really an edge... |
---|
| 615 | if(clicked_edge!=INVALID) |
---|
| 616 | { |
---|
| 617 | // the id map is not editable |
---|
| 618 | if (edgemap_to_edit == "id") return 0; |
---|
[1599] | 619 | |
---|
[1648] | 620 | //and there is activated map |
---|
| 621 | if(edgetextmap[clicked_edge]->property_text().get_value()!="") |
---|
| 622 | { |
---|
| 623 | //activate the general variable for it |
---|
| 624 | active_edge=clicked_edge; |
---|
[1599] | 625 | |
---|
[1648] | 626 | //create a dialog |
---|
[1837] | 627 | Gtk::Dialog dialog("Edit value", mainwin, true); |
---|
[1648] | 628 | dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); |
---|
| 629 | dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); |
---|
| 630 | Gtk::VBox* vbox = dialog.get_vbox(); |
---|
[1826] | 631 | Gtk::SpinButton spin(0.0, 4); |
---|
| 632 | spin.set_increments(1.0, 10.0); |
---|
| 633 | spin.set_range(-1000000.0, 1000000.0); |
---|
[1648] | 634 | spin.set_numeric(true); |
---|
| 635 | vbox->add(spin); |
---|
| 636 | spin.show(); |
---|
| 637 | switch (dialog.run()) |
---|
| 638 | { |
---|
| 639 | case Gtk::RESPONSE_NONE: |
---|
| 640 | case Gtk::RESPONSE_CANCEL: |
---|
| 641 | break; |
---|
| 642 | case Gtk::RESPONSE_ACCEPT: |
---|
| 643 | double new_value = spin.get_value(); |
---|
[1837] | 644 | (*(mainwin.mapstorage).edgemap_storage[edgemap_to_edit])[active_edge] = |
---|
[1648] | 645 | new_value; |
---|
| 646 | std::ostringstream ostr; |
---|
| 647 | ostr << new_value; |
---|
| 648 | edgetextmap[active_edge]->property_text().set_value( |
---|
| 649 | ostr.str()); |
---|
| 650 | //mapwin.updateEdge(active_edge); |
---|
[1837] | 651 | // mapwin.updateEdge(Edge(INVALID)); |
---|
| 652 | propertyUpdate(Edge(INVALID)); |
---|
[1648] | 653 | } |
---|
| 654 | } |
---|
| 655 | } |
---|
| 656 | break; |
---|
| 657 | } |
---|
| 658 | default: |
---|
| 659 | break; |
---|
[1550] | 660 | } |
---|
[1648] | 661 | } |
---|
[1550] | 662 | return false; |
---|
| 663 | } |
---|
| 664 | |
---|
| 665 | bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e) |
---|
| 666 | { |
---|
[1599] | 667 | if(actual_tool==NODE_MAP_EDIT) |
---|
[1648] | 668 | { |
---|
| 669 | switch(e->type) |
---|
[1579] | 670 | { |
---|
[1648] | 671 | case GDK_BUTTON_PRESS: |
---|
| 672 | { |
---|
| 673 | //for determine, whether it was a node |
---|
| 674 | Node clicked_node=INVALID; |
---|
[1594] | 675 | |
---|
[1648] | 676 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 677 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
[1594] | 678 | |
---|
[1648] | 679 | //find the activated item between texts |
---|
[1837] | 680 | for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i) |
---|
[1648] | 681 | { |
---|
| 682 | //at the same time only one can be active |
---|
| 683 | if(nodetextmap[i]==active_item) |
---|
| 684 | { |
---|
| 685 | clicked_node=i; |
---|
| 686 | } |
---|
| 687 | } |
---|
[1599] | 688 | |
---|
[1648] | 689 | //if there was not, search for it between nodes |
---|
| 690 | if(clicked_node==INVALID) |
---|
| 691 | { |
---|
[1837] | 692 | for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i) |
---|
[1648] | 693 | { |
---|
| 694 | //at the same time only one can be active |
---|
| 695 | if(nodesmap[i]==active_item) |
---|
| 696 | { |
---|
| 697 | clicked_node=i; |
---|
| 698 | } |
---|
| 699 | } |
---|
| 700 | } |
---|
[1599] | 701 | |
---|
[1648] | 702 | //if it was really a node... |
---|
| 703 | if(clicked_node!=INVALID) |
---|
| 704 | { |
---|
| 705 | // the id map is not editable |
---|
| 706 | if (nodemap_to_edit == "id") return 0; |
---|
[1599] | 707 | |
---|
[1648] | 708 | //and there is activated map |
---|
| 709 | if(nodetextmap[clicked_node]->property_text().get_value()!="") |
---|
| 710 | { |
---|
| 711 | //activate the general variable for it |
---|
| 712 | active_node=clicked_node; |
---|
[1599] | 713 | |
---|
[1648] | 714 | //create a dialog |
---|
[1837] | 715 | Gtk::Dialog dialog("Edit value", mainwin, true); |
---|
[1648] | 716 | dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); |
---|
| 717 | dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); |
---|
| 718 | Gtk::VBox* vbox = dialog.get_vbox(); |
---|
[1826] | 719 | Gtk::SpinButton spin(0.0, 4); |
---|
| 720 | spin.set_increments(1.0, 10.0); |
---|
| 721 | spin.set_range(-1000000.0, 1000000.0); |
---|
[1648] | 722 | spin.set_numeric(true); |
---|
| 723 | vbox->add(spin); |
---|
| 724 | spin.show(); |
---|
| 725 | switch (dialog.run()) |
---|
| 726 | { |
---|
| 727 | case Gtk::RESPONSE_NONE: |
---|
| 728 | case Gtk::RESPONSE_CANCEL: |
---|
| 729 | break; |
---|
| 730 | case Gtk::RESPONSE_ACCEPT: |
---|
| 731 | double new_value = spin.get_value(); |
---|
[1837] | 732 | (*(mainwin.mapstorage).nodemap_storage[nodemap_to_edit])[active_node] = |
---|
[1648] | 733 | new_value; |
---|
| 734 | std::ostringstream ostr; |
---|
| 735 | ostr << new_value; |
---|
| 736 | nodetextmap[active_node]->property_text().set_value( |
---|
| 737 | ostr.str()); |
---|
| 738 | //mapwin.updateNode(active_node); |
---|
[1837] | 739 | // mapwin.updateNode(Node(INVALID)); |
---|
| 740 | propertyUpdate(Node(INVALID)); |
---|
[1648] | 741 | } |
---|
| 742 | } |
---|
| 743 | } |
---|
| 744 | break; |
---|
| 745 | } |
---|
| 746 | default: |
---|
| 747 | break; |
---|
[1579] | 748 | } |
---|
[1648] | 749 | } |
---|
[1579] | 750 | return false; |
---|
[1550] | 751 | } |
---|
| 752 | |
---|
[1643] | 753 | void GraphDisplayerCanvas::deleteItem(Node node_to_delete) |
---|
[1510] | 754 | { |
---|
[1512] | 755 | delete(nodetextmap[node_to_delete]); |
---|
[1510] | 756 | delete(nodesmap[node_to_delete]); |
---|
[1837] | 757 | (mainwin.mapstorage).graph.erase(node_to_delete); |
---|
[1510] | 758 | } |
---|
| 759 | |
---|
[1643] | 760 | void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete) |
---|
[1510] | 761 | { |
---|
[1512] | 762 | delete(edgetextmap[edge_to_delete]); |
---|
[1510] | 763 | delete(edgesmap[edge_to_delete]); |
---|
[1837] | 764 | (mainwin.mapstorage).graph.erase(edge_to_delete); |
---|
[1510] | 765 | } |
---|
| 766 | |
---|
[1524] | 767 | void GraphDisplayerCanvas::textReposition(xy<double> new_place) |
---|
[1510] | 768 | { |
---|
| 769 | new_place+=(xy<double>(10,10)); |
---|
[1579] | 770 | edgetextmap[forming_edge]->property_x().set_value(new_place.x); |
---|
| 771 | edgetextmap[forming_edge]->property_y().set_value(new_place.y); |
---|
[1510] | 772 | } |
---|
| 773 | |
---|
[1524] | 774 | void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on) |
---|
[1510] | 775 | { |
---|
| 776 | if(on) |
---|
| 777 | { |
---|
[1579] | 778 | if(forming_edge!=INVALID) |
---|
[1510] | 779 | { |
---|
[1599] | 780 | std::cerr << "ERROR!!!! Valid edge found!" << std::endl; |
---|
[1510] | 781 | } |
---|
| 782 | else |
---|
| 783 | { |
---|
[1837] | 784 | for (EdgeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i) |
---|
[1510] | 785 | { |
---|
| 786 | if(edgesmap[i]==active_bre) |
---|
| 787 | { |
---|
[1579] | 788 | forming_edge=i; |
---|
[1510] | 789 | } |
---|
| 790 | } |
---|
| 791 | } |
---|
| 792 | } |
---|
| 793 | else |
---|
| 794 | { |
---|
[1579] | 795 | if(forming_edge!=INVALID) |
---|
[1510] | 796 | { |
---|
[1579] | 797 | forming_edge=INVALID; |
---|
[1510] | 798 | } |
---|
| 799 | else |
---|
| 800 | { |
---|
[1599] | 801 | std::cerr << "ERROR!!!! Invalid edge found!" << std::endl; |
---|
[1510] | 802 | } |
---|
| 803 | } |
---|
| 804 | |
---|
| 805 | } |
---|
[1550] | 806 | |
---|
[1597] | 807 | int GraphDisplayerCanvas::addNewEdgeMap(double default_value, std::string mapname) |
---|
[1585] | 808 | { |
---|
[1594] | 809 | //create the new map |
---|
[1837] | 810 | Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> ((mainwin.mapstorage).graph, default_value); |
---|
[1597] | 811 | |
---|
| 812 | //if addition was not successful addEdgeMap returns one. |
---|
| 813 | //cause can be that there is already a map named like the new one |
---|
[1837] | 814 | if((mainwin.mapstorage).addEdgeMap(mapname,emptr, default_value)) |
---|
[1597] | 815 | { |
---|
| 816 | return 1; |
---|
| 817 | } |
---|
| 818 | |
---|
[1594] | 819 | |
---|
| 820 | //add it to the list of the displayable maps |
---|
[1837] | 821 | mainwin.registerNewEdgeMap(mapname); |
---|
[1594] | 822 | |
---|
| 823 | //display it |
---|
[1592] | 824 | changeEdgeText(mapname); |
---|
[1597] | 825 | |
---|
| 826 | return 0; |
---|
[1585] | 827 | } |
---|
| 828 | |
---|
[1597] | 829 | int GraphDisplayerCanvas::addNewNodeMap(double default_value, std::string mapname) |
---|
[1585] | 830 | { |
---|
[1594] | 831 | //create the new map |
---|
[1837] | 832 | Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> ((mainwin.mapstorage).graph,default_value); |
---|
[1597] | 833 | |
---|
| 834 | //if addition was not successful addNodeMap returns one. |
---|
| 835 | //cause can be that there is already a map named like the new one |
---|
[1837] | 836 | if((mainwin.mapstorage).addNodeMap(mapname,emptr, default_value)) |
---|
[1597] | 837 | { |
---|
| 838 | return 1; |
---|
| 839 | } |
---|
[1594] | 840 | |
---|
| 841 | //add it to the list of the displayable maps |
---|
[1837] | 842 | mainwin.registerNewNodeMap(mapname); |
---|
[1594] | 843 | |
---|
| 844 | //display it |
---|
[1592] | 845 | changeNodeText(mapname); |
---|
[1597] | 846 | |
---|
| 847 | return 0; |
---|
[1585] | 848 | } |
---|
| 849 | |
---|