#include "graph_displayer_canvas.h" #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) { if(actual_tool!=newtool) { actual_handler.disconnect(); switch(actual_tool) { case CREATE_EDGE: { GdkEvent * generated=new GdkEvent(); generated->type=GDK_BUTTON_RELEASE; generated->button.button=3; createEdgeEventHandler(generated); break; } case EDGE_MAP_EDIT: //has to do the same thing as in the case of NODE_MAP_EDIT case NODE_MAP_EDIT: { break; } default: break; } active_item=NULL; target_item=NULL; active_edge=INVALID; active_node=INVALID; actual_tool=newtool; switch(newtool) { case MOVE: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false); break; case CREATE_NODE: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false); break; case CREATE_EDGE: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false); break; case ERASER: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false); break; case EDGE_MAP_EDIT: grab_focus(); actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false); break; case NODE_MAP_EDIT: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false); break; default: break; } } } int GraphDisplayerCanvas::getActualTool() { return actual_tool; } bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e) { static Gnome::Canvas::Text *coord_text = 0; switch(e->type) { case GDK_BUTTON_PRESS: //we mark the location of the event to be able to calculate parameters of dragging window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); active_item=(get_item_at(clicked_x, clicked_y)); active_node=INVALID; for (NodeIt i((mytab.mapstorage).graph); 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: if (coord_text) { delete coord_text; coord_text = 0; } isbutton=0; active_item=NULL; active_node=INVALID; 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) { (mytab.mapstorage).modified = true; //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 new_x, new_y; window_to_world (e->motion.x, e->motion.y, new_x, new_y); double dx=new_x-clicked_x; double dy=new_y-clicked_y; //repositioning node and its text active_item->move(dx, dy); nodetextmap[active_node]->move(dx, dy); // the new coordinates of the centre of the node double coord_x = new_x - (clicked_x - (mytab.mapstorage).coords[active_node].x); double coord_y = new_y - (clicked_y - (mytab.mapstorage).coords[active_node].y); // write back the new coordinates to the coords map (mytab.mapstorage).coords.set(active_node, xy(coord_x, coord_y)); clicked_x=new_x; clicked_y=new_y; // reposition the coordinates text std::ostringstream ostr; ostr << "(" << (mytab.mapstorage).coords[active_node].x << ", " << (mytab.mapstorage).coords[active_node].y << ")"; double radius = (nodesmap[active_node]->property_x2().get_value() - nodesmap[active_node]->property_x1().get_value()) / 2.0; if (coord_text) { coord_text->property_text().set_value(ostr.str()); coord_text->property_x().set_value((mytab.mapstorage).coords[active_node].x + radius); coord_text->property_y().set_value((mytab.mapstorage).coords[active_node].y - radius); } else { coord_text = new Gnome::Canvas::Text( displayed_graph, (mytab.mapstorage).coords[active_node].x + radius, (mytab.mapstorage).coords[active_node].y - radius, ostr.str()); coord_text->property_fill_color().set_value("black"); coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST); } //all the edges connected to the moved point has to be redrawn for(OutEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei) { XY moved_node_1(coord_x - dx, coord_y - dy); XY moved_node_2(coord_x, coord_y); Node target = mytab.mapstorage.graph.target(ei); XY fix_node(mytab.mapstorage.coords[target].x, mytab.mapstorage.coords[target].y); XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]); XY arrow_pos; if(isbutton==3) arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, false); else arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, true); mytab.mapstorage.arrow_pos.set(ei, arrow_pos); edgesmap[ei]->draw(); //reposition of edgetext XY text_pos=mytab.mapstorage.arrow_pos[ei]; text_pos+=(XY(10,10)); edgetextmap[ei]->property_x().set_value(text_pos.x); edgetextmap[ei]->property_y().set_value(text_pos.y); } for(InEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei) { XY moved_node_1(coord_x - dx, coord_y - dy); XY moved_node_2(coord_x, coord_y); Node source = mytab.mapstorage.graph.source(ei); XY fix_node(mytab.mapstorage.coords[source].x, mytab.mapstorage.coords[source].y); XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]); XY arrow_pos; if(isbutton==3) arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, false); else arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, true); mytab.mapstorage.arrow_pos.set(ei, arrow_pos); edgesmap[ei]->draw(); //reposition of edgetext XY text_pos=mytab.mapstorage.arrow_pos[ei]; 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 false; } XY GraphDisplayerCanvas::calcArrowPos(XY moved_node_1, XY moved_node_2, XY fix_node, XY old_arrow_pos, bool move) { if(!move) { return XY((moved_node_2.x + fix_node.x) / 2.0, (moved_node_2.y + fix_node.y) / 2.0); } else { ////////////////////////////////////////////////////////////////////////////////////////////////////// /////////// keeps shape-with scalar multiplication - version 2. ////////////////////////////////////////////////////////////////////////////////////////////////////// //old vector from one to the other node - a xy a_v(moved_node_1.x-fix_node.x,moved_node_1.y-fix_node.y); //new vector from one to the other node - b xy b_v(moved_node_2.x-fix_node.x,moved_node_2.y-fix_node.y); double absa=sqrt(a_v.normSquare()); double absb=sqrt(b_v.normSquare()); if ((absa == 0.0) || (absb == 0.0)) { return old_arrow_pos; } else { //old vector from one node to the breakpoint - c xy c_v(old_arrow_pos.x-fix_node.x,old_arrow_pos.y-fix_node.y); //unit vector with the same direction to a_v xy a_v_u(a_v.x/absa,a_v.y/absa); //normal vector of unit vector with the same direction to a_v xy a_v_u_n(((-1)*a_v_u.y),a_v_u.x); //unit vector with the same direction to b_v xy b_v_u(b_v.x/absb,b_v.y/absb); //normal vector of unit vector with the same direction to b_v xy b_v_u_n(((-1)*b_v_u.y),b_v_u.x); //vector c in a_v_u and a_v_u_n co-ordinate system xy c_a(c_v*a_v_u,c_v*a_v_u_n); //new vector from one node to the breakpoint - d - we have to calculate this one xy d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n); return XY(d_v.x+fix_node.x,d_v.y+fix_node.y); } } } bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e) { switch(e->type) { //move the new node case GDK_MOTION_NOTIFY: { GdkEvent * generated=new GdkEvent(); generated->motion.x=e->motion.x; generated->motion.y=e->motion.y; generated->type=GDK_MOTION_NOTIFY; moveEventHandler(generated); break; } case GDK_BUTTON_RELEASE: (mytab.mapstorage).modified = true; isbutton=1; active_node=(mytab.mapstorage).graph.addNode(); //initiating values corresponding to new node in maps window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); // update coordinates (mytab.mapstorage).coords.set(active_node, xy(clicked_x, clicked_y)); // update all other maps for (std::map*>::const_iterator it = (mytab.mapstorage).nodemap_storage.begin(); it != (mytab.mapstorage).nodemap_storage.end(); ++it) { if ((it->first != "coordinates_x") && (it->first != "coordinates_y")) { (*(it->second))[active_node] = (mytab.mapstorage).nodemap_default[it->first]; } } // increment the id map's default value (mytab.mapstorage).nodemap_default["label"] += 1.0; 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("blue"); *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black"); active_item->raise_to_top(); (nodesmap[active_node])->show(); nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph, clicked_x+node_property_defaults[N_RADIUS]+5, clicked_y+node_property_defaults[N_RADIUS]+5, ""); nodetextmap[active_node]->property_fill_color().set_value("darkblue"); nodetextmap[active_node]->raise_to_top(); // mapwin.updateNode(active_node); propertyUpdate(active_node); isbutton=0; target_item=NULL; active_item=NULL; active_node=INVALID; break; default: break; } return false; } bool GraphDisplayerCanvas::createEdgeEventHandler(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 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); active_item=(get_item_at(clicked_x, clicked_y)); active_node=INVALID; for (NodeIt i((mytab.mapstorage).graph); 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 { window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); target_item=(get_item_at(clicked_x, clicked_y)); Node target_node=INVALID; for (NodeIt i((mytab.mapstorage).graph); 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) { if(target_node!=active_node) { (mytab.mapstorage).modified = true; *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red"); //creating new edge active_edge=(mytab.mapstorage).graph.addEdge(active_node, target_node); // update maps for (std::map*>::const_iterator it = (mytab.mapstorage).edgemap_storage.begin(); it != (mytab.mapstorage).edgemap_storage.end(); ++it) { (*(it->second))[active_edge] = (mytab.mapstorage).edgemap_default[it->first]; } // increment the id map's default value (mytab.mapstorage).edgemap_default["label"] += 1.0; //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)); // set the coordinates of the arrow on the new edge MapStorage& ms = mytab.mapstorage; ms.arrow_pos.set(active_edge, (ms.coords[ms.graph.source(active_edge)] + ms.coords[ms.graph.target(active_edge)])/ 2.0); //drawing new edge edgesmap[active_edge]=new BrokenEdge(displayed_graph, active_edge, *this); *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green"); edgesmap[active_edge]->property_width_pixels().set_value(10); edgesmap[active_edge]->lower_to_bottom(); //initializing edge-text as well, to empty string XY text_pos=mytab.mapstorage.arrow_pos[active_edge]; 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( "darkgreen"); edgetextmap[active_edge]->raise_to_top(); //updating its properties // mapwin.updateEdge(active_edge); propertyUpdate(active_edge); } else { target_node=INVALID; std::cerr << "Loop edge is not yet implemented!" << std::endl; } } //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::eraserEventHandler(GdkEvent* e) { switch(e->type) { case GDK_BUTTON_PRESS: //finding the clicked items window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); active_item=(get_item_at(clicked_x, clicked_y)); active_node=INVALID; active_edge=INVALID; //was it a node? for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) { if(nodesmap[i]==active_item) { active_node=i; } } //or was it an edge? if(active_node==INVALID) { for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) { if(edgesmap[i]==active_item) { active_edge=i; } } } // return if the clicked object is neither an edge nor a node if (active_edge == INVALID) return false; //recolor activated item if(active_item) { *active_item << Gnome::Canvas::Properties::fill_color("red"); } break; case GDK_BUTTON_RELEASE: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); if(active_item) { //the cursor was not moved since pressing it if( active_item == ( get_item_at (clicked_x, clicked_y) ) ) { //a node was found if(active_node!=INVALID) { (mytab.mapstorage).modified = true; std::set edges_to_delete; for(OutEdgeIt e((mytab.mapstorage).graph,active_node);e!=INVALID;++e) { edges_to_delete.insert(e); } for(InEdgeIt e((mytab.mapstorage).graph,active_node);e!=INVALID;++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) { deleteItem(*edge_set_it); } deleteItem(active_node); } //a simple edge was chosen else if (active_edge != INVALID) { deleteItem(active_edge); } } //pointer was moved, deletion is cancelled else { if(active_node!=INVALID) { *active_item << Gnome::Canvas::Properties::fill_color("blue"); } else if (active_edge != INVALID) { *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 false; } bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e) { if(actual_tool==EDGE_MAP_EDIT) { switch(e->type) { case GDK_BUTTON_PRESS: { //for determine, whether it was an edge Edge clicked_edge=INVALID; window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); active_item=(get_item_at(clicked_x, clicked_y)); //find the activated item between texts for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) { //at the same time only one can be active if(edgetextmap[i]==active_item) { clicked_edge=i; } } //if it was not between texts, search for it between edges if(clicked_edge==INVALID) { for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) { //at the same time only one can be active if((edgesmap[i]==active_item)||(edgetextmap[i]==active_item)) { clicked_edge=i; } } } //if it was really an edge... if(clicked_edge!=INVALID) { // the id map is not editable if (edgemap_to_edit == "label") return 0; //and there is activated map if(edgetextmap[clicked_edge]->property_text().get_value()!="") { //activate the general variable for it active_edge=clicked_edge; //create a dialog Gtk::Dialog dialog("Edit value", true); dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); Gtk::VBox* vbox = dialog.get_vbox(); Gtk::SpinButton spin(0.0, 4); spin.set_increments(1.0, 10.0); spin.set_range(-1000000.0, 1000000.0); spin.set_numeric(true); vbox->add(spin); spin.show(); switch (dialog.run()) { case Gtk::RESPONSE_NONE: case Gtk::RESPONSE_CANCEL: break; case Gtk::RESPONSE_ACCEPT: double new_value = spin.get_value(); (*(mytab.mapstorage).edgemap_storage[edgemap_to_edit])[active_edge] = new_value; std::ostringstream ostr; ostr << new_value; edgetextmap[active_edge]->property_text().set_value( ostr.str()); //mapwin.updateEdge(active_edge); // mapwin.updateEdge(Edge(INVALID)); propertyUpdate(Edge(INVALID)); } } } break; } default: break; } } return false; } bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e) { if(actual_tool==NODE_MAP_EDIT) { switch(e->type) { case GDK_BUTTON_PRESS: { //for determine, whether it was a node Node clicked_node=INVALID; window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); active_item=(get_item_at(clicked_x, clicked_y)); //find the activated item between texts for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) { //at the same time only one can be active if(nodetextmap[i]==active_item) { clicked_node=i; } } //if there was not, search for it between nodes if(clicked_node==INVALID) { for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) { //at the same time only one can be active if(nodesmap[i]==active_item) { clicked_node=i; } } } //if it was really a node... if(clicked_node!=INVALID) { // the id map is not editable if (nodemap_to_edit == "label") return 0; //and there is activated map if(nodetextmap[clicked_node]->property_text().get_value()!="") { //activate the general variable for it active_node=clicked_node; //create a dialog Gtk::Dialog dialog("Edit value", true); dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); Gtk::VBox* vbox = dialog.get_vbox(); Gtk::SpinButton spin(0.0, 4); spin.set_increments(1.0, 10.0); spin.set_range(-1000000.0, 1000000.0); spin.set_numeric(true); vbox->add(spin); spin.show(); switch (dialog.run()) { case Gtk::RESPONSE_NONE: case Gtk::RESPONSE_CANCEL: break; case Gtk::RESPONSE_ACCEPT: double new_value = spin.get_value(); (*(mytab.mapstorage).nodemap_storage[nodemap_to_edit])[active_node] = new_value; std::ostringstream ostr; ostr << new_value; nodetextmap[active_node]->property_text().set_value( ostr.str()); //mapwin.updateNode(active_node); // mapwin.updateNode(Node(INVALID)); propertyUpdate(Node(INVALID)); } } } break; } default: break; } } return false; } void GraphDisplayerCanvas::deleteItem(Node node_to_delete) { delete(nodetextmap[node_to_delete]); delete(nodesmap[node_to_delete]); (mytab.mapstorage).graph.erase(node_to_delete); } void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete) { delete(edgetextmap[edge_to_delete]); delete(edgesmap[edge_to_delete]); (mytab.mapstorage).graph.erase(edge_to_delete); } void GraphDisplayerCanvas::textReposition(xy new_place) { new_place+=(xy(10,10)); edgetextmap[forming_edge]->property_x().set_value(new_place.x); edgetextmap[forming_edge]->property_y().set_value(new_place.y); } void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on) { if(on) { if(forming_edge!=INVALID) { std::cerr << "ERROR!!!! Valid edge found!" << std::endl; } else { for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) { if(edgesmap[i]==active_bre) { forming_edge=i; } } } } else { if(forming_edge!=INVALID) { forming_edge=INVALID; } else { std::cerr << "ERROR!!!! Invalid edge found!" << std::endl; } } }