alpar@174: /* -*- C++ -*- alpar@174: * alpar@174: * This file is a part of LEMON, a generic C++ optimization library alpar@174: * alpar@174: * Copyright (C) 2003-2006 alpar@174: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@174: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@174: * alpar@174: * Permission to use, modify and distribute this software is granted alpar@174: * provided that this copyright notice appears in all copies. For alpar@174: * precise terms see the accompanying LICENSE file. alpar@174: * alpar@174: * This software is provided "AS IS" with no warranty of any kind, alpar@174: * express or implied, and with no claim as to its suitability for any alpar@174: * purpose. alpar@174: * alpar@174: */ alpar@174: 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@149: case 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@149: case MAP_EDIT: hegyi@34: grab_focus(); hegyi@149: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), 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@187: bool GraphDisplayerCanvas::scrollEventHandler(GdkEvent* e) hegyi@187: { hegyi@187: bool handled=false; hegyi@187: if(e->type==GDK_SCROLL) hegyi@187: { hegyi@187: hegyi@187: //pointer shows this win point before zoom hegyi@187: XY win_coord(((GdkEventScroll*)e)->x, ((GdkEventScroll*)e)->y); hegyi@187: hegyi@187: //the original scroll settings hegyi@187: int scroll_offset_x, scroll_offset_y; hegyi@187: get_scroll_offsets(scroll_offset_x, scroll_offset_y); hegyi@187: hegyi@187: //pointer shows this canvas point before zoom hegyi@187: XY canvas_coord; hegyi@187: window_to_world(win_coord.x, win_coord.y, canvas_coord.x, canvas_coord.y); hegyi@187: hegyi@187: if(((GdkEventScroll*)e)->direction) //IN hegyi@187: { hegyi@187: zoomIn(); hegyi@187: } hegyi@187: else hegyi@187: { hegyi@187: zoomOut(); hegyi@187: } hegyi@187: hegyi@187: //pointer shows this window point after zoom hegyi@187: XY post_win_coord; hegyi@187: world_to_window(canvas_coord.x, canvas_coord.y, post_win_coord.x, post_win_coord.y); hegyi@187: hegyi@187: //we have to add the difference between new and old window point to original scroll offset hegyi@187: scroll_to(scroll_offset_x+(int)(post_win_coord.x-win_coord.x),scroll_offset_y+(int)(post_win_coord.y-win_coord.y)); hegyi@187: hegyi@187: //no other eventhandler is needed hegyi@187: handled=true; hegyi@187: } hegyi@187: return handled; hegyi@187: } hegyi@187: hegyi@30: bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e) hegyi@27: { ladanyi@70: static Gnome::Canvas::Text *coord_text = 0; hegyi@27: switch(e->type) hegyi@160: { 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@160: 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@148: isbutton=e->button.button; hegyi@27: break; hegyi@27: case GDK_BUTTON_RELEASE: ladanyi@70: if (coord_text) hegyi@160: { hegyi@160: delete coord_text; hegyi@160: coord_text = 0; hegyi@160: } 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@160: { hegyi@160: (mytab.mapstorage).modified = true; hegyi@160: hegyi@160: //new coordinates will be the old values, hegyi@160: //because the item will be moved to the hegyi@160: //new coordinate therefore the new movement hegyi@160: //has to be calculated from here hegyi@160: hegyi@160: double new_x, new_y; hegyi@160: hegyi@160: window_to_world (e->motion.x, e->motion.y, new_x, new_y); hegyi@160: hegyi@160: double dx=new_x-clicked_x; hegyi@160: double dy=new_y-clicked_y; hegyi@160: hegyi@160: moveNode(dx, dy); ladanyi@70: hegyi@160: clicked_x=new_x; hegyi@160: clicked_y=new_y; hegyi@27: hegyi@160: // reposition the coordinates text hegyi@160: std::ostringstream ostr; hegyi@160: ostr << "(" << hegyi@160: (mytab.mapstorage).coords[active_node].x << ", " << hegyi@160: (mytab.mapstorage).coords[active_node].y << ")"; hegyi@160: double radius = hegyi@160: (nodesmap[active_node]->property_x2().get_value() - hegyi@160: nodesmap[active_node]->property_x1().get_value()) / 2.0; hegyi@160: if (coord_text) hegyi@160: { hegyi@160: coord_text->property_text().set_value(ostr.str()); hegyi@160: coord_text->property_x().set_value((mytab.mapstorage).coords[active_node].x + hegyi@160: radius); hegyi@160: coord_text->property_y().set_value((mytab.mapstorage).coords[active_node].y - hegyi@160: radius); hegyi@160: } hegyi@160: else hegyi@160: { hegyi@160: coord_text = new Gnome::Canvas::Text( hegyi@160: displayed_graph, hegyi@160: (mytab.mapstorage).coords[active_node].x + radius, hegyi@160: (mytab.mapstorage).coords[active_node].y - radius, hegyi@160: ostr.str()); hegyi@160: coord_text->property_fill_color().set_value("black"); hegyi@160: coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST); hegyi@160: } hegyi@31: hegyi@31: hegyi@160: } hegyi@160: default: break; hegyi@160: } hegyi@27: hegyi@160: return false; hegyi@27: } hegyi@27: hegyi@148: XY GraphDisplayerCanvas::calcArrowPos(XY moved_node_1, XY moved_node_2, XY fix_node, XY old_arrow_pos, int move_code) ladanyi@98: { hegyi@148: switch(move_code) hegyi@148: { hegyi@148: case 1: hegyi@148: return XY((moved_node_2.x + fix_node.x) / 2.0, (moved_node_2.y + fix_node.y) / 2.0); hegyi@148: break; hegyi@148: case 2: hegyi@148: return old_arrow_pos; hegyi@148: break; hegyi@148: case 3: hegyi@148: { hegyi@148: ////////////////////////////////////////////////////////////////////////////////////////////////////// hegyi@148: /////////// keeps shape-with scalar multiplication - version 2. hegyi@148: ////////////////////////////////////////////////////////////////////////////////////////////////////// ladanyi@98: hegyi@148: //old vector from one to the other node - a hegyi@150: XY a_v(moved_node_1.x-fix_node.x,moved_node_1.y-fix_node.y); hegyi@148: //new vector from one to the other node - b hegyi@150: XY b_v(moved_node_2.x-fix_node.x,moved_node_2.y-fix_node.y); ladanyi@98: hegyi@148: double absa=sqrt(a_v.normSquare()); hegyi@148: double absb=sqrt(b_v.normSquare()); ladanyi@98: hegyi@148: if ((absa == 0.0) || (absb == 0.0)) hegyi@148: { hegyi@148: return old_arrow_pos; hegyi@148: } hegyi@148: else hegyi@148: { hegyi@148: //old vector from one node to the breakpoint - c hegyi@150: XY c_v(old_arrow_pos.x-fix_node.x,old_arrow_pos.y-fix_node.y); hegyi@148: hegyi@148: //unit vector with the same direction to a_v hegyi@150: XY a_v_u(a_v.x/absa,a_v.y/absa); hegyi@148: hegyi@148: //normal vector of unit vector with the same direction to a_v hegyi@150: XY a_v_u_n(((-1)*a_v_u.y),a_v_u.x); hegyi@148: hegyi@148: //unit vector with the same direction to b_v hegyi@150: XY b_v_u(b_v.x/absb,b_v.y/absb); hegyi@148: hegyi@148: //normal vector of unit vector with the same direction to b_v hegyi@150: XY b_v_u_n(((-1)*b_v_u.y),b_v_u.x); hegyi@148: hegyi@148: //vector c in a_v_u and a_v_u_n co-ordinate system hegyi@150: XY c_a(c_v*a_v_u,c_v*a_v_u_n); hegyi@148: hegyi@148: //new vector from one node to the breakpoint - d - we have to calculate this one hegyi@150: XY d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n); hegyi@148: hegyi@148: return XY(d_v.x+fix_node.x,d_v.y+fix_node.y); hegyi@148: } hegyi@148: break; hegyi@148: } hegyi@148: default: hegyi@148: break; ladanyi@98: } hegyi@148: } 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@178: is_drawn=true; hegyi@178: 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@150: (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 ladanyi@134: (mytab.mapstorage).nodemap_default["label"] += 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@151: (mytab.mapstorage).modified = true; ladanyi@151: ladanyi@151: *(nodesmap[target_node]) << ladanyi@151: Gnome::Canvas::Properties::fill_color("red"); ladanyi@151: ladanyi@151: //creating new edge ladanyi@151: active_edge=(mytab.mapstorage).graph.addEdge(active_node, ladanyi@151: target_node); ladanyi@151: ladanyi@151: // update maps ladanyi@151: for (std::map*>::const_iterator it = ladanyi@151: (mytab.mapstorage).edgemap_storage.begin(); it != ladanyi@151: (mytab.mapstorage).edgemap_storage.end(); ++it) ladanyi@151: { ladanyi@151: (*(it->second))[active_edge] = ladanyi@151: (mytab.mapstorage).edgemap_default[it->first]; ladanyi@151: } ladanyi@151: // increment the id map's default value ladanyi@151: (mytab.mapstorage).edgemap_default["label"] += 1.0; ladanyi@151: ladanyi@63: if(target_node!=active_node) ladanyi@63: { ladanyi@113: // set the coordinates of the arrow on the new edge ladanyi@113: MapStorage& ms = mytab.mapstorage; ladanyi@113: ms.arrow_pos.set(active_edge, ladanyi@113: (ms.coords[ms.graph.source(active_edge)] + ladanyi@113: ms.coords[ms.graph.target(active_edge)])/ 2.0); ladanyi@113: ladanyi@63: //drawing new edge ladanyi@98: edgesmap[active_edge]=new BrokenEdge(displayed_graph, active_edge, ladanyi@63: *this); ladanyi@63: } ladanyi@63: else ladanyi@63: { ladanyi@151: // set the coordinates of the arrow on the new edge ladanyi@151: MapStorage& ms = mytab.mapstorage; ladanyi@151: ms.arrow_pos.set(active_edge, ladanyi@151: (ms.coords[ms.graph.source(active_edge)] + ladanyi@151: XY(0.0, 80.0))); ladanyi@151: ladanyi@151: //drawing new edge ladanyi@151: edgesmap[active_edge]=new LoopEdge(displayed_graph, active_edge, ladanyi@151: *this); ladanyi@63: } ladanyi@151: ladanyi@151: //initializing edge-text as well, to empty string ladanyi@151: XY text_pos=mytab.mapstorage.arrow_pos[active_edge]; ladanyi@151: text_pos+=(XY(10,10)); ladanyi@151: ladanyi@151: edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, ladanyi@151: text_pos.x, text_pos.y, ""); ladanyi@151: edgetextmap[active_edge]->property_fill_color().set_value( ladanyi@151: "darkgreen"); ladanyi@151: edgetextmap[active_edge]->raise_to_top(); ladanyi@151: ladanyi@151: propertyUpdate(active_edge); 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: { hegyi@179: propertyUpdate(active_node,N_COLOR); ladanyi@63: active_item=NULL; ladanyi@63: } ladanyi@63: if(target_item) ladanyi@63: { hegyi@179: propertyUpdate((mytab.mapstorage).graph.target(active_edge),N_COLOR); 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: { ladanyi@153: if(edgesmap[i]->getLine()==active_item) hegyi@27: { hegyi@27: active_edge=i; hegyi@27: } hegyi@27: } hegyi@27: } hegyi@43: ladanyi@129: // return if the clicked object is neither an edge nor a node ladanyi@129: if (active_edge == INVALID) return false; ladanyi@129: 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 ladanyi@129: else if (active_edge != INVALID) 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: } ladanyi@129: else if (active_edge != INVALID) 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@149: bool GraphDisplayerCanvas::mapEditEventHandler(GdkEvent* e) hegyi@32: { hegyi@149: if(actual_tool==MAP_EDIT) hegyi@32: { hegyi@149: switch(e->type) hegyi@149: { hegyi@149: case GDK_BUTTON_PRESS: hegyi@149: { hegyi@149: //for determine, whether it was an edge hegyi@149: Edge clicked_edge=INVALID; hegyi@149: //for determine, whether it was a node hegyi@149: Node clicked_node=INVALID; hegyi@43: hegyi@149: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@149: active_item=(get_item_at(clicked_x, clicked_y)); hegyi@48: hegyi@149: //find the activated item between text of nodes hegyi@149: for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) hegyi@149: { hegyi@149: //at the same time only one can be active hegyi@149: if(nodetextmap[i]==active_item) hegyi@149: { hegyi@149: clicked_node=i; hegyi@149: } hegyi@149: } ladanyi@65: hegyi@149: //if there was not, search for it between nodes hegyi@149: if(clicked_node==INVALID) hegyi@149: { hegyi@149: for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) hegyi@149: { hegyi@149: //at the same time only one can be active hegyi@149: if(nodesmap[i]==active_item) hegyi@149: { hegyi@149: clicked_node=i; hegyi@149: } hegyi@149: } hegyi@149: } hegyi@48: hegyi@149: if(clicked_node==INVALID) hegyi@149: { hegyi@149: //find the activated item between texts hegyi@149: for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) hegyi@149: { hegyi@149: //at the same time only one can be active hegyi@149: if(edgetextmap[i]==active_item) hegyi@149: { hegyi@149: clicked_edge=i; hegyi@149: } hegyi@149: } hegyi@48: hegyi@149: //if it was not between texts, search for it between edges hegyi@149: if(clicked_edge==INVALID) hegyi@149: { hegyi@149: for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) hegyi@149: { hegyi@149: //at the same time only one can be active hegyi@149: if((edgesmap[i]->getLine())==active_item) hegyi@149: { hegyi@149: clicked_edge=i; hegyi@149: } hegyi@149: } hegyi@149: } hegyi@149: } hegyi@149: hegyi@149: //if it was really a node... hegyi@149: if(clicked_node!=INVALID) hegyi@149: { hegyi@149: // the id map is not editable hegyi@149: if (nodemap_to_edit == "label") return 0; hegyi@149: hegyi@149: //and there is activated map hegyi@149: if(nodetextmap[clicked_node]->property_text().get_value()!="") hegyi@149: { hegyi@149: //activate the general variable for it hegyi@149: active_node=clicked_node; hegyi@149: hegyi@149: //create a dialog hegyi@149: Gtk::Dialog dialog("Edit value", true); hegyi@149: dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); hegyi@149: dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); hegyi@149: Gtk::VBox* vbox = dialog.get_vbox(); hegyi@149: Gtk::SpinButton spin(0.0, 4); hegyi@149: spin.set_increments(1.0, 10.0); hegyi@149: spin.set_range(-1000000.0, 1000000.0); hegyi@149: spin.set_numeric(true); hegyi@149: spin.set_value(atof(nodetextmap[active_node]->property_text().get_value().c_str())); hegyi@149: vbox->add(spin); hegyi@149: spin.show(); hegyi@149: switch (dialog.run()) hegyi@149: { hegyi@149: case Gtk::RESPONSE_NONE: hegyi@149: case Gtk::RESPONSE_CANCEL: hegyi@149: break; hegyi@149: case Gtk::RESPONSE_ACCEPT: hegyi@149: double new_value = spin.get_value(); hegyi@149: (*(mytab.mapstorage).nodemap_storage[nodemap_to_edit])[active_node] = hegyi@149: new_value; hegyi@149: std::ostringstream ostr; hegyi@149: ostr << new_value; hegyi@149: nodetextmap[active_node]->property_text().set_value(ostr.str()); hegyi@149: //mapwin.updateNode(active_node); hegyi@149: //mapwin.updateNode(Node(INVALID)); hegyi@149: propertyUpdate(Node(INVALID)); hegyi@149: } hegyi@149: } hegyi@149: } hegyi@149: else hegyi@149: //if it was really an edge... hegyi@149: if(clicked_edge!=INVALID) hegyi@149: { hegyi@149: // the id map is not editable hegyi@149: if (edgemap_to_edit == "label") return 0; hegyi@149: hegyi@149: //and there is activated map hegyi@149: if(edgetextmap[clicked_edge]->property_text().get_value()!="") hegyi@149: { hegyi@149: //activate the general variable for it hegyi@149: active_edge=clicked_edge; hegyi@149: hegyi@149: //create a dialog hegyi@149: Gtk::Dialog dialog("Edit value", true); hegyi@149: dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); hegyi@149: dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); hegyi@149: Gtk::VBox* vbox = dialog.get_vbox(); hegyi@149: Gtk::SpinButton spin(0.0, 4); hegyi@149: spin.set_increments(1.0, 10.0); hegyi@149: spin.set_range(-1000000.0, 1000000.0); hegyi@149: spin.set_numeric(true); hegyi@149: spin.set_value(atof(edgetextmap[active_edge]->property_text().get_value().c_str())); hegyi@149: vbox->add(spin); hegyi@149: spin.show(); hegyi@149: switch (dialog.run()) hegyi@149: { hegyi@149: case Gtk::RESPONSE_NONE: hegyi@149: case Gtk::RESPONSE_CANCEL: hegyi@149: break; hegyi@149: case Gtk::RESPONSE_ACCEPT: hegyi@149: double new_value = spin.get_value(); hegyi@149: (*(mytab.mapstorage).edgemap_storage[edgemap_to_edit])[active_edge] = hegyi@149: new_value; hegyi@149: std::ostringstream ostr; hegyi@149: ostr << new_value; hegyi@149: edgetextmap[active_edge]->property_text().set_value( hegyi@149: ostr.str()); hegyi@149: //mapwin.updateEdge(active_edge); hegyi@149: // mapwin.updateEdge(Edge(INVALID)); hegyi@149: propertyUpdate(Edge(INVALID)); hegyi@149: } hegyi@149: } hegyi@149: } hegyi@149: break; hegyi@149: } hegyi@149: default: hegyi@149: break; hegyi@149: } hegyi@32: } 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@150: void GraphDisplayerCanvas::textReposition(XY new_place) hegyi@27: { hegyi@150: 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: ladanyi@147: void GraphDisplayerCanvas::toggleEdgeActivity(EdgeBase* active_bre, bool on) hegyi@27: { hegyi@27: if(on) ladanyi@147: { ladanyi@147: if(forming_edge!=INVALID) hegyi@27: { ladanyi@147: std::cerr << "ERROR!!!! Valid edge found!" << std::endl; hegyi@27: } ladanyi@147: else ladanyi@147: { ladanyi@147: for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i) ladanyi@147: { ladanyi@147: if(edgesmap[i]==active_bre) ladanyi@147: { ladanyi@147: forming_edge=i; ladanyi@147: } ladanyi@147: } ladanyi@147: } ladanyi@147: } hegyi@27: else hegyi@27: { hegyi@160: if(forming_edge!=INVALID) hegyi@160: { hegyi@160: forming_edge=INVALID; hegyi@160: } hegyi@160: else hegyi@160: { hegyi@160: std::cerr << "ERROR!!!! Invalid edge found!" << std::endl; hegyi@160: } hegyi@27: } hegyi@160: } hegyi@160: hegyi@160: void GraphDisplayerCanvas::moveNode(double dx, double dy, Gnome::Canvas::Item * item, Node node) hegyi@160: { hegyi@160: Gnome::Canvas::Item * moved_item=item; hegyi@160: Node moved_node=node; hegyi@160: hegyi@160: if(item==NULL && node==INVALID) ladanyi@147: { hegyi@160: moved_item=active_item; hegyi@160: moved_node=active_node; ladanyi@147: } hegyi@160: else hegyi@160: { hegyi@160: isbutton=1; hegyi@160: } hegyi@160: hegyi@160: //repositioning node and its text hegyi@160: moved_item->move(dx, dy); hegyi@160: nodetextmap[moved_node]->move(dx, dy); hegyi@160: hegyi@160: // the new coordinates of the centre of the node hegyi@160: double coord_x = dx + (mytab.mapstorage).coords[moved_node].x; hegyi@160: double coord_y = dy + (mytab.mapstorage).coords[moved_node].y; hegyi@160: hegyi@160: // write back the new coordinates to the coords map hegyi@160: (mytab.mapstorage).coords.set(moved_node, XY(coord_x, coord_y)); hegyi@160: hegyi@160: //all the edges connected to the moved point has to be redrawn hegyi@160: for(OutEdgeIt ei((mytab.mapstorage).graph,moved_node);ei!=INVALID;++ei) hegyi@160: { hegyi@160: XY arrow_pos; hegyi@160: hegyi@160: if (mytab.mapstorage.graph.source(ei) == mytab.mapstorage.graph.target(ei)) hegyi@160: { hegyi@160: arrow_pos = mytab.mapstorage.arrow_pos[ei] + XY(dx, dy); hegyi@160: } hegyi@160: else hegyi@160: { hegyi@160: XY moved_node_1(coord_x - dx, coord_y - dy); hegyi@160: XY moved_node_2(coord_x, coord_y); hegyi@160: Node target = mytab.mapstorage.graph.target(ei); hegyi@160: XY fix_node(mytab.mapstorage.coords[target].x, hegyi@160: mytab.mapstorage.coords[target].y); hegyi@160: XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]); hegyi@160: hegyi@160: arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton); hegyi@160: } hegyi@160: hegyi@160: mytab.mapstorage.arrow_pos.set(ei, arrow_pos); hegyi@160: edgesmap[ei]->draw(); hegyi@160: hegyi@160: //reposition of edgetext hegyi@160: XY text_pos=mytab.mapstorage.arrow_pos[ei]; hegyi@160: text_pos+=(XY(10,10)); hegyi@160: edgetextmap[ei]->property_x().set_value(text_pos.x); hegyi@160: edgetextmap[ei]->property_y().set_value(text_pos.y); hegyi@160: } hegyi@160: hegyi@160: for(InEdgeIt ei((mytab.mapstorage).graph,moved_node);ei!=INVALID;++ei) hegyi@160: { hegyi@160: if (mytab.mapstorage.graph.source(ei) != mytab.mapstorage.graph.target(ei)) hegyi@160: { hegyi@160: XY moved_node_1(coord_x - dx, coord_y - dy); hegyi@160: XY moved_node_2(coord_x, coord_y); hegyi@160: Node source = mytab.mapstorage.graph.source(ei); hegyi@160: XY fix_node(mytab.mapstorage.coords[source].x, hegyi@160: mytab.mapstorage.coords[source].y); hegyi@160: XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]); hegyi@160: hegyi@160: XY arrow_pos; hegyi@160: arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton); hegyi@160: hegyi@160: mytab.mapstorage.arrow_pos.set(ei, arrow_pos); hegyi@160: edgesmap[ei]->draw(); hegyi@160: hegyi@160: //reposition of edgetext hegyi@160: XY text_pos=mytab.mapstorage.arrow_pos[ei]; hegyi@160: text_pos+=(XY(10,10)); hegyi@160: edgetextmap[ei]->property_x().set_value(text_pos.x); hegyi@160: edgetextmap[ei]->property_y().set_value(text_pos.y); hegyi@160: } hegyi@160: } hegyi@27: } hegyi@179: hegyi@179: Gdk::Color GraphDisplayerCanvas::rainbowColorCounter(double min, double max, double w) hegyi@179: { hegyi@179: Gdk::Color color; hegyi@179: hegyi@179: double pos=(w-min)/(max-min); hegyi@179: int phase=0; hegyi@179: hegyi@179: //rainbow transitions contain 6 phase hegyi@179: //in each phase only one color is changed hegyi@179: //first we determine the phase, in which hegyi@179: //the actual value belongs to hegyi@179: for (int i=0;i<=5;i++) hegyi@179: { hegyi@179: if(((double)i/6