/* -*- C++ -*- * * This file is a part of LEMON, a generic C++ optimization library * * Copyright (C) 2003-2006 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Research Group on Combinatorial Optimization, EGRES). * * Permission to use, modify and distribute this software is granted * provided that this copyright notice appears in all copies. For * precise terms see the accompanying LICENSE file. * * This software is provided "AS IS" with no warranty of any kind, * express or implied, and with no claim as to its suitability for any * purpose. * */ #include #include #include #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 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 MAP_EDIT: grab_focus(); actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false); break; default: break; } } } int GraphDisplayerCanvas::getActualTool() { return actual_tool; } bool GraphDisplayerCanvas::scrollEventHandler(GdkEvent* e) { bool handled=false; if(e->type==GDK_SCROLL) { //pointer shows this win point before zoom XY win_coord(((GdkEventScroll*)e)->x, ((GdkEventScroll*)e)->y); //the original scroll settings int scroll_offset_x, scroll_offset_y; get_scroll_offsets(scroll_offset_x, scroll_offset_y); //pointer shows this canvas point before zoom XY canvas_coord; window_to_world(win_coord.x, win_coord.y, canvas_coord.x, canvas_coord.y); if(((GdkEventScroll*)e)->direction) //IN { zoomIn(); } else { zoomOut(); } //pointer shows this window point after zoom XY post_win_coord; world_to_window(canvas_coord.x, canvas_coord.y, post_win_coord.x, post_win_coord.y); //we have to add the difference between new and old window point to original scroll offset scroll_to(scroll_offset_x+(int)(post_win_coord.x-win_coord.x),scroll_offset_y+(int)(post_win_coord.y-win_coord.y)); //no other eventhandler is needed handled=true; } return handled; } 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((mytab.mapstorage)->graph); i!=INVALID; ++i) { if(nodesmap[i]==active_item) { active_node=i; } } isbutton=e->button.button; 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) { (mytab.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; moveNode(dx, dy); clicked_x=new_x; clicked_y=new_y; // reposition the coordinates text std::ostringstream ostr; ostr << "(" << (mytab.mapstorage)->coords[active_node].x << ", " << (mytab.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((mytab.mapstorage)->coords[active_node].x + radius); coord_text->property_y().set_value((mytab.mapstorage)->coords[active_node].y - radius); } else { coord_text = new Gnome::Canvas::Text( displayed_graph, (mytab.mapstorage)->coords[active_node].x + radius, (mytab.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); } } default: break; } return false; } XY GraphDisplayerCanvas::calcArrowPos(XY moved_node_1, XY moved_node_2, XY fix_node, XY old_arrow_pos, int move_code) { switch(move_code) { case 1: return XY((moved_node_2.x + fix_node.x) / 2.0, (moved_node_2.y + fix_node.y) / 2.0); break; case 2: return old_arrow_pos; break; case 3: { ////////////////////////////////////////////////////////////////////////////////////////////////////// /////////// keeps shape-with scalar multiplication - version 2. ////////////////////////////////////////////////////////////////////////////////////////////////////// //old vector from one to the other node - a XY a_v(moved_node_1.x-fix_node.x,moved_node_1.y-fix_node.y); //new vector from one to the other node - b XY b_v(moved_node_2.x-fix_node.x,moved_node_2.y-fix_node.y); double absa=sqrt(a_v.normSquare()); double absb=sqrt(b_v.normSquare()); if ((absa == 0.0) || (absb == 0.0)) { return old_arrow_pos; } else { //old vector from one node to the breakpoint - c XY c_v(old_arrow_pos.x-fix_node.x,old_arrow_pos.y-fix_node.y); //unit vector with the same direction to a_v XY a_v_u(a_v.x/absa,a_v.y/absa); //normal vector of unit vector with the same direction to a_v XY a_v_u_n(((-1)*a_v_u.y),a_v_u.x); //unit vector with the same direction to b_v XY b_v_u(b_v.x/absb,b_v.y/absb); //normal vector of unit vector with the same direction to b_v XY b_v_u_n(((-1)*b_v_u.y),b_v_u.x); //vector c in a_v_u and a_v_u_n co-ordinate system XY c_a(c_v*a_v_u,c_v*a_v_u_n); //new vector from one node to the breakpoint - d - we have to calculate this one XY d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n); return XY(d_v.x+fix_node.x,d_v.y+fix_node.y); } break; } default: break; } } 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: (mytab.mapstorage)->modified = true; is_drawn=true; isbutton=1; active_node=(mytab.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 (mytab.mapstorage)->coords.set(active_node, XY(clicked_x, clicked_y)); // update all other maps for (std::map*>::const_iterator it = (mytab.mapstorage)->nodemap_storage.begin(); it != (mytab.mapstorage)->nodemap_storage.end(); ++it) { if ((it->first != "coordinates_x") && (it->first != "coordinates_y")) { (*(it->second))[active_node] = (mytab.mapstorage)->nodemap_default[it->first]; } } // increment the id map's default value (mytab.mapstorage)->nodemap_default["label"] += 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); propertyUpdate(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((mytab.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((mytab.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) { (mytab.mapstorage)->modified = true; *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red"); //creating new edge active_edge=(mytab.mapstorage)->graph.addEdge(active_node, target_node); // update maps for (std::map*>::const_iterator it = (mytab.mapstorage)->edgemap_storage.begin(); it != (mytab.mapstorage)->edgemap_storage.end(); ++it) { (*(it->second))[active_edge] = (mytab.mapstorage)->edgemap_default[it->first]; } // increment the id map's default value (mytab.mapstorage)->edgemap_default["label"] += 1.0; if(target_node!=active_node) { // set the coordinates of the arrow on the new edge MapStorage& ms = *mytab.mapstorage; ms.arrow_pos.set(active_edge, (ms.coords[ms.graph.source(active_edge)] + ms.coords[ms.graph.target(active_edge)])/ 2.0); //drawing new edge edgesmap[active_edge]=new BrokenEdge(displayed_graph, active_edge, *this); } else { // set the coordinates of the arrow on the new edge MapStorage& ms = *mytab.mapstorage; ms.arrow_pos.set(active_edge, (ms.coords[ms.graph.source(active_edge)] + XY(0.0, 80.0))); //drawing new edge edgesmap[active_edge]=new LoopEdge(displayed_graph, active_edge, *this); } //initializing edge-text as well, to empty string XY text_pos=mytab.mapstorage->arrow_pos[active_edge]; 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(); propertyUpdate(active_edge); } //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) { propertyUpdate(active_node,N_COLOR); active_item=NULL; } if(target_item) { propertyUpdate((mytab.mapstorage)->graph.target(active_edge),N_COLOR); 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((mytab.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((mytab.mapstorage)->graph); i!=INVALID; ++i) { if(edgesmap[i]->getLine()==active_item) { active_edge=i; } } } // return if the clicked object is neither an edge nor a node if (active_edge == INVALID) return false; //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) { (mytab.mapstorage)->modified = true; std::set edges_to_delete; for(OutEdgeIt e((mytab.mapstorage)->graph,active_node);e!=INVALID;++e) { edges_to_delete.insert(e); } for(InEdgeIt e((mytab.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 if (active_edge != INVALID) { deleteItem(active_edge); } } //pointer was moved, deletion is cancelled else { if(active_node!=INVALID) { *active_item << Gnome::Canvas::Properties::fill_color("blue"); } else if (active_edge != INVALID) { *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::mapEditEventHandler(GdkEvent* e) { if(actual_tool==MAP_EDIT) { switch(e->type) { case GDK_BUTTON_PRESS: { //for determine, whether it was an edge Edge clicked_edge=INVALID; //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 text of nodes for (NodeIt i((mytab.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((mytab.mapstorage)->graph); i!=INVALID; ++i) { //at the same time only one can be active if(nodesmap[i]==active_item) { clicked_node=i; } } } if(clicked_node==INVALID) { //find the activated item between texts for (EdgeIt i((mytab.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((mytab.mapstorage)->graph); i!=INVALID; ++i) { //at the same time only one can be active if((edgesmap[i]->getLine())==active_item) { clicked_edge=i; } } } } //if it was really a node... if(clicked_node!=INVALID) { // the id map is not editable if (nodemap_to_edit == "label") 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", 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::SpinButton spin(0.0, 4); spin.set_increments(1.0, 10.0); spin.set_range(-1000000.0, 1000000.0); spin.set_numeric(true); spin.set_value(atof(nodetextmap[active_node]->property_text().get_value().c_str())); 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(); (*(mytab.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)); propertyUpdate(Node(INVALID)); } } } else //if it was really an edge... if(clicked_edge!=INVALID) { // the id map is not editable if (edgemap_to_edit == "label") 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", 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::SpinButton spin(0.0, 4); spin.set_increments(1.0, 10.0); spin.set_range(-1000000.0, 1000000.0); spin.set_numeric(true); spin.set_value(atof(edgetextmap[active_edge]->property_text().get_value().c_str())); 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(); (*(mytab.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)); propertyUpdate(Edge(INVALID)); } } } break; } default: break; } } return false; } void GraphDisplayerCanvas::deleteItem(Node node_to_delete) { delete(nodetextmap[node_to_delete]); delete(nodesmap[node_to_delete]); (mytab.mapstorage)->graph.erase(node_to_delete); } void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete) { delete(edgetextmap[edge_to_delete]); delete(edgesmap[edge_to_delete]); (mytab.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(EdgeBase* active_bre, bool on) { if(on) { if(forming_edge!=INVALID) { std::cerr << "ERROR!!!! Valid edge found!" << std::endl; } else { for (EdgeIt i((mytab.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; } } } void GraphDisplayerCanvas::moveNode(double dx, double dy, Gnome::Canvas::Item * item, Node node) { Gnome::Canvas::Item * moved_item=item; Node moved_node=node; if(item==NULL && node==INVALID) { moved_item=active_item; moved_node=active_node; } else { isbutton=1; } //repositioning node and its text moved_item->move(dx, dy); nodetextmap[moved_node]->move(dx, dy); // the new coordinates of the centre of the node double coord_x = dx + (mytab.mapstorage)->coords[moved_node].x; double coord_y = dy + (mytab.mapstorage)->coords[moved_node].y; // write back the new coordinates to the coords map (mytab.mapstorage)->coords.set(moved_node, XY(coord_x, coord_y)); //all the edges connected to the moved point has to be redrawn for(OutEdgeIt ei((mytab.mapstorage)->graph,moved_node);ei!=INVALID;++ei) { XY arrow_pos; if (mytab.mapstorage->graph.source(ei) == mytab.mapstorage->graph.target(ei)) { arrow_pos = mytab.mapstorage->arrow_pos[ei] + XY(dx, dy); } else { XY moved_node_1(coord_x - dx, coord_y - dy); XY moved_node_2(coord_x, coord_y); Node target = mytab.mapstorage->graph.target(ei); XY fix_node(mytab.mapstorage->coords[target].x, mytab.mapstorage->coords[target].y); XY old_arrow_pos(mytab.mapstorage->arrow_pos[ei]); arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton); } mytab.mapstorage->arrow_pos.set(ei, arrow_pos); edgesmap[ei]->draw(); //reposition of edgetext XY text_pos=mytab.mapstorage->arrow_pos[ei]; 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((mytab.mapstorage)->graph,moved_node);ei!=INVALID;++ei) { if (mytab.mapstorage->graph.source(ei) != mytab.mapstorage->graph.target(ei)) { XY moved_node_1(coord_x - dx, coord_y - dy); XY moved_node_2(coord_x, coord_y); Node source = mytab.mapstorage->graph.source(ei); XY fix_node(mytab.mapstorage->coords[source].x, mytab.mapstorage->coords[source].y); XY old_arrow_pos(mytab.mapstorage->arrow_pos[ei]); XY arrow_pos; arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton); mytab.mapstorage->arrow_pos.set(ei, arrow_pos); edgesmap[ei]->draw(); //reposition of edgetext XY text_pos=mytab.mapstorage->arrow_pos[ei]; text_pos+=(XY(10,10)); edgetextmap[ei]->property_x().set_value(text_pos.x); edgetextmap[ei]->property_y().set_value(text_pos.y); } } } Gdk::Color GraphDisplayerCanvas::rainbowColorCounter(double min, double max, double w) { Gdk::Color color; double pos=(w-min)/(max-min); int phase=0; //rainbow transitions contain 6 phase //in each phase only one color is changed //first we determine the phase, in which //the actual value belongs to for (int i=0;i<=5;i++) { if(((double)i/6