ladanyi@53: #include "graph_displayer_canvas.h" alpar@59: #include hegyi@27: hegyi@27: hegyi@27: bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event) hegyi@27: { hegyi@27: Gnome::Canvas::CanvasAA::on_expose_event(event); hegyi@27: //usleep(10000); hegyi@27: //rezoom(); hegyi@27: return true; hegyi@27: } hegyi@27: hegyi@27: void GraphDisplayerCanvas::changeEditorialTool(int newtool) hegyi@27: { hegyi@34: if(actual_tool!=newtool) hegyi@34: { hegyi@27: hegyi@34: actual_handler.disconnect(); hegyi@27: hegyi@34: switch(actual_tool) hegyi@34: { hegyi@34: case CREATE_EDGE: hegyi@34: { hegyi@34: GdkEvent * generated=new GdkEvent(); hegyi@34: generated->type=GDK_BUTTON_RELEASE; hegyi@34: generated->button.button=3; hegyi@34: createEdgeEventHandler(generated); hegyi@34: break; hegyi@34: } hegyi@34: case EDGE_MAP_EDIT: hegyi@35: //has to do the same thing as in the case of NODE_MAP_EDIT hegyi@35: case NODE_MAP_EDIT: hegyi@35: { hegyi@35: break; hegyi@35: } hegyi@34: default: hegyi@34: break; hegyi@34: } hegyi@27: hegyi@34: active_item=NULL; hegyi@34: target_item=NULL; hegyi@34: active_edge=INVALID; hegyi@34: active_node=INVALID; hegyi@33: hegyi@27: hegyi@34: actual_tool=newtool; hegyi@34: hegyi@34: switch(newtool) hegyi@34: { hegyi@34: case MOVE: hegyi@34: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false); hegyi@34: break; hegyi@27: hegyi@34: case CREATE_NODE: hegyi@34: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false); hegyi@34: break; hegyi@27: hegyi@34: case CREATE_EDGE: hegyi@34: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false); hegyi@34: break; hegyi@27: hegyi@34: case ERASER: hegyi@34: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false); hegyi@34: break; hegyi@32: hegyi@34: case EDGE_MAP_EDIT: hegyi@34: grab_focus(); hegyi@34: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false); hegyi@34: break; hegyi@32: hegyi@34: case NODE_MAP_EDIT: hegyi@34: actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false); hegyi@34: break; hegyi@34: hegyi@34: default: hegyi@34: break; hegyi@34: } hegyi@27: } hegyi@27: } hegyi@27: hegyi@30: int GraphDisplayerCanvas::getActualTool() hegyi@27: { hegyi@27: return actual_tool; hegyi@27: } hegyi@27: hegyi@30: bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e) hegyi@27: { ladanyi@70: static Gnome::Canvas::Text *coord_text = 0; hegyi@27: switch(e->type) hegyi@27: { hegyi@27: case GDK_BUTTON_PRESS: hegyi@27: //we mark the location of the event to be able to calculate parameters of dragging hegyi@31: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@31: hegyi@31: active_item=(get_item_at(clicked_x, clicked_y)); hegyi@27: active_node=INVALID; 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: ladanyi@70: if (coord_text) ladanyi@70: { ladanyi@70: delete coord_text; ladanyi@70: coord_text = 0; ladanyi@70: } hegyi@27: isbutton=0; hegyi@27: active_item=NULL; hegyi@27: active_node=INVALID; hegyi@27: break; hegyi@27: case GDK_MOTION_NOTIFY: hegyi@27: //we only have to do sg. if the mouse button is pressed AND the click was on a node that was found in the set of nodes hegyi@27: if(active_node!=INVALID) hegyi@27: { ladanyi@53: mapstorage.modified = true; ladanyi@70: hegyi@27: //new coordinates will be the old values, hegyi@27: //because the item will be moved to the hegyi@27: //new coordinate therefore the new movement hegyi@27: //has to be calculated from here hegyi@27: hegyi@31: double new_x, new_y; hegyi@31: hegyi@31: window_to_world (e->motion.x, e->motion.y, new_x, new_y); hegyi@31: hegyi@31: double dx=new_x-clicked_x; hegyi@31: double dy=new_y-clicked_y; hegyi@27: hegyi@28: //repositioning node and its text hegyi@27: active_item->move(dx, dy); hegyi@28: nodetextmap[active_node]->move(dx, dy); hegyi@27: ladanyi@70: // the new coordinates of the centre of the node ladanyi@70: double coord_x = new_x - (clicked_x - mapstorage.coords[active_node].x); ladanyi@70: double coord_y = new_y - (clicked_y - mapstorage.coords[active_node].y); ladanyi@70: hegyi@31: clicked_x=new_x; hegyi@31: clicked_y=new_y; hegyi@27: ladanyi@70: // write back the new coordinates to the coords map ladanyi@70: mapstorage.coords.set(active_node, xy(coord_x, coord_y)); hegyi@27: ladanyi@70: // reposition the coordinates text ladanyi@70: std::ostringstream ostr; ladanyi@70: ostr << "(" << ladanyi@70: mapstorage.coords[active_node].x << ", " << ladanyi@70: mapstorage.coords[active_node].y << ")"; ladanyi@72: double radius = ladanyi@72: (nodesmap[active_node]->property_x2().get_value() - ladanyi@72: nodesmap[active_node]->property_x1().get_value()) / 2.0; ladanyi@70: if (coord_text) ladanyi@70: { ladanyi@70: coord_text->property_text().set_value(ostr.str()); ladanyi@70: coord_text->property_x().set_value(mapstorage.coords[active_node].x + ladanyi@72: radius); ladanyi@72: coord_text->property_y().set_value(mapstorage.coords[active_node].y - ladanyi@72: radius); ladanyi@70: } ladanyi@70: else ladanyi@70: { ladanyi@70: coord_text = new Gnome::Canvas::Text( ladanyi@70: displayed_graph, ladanyi@72: mapstorage.coords[active_node].x + radius, ladanyi@72: mapstorage.coords[active_node].y - radius, ladanyi@70: ostr.str()); ladanyi@70: coord_text->property_fill_color().set_value("black"); ladanyi@72: coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST); ladanyi@70: } ladanyi@70: ladanyi@70: //all the edges connected to the moved point has to be redrawn alpar@69: for(OutEdgeIt ei(mapstorage.graph,active_node);ei!=INVALID;++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: alpar@69: for(InEdgeIt ei(mapstorage.graph,active_node);ei!=INVALID;++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) 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: ladanyi@53: mapstorage.modified = true; ladanyi@53: hegyi@27: isbutton=1; hegyi@27: alpar@62: active_node=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 ladanyi@53: 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 = ladanyi@63: mapstorage.nodemap_storage.begin(); it != ladanyi@63: 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] = ladanyi@63: mapstorage.nodemap_default[it->first]; ladanyi@63: } ladanyi@63: } ladanyi@64: // increment the id map's default value ladanyi@64: mapstorage.nodemap_default["id"] += 1.0; ladanyi@63: ladanyi@53: nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, ladanyi@63: clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20); hegyi@27: active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]); ladanyi@63: *(nodesmap[active_node]) << ladanyi@63: Gnome::Canvas::Properties::fill_color("blue"); ladanyi@63: *(nodesmap[active_node]) << ladanyi@63: Gnome::Canvas::Properties::outline_color("black"); ladanyi@63: active_item->raise_to_top(); ladanyi@63: hegyi@27: (nodesmap[active_node])->show(); hegyi@28: ladanyi@53: nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph, ladanyi@63: clicked_x+node_property_defaults[N_RADIUS]+5, ladanyi@63: clicked_y+node_property_defaults[N_RADIUS]+5, ""); hegyi@28: nodetextmap[active_node]->property_fill_color().set_value("darkblue"); ladanyi@63: nodetextmap[active_node]->raise_to_top(); hegyi@28: ladanyi@53: mapwin.updateNode(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; ladanyi@63: for (NodeIt i(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; ladanyi@63: for (NodeIt i(mapstorage.graph); i!=INVALID; ++i) ladanyi@63: { ladanyi@63: if(nodesmap[i]==target_item) ladanyi@63: { ladanyi@63: target_node=i; ladanyi@63: } ladanyi@63: } ladanyi@63: //the clicked item is a node, the edge can be drawn ladanyi@63: if(target_node!=INVALID) ladanyi@63: { ladanyi@63: if(target_node!=active_node) ladanyi@63: { ladanyi@63: mapstorage.modified = true; ladanyi@53: ladanyi@63: *(nodesmap[target_node]) << ladanyi@63: Gnome::Canvas::Properties::fill_color("red"); hegyi@27: ladanyi@63: //creating new edge ladanyi@63: active_edge=mapstorage.graph.addEdge(active_node, ladanyi@63: target_node); hegyi@27: ladanyi@64: // update maps ladanyi@63: for (std::map*>::const_iterator it = ladanyi@63: mapstorage.edgemap_storage.begin(); it != ladanyi@63: mapstorage.edgemap_storage.end(); ++it) ladanyi@63: { ladanyi@64: (*(it->second))[active_edge] = ladanyi@64: mapstorage.edgemap_default[it->first]; ladanyi@63: } ladanyi@64: // increment the id map's default value ladanyi@64: mapstorage.edgemap_default["id"] += 1.0; hegyi@27: ladanyi@63: //calculating coordinates of new edge ladanyi@63: Gnome::Canvas::Points coos; ladanyi@63: double x1, x2, y1, y2; hegyi@27: ladanyi@63: active_item->get_bounds(x1, y1, x2, y2); ladanyi@63: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@27: ladanyi@63: target_item->get_bounds(x1, y1, x2, y2); ladanyi@63: coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); hegyi@27: ladanyi@63: //drawing new edge ladanyi@63: edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, ladanyi@63: *this); ladanyi@63: *(edgesmap[active_edge]) << ladanyi@63: Gnome::Canvas::Properties::fill_color("green"); ladanyi@63: edgesmap[active_edge]->property_width_pixels().set_value(10); hegyi@27: ladanyi@63: edgesmap[active_edge]->lower_to_bottom(); hegyi@28: ladanyi@63: //initializing edge-text as well, to empty string ladanyi@63: xy text_pos=edgesmap[active_edge]->getArrowPos(); ladanyi@63: text_pos+=(xy(10,10)); ladanyi@63: ladanyi@63: edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, ladanyi@63: text_pos.x, text_pos.y, ""); ladanyi@63: edgetextmap[active_edge]->property_fill_color().set_value( ladanyi@63: "darkgreen"); ladanyi@63: edgetextmap[active_edge]->raise_to_top(); ladanyi@63: ladanyi@63: //updating its properties ladanyi@63: mapwin.updateEdge(active_edge); ladanyi@63: } ladanyi@63: else ladanyi@63: { ladanyi@63: target_node=INVALID; ladanyi@63: std::cerr << "Loop edge is not yet implemented!" << std::endl; ladanyi@63: } ladanyi@63: } ladanyi@63: //clicked item was not a node. it could be an e.g. edge. we do not ladanyi@63: //deal with it furthermore. ladanyi@63: else ladanyi@63: { ladanyi@63: target_item=NULL; ladanyi@63: } ladanyi@63: } ladanyi@63: } hegyi@27: break; hegyi@27: case GDK_BUTTON_RELEASE: hegyi@27: isbutton=0; hegyi@27: //we clear settings in two cases hegyi@27: //1: the edge is ready (target_item has valid value) hegyi@27: //2: the edge creation is cancelled with right button hegyi@27: if((target_item)||(e->button.button==3)) ladanyi@63: { ladanyi@63: if(active_item) ladanyi@63: { ladanyi@63: *active_item << Gnome::Canvas::Properties::fill_color("blue"); ladanyi@63: active_item=NULL; ladanyi@63: } ladanyi@63: if(target_item) ladanyi@63: { ladanyi@63: *target_item << Gnome::Canvas::Properties::fill_color("blue"); ladanyi@63: target_item=NULL; ladanyi@63: } ladanyi@63: active_node=INVALID; ladanyi@63: active_edge=INVALID; ladanyi@63: } hegyi@27: break; hegyi@27: default: hegyi@27: break; ladanyi@63: } hegyi@27: return false; hegyi@27: } hegyi@27: hegyi@30: bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e) hegyi@27: { hegyi@27: switch(e->type) hegyi@27: { hegyi@27: case GDK_BUTTON_PRESS: hegyi@43: //finding the clicked items hegyi@31: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); hegyi@31: active_item=(get_item_at(clicked_x, clicked_y)); hegyi@27: active_node=INVALID; hegyi@27: active_edge=INVALID; hegyi@43: //was it a node? 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: std::set edges_to_delete; hegyi@27: alpar@69: for(OutEdgeIt e(mapstorage.graph,active_node);e!=INVALID;++e) hegyi@31: { hegyi@31: edges_to_delete.insert(e); hegyi@31: } alpar@69: alpar@69: for(InEdgeIt e(mapstorage.graph,active_node);e!=INVALID;++e) hegyi@31: { hegyi@31: edges_to_delete.insert(e); hegyi@31: } alpar@69: hegyi@31: //deleting collected edges alpar@69: for(std::set::iterator alpar@69: edge_set_it=edges_to_delete.begin(); alpar@69: edge_set_it!=edges_to_delete.end(); alpar@69: ++edge_set_it) hegyi@31: { hegyi@31: deleteItem(*edge_set_it); hegyi@31: } hegyi@31: deleteItem(active_node); hegyi@31: } hegyi@31: //a simple edge was chosen hegyi@31: else hegyi@27: { hegyi@31: deleteItem(active_edge); hegyi@27: } hegyi@27: } hegyi@31: //pointer was moved, deletion is cancelled hegyi@27: else hegyi@27: { hegyi@31: if(active_node!=INVALID) hegyi@31: { hegyi@31: *active_item << Gnome::Canvas::Properties::fill_color("blue"); hegyi@31: } hegyi@31: else hegyi@31: { hegyi@31: *active_item << Gnome::Canvas::Properties::fill_color("green"); hegyi@31: } hegyi@27: } hegyi@27: } hegyi@27: //reseting datas hegyi@27: active_item=NULL; hegyi@27: active_edge=INVALID; hegyi@27: active_node=INVALID; hegyi@27: break; hegyi@27: hegyi@27: case GDK_MOTION_NOTIFY: hegyi@27: break; hegyi@27: hegyi@27: default: hegyi@27: break; hegyi@27: } hegyi@31: return false; hegyi@27: } hegyi@27: hegyi@32: bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e) hegyi@32: { hegyi@48: if(actual_tool==EDGE_MAP_EDIT) ladanyi@66: { ladanyi@66: switch(e->type) hegyi@32: { ladanyi@66: case GDK_BUTTON_PRESS: ladanyi@66: { ladanyi@66: //for determine, whether it was an edge ladanyi@66: Edge clicked_edge=INVALID; hegyi@43: ladanyi@66: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); ladanyi@66: active_item=(get_item_at(clicked_x, clicked_y)); hegyi@48: ladanyi@66: //find the activated item between texts ladanyi@66: for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i) ladanyi@66: { ladanyi@66: //at the same time only one can be active ladanyi@66: if(edgetextmap[i]==active_item) ladanyi@66: { ladanyi@66: clicked_edge=i; ladanyi@66: } ladanyi@66: } ladanyi@65: ladanyi@66: //if it was not between texts, search for it between edges ladanyi@66: if(clicked_edge==INVALID) ladanyi@66: { ladanyi@66: for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i) ladanyi@66: { ladanyi@66: //at the same time only one can be active ladanyi@66: if((edgesmap[i]==active_item)||(edgetextmap[i]==active_item)) ladanyi@66: { ladanyi@66: clicked_edge=i; ladanyi@66: } ladanyi@66: } ladanyi@66: } ladanyi@66: ladanyi@66: //if it was really an edge... ladanyi@66: if(clicked_edge!=INVALID) ladanyi@66: { ladanyi@66: // the id map is not editable ladanyi@66: if (edgemap_to_edit == "id") return 0; hegyi@48: ladanyi@66: //and there is activated map ladanyi@66: if(edgetextmap[clicked_edge]->property_text().get_value()!="") ladanyi@66: { ladanyi@66: //activate the general variable for it ladanyi@66: active_edge=clicked_edge; hegyi@48: ladanyi@66: //create a dialog ladanyi@66: Gtk::Dialog dialog("Edit value", *parentwin, true); ladanyi@66: dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); ladanyi@66: dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); ladanyi@66: Gtk::VBox* vbox = dialog.get_vbox(); ladanyi@66: Gtk::Adjustment adj( ladanyi@66: (*mapstorage.edgemap_storage[edgemap_to_edit])[active_edge], ladanyi@66: -1000000.0, ladanyi@66: 1000000.0, ladanyi@66: 1.0, 5.0, 0.0); ladanyi@66: //TODO: find out why doesn't it work with ladanyi@66: //numeric_limits::min/max ladanyi@66: Gtk::SpinButton spin(adj); ladanyi@66: spin.set_numeric(true); ladanyi@66: spin.set_digits(4); ladanyi@66: vbox->add(spin); ladanyi@66: spin.show(); ladanyi@66: switch (dialog.run()) ladanyi@66: { ladanyi@66: case Gtk::RESPONSE_NONE: ladanyi@66: case Gtk::RESPONSE_CANCEL: ladanyi@66: break; ladanyi@66: case Gtk::RESPONSE_ACCEPT: ladanyi@66: double new_value = spin.get_value(); ladanyi@66: (*mapstorage.edgemap_storage[edgemap_to_edit])[active_edge] = ladanyi@66: new_value; ladanyi@66: std::ostringstream ostr; ladanyi@66: ostr << new_value; ladanyi@66: edgetextmap[active_edge]->property_text().set_value( ladanyi@66: ostr.str()); ladanyi@66: //mapwin.updateEdge(active_edge); ladanyi@66: mapwin.updateEdge(Edge(INVALID)); ladanyi@66: } ladanyi@66: } ladanyi@66: } ladanyi@66: break; ladanyi@66: } ladanyi@66: default: ladanyi@66: break; hegyi@32: } ladanyi@66: } hegyi@32: return false; hegyi@32: } hegyi@32: hegyi@32: bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e) hegyi@32: { hegyi@48: if(actual_tool==NODE_MAP_EDIT) ladanyi@66: { ladanyi@66: switch(e->type) hegyi@35: { ladanyi@66: case GDK_BUTTON_PRESS: ladanyi@66: { ladanyi@66: //for determine, whether it was a node ladanyi@66: Node clicked_node=INVALID; hegyi@43: ladanyi@66: window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); ladanyi@66: active_item=(get_item_at(clicked_x, clicked_y)); hegyi@43: ladanyi@66: //find the activated item between texts ladanyi@66: for (NodeIt i(mapstorage.graph); i!=INVALID; ++i) ladanyi@66: { ladanyi@66: //at the same time only one can be active ladanyi@66: if(nodetextmap[i]==active_item) ladanyi@66: { ladanyi@66: clicked_node=i; ladanyi@66: } ladanyi@66: } hegyi@48: ladanyi@66: //if there was not, search for it between nodes ladanyi@66: if(clicked_node==INVALID) ladanyi@66: { ladanyi@66: for (NodeIt i(mapstorage.graph); i!=INVALID; ++i) ladanyi@66: { ladanyi@66: //at the same time only one can be active ladanyi@66: if(nodesmap[i]==active_item) ladanyi@66: { ladanyi@66: clicked_node=i; ladanyi@66: } ladanyi@66: } ladanyi@66: } hegyi@48: ladanyi@66: //if it was really a node... ladanyi@66: if(clicked_node!=INVALID) ladanyi@66: { ladanyi@66: // the id map is not editable ladanyi@66: if (nodemap_to_edit == "id") return 0; hegyi@48: ladanyi@66: //and there is activated map ladanyi@66: if(nodetextmap[clicked_node]->property_text().get_value()!="") ladanyi@66: { ladanyi@66: //activate the general variable for it ladanyi@66: active_node=clicked_node; hegyi@48: ladanyi@66: //create a dialog ladanyi@66: Gtk::Dialog dialog("Edit value", *parentwin, true); ladanyi@66: dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); ladanyi@66: dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); ladanyi@66: Gtk::VBox* vbox = dialog.get_vbox(); ladanyi@66: Gtk::Adjustment adj( ladanyi@66: (*mapstorage.nodemap_storage[nodemap_to_edit])[active_node], ladanyi@66: -1000000.0, ladanyi@66: 1000000.0, ladanyi@66: 1.0, 5.0, 0.0); ladanyi@66: //TODO: find out why doesn't it work with ladanyi@66: //numeric_limits::min/max ladanyi@66: Gtk::SpinButton spin(adj); ladanyi@66: spin.set_numeric(true); ladanyi@66: spin.set_digits(4); ladanyi@66: vbox->add(spin); ladanyi@66: spin.show(); ladanyi@66: switch (dialog.run()) ladanyi@66: { ladanyi@66: case Gtk::RESPONSE_NONE: ladanyi@66: case Gtk::RESPONSE_CANCEL: ladanyi@66: break; ladanyi@66: case Gtk::RESPONSE_ACCEPT: ladanyi@66: double new_value = spin.get_value(); ladanyi@66: (*mapstorage.nodemap_storage[nodemap_to_edit])[active_node] = ladanyi@66: new_value; ladanyi@66: std::ostringstream ostr; ladanyi@66: ostr << new_value; ladanyi@66: nodetextmap[active_node]->property_text().set_value( ladanyi@66: ostr.str()); ladanyi@66: //mapwin.updateNode(active_node); ladanyi@66: mapwin.updateNode(Node(INVALID)); ladanyi@66: } ladanyi@66: } ladanyi@66: } ladanyi@66: break; ladanyi@66: } ladanyi@66: default: ladanyi@66: break; hegyi@35: } ladanyi@66: } hegyi@35: return false; hegyi@32: } hegyi@32: alpar@62: void GraphDisplayerCanvas::deleteItem(Node node_to_delete) hegyi@27: { hegyi@28: delete(nodetextmap[node_to_delete]); hegyi@27: delete(nodesmap[node_to_delete]); ladanyi@53: 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]); 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 ladanyi@63: if(mapstorage.addEdgeMap(mapname,emptr, default_value)) 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 ladanyi@63: if(mapstorage.addNodeMap(mapname,emptr, default_value)) 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: