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