ladanyi@53: #include "graph_displayer_canvas.h" alpar@59: #include hegyi@27: hegyi@27: hegyi@27: bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event) hegyi@27: { hegyi@27: Gnome::Canvas::CanvasAA::on_expose_event(event); hegyi@27: //usleep(10000); hegyi@27: //rezoom(); hegyi@27: return true; hegyi@27: } hegyi@27: hegyi@27: void GraphDisplayerCanvas::changeEditorialTool(int newtool) hegyi@27: { hegyi@34: if(actual_tool!=newtool) hegyi@34: { hegyi@27: hegyi@34: actual_handler.disconnect(); hegyi@27: hegyi@34: switch(actual_tool) hegyi@34: { hegyi@34: case CREATE_EDGE: hegyi@34: { hegyi@34: GdkEvent * generated=new GdkEvent(); hegyi@34: generated->type=GDK_BUTTON_RELEASE; hegyi@34: generated->button.button=3; hegyi@34: createEdgeEventHandler(generated); hegyi@34: break; hegyi@34: } hegyi@34: case EDGE_MAP_EDIT: hegyi@35: //has to do the same thing as in the case of NODE_MAP_EDIT hegyi@35: case NODE_MAP_EDIT: hegyi@35: { hegyi@35: break; hegyi@35: } hegyi@34: default: hegyi@34: break; hegyi@34: } hegyi@27: hegyi@34: active_item=NULL; hegyi@34: target_item=NULL; hegyi@34: active_edge=INVALID; hegyi@34: active_node=INVALID; hegyi@33: hegyi@27: hegyi@34: actual_tool=newtool; hegyi@34: hegyi@34: switch(newtool) hegyi@34: { hegyi@34: case MOVE: hegyi@34: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false); hegyi@34: break; hegyi@27: hegyi@34: case CREATE_NODE: hegyi@34: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false); hegyi@34: break; hegyi@27: hegyi@34: case CREATE_EDGE: hegyi@34: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false); hegyi@34: break; hegyi@27: hegyi@34: case ERASER: hegyi@34: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false); hegyi@34: break; hegyi@32: hegyi@34: case EDGE_MAP_EDIT: hegyi@34: grab_focus(); hegyi@34: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false); hegyi@34: break; hegyi@32: hegyi@34: case NODE_MAP_EDIT: hegyi@34: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false); hegyi@34: break; hegyi@34: hegyi@34: default: hegyi@34: break; hegyi@34: } hegyi@27: } hegyi@27: } hegyi@27: hegyi@30: int GraphDisplayerCanvas::getActualTool() hegyi@27: { hegyi@27: return actual_tool; hegyi@27: } hegyi@27: hegyi@30: bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e) hegyi@27: { ladanyi@70: static Gnome::Canvas::Text *coord_text = 0; hegyi@27: switch(e->type) hegyi@27: { hegyi@27: case GDK_BUTTON_PRESS: hegyi@27: //we mark the location of the event to be able to calculate parameters of dragging hegyi@31: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@31: hegyi@31: active_item=(get_item_at(clicked_x, clicked_y)); hegyi@27: active_node=INVALID; hegyi@96: for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) hegyi@27: { hegyi@27: if(nodesmap[i]==active_item) hegyi@27: { hegyi@27: active_node=i; hegyi@27: } hegyi@27: } hegyi@27: switch(e->button.button) hegyi@27: { hegyi@27: case 3: hegyi@27: isbutton=3; hegyi@27: break; hegyi@27: default: hegyi@27: isbutton=1; hegyi@27: break; hegyi@27: } hegyi@27: break; hegyi@27: case GDK_BUTTON_RELEASE: ladanyi@70: if (coord_text) ladanyi@70: { ladanyi@70: delete coord_text; ladanyi@70: coord_text = 0; ladanyi@70: } hegyi@27: isbutton=0; hegyi@27: active_item=NULL; hegyi@27: active_node=INVALID; hegyi@27: break; hegyi@27: case GDK_MOTION_NOTIFY: hegyi@27: //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 hegyi@27: if(active_node!=INVALID) hegyi@27: { hegyi@96: (mytab.mapstorage).modified = true; ladanyi@70: hegyi@27: //new coordinates will be the old values, hegyi@27: //because the item will be moved to the hegyi@27: //new coordinate therefore the new movement hegyi@27: //has to be calculated from here hegyi@27: hegyi@31: double new_x, new_y; hegyi@31: hegyi@31: window_to_world (e->motion.x, e->motion.y, new_x, new_y); hegyi@31: hegyi@31: double dx=new_x-clicked_x; hegyi@31: double dy=new_y-clicked_y; hegyi@27: hegyi@28: //repositioning node and its text hegyi@27: active_item->move(dx, dy); hegyi@28: nodetextmap[active_node]->move(dx, dy); hegyi@27: ladanyi@70: // the new coordinates of the centre of the node hegyi@96: double coord_x = new_x - (clicked_x - (mytab.mapstorage).coords[active_node].x); hegyi@96: double coord_y = new_y - (clicked_y - (mytab.mapstorage).coords[active_node].y); ladanyi@70: ladanyi@98: // write back the new coordinates to the coords map ladanyi@98: (mytab.mapstorage).coords.set(active_node, xy(coord_x, coord_y)); ladanyi@98: hegyi@31: clicked_x=new_x; hegyi@31: clicked_y=new_y; hegyi@27: ladanyi@70: // reposition the coordinates text ladanyi@70: std::ostringstream ostr; ladanyi@70: ostr << "(" << hegyi@96: (mytab.mapstorage).coords[active_node].x << ", " << hegyi@96: (mytab.mapstorage).coords[active_node].y << ")"; ladanyi@72: double radius = ladanyi@72: (nodesmap[active_node]->property_x2().get_value() - ladanyi@72: nodesmap[active_node]->property_x1().get_value()) / 2.0; ladanyi@70: if (coord_text) ladanyi@70: { ladanyi@70: coord_text->property_text().set_value(ostr.str()); hegyi@96: coord_text->property_x().set_value((mytab.mapstorage).coords[active_node].x + ladanyi@72: radius); hegyi@96: coord_text->property_y().set_value((mytab.mapstorage).coords[active_node].y - ladanyi@72: radius); ladanyi@70: } ladanyi@70: else ladanyi@70: { ladanyi@70: coord_text = new Gnome::Canvas::Text( ladanyi@70: displayed_graph, hegyi@96: (mytab.mapstorage).coords[active_node].x + radius, hegyi@96: (mytab.mapstorage).coords[active_node].y - radius, ladanyi@70: ostr.str()); ladanyi@70: coord_text->property_fill_color().set_value("black"); ladanyi@72: coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST); ladanyi@70: } ladanyi@70: ladanyi@70: //all the edges connected to the moved point has to be redrawn hegyi@96: for(OutEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei) hegyi@27: { ladanyi@98: XY moved_node_1(coord_x - dx, coord_y - dy); ladanyi@98: XY moved_node_2(coord_x, coord_y); ladanyi@98: Node target = mytab.mapstorage.graph.target(ei); ladanyi@98: XY fix_node(mytab.mapstorage.coords[target].x, ladanyi@98: mytab.mapstorage.coords[target].y); ladanyi@98: XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]); hegyi@27: ladanyi@98: XY arrow_pos; ladanyi@98: if(isbutton==3) ladanyi@98: arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, false); ladanyi@98: else ladanyi@98: arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, true); hegyi@27: ladanyi@98: mytab.mapstorage.arrow_pos.set(ei, arrow_pos); ladanyi@98: edgesmap[ei]->draw(); hegyi@27: hegyi@28: //reposition of edgetext ladanyi@98: XY text_pos=mytab.mapstorage.arrow_pos[ei]; ladanyi@98: text_pos+=(XY(10,10)); hegyi@27: edgetextmap[ei]->property_x().set_value(text_pos.x); hegyi@27: edgetextmap[ei]->property_y().set_value(text_pos.y); hegyi@27: } hegyi@27: hegyi@96: for(InEdgeIt ei((mytab.mapstorage).graph,active_node);ei!=INVALID;++ei) hegyi@27: { ladanyi@98: XY moved_node_1(coord_x - dx, coord_y - dy); ladanyi@98: XY moved_node_2(coord_x, coord_y); ladanyi@98: Node source = mytab.mapstorage.graph.source(ei); ladanyi@98: XY fix_node(mytab.mapstorage.coords[source].x, ladanyi@98: mytab.mapstorage.coords[source].y); ladanyi@98: XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]); hegyi@27: ladanyi@98: XY arrow_pos; ladanyi@98: if(isbutton==3) ladanyi@98: arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, false); ladanyi@98: else ladanyi@98: arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, true); hegyi@27: ladanyi@98: mytab.mapstorage.arrow_pos.set(ei, arrow_pos); ladanyi@98: edgesmap[ei]->draw(); hegyi@27: ladanyi@98: //reposition of edgetext ladanyi@98: XY text_pos=mytab.mapstorage.arrow_pos[ei]; ladanyi@98: text_pos+=(XY(10,10)); hegyi@27: edgetextmap[ei]->property_x().set_value(text_pos.x); hegyi@27: edgetextmap[ei]->property_y().set_value(text_pos.y); hegyi@27: } hegyi@27: } hegyi@27: default: break; hegyi@27: } hegyi@27: hegyi@31: return false; hegyi@27: } hegyi@27: ladanyi@98: XY GraphDisplayerCanvas::calcArrowPos(XY moved_node_1, XY moved_node_2, XY fix_node, XY old_arrow_pos, bool move) ladanyi@98: { ladanyi@98: if(!move) ladanyi@98: { ladanyi@98: return XY((moved_node_2.x + fix_node.x) / 2.0, (moved_node_2.y + fix_node.y) / 2.0); ladanyi@98: } ladanyi@98: else ladanyi@98: { ladanyi@98: ////////////////////////////////////////////////////////////////////////////////////////////////////// ladanyi@98: /////////// keeps shape-with scalar multiplication - version 2. ladanyi@98: ////////////////////////////////////////////////////////////////////////////////////////////////////// ladanyi@98: ladanyi@98: //old vector from one to the other node - a ladanyi@98: xy a_v(moved_node_1.x-fix_node.x,moved_node_1.y-fix_node.y); ladanyi@98: //new vector from one to the other node - b ladanyi@98: xy b_v(moved_node_2.x-fix_node.x,moved_node_2.y-fix_node.y); ladanyi@98: ladanyi@98: double absa=sqrt(a_v.normSquare()); ladanyi@98: double absb=sqrt(b_v.normSquare()); ladanyi@98: ladanyi@98: if ((absa == 0.0) || (absb == 0.0)) ladanyi@98: { ladanyi@98: return old_arrow_pos; ladanyi@98: } ladanyi@98: else ladanyi@98: { ladanyi@98: //old vector from one node to the breakpoint - c ladanyi@98: xy c_v(old_arrow_pos.x-fix_node.x,old_arrow_pos.y-fix_node.y); ladanyi@98: ladanyi@98: //unit vector with the same direction to a_v ladanyi@98: xy a_v_u(a_v.x/absa,a_v.y/absa); ladanyi@98: ladanyi@98: //normal vector of unit vector with the same direction to a_v ladanyi@98: xy a_v_u_n(((-1)*a_v_u.y),a_v_u.x); ladanyi@98: ladanyi@98: //unit vector with the same direction to b_v ladanyi@98: xy b_v_u(b_v.x/absb,b_v.y/absb); ladanyi@98: ladanyi@98: //normal vector of unit vector with the same direction to b_v ladanyi@98: xy b_v_u_n(((-1)*b_v_u.y),b_v_u.x); ladanyi@98: ladanyi@98: //vector c in a_v_u and a_v_u_n co-ordinate system ladanyi@98: xy c_a(c_v*a_v_u,c_v*a_v_u_n); ladanyi@98: ladanyi@98: //new vector from one node to the breakpoint - d - we have to calculate this one ladanyi@98: xy d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n); ladanyi@98: ladanyi@98: return XY(d_v.x+fix_node.x,d_v.y+fix_node.y); ladanyi@98: } ladanyi@98: } ladanyi@98: } ladanyi@98: hegyi@30: bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e) hegyi@27: { hegyi@27: switch(e->type) ladanyi@63: { ladanyi@63: //move the new node ladanyi@63: case GDK_MOTION_NOTIFY: ladanyi@63: { ladanyi@63: GdkEvent * generated=new GdkEvent(); ladanyi@63: generated->motion.x=e->motion.x; ladanyi@63: generated->motion.y=e->motion.y; ladanyi@63: generated->type=GDK_MOTION_NOTIFY; ladanyi@63: moveEventHandler(generated); ladanyi@63: break; ladanyi@63: } hegyi@27: ladanyi@63: case GDK_BUTTON_RELEASE: hegyi@96: (mytab.mapstorage).modified = true; ladanyi@53: hegyi@27: isbutton=1; hegyi@27: hegyi@96: active_node=(mytab.mapstorage).graph.addNode(); hegyi@27: hegyi@27: //initiating values corresponding to new node in maps hegyi@27: hegyi@27: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@27: ladanyi@63: // update coordinates hegyi@96: (mytab.mapstorage).coords.set(active_node, xy(clicked_x, clicked_y)); ladanyi@63: ladanyi@63: // update all other maps ladanyi@63: for (std::map*>::const_iterator it = hegyi@96: (mytab.mapstorage).nodemap_storage.begin(); it != hegyi@96: (mytab.mapstorage).nodemap_storage.end(); ++it) ladanyi@63: { ladanyi@64: if ((it->first != "coordinates_x") && ladanyi@64: (it->first != "coordinates_y")) ladanyi@63: { ladanyi@63: (*(it->second))[active_node] = hegyi@96: (mytab.mapstorage).nodemap_default[it->first]; ladanyi@63: } ladanyi@63: } ladanyi@64: // increment the id map's default value hegyi@96: (mytab.mapstorage).nodemap_default["id"] += 1.0; ladanyi@63: ladanyi@53: nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, ladanyi@63: clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20); hegyi@27: active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]); ladanyi@63: *(nodesmap[active_node]) << ladanyi@63: Gnome::Canvas::Properties::fill_color("blue"); ladanyi@63: *(nodesmap[active_node]) << ladanyi@63: Gnome::Canvas::Properties::outline_color("black"); ladanyi@63: active_item->raise_to_top(); ladanyi@63: hegyi@27: (nodesmap[active_node])->show(); hegyi@28: ladanyi@53: nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph, ladanyi@63: clicked_x+node_property_defaults[N_RADIUS]+5, ladanyi@63: clicked_y+node_property_defaults[N_RADIUS]+5, ""); hegyi@28: nodetextmap[active_node]->property_fill_color().set_value("darkblue"); ladanyi@63: nodetextmap[active_node]->raise_to_top(); hegyi@28: hegyi@94: // mapwin.updateNode(active_node); hegyi@94: propertyUpdate(active_node); hegyi@28: hegyi@27: isbutton=0; hegyi@31: target_item=NULL; hegyi@27: active_item=NULL; hegyi@27: active_node=INVALID; hegyi@27: break; hegyi@27: default: hegyi@27: break; ladanyi@63: } hegyi@27: return false; hegyi@27: } hegyi@27: hegyi@30: bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e) hegyi@27: { hegyi@27: switch(e->type) ladanyi@63: { hegyi@27: case GDK_BUTTON_PRESS: hegyi@27: //in edge creation right button has special meaning hegyi@27: if(e->button.button!=3) ladanyi@63: { ladanyi@63: //there is not yet selected node ladanyi@63: if(active_node==INVALID) ladanyi@63: { ladanyi@63: //we mark the location of the event to be able to calculate parameters of dragging hegyi@31: ladanyi@63: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@31: ladanyi@63: active_item=(get_item_at(clicked_x, clicked_y)); ladanyi@63: active_node=INVALID; hegyi@96: for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) ladanyi@63: { ladanyi@63: if(nodesmap[i]==active_item) ladanyi@63: { ladanyi@63: active_node=i; ladanyi@63: } ladanyi@63: } ladanyi@63: //the clicked item is really a node ladanyi@63: if(active_node!=INVALID) ladanyi@63: { ladanyi@63: *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); ladanyi@63: isbutton=1; ladanyi@63: } ladanyi@63: //clicked item was not a node. It could be e.g. edge. ladanyi@63: else ladanyi@63: { ladanyi@63: active_item=NULL; ladanyi@63: } ladanyi@63: } ladanyi@63: //we only have to do sg. if the mouse button ladanyi@63: // is pressed already once AND the click was ladanyi@63: // on a node that was found in the set of ladanyi@63: //nodes, and now we only search for the second ladanyi@63: //node ladanyi@63: else ladanyi@63: { ladanyi@63: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); ladanyi@63: target_item=(get_item_at(clicked_x, clicked_y)); ladanyi@63: Node target_node=INVALID; hegyi@96: for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) ladanyi@63: { ladanyi@63: if(nodesmap[i]==target_item) ladanyi@63: { ladanyi@63: target_node=i; ladanyi@63: } ladanyi@63: } ladanyi@63: //the clicked item is a node, the edge can be drawn ladanyi@63: if(target_node!=INVALID) ladanyi@63: { ladanyi@63: if(target_node!=active_node) ladanyi@63: { hegyi@96: (mytab.mapstorage).modified = true; ladanyi@53: ladanyi@63: *(nodesmap[target_node]) << ladanyi@63: Gnome::Canvas::Properties::fill_color("red"); hegyi@27: ladanyi@63: //creating new edge hegyi@96: active_edge=(mytab.mapstorage).graph.addEdge(active_node, ladanyi@63: target_node); hegyi@27: ladanyi@64: // update maps ladanyi@63: for (std::map*>::const_iterator it = hegyi@96: (mytab.mapstorage).edgemap_storage.begin(); it != hegyi@96: (mytab.mapstorage).edgemap_storage.end(); ++it) ladanyi@63: { ladanyi@64: (*(it->second))[active_edge] = hegyi@96: (mytab.mapstorage).edgemap_default[it->first]; ladanyi@63: } ladanyi@64: // increment the id map's default value hegyi@96: (mytab.mapstorage).edgemap_default["id"] += 1.0; hegyi@27: ladanyi@63: //calculating coordinates of new edge ladanyi@63: Gnome::Canvas::Points coos; ladanyi@63: double x1, x2, y1, y2; hegyi@27: ladanyi@63: active_item->get_bounds(x1, y1, x2, y2); ladanyi@63: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@27: ladanyi@63: target_item->get_bounds(x1, y1, x2, y2); ladanyi@63: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@27: ladanyi@63: //drawing new edge ladanyi@98: edgesmap[active_edge]=new BrokenEdge(displayed_graph, active_edge, ladanyi@63: *this); ladanyi@63: *(edgesmap[active_edge]) << ladanyi@63: Gnome::Canvas::Properties::fill_color("green"); ladanyi@63: edgesmap[active_edge]->property_width_pixels().set_value(10); hegyi@27: ladanyi@63: edgesmap[active_edge]->lower_to_bottom(); hegyi@28: ladanyi@63: //initializing edge-text as well, to empty string ladanyi@98: XY text_pos=mytab.mapstorage.arrow_pos[active_edge]; ladanyi@98: text_pos+=(XY(10,10)); ladanyi@63: ladanyi@63: edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, ladanyi@63: text_pos.x, text_pos.y, ""); ladanyi@63: edgetextmap[active_edge]->property_fill_color().set_value( ladanyi@63: "darkgreen"); ladanyi@63: edgetextmap[active_edge]->raise_to_top(); ladanyi@63: ladanyi@63: //updating its properties hegyi@94: // mapwin.updateEdge(active_edge); hegyi@94: propertyUpdate(active_edge); ladanyi@63: } ladanyi@63: else ladanyi@63: { ladanyi@63: target_node=INVALID; ladanyi@63: std::cerr << "Loop edge is not yet implemented!" << std::endl; ladanyi@63: } ladanyi@63: } ladanyi@63: //clicked item was not a node. it could be an e.g. edge. we do not ladanyi@63: //deal with it furthermore. ladanyi@63: else ladanyi@63: { ladanyi@63: target_item=NULL; ladanyi@63: } ladanyi@63: } ladanyi@63: } hegyi@27: break; hegyi@27: case GDK_BUTTON_RELEASE: hegyi@27: isbutton=0; hegyi@27: //we clear settings in two cases hegyi@27: //1: the edge is ready (target_item has valid value) hegyi@27: //2: the edge creation is cancelled with right button hegyi@27: if((target_item)||(e->button.button==3)) ladanyi@63: { ladanyi@63: if(active_item) ladanyi@63: { ladanyi@63: *active_item << Gnome::Canvas::Properties::fill_color("blue"); ladanyi@63: active_item=NULL; ladanyi@63: } ladanyi@63: if(target_item) ladanyi@63: { ladanyi@63: *target_item << Gnome::Canvas::Properties::fill_color("blue"); ladanyi@63: target_item=NULL; ladanyi@63: } ladanyi@63: active_node=INVALID; ladanyi@63: active_edge=INVALID; ladanyi@63: } hegyi@27: break; hegyi@27: default: hegyi@27: break; ladanyi@63: } hegyi@27: return false; hegyi@27: } hegyi@27: hegyi@30: bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e) hegyi@27: { hegyi@27: switch(e->type) hegyi@27: { hegyi@27: case GDK_BUTTON_PRESS: hegyi@43: //finding the clicked items hegyi@31: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@31: active_item=(get_item_at(clicked_x, clicked_y)); hegyi@27: active_node=INVALID; hegyi@27: active_edge=INVALID; hegyi@43: //was it a node? hegyi@96: for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) hegyi@27: { hegyi@27: if(nodesmap[i]==active_item) hegyi@27: { hegyi@27: active_node=i; hegyi@27: } hegyi@27: } hegyi@43: //or was it an edge? hegyi@27: if(active_node==INVALID) hegyi@27: { hegyi@96: for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) hegyi@27: { hegyi@27: if(edgesmap[i]==active_item) hegyi@27: { hegyi@27: active_edge=i; hegyi@27: } hegyi@27: } hegyi@27: } hegyi@43: hegyi@43: //recolor activated item hegyi@31: if(active_item) hegyi@31: { hegyi@31: *active_item << Gnome::Canvas::Properties::fill_color("red"); hegyi@31: } hegyi@27: break; hegyi@27: hegyi@27: case GDK_BUTTON_RELEASE: hegyi@31: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@31: if(active_item) hegyi@27: { hegyi@43: //the cursor was not moved since pressing it hegyi@31: if( active_item == ( get_item_at (clicked_x, clicked_y) ) ) hegyi@27: { hegyi@43: //a node was found hegyi@31: if(active_node!=INVALID) hegyi@31: { hegyi@96: (mytab.mapstorage).modified = true; hegyi@27: hegyi@31: std::set edges_to_delete; hegyi@27: hegyi@96: for(OutEdgeIt e((mytab.mapstorage).graph,active_node);e!=INVALID;++e) hegyi@31: { hegyi@31: edges_to_delete.insert(e); hegyi@31: } alpar@69: hegyi@96: for(InEdgeIt e((mytab.mapstorage).graph,active_node);e!=INVALID;++e) hegyi@31: { hegyi@31: edges_to_delete.insert(e); hegyi@31: } alpar@69: hegyi@31: //deleting collected edges alpar@69: for(std::set::iterator alpar@69: edge_set_it=edges_to_delete.begin(); alpar@69: edge_set_it!=edges_to_delete.end(); alpar@69: ++edge_set_it) hegyi@31: { hegyi@31: deleteItem(*edge_set_it); hegyi@31: } hegyi@31: deleteItem(active_node); hegyi@31: } hegyi@31: //a simple edge was chosen hegyi@31: else hegyi@27: { hegyi@31: deleteItem(active_edge); hegyi@27: } hegyi@27: } hegyi@31: //pointer was moved, deletion is cancelled hegyi@27: else hegyi@27: { hegyi@31: if(active_node!=INVALID) hegyi@31: { hegyi@31: *active_item << Gnome::Canvas::Properties::fill_color("blue"); hegyi@31: } hegyi@31: else hegyi@31: { hegyi@31: *active_item << Gnome::Canvas::Properties::fill_color("green"); hegyi@31: } hegyi@27: } hegyi@27: } hegyi@27: //reseting datas hegyi@27: active_item=NULL; hegyi@27: active_edge=INVALID; hegyi@27: active_node=INVALID; hegyi@27: break; hegyi@27: hegyi@27: case GDK_MOTION_NOTIFY: hegyi@27: break; hegyi@27: hegyi@27: default: hegyi@27: break; hegyi@27: } hegyi@31: return false; hegyi@27: } hegyi@27: hegyi@32: bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e) hegyi@32: { hegyi@48: if(actual_tool==EDGE_MAP_EDIT) ladanyi@66: { ladanyi@66: switch(e->type) hegyi@32: { ladanyi@66: case GDK_BUTTON_PRESS: ladanyi@66: { ladanyi@66: //for determine, whether it was an edge ladanyi@66: Edge clicked_edge=INVALID; hegyi@43: ladanyi@66: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); ladanyi@66: active_item=(get_item_at(clicked_x, clicked_y)); hegyi@48: ladanyi@66: //find the activated item between texts hegyi@96: for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) ladanyi@66: { ladanyi@66: //at the same time only one can be active ladanyi@66: if(edgetextmap[i]==active_item) ladanyi@66: { ladanyi@66: clicked_edge=i; ladanyi@66: } ladanyi@66: } ladanyi@65: ladanyi@66: //if it was not between texts, search for it between edges ladanyi@66: if(clicked_edge==INVALID) ladanyi@66: { hegyi@96: for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) ladanyi@66: { ladanyi@66: //at the same time only one can be active ladanyi@66: if((edgesmap[i]==active_item)||(edgetextmap[i]==active_item)) ladanyi@66: { ladanyi@66: clicked_edge=i; ladanyi@66: } ladanyi@66: } ladanyi@66: } ladanyi@66: ladanyi@66: //if it was really an edge... ladanyi@66: if(clicked_edge!=INVALID) ladanyi@66: { ladanyi@66: // the id map is not editable ladanyi@66: if (edgemap_to_edit == "id") return 0; hegyi@48: ladanyi@66: //and there is activated map ladanyi@66: if(edgetextmap[clicked_edge]->property_text().get_value()!="") ladanyi@66: { ladanyi@66: //activate the general variable for it ladanyi@66: active_edge=clicked_edge; hegyi@48: ladanyi@66: //create a dialog hegyi@96: Gtk::Dialog dialog("Edit value", true); ladanyi@66: dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); ladanyi@66: dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); ladanyi@66: Gtk::VBox* vbox = dialog.get_vbox(); ladanyi@92: Gtk::SpinButton spin(0.0, 4); ladanyi@92: spin.set_increments(1.0, 10.0); ladanyi@92: spin.set_range(-1000000.0, 1000000.0); ladanyi@66: spin.set_numeric(true); ladanyi@66: vbox->add(spin); ladanyi@66: spin.show(); ladanyi@66: switch (dialog.run()) ladanyi@66: { ladanyi@66: case Gtk::RESPONSE_NONE: ladanyi@66: case Gtk::RESPONSE_CANCEL: ladanyi@66: break; ladanyi@66: case Gtk::RESPONSE_ACCEPT: ladanyi@66: double new_value = spin.get_value(); hegyi@96: (*(mytab.mapstorage).edgemap_storage[edgemap_to_edit])[active_edge] = ladanyi@66: new_value; ladanyi@66: std::ostringstream ostr; ladanyi@66: ostr << new_value; ladanyi@66: edgetextmap[active_edge]->property_text().set_value( ladanyi@66: ostr.str()); ladanyi@66: //mapwin.updateEdge(active_edge); hegyi@94: // mapwin.updateEdge(Edge(INVALID)); hegyi@94: propertyUpdate(Edge(INVALID)); ladanyi@66: } ladanyi@66: } ladanyi@66: } ladanyi@66: break; ladanyi@66: } ladanyi@66: default: ladanyi@66: break; hegyi@32: } ladanyi@66: } hegyi@32: return false; hegyi@32: } hegyi@32: hegyi@32: bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e) hegyi@32: { hegyi@48: if(actual_tool==NODE_MAP_EDIT) ladanyi@66: { ladanyi@66: switch(e->type) hegyi@35: { ladanyi@66: case GDK_BUTTON_PRESS: ladanyi@66: { ladanyi@66: //for determine, whether it was a node ladanyi@66: Node clicked_node=INVALID; hegyi@43: ladanyi@66: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); ladanyi@66: active_item=(get_item_at(clicked_x, clicked_y)); hegyi@43: ladanyi@66: //find the activated item between texts hegyi@96: for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) ladanyi@66: { ladanyi@66: //at the same time only one can be active ladanyi@66: if(nodetextmap[i]==active_item) ladanyi@66: { ladanyi@66: clicked_node=i; ladanyi@66: } ladanyi@66: } hegyi@48: ladanyi@66: //if there was not, search for it between nodes ladanyi@66: if(clicked_node==INVALID) ladanyi@66: { hegyi@96: for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) ladanyi@66: { ladanyi@66: //at the same time only one can be active ladanyi@66: if(nodesmap[i]==active_item) ladanyi@66: { ladanyi@66: clicked_node=i; ladanyi@66: } ladanyi@66: } ladanyi@66: } hegyi@48: ladanyi@66: //if it was really a node... ladanyi@66: if(clicked_node!=INVALID) ladanyi@66: { ladanyi@66: // the id map is not editable ladanyi@66: if (nodemap_to_edit == "id") return 0; hegyi@48: ladanyi@66: //and there is activated map ladanyi@66: if(nodetextmap[clicked_node]->property_text().get_value()!="") ladanyi@66: { ladanyi@66: //activate the general variable for it ladanyi@66: active_node=clicked_node; hegyi@48: ladanyi@66: //create a dialog hegyi@96: Gtk::Dialog dialog("Edit value", true); ladanyi@66: dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); ladanyi@66: dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); ladanyi@66: Gtk::VBox* vbox = dialog.get_vbox(); ladanyi@92: Gtk::SpinButton spin(0.0, 4); ladanyi@92: spin.set_increments(1.0, 10.0); ladanyi@92: spin.set_range(-1000000.0, 1000000.0); ladanyi@66: spin.set_numeric(true); ladanyi@66: vbox->add(spin); ladanyi@66: spin.show(); ladanyi@66: switch (dialog.run()) ladanyi@66: { ladanyi@66: case Gtk::RESPONSE_NONE: ladanyi@66: case Gtk::RESPONSE_CANCEL: ladanyi@66: break; ladanyi@66: case Gtk::RESPONSE_ACCEPT: ladanyi@66: double new_value = spin.get_value(); hegyi@96: (*(mytab.mapstorage).nodemap_storage[nodemap_to_edit])[active_node] = ladanyi@66: new_value; ladanyi@66: std::ostringstream ostr; ladanyi@66: ostr << new_value; ladanyi@66: nodetextmap[active_node]->property_text().set_value( ladanyi@66: ostr.str()); ladanyi@66: //mapwin.updateNode(active_node); hegyi@94: // mapwin.updateNode(Node(INVALID)); hegyi@94: propertyUpdate(Node(INVALID)); ladanyi@66: } ladanyi@66: } ladanyi@66: } ladanyi@66: break; ladanyi@66: } ladanyi@66: default: ladanyi@66: break; hegyi@35: } ladanyi@66: } hegyi@35: return false; hegyi@32: } hegyi@32: alpar@62: void GraphDisplayerCanvas::deleteItem(Node node_to_delete) hegyi@27: { hegyi@28: delete(nodetextmap[node_to_delete]); hegyi@27: delete(nodesmap[node_to_delete]); hegyi@96: (mytab.mapstorage).graph.erase(node_to_delete); hegyi@27: } hegyi@27: alpar@62: void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete) hegyi@27: { hegyi@28: delete(edgetextmap[edge_to_delete]); hegyi@27: delete(edgesmap[edge_to_delete]); hegyi@96: (mytab.mapstorage).graph.erase(edge_to_delete); hegyi@27: } hegyi@27: hegyi@30: void GraphDisplayerCanvas::textReposition(xy new_place) hegyi@27: { hegyi@27: new_place+=(xy(10,10)); hegyi@35: edgetextmap[forming_edge]->property_x().set_value(new_place.x); hegyi@35: edgetextmap[forming_edge]->property_y().set_value(new_place.y); hegyi@27: } hegyi@27: hegyi@30: void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on) hegyi@27: { hegyi@27: if(on) hegyi@27: { hegyi@35: if(forming_edge!=INVALID) hegyi@27: { hegyi@48: std::cerr << "ERROR!!!! Valid edge found!" << std::endl; hegyi@27: } hegyi@27: else hegyi@27: { hegyi@96: for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) hegyi@27: { hegyi@27: if(edgesmap[i]==active_bre) hegyi@27: { hegyi@35: forming_edge=i; hegyi@27: } hegyi@27: } hegyi@27: } hegyi@27: } hegyi@27: else hegyi@27: { hegyi@35: if(forming_edge!=INVALID) hegyi@27: { hegyi@35: forming_edge=INVALID; hegyi@27: } hegyi@27: else hegyi@27: { hegyi@48: std::cerr << "ERROR!!!! Invalid edge found!" << std::endl; hegyi@27: } hegyi@27: } hegyi@27: }