ladanyi@1606: #include "graph_displayer_canvas.h" ladanyi@1606: #include "broken_edge.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: GdkEvent * generated=new GdkEvent(); hegyi@1579: generated->type=GDK_KEY_PRESS; hegyi@1579: ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; hegyi@1579: entryWidgetChangeHandler(generated); hegyi@1579: entrywidget.hide(); 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: { 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; ladanyi@1606: for (NodeIt i(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: 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: { ladanyi@1606: mapstorage.modified = true; ladanyi@1606: mapstorage.coords.set(active_node, xy(clicked_x, clicked_y)); 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: hegyi@1525: clicked_x=new_x; hegyi@1525: clicked_y=new_y; hegyi@1510: hegyi@1510: //all the edges connected to the moved point has to be redrawn hegyi@1510: EdgeIt ei; hegyi@1510: ladanyi@1606: mapstorage.graph.firstOut(ei,active_node); hegyi@1510: ladanyi@1606: for(;ei!=INVALID;mapstorage.graph.nextOut(ei)) hegyi@1510: { hegyi@1510: Gnome::Canvas::Points coos; hegyi@1510: double x1, x2, y1, y2; hegyi@1510: ladanyi@1606: nodesmap[mapstorage.graph.source(ei)]->get_bounds(x1, y1, x2, y2); hegyi@1510: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@1510: ladanyi@1606: nodesmap[mapstorage.graph.target(ei)]->get_bounds(x1, y1, x2, y2); hegyi@1510: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@1510: hegyi@1510: if(isbutton==3) hegyi@1510: { hegyi@1524: edgesmap[ei]->setPoints(coos); hegyi@1510: } hegyi@1510: else hegyi@1510: { hegyi@1524: edgesmap[ei]->setPoints(coos,true); hegyi@1510: } hegyi@1510: hegyi@1512: //reposition of edgetext hegyi@1524: xy text_pos=edgesmap[ei]->getArrowPos(); hegyi@1510: 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: ladanyi@1606: mapstorage.graph.firstIn(ei,active_node); ladanyi@1606: for(;ei!=INVALID;mapstorage.graph.nextIn(ei)) hegyi@1510: { hegyi@1510: Gnome::Canvas::Points coos; hegyi@1510: double x1, x2, y1, y2; hegyi@1510: ladanyi@1606: nodesmap[mapstorage.graph.source(ei)]->get_bounds(x1, y1, x2, y2); hegyi@1510: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@1510: ladanyi@1606: nodesmap[mapstorage.graph.target(ei)]->get_bounds(x1, y1, x2, y2); hegyi@1510: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@1510: hegyi@1510: if(isbutton==3) hegyi@1510: { hegyi@1524: edgesmap[ei]->setPoints(coos); hegyi@1510: } hegyi@1510: else hegyi@1510: { hegyi@1524: edgesmap[ei]->setPoints(coos,true); hegyi@1510: } hegyi@1510: hegyi@1524: xy text_pos=edgesmap[ei]->getArrowPos(); hegyi@1510: 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: hegyi@1524: bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e) hegyi@1510: { hegyi@1510: switch(e->type) hegyi@1510: { hegyi@1510: hegyi@1510: //draw the new node in red at the clicked place hegyi@1525: case GDK_2BUTTON_PRESS: hegyi@1599: //std::cout << "double click" << std::endl; hegyi@1525: break; hegyi@1510: case GDK_BUTTON_PRESS: ladanyi@1606: mapstorage.modified = true; ladanyi@1606: hegyi@1510: isbutton=1; hegyi@1510: ladanyi@1606: active_node=NodeIt(mapstorage.graph,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@1606: mapstorage.coords.set(active_node, xy(clicked_x, clicked_y)); ladanyi@1606: (*mapstorage.nodemap_storage["id"])[active_node] = ladanyi@1606: mapstorage.graph.id(active_node); ladanyi@1606: ladanyi@1606: nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, ladanyi@1606: clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20); hegyi@1510: active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]); hegyi@1510: *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); hegyi@1510: *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black"); hegyi@1510: (nodesmap[active_node])->show(); hegyi@1512: ladanyi@1606: nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph, ladanyi@1606: clicked_x+node_property_defaults[N_RADIUS]+5, ladanyi@1606: clicked_y+node_property_defaults[N_RADIUS]+5, ""); hegyi@1512: nodetextmap[active_node]->property_fill_color().set_value("darkblue"); hegyi@1512: ladanyi@1606: mapwin.updateNode(active_node); hegyi@1512: hegyi@1510: break; hegyi@1510: hegyi@1510: //move the new node hegyi@1510: case GDK_MOTION_NOTIFY: hegyi@1510: { hegyi@1510: GdkEvent * generated=new GdkEvent(); hegyi@1525: generated->motion.x=e->motion.x; hegyi@1525: generated->motion.y=e->motion.y; hegyi@1510: generated->type=GDK_MOTION_NOTIFY; hegyi@1524: moveEventHandler(generated); hegyi@1510: break; hegyi@1510: } hegyi@1510: hegyi@1510: //finalize the new node hegyi@1510: case GDK_BUTTON_RELEASE: hegyi@1579: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@1579: hegyi@1579: active_item->lower_to_bottom(); hegyi@1579: hegyi@1579: target_item=NULL; hegyi@1579: target_item=get_item_at(clicked_x, clicked_y); hegyi@1579: hegyi@1579: active_item->raise_to_top(); hegyi@1579: hegyi@1510: isbutton=0; hegyi@1579: if(target_item==active_item) hegyi@1525: { hegyi@1525: //Its appropriate color is given by update. hegyi@1579: *active_item << Gnome::Canvas::Properties::fill_color("blue"); hegyi@1525: } hegyi@1525: else hegyi@1525: { hegyi@1525: //In this case the given color has to be overwritten, because the noe covers an other item. hegyi@1525: *active_item << Gnome::Canvas::Properties::fill_color("lightblue"); hegyi@1525: } hegyi@1525: target_item=NULL; hegyi@1510: active_item=NULL; hegyi@1510: active_node=INVALID; hegyi@1510: break; hegyi@1510: default: hegyi@1510: break; hegyi@1510: } hegyi@1510: return false; hegyi@1510: } hegyi@1510: hegyi@1524: bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e) hegyi@1510: { hegyi@1510: switch(e->type) hegyi@1510: { hegyi@1510: case GDK_BUTTON_PRESS: hegyi@1510: //in edge creation right button has special meaning hegyi@1510: if(e->button.button!=3) hegyi@1510: { hegyi@1510: //there is not yet selected node hegyi@1510: if(active_node==INVALID) hegyi@1510: { hegyi@1510: //we mark the location of the event to be able to calculate parameters of dragging hegyi@1525: 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; ladanyi@1606: for (NodeIt i(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: //the clicked item is really a node hegyi@1510: if(active_node!=INVALID) hegyi@1510: { hegyi@1510: *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); hegyi@1510: isbutton=1; hegyi@1510: } hegyi@1510: //clicked item was not a node. It could be e.g. edge. hegyi@1510: else hegyi@1510: { hegyi@1510: active_item=NULL; hegyi@1510: } hegyi@1510: } hegyi@1510: //we only have to do sg. if the mouse button hegyi@1510: // is pressed already once AND the click was hegyi@1510: // on a node that was found in the set of hegyi@1510: //nodes, and now we only search for the second hegyi@1510: //node hegyi@1510: else hegyi@1510: { hegyi@1525: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@1525: target_item=(get_item_at(clicked_x, clicked_y)); hegyi@1510: Graph::NodeIt target_node=INVALID; ladanyi@1606: for (NodeIt i(mapstorage.graph); i!=INVALID; ++i) hegyi@1510: { hegyi@1510: if(nodesmap[i]==target_item) hegyi@1510: { hegyi@1510: target_node=i; hegyi@1510: } hegyi@1510: } hegyi@1510: //the clicked item is a node, the edge can be drawn hegyi@1510: if(target_node!=INVALID) hegyi@1510: { hegyi@1512: if(target_node!=active_node) hegyi@1512: { ladanyi@1606: mapstorage.modified = true; ladanyi@1606: hegyi@1512: *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red"); hegyi@1510: hegyi@1512: //creating new edge ladanyi@1606: active_edge=EdgeIt(mapstorage.graph,mapstorage.graph.addEdge(active_node, target_node)); hegyi@1510: hegyi@1512: //initiating values corresponding to new edge in maps hegyi@1524: mapstorage.initMapsForEdge(active_edge); ladanyi@1606: (*mapstorage.edgemap_storage["id"])[active_edge] = mapstorage.graph.id(active_edge); hegyi@1510: hegyi@1512: //calculating coordinates of new edge hegyi@1512: Gnome::Canvas::Points coos; hegyi@1512: double x1, x2, y1, y2; hegyi@1510: hegyi@1512: active_item->get_bounds(x1, y1, x2, y2); hegyi@1512: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@1510: hegyi@1512: target_item->get_bounds(x1, y1, x2, y2); hegyi@1512: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@1510: hegyi@1512: //drawing new edge hegyi@1512: edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this); hegyi@1512: *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green"); hegyi@1512: edgesmap[active_edge]->property_width_pixels().set_value(10); hegyi@1510: hegyi@1512: //redraw nodes to blank terminations of the new edge hegyi@1512: target_item->raise_to_top(); hegyi@1512: active_item->raise_to_top(); hegyi@1510: hegyi@1512: //initializing edge-text as well, to empty string hegyi@1524: xy text_pos=edgesmap[active_edge]->getArrowPos(); hegyi@1512: text_pos+=(xy(10,10)); hegyi@1510: hegyi@1512: edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, ""); hegyi@1512: edgetextmap[active_edge]->property_fill_color().set_value("darkgreen"); hegyi@1512: hegyi@1512: //updating its properties ladanyi@1606: mapwin.updateEdge(active_edge); hegyi@1512: } hegyi@1512: else hegyi@1512: { hegyi@1512: target_node=INVALID; hegyi@1599: std::cerr << "Loop edge is not yet implemented!" << std::endl; hegyi@1512: } hegyi@1510: } hegyi@1510: //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore. hegyi@1510: else hegyi@1510: { hegyi@1510: target_item=NULL; hegyi@1510: } hegyi@1510: } hegyi@1510: } 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)) hegyi@1510: { hegyi@1510: if(active_item) hegyi@1510: { hegyi@1510: *active_item << Gnome::Canvas::Properties::fill_color("blue"); hegyi@1510: active_item=NULL; hegyi@1510: } hegyi@1510: if(target_item) hegyi@1510: { hegyi@1510: *target_item << Gnome::Canvas::Properties::fill_color("blue"); hegyi@1510: target_item=NULL; hegyi@1510: } hegyi@1510: active_node=INVALID; hegyi@1510: active_edge=INVALID; hegyi@1510: } hegyi@1510: break; hegyi@1510: default: hegyi@1510: break; hegyi@1510: } 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? ladanyi@1606: for (NodeIt i(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: { ladanyi@1606: for (EdgeIt i(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: { ladanyi@1606: mapstorage.modified = true; hegyi@1510: hegyi@1525: //collecting edges to delete hegyi@1525: EdgeIt e; hegyi@1525: std::set edges_to_delete; hegyi@1510: ladanyi@1606: mapstorage.graph.firstOut(e,active_node); ladanyi@1606: for(;e!=INVALID;mapstorage.graph.nextOut(e)) hegyi@1525: { hegyi@1525: edges_to_delete.insert(e); hegyi@1525: } hegyi@1525: ladanyi@1606: mapstorage.graph.firstIn(e,active_node); ladanyi@1606: for(;e!=INVALID;mapstorage.graph.nextIn(e)) hegyi@1525: { hegyi@1525: edges_to_delete.insert(e); hegyi@1525: } hegyi@1525: hegyi@1525: //deleting collected edges hegyi@1525: for(std::set::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();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) hegyi@1550: { hegyi@1599: switch(e->type) hegyi@1599: { hegyi@1599: case GDK_KEY_PRESS: hegyi@1599: //for Escape or Enter hide the displayed widget hegyi@1599: { hegyi@1599: nodeMapEditEventHandler(e); hegyi@1599: break; hegyi@1599: } hegyi@1599: case GDK_BUTTON_PRESS: hegyi@1599: //If the click happened on an edge we place the entrywidget there and fill in the value of the activated map at that edge. hegyi@1599: { hegyi@1599: //for determine, whether it was an edge hegyi@1599: Graph::EdgeIt clicked_edge=INVALID; hegyi@1594: hegyi@1599: //find the activated item between texts hegyi@1599: active_item=(get_item_at(e->button.x, e->button.y)); ladanyi@1606: for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i) hegyi@1562: { hegyi@1599: if(edgetextmap[i]==active_item) hegyi@1599: { hegyi@1599: clicked_edge=i; hegyi@1599: } hegyi@1562: } hegyi@1599: hegyi@1599: //if it was not between texts, search for it between edges hegyi@1599: if(clicked_edge==INVALID) hegyi@1562: { hegyi@1599: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@1599: active_item=(get_item_at(clicked_x, clicked_y)); hegyi@1599: ladanyi@1606: for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i) hegyi@1599: { hegyi@1599: //at the same time only one can be active hegyi@1599: if((edgesmap[i]==active_item)||(edgetextmap[i]==active_item)) hegyi@1599: { hegyi@1599: clicked_edge=i; hegyi@1599: } hegyi@1599: } hegyi@1599: } hegyi@1599: //if it was really an edge... hegyi@1599: if(clicked_edge!=INVALID) hegyi@1599: { hegyi@1599: //If there is already edited edge, it has to be saved first hegyi@1599: if(entrywidget.is_visible()) hegyi@1599: { hegyi@1599: GdkEvent * generated=new GdkEvent(); hegyi@1599: generated->type=GDK_KEY_PRESS; hegyi@1599: ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; hegyi@1599: entryWidgetChangeHandler(generated); hegyi@1599: } hegyi@1599: //If the previous value could be saved, we can go further, otherwise not hegyi@1599: if(!entrywidget.is_visible()) hegyi@1599: { hegyi@1599: //and there is activated map hegyi@1599: if(edgetextmap[clicked_edge]->property_text().get_value()!="") hegyi@1599: { hegyi@1599: //activate the general variable for it hegyi@1599: active_edge=clicked_edge; hegyi@1599: //delete visible widget if there is hegyi@1599: if(canvasentrywidget) hegyi@1599: { hegyi@1599: delete(canvasentrywidget); hegyi@1599: } hegyi@1599: hegyi@1599: //initialize the entry hegyi@1599: entrywidget.show(); hegyi@1599: hegyi@1599: //fill in the correct value hegyi@1599: entrywidget.set_text(edgetextmap[active_edge]->property_text().get_value()); hegyi@1599: hegyi@1599: //replace and resize the entry to the activated edge and put it in a Canvas::Widget to be able to display it on gdc hegyi@1599: xy entry_coos; hegyi@1599: entry_coos.x=(edgetextmap[active_edge])->property_x().get_value(); hegyi@1599: entry_coos.x-=edgetextmap[active_edge]->property_text_width().get_value()/2; hegyi@1599: entry_coos.y=(edgetextmap[active_edge])->property_y().get_value(); hegyi@1599: entry_coos.y-=edgetextmap[active_edge]->property_text_height().get_value()*1.5/2; hegyi@1599: canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget); hegyi@1599: canvasentrywidget->property_width().set_value(edgetextmap[active_edge]->property_text_width().get_value()*4); hegyi@1599: canvasentrywidget->property_height().set_value(edgetextmap[active_edge]->property_text_height().get_value()*1.5); hegyi@1614: hegyi@1614: //setting the focus to newly created widget hegyi@1614: parentwin->set_focus(entrywidget); hegyi@1614: parentwin->activate_focus(); hegyi@1599: } hegyi@1599: } hegyi@1599: } hegyi@1599: //if it was not an edge... hegyi@1599: else hegyi@1599: { hegyi@1599: //In this case the click did not happen on an edge hegyi@1599: //if there is visible entry we save the value in it hegyi@1599: //we pretend like an Enter was presse din the Entry widget hegyi@1594: GdkEvent * generated=new GdkEvent(); hegyi@1594: generated->type=GDK_KEY_PRESS; hegyi@1594: ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; hegyi@1594: entryWidgetChangeHandler(generated); hegyi@1594: } hegyi@1599: break; hegyi@1562: } hegyi@1599: default: hegyi@1599: break; hegyi@1599: } hegyi@1550: } 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) hegyi@1579: { hegyi@1599: switch(e->type) hegyi@1599: { hegyi@1599: case GDK_KEY_PRESS: hegyi@1599: //for Escape or Enter hide the displayed widget hegyi@1579: { hegyi@1599: switch(((GdkEventKey*)e)->keyval) hegyi@1599: { hegyi@1599: case GDK_Escape: hegyi@1599: entrywidget.hide(); hegyi@1599: break; hegyi@1599: case GDK_Return: hegyi@1599: case GDK_KP_Enter: hegyi@1599: entrywidget.hide(); hegyi@1599: break; hegyi@1599: default: hegyi@1599: break; hegyi@1599: } hegyi@1599: hegyi@1579: break; hegyi@1579: } hegyi@1599: case GDK_BUTTON_PRESS: hegyi@1599: //If the click happened on an edge we place the entrywidget there and fill in the value of the activated map at that edge. hegyi@1599: { hegyi@1599: //for determine, whether it was a node hegyi@1599: Graph::NodeIt clicked_node=INVALID; hegyi@1594: hegyi@1599: //find the activated item between texts hegyi@1599: active_item=(get_item_at(e->button.x, e->button.y)); ladanyi@1606: for (NodeIt i(mapstorage.graph); i!=INVALID; ++i) hegyi@1579: { hegyi@1599: //at the same time only one can be active hegyi@1599: if(nodetextmap[i]==active_item) hegyi@1599: { hegyi@1599: clicked_node=i; hegyi@1599: } hegyi@1579: } hegyi@1594: hegyi@1599: //if there was not, search for it between nodes hegyi@1599: if(clicked_node==INVALID) hegyi@1579: { hegyi@1599: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@1599: active_item=(get_item_at(clicked_x, clicked_y)); hegyi@1599: ladanyi@1606: for (NodeIt i(mapstorage.graph); i!=INVALID; ++i) hegyi@1599: { hegyi@1599: //at the same time only one can be active hegyi@1599: if(nodesmap[i]==active_item) hegyi@1599: { hegyi@1599: clicked_node=i; hegyi@1599: } hegyi@1599: } hegyi@1599: } hegyi@1599: //if it was really an edge... hegyi@1599: if(clicked_node!=INVALID) hegyi@1599: { hegyi@1599: //If there is already edited edge, it has to be saved first hegyi@1599: if(entrywidget.is_visible()) hegyi@1599: { hegyi@1599: GdkEvent * generated=new GdkEvent(); hegyi@1599: generated->type=GDK_KEY_PRESS; hegyi@1599: ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; hegyi@1599: entryWidgetChangeHandler(generated); hegyi@1599: } hegyi@1599: //If the previous value could be saved, we can go further, otherwise not hegyi@1599: if(!entrywidget.is_visible()) hegyi@1599: { hegyi@1599: //and there is activated map hegyi@1599: if(nodetextmap[clicked_node]->property_text().get_value()!="") hegyi@1599: { hegyi@1599: //activate the general variable for it hegyi@1599: active_node=clicked_node; hegyi@1599: //delete visible widget if there is hegyi@1599: if(canvasentrywidget) hegyi@1599: { hegyi@1599: delete(canvasentrywidget); hegyi@1599: } hegyi@1599: hegyi@1599: //initialize the entry hegyi@1599: entrywidget.show(); hegyi@1599: hegyi@1599: //fill in the correct value hegyi@1599: entrywidget.set_text(nodetextmap[active_node]->property_text().get_value()); hegyi@1599: hegyi@1599: //replace and resize the entry to the activated node and put it in a Canvas::Widget to be able to display it on gdc hegyi@1599: xy entry_coos; hegyi@1599: entry_coos.x=(nodetextmap[active_node])->property_x().get_value(); hegyi@1599: entry_coos.x-=nodetextmap[active_node]->property_text_width().get_value()/2; hegyi@1599: entry_coos.y=(nodetextmap[active_node])->property_y().get_value(); hegyi@1599: entry_coos.y-=nodetextmap[active_node]->property_text_height().get_value()*1.5/2; hegyi@1599: canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget); hegyi@1599: canvasentrywidget->property_width().set_value(nodetextmap[active_node]->property_text_width().get_value()*4); hegyi@1599: canvasentrywidget->property_height().set_value(nodetextmap[active_node]->property_text_height().get_value()*1.5); hegyi@1599: } hegyi@1599: } hegyi@1599: } hegyi@1599: //if it was not an edge... hegyi@1599: else hegyi@1599: { hegyi@1599: //In this case the click did not happen on an edge hegyi@1599: //if there is visible entry we save the value in it hegyi@1599: //we pretend like an Enter was presse din the Entry widget hegyi@1579: GdkEvent * generated=new GdkEvent(); hegyi@1579: generated->type=GDK_KEY_PRESS; hegyi@1579: ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; hegyi@1579: entryWidgetChangeHandler(generated); hegyi@1579: } hegyi@1599: break; hegyi@1579: } hegyi@1599: default: hegyi@1599: break; hegyi@1599: } hegyi@1579: } hegyi@1579: return false; hegyi@1550: } hegyi@1550: hegyi@1562: bool GraphDisplayerCanvas::entryWidgetChangeHandler(GdkEvent* e) hegyi@1562: { hegyi@1579: if(entrywidget.is_visible()) hegyi@1579: { hegyi@1579: if(e->type==GDK_KEY_PRESS) hegyi@1579: { hegyi@1579: switch(((GdkEventKey*)e)->keyval) hegyi@1579: { hegyi@1579: case GDK_Escape: hegyi@1579: entrywidget.hide(); hegyi@1579: break; hegyi@1579: case GDK_KP_Enter: hegyi@1579: case GDK_Return: hegyi@1579: { hegyi@1594: //these variables check whether the text in the entry is valid hegyi@1592: bool valid_double=true; hegyi@1592: int point_num=0; hegyi@1594: hegyi@1594: //getting the value from the entry and converting it to double hegyi@1592: Glib::ustring mapvalue_str = entrywidget.get_text(); hegyi@1579: hegyi@1592: char * mapvalue_ch=new char [mapvalue_str.length()]; hegyi@1592: for(int i=0;i<(int)(mapvalue_str.length());i++) hegyi@1579: { hegyi@1592: if(((mapvalue_str[i]<'0')||(mapvalue_str[i]>'9'))&&(mapvalue_str[i]!='.')) hegyi@1579: { hegyi@1592: valid_double=false; hegyi@1579: } hegyi@1579: else hegyi@1579: { hegyi@1592: if(mapvalue_str[i]=='.') hegyi@1592: { hegyi@1592: point_num++; hegyi@1592: } hegyi@1579: } hegyi@1592: mapvalue_ch[i]=mapvalue_str[i]; hegyi@1579: } hegyi@1596: hegyi@1594: //if the text in the entry was correct hegyi@1592: if((point_num<=1)&&(valid_double)) hegyi@1579: { hegyi@1596: double mapvalue_d=atof(mapvalue_ch); hegyi@1596: hegyi@1596: //reconvert the double to string for the correct format hegyi@1596: std::ostringstream ostr; hegyi@1596: ostr << mapvalue_d; hegyi@1596: hegyi@1594: //save the value to the correct place hegyi@1579: switch(actual_tool) hegyi@1579: { hegyi@1579: case EDGE_MAP_EDIT: hegyi@1596: edgetextmap[active_edge]->property_text().set_value(ostr.str()); hegyi@1592: (*(mapstorage.edgemap_storage)[edgemap_to_edit])[active_edge]=mapvalue_d; ladanyi@1606: mapwin.updateEdge(active_edge); hegyi@1579: break; hegyi@1579: case NODE_MAP_EDIT: hegyi@1596: nodetextmap[active_node]->property_text().set_value(ostr.str()); hegyi@1592: (*(mapstorage.nodemap_storage)[nodemap_to_edit])[active_node]=mapvalue_d; ladanyi@1606: mapwin.updateNode(active_node); hegyi@1579: break; hegyi@1579: default: hegyi@1579: break; hegyi@1579: } hegyi@1579: entrywidget.hide(); hegyi@1579: } hegyi@1594: //the text in the entry was not correct for a double hegyi@1579: else hegyi@1579: { hegyi@1599: std::cerr << "ERROR: only handling of double values is implemented yet!" << std::endl; hegyi@1579: } hegyi@1579: hegyi@1579: break; hegyi@1579: } hegyi@1579: default: hegyi@1579: break; hegyi@1579: } hegyi@1579: } hegyi@1579: } hegyi@1562: return false; hegyi@1562: } hegyi@1562: hegyi@1524: void GraphDisplayerCanvas::deleteItem(NodeIt node_to_delete) hegyi@1510: { hegyi@1512: delete(nodetextmap[node_to_delete]); hegyi@1510: delete(nodesmap[node_to_delete]); ladanyi@1606: mapstorage.graph.erase(node_to_delete); hegyi@1510: } hegyi@1510: hegyi@1524: void GraphDisplayerCanvas::deleteItem(EdgeIt edge_to_delete) hegyi@1510: { hegyi@1512: delete(edgetextmap[edge_to_delete]); hegyi@1510: delete(edgesmap[edge_to_delete]); ladanyi@1606: mapstorage.graph.erase(edge_to_delete); hegyi@1510: } hegyi@1510: hegyi@1524: void GraphDisplayerCanvas::deleteItem(Graph::Edge edge_to_delete) hegyi@1510: { hegyi@1512: delete(edgetextmap[edge_to_delete]); hegyi@1510: delete(edgesmap[edge_to_delete]); ladanyi@1606: 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: { ladanyi@1606: for (EdgeIt i(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 ladanyi@1606: Graph::EdgeMap * emptr=new Graph::EdgeMap (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@1597: if(mapstorage.addEdgeMap(mapname,emptr)) hegyi@1597: { hegyi@1597: return 1; hegyi@1597: } hegyi@1597: hegyi@1594: hegyi@1594: //add it to the list of the displayable maps ladanyi@1606: mapwin.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 ladanyi@1606: Graph::NodeMap * emptr=new Graph::NodeMap (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@1597: if(mapstorage.addNodeMap(mapname,emptr)) hegyi@1597: { hegyi@1597: return 1; hegyi@1597: } hegyi@1594: hegyi@1594: //add it to the list of the displayable maps ladanyi@1606: mapwin.registerNewNodeMap(mapname); hegyi@1594: hegyi@1594: //display it hegyi@1592: changeNodeText(mapname); hegyi@1597: hegyi@1597: return 0; hegyi@1585: } hegyi@1585: