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