#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: { GdkEvent * generated=new GdkEvent(); generated->type=GDK_KEY_PRESS; ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; entryWidgetChangeHandler(generated); entrywidget.hide(); 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) { 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: 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; mapstorage.coords.set(active_node, xy(clicked_x, clicked_y)); //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); clicked_x=new_x; clicked_y=new_y; //all the edges connected to the moved point has to be redrawn EdgeIt ei; mapstorage.graph.firstOut(ei,active_node); for(;ei!=INVALID;mapstorage.graph.nextOut(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); } mapstorage.graph.firstIn(ei,active_node); for(;ei!=INVALID;mapstorage.graph.nextIn(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; //collecting edges to delete EdgeIt e; std::set edges_to_delete; mapstorage.graph.firstOut(e,active_node); for(;e!=INVALID;mapstorage.graph.nextOut(e)) { edges_to_delete.insert(e); } mapstorage.graph.firstIn(e,active_node); for(;e!=INVALID;mapstorage.graph.nextIn(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_KEY_PRESS: //for Escape or Enter hide the displayed widget { nodeMapEditEventHandler(e); break; } case GDK_BUTTON_PRESS: //If the click happened on an edge we place the entrywidget there and fill in the value of the activated map at that edge. { //for determine, whether it was an edge Edge clicked_edge=INVALID; //find the activated item between texts window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); active_item=(get_item_at(clicked_x, clicked_y)); for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i) { 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; //If there is already edited edge, it has to be saved first if(entrywidget.is_visible()) { GdkEvent * generated=new GdkEvent(); generated->type=GDK_KEY_PRESS; ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; entryWidgetChangeHandler(generated); } //If the previous value could be saved, we can go further, otherwise not if(!entrywidget.is_visible()) { //and there is activated map if(edgetextmap[clicked_edge]->property_text().get_value()!="") { //activate the general variable for it active_edge=clicked_edge; //delete visible widget if there is if(canvasentrywidget) { delete(canvasentrywidget); } //initialize the entry entrywidget.show(); //fill in the correct value entrywidget.set_text(edgetextmap[active_edge]->property_text().get_value()); //replace and resize the entry to the activated edge and put it in a Canvas::Widget to be able to display it on gdc xy entry_coos; entry_coos.x=(edgetextmap[active_edge])->property_x().get_value(); entry_coos.x-=edgetextmap[active_edge]->property_text_width().get_value()/2; entry_coos.y=(edgetextmap[active_edge])->property_y().get_value(); entry_coos.y-=edgetextmap[active_edge]->property_text_height().get_value()*1.5/2; canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget); canvasentrywidget->property_width().set_value(edgetextmap[active_edge]->property_text_width().get_value()*4); canvasentrywidget->property_height().set_value(edgetextmap[active_edge]->property_text_height().get_value()*1.5); //setting the focus to newly created widget parentwin->set_focus(entrywidget); parentwin->activate_focus(); } } } //if it was not an edge... else { //In this case the click did not happen on an edge //if there is visible entry we save the value in it //we pretend like an Enter was presse din the Entry widget GdkEvent * generated=new GdkEvent(); generated->type=GDK_KEY_PRESS; ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; entryWidgetChangeHandler(generated); } break; } default: break; } } return false; } bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e) { if(actual_tool==NODE_MAP_EDIT) { switch(e->type) { case GDK_KEY_PRESS: //for Escape or Enter hide the displayed widget { switch(((GdkEventKey*)e)->keyval) { case GDK_Escape: entrywidget.hide(); break; case GDK_Return: case GDK_KP_Enter: entrywidget.hide(); break; default: break; } break; } case GDK_BUTTON_PRESS: //If the click happened on an edge we place the entrywidget there and fill in the value of the activated map at that edge. { //for determine, whether it was a node Node clicked_node=INVALID; //find the activated item between texts active_item=(get_item_at(e->button.x, e->button.y)); 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) { window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); active_item=(get_item_at(clicked_x, clicked_y)); 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 an edge... if(clicked_node!=INVALID) { // the id map is not editable if (nodemap_to_edit == "id") return 0; //If there is already edited edge, it has to be saved first if(entrywidget.is_visible()) { GdkEvent * generated=new GdkEvent(); generated->type=GDK_KEY_PRESS; ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; entryWidgetChangeHandler(generated); } //If the previous value could be saved, we can go further, otherwise not if(!entrywidget.is_visible()) { //and there is activated map if(nodetextmap[clicked_node]->property_text().get_value()!="") { //activate the general variable for it active_node=clicked_node; //delete visible widget if there is if(canvasentrywidget) { delete(canvasentrywidget); } //initialize the entry entrywidget.show(); //fill in the correct value entrywidget.set_text(nodetextmap[active_node]->property_text().get_value()); //replace and resize the entry to the activated node and put it in a Canvas::Widget to be able to display it on gdc xy entry_coos; entry_coos.x=(nodetextmap[active_node])->property_x().get_value(); entry_coos.x-=nodetextmap[active_node]->property_text_width().get_value()/2; entry_coos.y=(nodetextmap[active_node])->property_y().get_value(); entry_coos.y-=nodetextmap[active_node]->property_text_height().get_value()*1.5/2; canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget); canvasentrywidget->property_width().set_value(nodetextmap[active_node]->property_text_width().get_value()*4); canvasentrywidget->property_height().set_value(nodetextmap[active_node]->property_text_height().get_value()*1.5); } } } //if it was not an edge... else { //In this case the click did not happen on an edge //if there is visible entry we save the value in it //we pretend like an Enter was presse din the Entry widget GdkEvent * generated=new GdkEvent(); generated->type=GDK_KEY_PRESS; ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; entryWidgetChangeHandler(generated); } break; } default: break; } } return false; } bool GraphDisplayerCanvas::entryWidgetChangeHandler(GdkEvent* e) { if(entrywidget.is_visible()) { if(e->type==GDK_KEY_PRESS) { switch(((GdkEventKey*)e)->keyval) { case GDK_Escape: entrywidget.hide(); break; case GDK_KP_Enter: case GDK_Return: { //these variables check whether the text in the entry is valid bool valid_double=true; int point_num=0; //getting the value from the entry and converting it to double Glib::ustring mapvalue_str = entrywidget.get_text(); char * mapvalue_ch=new char [mapvalue_str.length()]; for(int i=0;i<(int)(mapvalue_str.length());i++) { if(((mapvalue_str[i]<'0')||(mapvalue_str[i]>'9'))&&(mapvalue_str[i]!='.')) { valid_double=false; } else { if(mapvalue_str[i]=='.') { point_num++; } } mapvalue_ch[i]=mapvalue_str[i]; } //if the text in the entry was correct if((point_num<=1)&&(valid_double)) { double mapvalue_d=atof(mapvalue_ch); //reconvert the double to string for the correct format std::ostringstream ostr; ostr << mapvalue_d; //save the value to the correct place switch(actual_tool) { case EDGE_MAP_EDIT: edgetextmap[active_edge]->property_text().set_value(ostr.str()); (*(mapstorage.edgemap_storage)[edgemap_to_edit])[active_edge]=mapvalue_d; mapwin.updateEdge(active_edge); break; case NODE_MAP_EDIT: nodetextmap[active_node]->property_text().set_value(ostr.str()); (*(mapstorage.nodemap_storage)[nodemap_to_edit])[active_node]=mapvalue_d; mapwin.updateNode(active_node); break; default: break; } entrywidget.hide(); } //the text in the entry was not correct for a double else { std::cerr << "ERROR: only handling of double values is implemented yet!" << std::endl; } 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; }