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