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