# HG changeset patch # User hegyi # Date 1119599898 0 # Node ID cde847387b5a8e3f3dedf597eb3d6cee905b7387 # Parent f9113440b66712f65b21f86a19fa0297b4af607d File graph_displayer is split in functional parts. diff -r f9113440b667 -r cde847387b5a gui/Makefile.am --- a/gui/Makefile.am Thu Jun 23 17:56:24 2005 +0000 +++ b/gui/Makefile.am Fri Jun 24 07:58:18 2005 +0000 @@ -6,6 +6,9 @@ gd_SOURCES = \ all_include.h \ graph_displayer_canvas.cc \ + graph_displayer_canvas-edge.cc \ + graph_displayer_canvas-event.cc \ + graph_displayer_canvas-zoom.cc \ graph_displayer_canvas.h \ graph-displayer.cc \ main_win.cc \ diff -r f9113440b667 -r cde847387b5a gui/graph_displayer_canvas-edge.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gui/graph_displayer_canvas-edge.cc Fri Jun 24 07:58:18 2005 +0000 @@ -0,0 +1,93 @@ +#include +#include +#include + + +int GraphDisplayerCanvas::changeLineWidth (std::string mapname) +{ + for (EdgeIt i(g); i!=INVALID; ++i) + { + int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i]; + if(w>=0) + { + edgesmap[i]->property_width_pixels().set_value(w); + } + } + return 0; +}; + +int GraphDisplayerCanvas::changeColor (std::string mapname) +{ + + //function maps the range of the maximum and + //the minimum of the nodemap to the range of + //green in RGB + + for (EdgeIt i(g); i!=INVALID; ++i) + { + double w=(*(mapstorage.edgemap_storage)[mapname])[i]; + double max=mapstorage.maxOfEdgeMap(mapname); + double min=mapstorage.minOfEdgeMap(mapname); + + //std::cout<property_fill_color_gdk().set_value(color); + } + return 0; +}; + +int GraphDisplayerCanvas::changeText (std::string mapname) +{ + + //the number in the map will be written on the edge + //EXCEPT when the name of the map is Text, because + //in that case empty string will be written, because + //that is the deleter map + //\todo isn't it a bit woodcutter? + + for (EdgeIt i(g); i!=INVALID; ++i) + { + if(mapname!="Text") + { + double number=(*(mapstorage.edgemap_storage)[mapname])[i]; + int length=1; + //if number is smaller than one, length would be negative, or invalid + if(number>=1) + { + length=(int)(floor(log(number)/log(10)))+1; + } + int maxpos=(int)(pow(10,length-1)); + int strl=length+1+RANGE; + char * str=new char[strl]; + str[length]='.'; + str[strl]='\0'; + + for(int j=0;jproperty_text().set_value(str); + } + else + { + edgetextmap[i]->property_text().set_value(""); + } + } + return 0; +}; diff -r f9113440b667 -r cde847387b5a gui/graph_displayer_canvas-event.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gui/graph_displayer_canvas-event.cc Fri Jun 24 07:58:18 2005 +0000 @@ -0,0 +1,492 @@ +#include +#include +#include + + +bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event) +{ + Gnome::Canvas::CanvasAA::on_expose_event(event); + //usleep(10000); + //rezoom(); + return true; +} + +void GraphDisplayerCanvas::changeEditorialTool(int newtool) +{ + actual_handler.disconnect(); + + if(actual_tool==CREATE_EDGE) + { + GdkEvent * generated=new GdkEvent(); + generated->type=GDK_BUTTON_RELEASE; + generated->button.button=3; + create_edge_event_handler(generated); + } + + actual_tool=newtool; + + switch(newtool) + { + case MOVE: + actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false); + break; + + //it has to assigned to canvas, because all the canvas has to be monitored, not only the elements of the already drawn group + case CREATE_NODE: + actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false); + break; + + case CREATE_EDGE: + actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false); + break; + + case ERASER: + actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraser_event_handler), false); + break; + + default: + break; + } +} + +int GraphDisplayerCanvas::get_actual_tool() +{ + return actual_tool; +} + +bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e) +{ + switch(e->type) + { + case GDK_BUTTON_PRESS: + //we mark the location of the event to be able to calculate parameters of dragging + clicked_x=e->button.x; + clicked_y=e->button.y; + active_item=(get_item_at(e->button.x, e->button.y)); + active_node=INVALID; + for (NodeIt i(g); i!=INVALID; ++i) + { + if(nodesmap[i]==active_item) + { + active_node=i; + } + } + switch(e->button.button) + { + case 3: + isbutton=3; + break; + default: + isbutton=1; + break; + } + break; + case GDK_BUTTON_RELEASE: + isbutton=0; + active_item=NULL; + active_node=INVALID; + updateScrollRegion(); + break; + case GDK_MOTION_NOTIFY: + //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 + if(active_node!=INVALID) + { + //new coordinates will be the old values, + //because the item will be moved to the + //new coordinate therefore the new movement + //has to be calculated from here + + double dx=e->motion.x-clicked_x; + double dy=e->motion.y-clicked_y; + + active_item->move(dx, dy); + + clicked_x=e->motion.x; + clicked_y=e->motion.y; + + //all the edges connected to the moved point has to be redrawn + EdgeIt ei; + + g.firstOut(ei,active_node); + + for(;ei!=INVALID;g.nextOut(ei)) + { + Gnome::Canvas::Points coos; + double x1, x2, y1, y2; + + nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2); + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); + + nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2); + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); + + if(isbutton==3) + { + edgesmap[ei]->set_points(coos); + } + else + { + edgesmap[ei]->set_points(coos,true); + } + + xy text_pos=edgesmap[ei]->get_arrow_pos(); + text_pos+=(xy(10,10)); + edgetextmap[ei]->property_x().set_value(text_pos.x); + edgetextmap[ei]->property_y().set_value(text_pos.y); + } + + g.firstIn(ei,active_node); + for(;ei!=INVALID;g.nextIn(ei)) + { + Gnome::Canvas::Points coos; + double x1, x2, y1, y2; + + nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2); + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); + + nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2); + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); + + if(isbutton==3) + { + edgesmap[ei]->set_points(coos); + } + else + { + edgesmap[ei]->set_points(coos,true); + } + + xy text_pos=edgesmap[ei]->get_arrow_pos(); + text_pos+=(xy(10,10)); + edgetextmap[ei]->property_x().set_value(text_pos.x); + edgetextmap[ei]->property_y().set_value(text_pos.y); + } + } + default: break; + } + + return true; +} + +bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e) +{ + switch(e->type) + { + + //draw the new node in red at the clicked place + case GDK_BUTTON_PRESS: + isbutton=1; + + active_node=NodeIt(g,g.addNode()); + + //initiating values corresponding to new node in maps + + + window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); + + nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20); + active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]); + *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); + *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black"); + (nodesmap[active_node])->show(); + break; + + //move the new node + case GDK_MOTION_NOTIFY: + { + double world_motion_x, world_motion_y; + GdkEvent * generated=new GdkEvent(); + window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y); + generated->motion.x=world_motion_x; + generated->motion.y=world_motion_y; + generated->type=GDK_MOTION_NOTIFY; + move_event_handler(generated); + break; + } + + //finalize the new node + case GDK_BUTTON_RELEASE: + isbutton=0; + *active_item << Gnome::Canvas::Properties::fill_color("blue"); + active_item=NULL; + active_node=INVALID; + updateScrollRegion(); + break; + default: + break; + } + return false; +} + +bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e) +{ + switch(e->type) + { + case GDK_BUTTON_PRESS: + //in edge creation right button has special meaning + if(e->button.button!=3) + { + //there is not yet selected node + if(active_node==INVALID) + { + //we mark the location of the event to be able to calculate parameters of dragging + clicked_x=e->button.x; + clicked_y=e->button.y; + active_item=(get_item_at(e->button.x, e->button.y)); + active_node=INVALID; + for (NodeIt i(g); i!=INVALID; ++i) + { + if(nodesmap[i]==active_item) + { + active_node=i; + } + } + //the clicked item is really a node + if(active_node!=INVALID) + { + *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); + isbutton=1; + } + //clicked item was not a node. It could be e.g. edge. + else + { + active_item=NULL; + } + } + //we only have to do sg. if the mouse button + // is pressed already once AND the click was + // on a node that was found in the set of + //nodes, and now we only search for the second + //node + else + { + target_item=(get_item_at(e->button.x, e->button.y)); + Graph::NodeIt target_node=INVALID; + for (NodeIt i(g); i!=INVALID; ++i) + { + if(nodesmap[i]==target_item) + { + target_node=i; + } + } + //the clicked item is a node, the edge can be drawn + if(target_node!=INVALID) + { + *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red"); + + //creating new edge + active_edge=EdgeIt(g,g.addEdge(active_node, target_node)); + + //initiating values corresponding to new edge in maps + mapstorage.init_maps_for_edge(active_edge); + + //calculating coordinates of new edge + Gnome::Canvas::Points coos; + double x1, x2, y1, y2; + + active_item->get_bounds(x1, y1, x2, y2); + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); + + target_item->get_bounds(x1, y1, x2, y2); + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); + + //drawing new edge + edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this); + *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green"); + edgesmap[active_edge]->property_width_pixels().set_value(10); + + //redraw nodes to blank terminations of the new edge + target_item->raise_to_top(); + active_item->raise_to_top(); + + //initializing edge-text as well, to empty string + xy text_pos=edgesmap[active_edge]->get_arrow_pos(); + text_pos+=(xy(10,10)); + + edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, ""); + edgetextmap[active_edge]->property_fill_color().set_value("black"); + } + //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore. + else + { + target_item=NULL; + } + } + } + break; + case GDK_BUTTON_RELEASE: + isbutton=0; + //we clear settings in two cases + //1: the edge is ready (target_item has valid value) + //2: the edge creation is cancelled with right button + if((target_item)||(e->button.button==3)) + { + if(active_item) + { + *active_item << Gnome::Canvas::Properties::fill_color("blue"); + active_item=NULL; + } + if(target_item) + { + *target_item << Gnome::Canvas::Properties::fill_color("blue"); + target_item=NULL; + } + active_node=INVALID; + active_edge=INVALID; + } + break; + default: + break; + } + return false; +} + +bool GraphDisplayerCanvas::eraser_event_handler(GdkEvent* e) +{ + switch(e->type) + { + case GDK_BUTTON_PRESS: + active_item=(get_item_at(e->button.x, e->button.y)); + active_node=INVALID; + active_edge=INVALID; + for (NodeIt i(g); i!=INVALID; ++i) + { + if(nodesmap[i]==active_item) + { + active_node=i; + } + } + if(active_node==INVALID) + { + for (EdgeIt i(g); i!=INVALID; ++i) + { + if(edgesmap[i]==active_item) + { + active_edge=i; + } + } + } + *active_item << Gnome::Canvas::Properties::fill_color("red"); + break; + + case GDK_BUTTON_RELEASE: + if(active_item==(get_item_at(e->button.x, e->button.y))) + { + if(active_node!=INVALID) + { + + //collecting edges to delete + EdgeIt e; + std::set edges_to_delete; + + g.firstOut(e,active_node); + for(;e!=INVALID;g.nextOut(e)) + { + edges_to_delete.insert(e); + } + + g.firstIn(e,active_node); + for(;e!=INVALID;g.nextIn(e)) + { + edges_to_delete.insert(e); + } + + //deleting collected edges + for(std::set::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++) + { + delete_item(*edge_set_it); + } + delete_item(active_node); + } + //a simple edge was chosen + else + { + delete_item(active_edge); + } + + + } + //pointer was moved, deletion is cancelled + else + { + if(active_node!=INVALID) + { + *active_item << Gnome::Canvas::Properties::fill_color("blue"); + } + else + { + *active_item << Gnome::Canvas::Properties::fill_color("green"); + } + } + //reseting datas + active_item=NULL; + active_edge=INVALID; + active_node=INVALID; + break; + + case GDK_MOTION_NOTIFY: + break; + + default: + break; + } + return true; +} + +void GraphDisplayerCanvas::delete_item(NodeIt node_to_delete) +{ + delete(nodesmap[node_to_delete]); + g.erase(node_to_delete); +} + +void GraphDisplayerCanvas::delete_item(EdgeIt edge_to_delete) +{ + delete(edgesmap[edge_to_delete]); + g.erase(edge_to_delete); +} + +void GraphDisplayerCanvas::delete_item(Graph::Edge edge_to_delete) +{ + delete(edgesmap[edge_to_delete]); + g.erase(edge_to_delete); +} + +void GraphDisplayerCanvas::text_reposition(xy new_place) +{ + new_place+=(xy(10,10)); + edgetextmap[active_edge]->property_x().set_value(new_place.x); + edgetextmap[active_edge]->property_y().set_value(new_place.y); +} + +void GraphDisplayerCanvas::toggle_edge_activity(BrokenEdge* active_bre, bool on) +{ + if(on) + { + if(active_edge!=INVALID) + { + std::cout << "ERROR!!!! Valid edge found!" << std::endl; + } + else + { + for (EdgeIt i(g); i!=INVALID; ++i) + { + if(edgesmap[i]==active_bre) + { + active_edge=i; + } + } + } + } + else + { + if(active_edge!=INVALID) + { + active_edge=INVALID; + } + else + { + std::cout << "ERROR!!!! Invalid edge found!" << std::endl; + } + } + +} diff -r f9113440b667 -r cde847387b5a gui/graph_displayer_canvas-zoom.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gui/graph_displayer_canvas-zoom.cc Fri Jun 24 07:58:18 2005 +0000 @@ -0,0 +1,49 @@ +#include +#include +#include + +void GraphDisplayerCanvas::zoomIn() +{ + set_pixels_per_unit( + (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit()); +} + +void GraphDisplayerCanvas::zoomOut() +{ + set_pixels_per_unit( + (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit()); +} + +void GraphDisplayerCanvas::zoomFit() +{ + // get the height and width of the canvas + Gtk::Allocation a = get_allocation(); + int aw = a.get_width(); + int ah = a.get_height(); + // add some space + aw -= 5; if (aw < 0) aw = 0; + ah -= 5; if (ah < 0) ah = 0; + + // get the bounding box of the graph + double wx1, wy1, wx2, wy2; + Gnome::Canvas::Item* pCanvasItem = root(); + pCanvasItem->get_bounds(wx1, wy1, wx2, wy2); + + // fit the graph to the window + double ppu1 = (double) aw / fabs(wx2 - wx1); + double ppu2 = (double) ah / fabs(wy2 - wy1); + set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2); +} + +void GraphDisplayerCanvas::zoom100() +{ + set_pixels_per_unit(1.0); +} + +void GraphDisplayerCanvas::updateScrollRegion() +{ + double wx1, wy1, wx2, wy2; + Gnome::Canvas::Item* pCanvasItem = root(); + pCanvasItem->get_bounds(wx1, wy1, wx2, wy2); + set_scroll_region(wx1, wy1, wx2, wy2); +} diff -r f9113440b667 -r cde847387b5a gui/graph_displayer_canvas.cc --- a/gui/graph_displayer_canvas.cc Thu Jun 23 17:56:24 2005 +0000 +++ b/gui/graph_displayer_canvas.cc Fri Jun 24 07:58:18 2005 +0000 @@ -91,706 +91,3 @@ writer.writeNodeMap("coordinates_y", yc); writer.run(); } - -int GraphDisplayerCanvas::changeLineWidth (std::string mapname) -{ - for (EdgeIt i(g); i!=INVALID; ++i) - { - int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i]; - if(w>=0) - { - edgesmap[i]->property_width_pixels().set_value(w); - } - } - return 0; -}; - -int GraphDisplayerCanvas::changeColor (std::string mapname) -{ - - //function maps the range of the maximum and - //the minimum of the nodemap to the range of - //green in RGB - - for (EdgeIt i(g); i!=INVALID; ++i) - { - double w=(*(mapstorage.edgemap_storage)[mapname])[i]; - double max=mapstorage.maxOfEdgeMap(mapname); - double min=mapstorage.minOfEdgeMap(mapname); - - //std::cout<property_fill_color_gdk().set_value(color); - } - return 0; -}; - -int GraphDisplayerCanvas::changeText (std::string mapname) -{ - - //the number in the map will be written on the edge - //EXCEPT when the name of the map is Text, because - //in that case empty string will be written, because - //that is the deleter map - //\todo isn't it a bit woodcutter? - - for (EdgeIt i(g); i!=INVALID; ++i) - { - if(mapname!="Text") - { - double number=(*(mapstorage.edgemap_storage)[mapname])[i]; - int length=1; - //if number is smaller than one, length would be negative, or invalid - if(number>=1) - { - length=(int)(floor(log(number)/log(10)))+1; - } - int maxpos=(int)(pow(10,length-1)); - int strl=length+1+RANGE; - char * str=new char[strl]; - str[length]='.'; - str[strl]='\0'; - - for(int j=0;jproperty_text().set_value(str); - } - else - { - edgetextmap[i]->property_text().set_value(""); - } - } - return 0; -}; - -//Deprecated -bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n) -{ - switch(e->type) - { - case GDK_BUTTON_PRESS: - //we mark the location of the event to be able to calculate parameters of dragging - clicked_x=e->button.x; - clicked_y=e->button.y; - active_item=(get_item_at(e->button.x, e->button.y)); - isbutton=1; - break; - case GDK_BUTTON_RELEASE: - isbutton=0; - active_item=NULL; - updateScrollRegion(); - break; - case GDK_MOTION_NOTIFY: - //we only have to do sg. if the mouse button is pressed - if(isbutton) - { - //new coordinates will be the old values, - //because the item will be moved to the - //new coordinate therefore the new movement - //has to be calculated from here - - double dx=e->motion.x-clicked_x; - double dy=e->motion.y-clicked_y; - active_item->move(dx, dy); - clicked_x=e->motion.x; - clicked_y=e->motion.y; - - //all the edges connected to the moved point has to be redrawn - - EdgeIt e; - g.firstOut(e,n); - for(;e!=INVALID;g.nextOut(e)) - { - Gnome::Canvas::Points coos; - double x1, x2, y1, y2; - - nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); - - nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); - - edgesmap[e]->property_points().set_value(coos); - - edgesmap[e]->get_bounds(x1, y1, x2, y2); - - edgetextmap[e]->property_x().set_value((x1+x2)/2); - edgetextmap[e]->property_y().set_value((y1+y2)/2); - } - - g.firstIn(e,n); - for(;e!=INVALID;g.nextIn(e)) - { - Gnome::Canvas::Points coos; - double x1, x2, y1, y2; - - nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); - - nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); - - edgesmap[e]->property_points().set_value(coos); - - edgesmap[e]->get_bounds(x1, y1, x2, y2); - - edgetextmap[e]->property_x().set_value((x1+x2)/2); - edgetextmap[e]->property_y().set_value((y1+y2)/2); - } - } - default: break; - } - return true; -} - -bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event) -{ - Gnome::Canvas::CanvasAA::on_expose_event(event); - //usleep(10000); - //rezoom(); - return true; -} - -void GraphDisplayerCanvas::zoomIn() -{ - set_pixels_per_unit( - (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit()); -} - -void GraphDisplayerCanvas::zoomOut() -{ - set_pixels_per_unit( - (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit()); -} - -void GraphDisplayerCanvas::zoomFit() -{ - // get the height and width of the canvas - Gtk::Allocation a = get_allocation(); - int aw = a.get_width(); - int ah = a.get_height(); - // add some space - aw -= 5; if (aw < 0) aw = 0; - ah -= 5; if (ah < 0) ah = 0; - - // get the bounding box of the graph - double wx1, wy1, wx2, wy2; - Gnome::Canvas::Item* pCanvasItem = root(); - pCanvasItem->get_bounds(wx1, wy1, wx2, wy2); - - // fit the graph to the window - double ppu1 = (double) aw / fabs(wx2 - wx1); - double ppu2 = (double) ah / fabs(wy2 - wy1); - set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2); -} - -void GraphDisplayerCanvas::zoom100() -{ - set_pixels_per_unit(1.0); -} - -void GraphDisplayerCanvas::updateScrollRegion() -{ - double wx1, wy1, wx2, wy2; - Gnome::Canvas::Item* pCanvasItem = root(); - pCanvasItem->get_bounds(wx1, wy1, wx2, wy2); - set_scroll_region(wx1, wy1, wx2, wy2); -} - -void GraphDisplayerCanvas::changeEditorialTool(int newtool) -{ - actual_handler.disconnect(); - - if(actual_tool==CREATE_EDGE) - { - GdkEvent * generated=new GdkEvent(); - generated->type=GDK_BUTTON_RELEASE; - generated->button.button=3; - create_edge_event_handler(generated); - } - - actual_tool=newtool; - - switch(newtool) - { - case MOVE: - actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false); - break; - - //it has to assigned to canvas, because all the canvas has to be monitored, not only the elements of the already drawn group - case CREATE_NODE: - actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false); - break; - - case CREATE_EDGE: - actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false); - break; - - case ERASER: - actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraser_event_handler), false); - break; - - default: - break; - } -} - -int GraphDisplayerCanvas::get_actual_tool() -{ - return actual_tool; -} - -bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e) -{ - switch(e->type) - { - case GDK_BUTTON_PRESS: - //we mark the location of the event to be able to calculate parameters of dragging - clicked_x=e->button.x; - clicked_y=e->button.y; - active_item=(get_item_at(e->button.x, e->button.y)); - active_node=INVALID; - for (NodeIt i(g); i!=INVALID; ++i) - { - if(nodesmap[i]==active_item) - { - active_node=i; - } - } - switch(e->button.button) - { - case 3: - isbutton=3; - break; - default: - isbutton=1; - break; - } - break; - case GDK_BUTTON_RELEASE: - isbutton=0; - active_item=NULL; - active_node=INVALID; - updateScrollRegion(); - break; - case GDK_MOTION_NOTIFY: - //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 - if(active_node!=INVALID) - { - //new coordinates will be the old values, - //because the item will be moved to the - //new coordinate therefore the new movement - //has to be calculated from here - - double dx=e->motion.x-clicked_x; - double dy=e->motion.y-clicked_y; - - active_item->move(dx, dy); - - clicked_x=e->motion.x; - clicked_y=e->motion.y; - - //all the edges connected to the moved point has to be redrawn - EdgeIt ei; - - g.firstOut(ei,active_node); - - for(;ei!=INVALID;g.nextOut(ei)) - { - Gnome::Canvas::Points coos; - double x1, x2, y1, y2; - - nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); - - nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); - - if(isbutton==3) - { - edgesmap[ei]->set_points(coos); - } - else - { - edgesmap[ei]->set_points(coos,true); - } - - xy text_pos=edgesmap[ei]->get_arrow_pos(); - text_pos+=(xy(10,10)); - edgetextmap[ei]->property_x().set_value(text_pos.x); - edgetextmap[ei]->property_y().set_value(text_pos.y); - } - - g.firstIn(ei,active_node); - for(;ei!=INVALID;g.nextIn(ei)) - { - Gnome::Canvas::Points coos; - double x1, x2, y1, y2; - - nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); - - nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); - - if(isbutton==3) - { - edgesmap[ei]->set_points(coos); - } - else - { - edgesmap[ei]->set_points(coos,true); - } - - xy text_pos=edgesmap[ei]->get_arrow_pos(); - text_pos+=(xy(10,10)); - edgetextmap[ei]->property_x().set_value(text_pos.x); - edgetextmap[ei]->property_y().set_value(text_pos.y); - } - } - default: break; - } - - return true; -} - -bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e) -{ - switch(e->type) - { - - //draw the new node in red at the clicked place - case GDK_BUTTON_PRESS: - isbutton=1; - - active_node=NodeIt(g,g.addNode()); - - //initiating values corresponding to new node in maps - - - window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); - - nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20); - active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]); - *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); - *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black"); - (nodesmap[active_node])->show(); - break; - - //move the new node - case GDK_MOTION_NOTIFY: - { - double world_motion_x, world_motion_y; - GdkEvent * generated=new GdkEvent(); - window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y); - generated->motion.x=world_motion_x; - generated->motion.y=world_motion_y; - generated->type=GDK_MOTION_NOTIFY; - move_event_handler(generated); - break; - } - - //finalize the new node - case GDK_BUTTON_RELEASE: - isbutton=0; - *active_item << Gnome::Canvas::Properties::fill_color("blue"); - active_item=NULL; - active_node=INVALID; - updateScrollRegion(); - break; - default: - break; - } - return false; -} - -bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e) -{ - switch(e->type) - { - case GDK_BUTTON_PRESS: - //in edge creation right button has special meaning - if(e->button.button!=3) - { - //there is not yet selected node - if(active_node==INVALID) - { - //we mark the location of the event to be able to calculate parameters of dragging - clicked_x=e->button.x; - clicked_y=e->button.y; - active_item=(get_item_at(e->button.x, e->button.y)); - active_node=INVALID; - for (NodeIt i(g); i!=INVALID; ++i) - { - if(nodesmap[i]==active_item) - { - active_node=i; - } - } - //the clicked item is really a node - if(active_node!=INVALID) - { - *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); - isbutton=1; - } - //clicked item was not a node. It could be e.g. edge. - else - { - active_item=NULL; - } - } - //we only have to do sg. if the mouse button - // is pressed already once AND the click was - // on a node that was found in the set of - //nodes, and now we only search for the second - //node - else - { - target_item=(get_item_at(e->button.x, e->button.y)); - Graph::NodeIt target_node=INVALID; - for (NodeIt i(g); i!=INVALID; ++i) - { - if(nodesmap[i]==target_item) - { - target_node=i; - } - } - //the clicked item is a node, the edge can be drawn - if(target_node!=INVALID) - { - *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red"); - - //creating new edge - active_edge=EdgeIt(g,g.addEdge(active_node, target_node)); - - //initiating values corresponding to new edge in maps - mapstorage.init_maps_for_edge(active_edge); - - //calculating coordinates of new edge - Gnome::Canvas::Points coos; - double x1, x2, y1, y2; - - active_item->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); - - target_item->get_bounds(x1, y1, x2, y2); - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); - - //drawing new edge - edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this); - *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green"); - edgesmap[active_edge]->property_width_pixels().set_value(10); - - //redraw nodes to blank terminations of the new edge - target_item->raise_to_top(); - active_item->raise_to_top(); - - //initializing edge-text as well, to empty string - xy text_pos=edgesmap[active_edge]->get_arrow_pos(); - text_pos+=(xy(10,10)); - - edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, ""); - edgetextmap[active_edge]->property_fill_color().set_value("black"); - } - //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore. - else - { - target_item=NULL; - } - } - } - break; - case GDK_BUTTON_RELEASE: - isbutton=0; - //we clear settings in two cases - //1: the edge is ready (target_item has valid value) - //2: the edge creation is cancelled with right button - if((target_item)||(e->button.button==3)) - { - if(active_item) - { - *active_item << Gnome::Canvas::Properties::fill_color("blue"); - active_item=NULL; - } - if(target_item) - { - *target_item << Gnome::Canvas::Properties::fill_color("blue"); - target_item=NULL; - } - active_node=INVALID; - active_edge=INVALID; - } - break; - default: - break; - } - return false; -} - -bool GraphDisplayerCanvas::eraser_event_handler(GdkEvent* e) -{ - switch(e->type) - { - case GDK_BUTTON_PRESS: - active_item=(get_item_at(e->button.x, e->button.y)); - active_node=INVALID; - active_edge=INVALID; - for (NodeIt i(g); i!=INVALID; ++i) - { - if(nodesmap[i]==active_item) - { - active_node=i; - } - } - if(active_node==INVALID) - { - for (EdgeIt i(g); i!=INVALID; ++i) - { - if(edgesmap[i]==active_item) - { - active_edge=i; - } - } - } - *active_item << Gnome::Canvas::Properties::fill_color("red"); - break; - - case GDK_BUTTON_RELEASE: - if(active_item==(get_item_at(e->button.x, e->button.y))) - { - if(active_node!=INVALID) - { - - //collecting edges to delete - EdgeIt e; - std::set edges_to_delete; - - g.firstOut(e,active_node); - for(;e!=INVALID;g.nextOut(e)) - { - edges_to_delete.insert(e); - } - - g.firstIn(e,active_node); - for(;e!=INVALID;g.nextIn(e)) - { - edges_to_delete.insert(e); - } - - //deleting collected edges - for(std::set::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++) - { - delete_item(*edge_set_it); - } - delete_item(active_node); - } - //a simple edge was chosen - else - { - delete_item(active_edge); - } - - - } - //pointer was moved, deletion is cancelled - else - { - if(active_node!=INVALID) - { - *active_item << Gnome::Canvas::Properties::fill_color("blue"); - } - else - { - *active_item << Gnome::Canvas::Properties::fill_color("green"); - } - } - //reseting datas - active_item=NULL; - active_edge=INVALID; - active_node=INVALID; - break; - - case GDK_MOTION_NOTIFY: - break; - - default: - break; - } - return true; -} - -void GraphDisplayerCanvas::delete_item(NodeIt node_to_delete) -{ - delete(nodesmap[node_to_delete]); - g.erase(node_to_delete); -} - -void GraphDisplayerCanvas::delete_item(EdgeIt edge_to_delete) -{ - delete(edgesmap[edge_to_delete]); - g.erase(edge_to_delete); -} - -void GraphDisplayerCanvas::delete_item(Graph::Edge edge_to_delete) -{ - delete(edgesmap[edge_to_delete]); - g.erase(edge_to_delete); -} - -void GraphDisplayerCanvas::text_reposition(xy new_place) -{ - new_place+=(xy(10,10)); - edgetextmap[active_edge]->property_x().set_value(new_place.x); - edgetextmap[active_edge]->property_y().set_value(new_place.y); -} - -void GraphDisplayerCanvas::toggle_edge_activity(BrokenEdge* active_bre, bool on) -{ - if(on) - { - if(active_edge!=INVALID) - { - std::cout << "ERROR!!!! Valid edge found!" << std::endl; - } - else - { - for (EdgeIt i(g); i!=INVALID; ++i) - { - if(edgesmap[i]==active_bre) - { - active_edge=i; - } - } - } - } - else - { - if(active_edge!=INVALID) - { - active_edge=INVALID; - } - else - { - std::cout << "ERROR!!!! Invalid edge found!" << std::endl; - } - } - -}