Add autopackage specfile (not yet working).
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #include "graph_displayer_canvas.h"
23 bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
25 Gnome::Canvas::CanvasAA::on_expose_event(event);
31 void GraphDisplayerCanvas::changeEditorialTool(int newtool)
33 if(actual_tool!=newtool)
36 actual_handler.disconnect();
42 GdkEvent * generated=new GdkEvent();
43 generated->type=GDK_BUTTON_RELEASE;
44 generated->button.button=3;
45 createEdgeEventHandler(generated);
67 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
71 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false);
75 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false);
79 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false);
84 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
93 int GraphDisplayerCanvas::getActualTool()
98 bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e)
100 static Gnome::Canvas::Text *coord_text = 0;
103 case GDK_BUTTON_PRESS:
104 //we mark the location of the event to be able to calculate parameters of dragging
105 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
107 active_item=(get_item_at(clicked_x, clicked_y));
109 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
111 if(nodesmap[i]==active_item)
116 isbutton=e->button.button;
118 case GDK_BUTTON_RELEASE:
128 case GDK_MOTION_NOTIFY:
129 //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
130 if(active_node!=INVALID)
132 (mytab.mapstorage).modified = true;
134 //new coordinates will be the old values,
135 //because the item will be moved to the
136 //new coordinate therefore the new movement
137 //has to be calculated from here
141 window_to_world (e->motion.x, e->motion.y, new_x, new_y);
143 double dx=new_x-clicked_x;
144 double dy=new_y-clicked_y;
151 // reposition the coordinates text
152 std::ostringstream ostr;
154 (mytab.mapstorage).coords[active_node].x << ", " <<
155 (mytab.mapstorage).coords[active_node].y << ")";
157 (nodesmap[active_node]->property_x2().get_value() -
158 nodesmap[active_node]->property_x1().get_value()) / 2.0;
161 coord_text->property_text().set_value(ostr.str());
162 coord_text->property_x().set_value((mytab.mapstorage).coords[active_node].x +
164 coord_text->property_y().set_value((mytab.mapstorage).coords[active_node].y -
169 coord_text = new Gnome::Canvas::Text(
171 (mytab.mapstorage).coords[active_node].x + radius,
172 (mytab.mapstorage).coords[active_node].y - radius,
174 coord_text->property_fill_color().set_value("black");
175 coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST);
186 XY GraphDisplayerCanvas::calcArrowPos(XY moved_node_1, XY moved_node_2, XY fix_node, XY old_arrow_pos, int move_code)
191 return XY((moved_node_2.x + fix_node.x) / 2.0, (moved_node_2.y + fix_node.y) / 2.0);
194 return old_arrow_pos;
198 //////////////////////////////////////////////////////////////////////////////////////////////////////
199 /////////// keeps shape-with scalar multiplication - version 2.
200 //////////////////////////////////////////////////////////////////////////////////////////////////////
202 //old vector from one to the other node - a
203 XY a_v(moved_node_1.x-fix_node.x,moved_node_1.y-fix_node.y);
204 //new vector from one to the other node - b
205 XY b_v(moved_node_2.x-fix_node.x,moved_node_2.y-fix_node.y);
207 double absa=sqrt(a_v.normSquare());
208 double absb=sqrt(b_v.normSquare());
210 if ((absa == 0.0) || (absb == 0.0))
212 return old_arrow_pos;
216 //old vector from one node to the breakpoint - c
217 XY c_v(old_arrow_pos.x-fix_node.x,old_arrow_pos.y-fix_node.y);
219 //unit vector with the same direction to a_v
220 XY a_v_u(a_v.x/absa,a_v.y/absa);
222 //normal vector of unit vector with the same direction to a_v
223 XY a_v_u_n(((-1)*a_v_u.y),a_v_u.x);
225 //unit vector with the same direction to b_v
226 XY b_v_u(b_v.x/absb,b_v.y/absb);
228 //normal vector of unit vector with the same direction to b_v
229 XY b_v_u_n(((-1)*b_v_u.y),b_v_u.x);
231 //vector c in a_v_u and a_v_u_n co-ordinate system
232 XY c_a(c_v*a_v_u,c_v*a_v_u_n);
234 //new vector from one node to the breakpoint - d - we have to calculate this one
235 XY d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n);
237 return XY(d_v.x+fix_node.x,d_v.y+fix_node.y);
247 bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
252 case GDK_MOTION_NOTIFY:
254 GdkEvent * generated=new GdkEvent();
255 generated->motion.x=e->motion.x;
256 generated->motion.y=e->motion.y;
257 generated->type=GDK_MOTION_NOTIFY;
258 moveEventHandler(generated);
262 case GDK_BUTTON_RELEASE:
263 (mytab.mapstorage).modified = true;
267 active_node=(mytab.mapstorage).graph.addNode();
269 //initiating values corresponding to new node in maps
271 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
273 // update coordinates
274 (mytab.mapstorage).coords.set(active_node, XY(clicked_x, clicked_y));
276 // update all other maps
277 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
278 (mytab.mapstorage).nodemap_storage.begin(); it !=
279 (mytab.mapstorage).nodemap_storage.end(); ++it)
281 if ((it->first != "coordinates_x") &&
282 (it->first != "coordinates_y"))
284 (*(it->second))[active_node] =
285 (mytab.mapstorage).nodemap_default[it->first];
288 // increment the id map's default value
289 (mytab.mapstorage).nodemap_default["label"] += 1.0;
291 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph,
292 clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
293 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
294 *(nodesmap[active_node]) <<
295 Gnome::Canvas::Properties::fill_color("blue");
296 *(nodesmap[active_node]) <<
297 Gnome::Canvas::Properties::outline_color("black");
298 active_item->raise_to_top();
300 (nodesmap[active_node])->show();
302 nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph,
303 clicked_x+node_property_defaults[N_RADIUS]+5,
304 clicked_y+node_property_defaults[N_RADIUS]+5, "");
305 nodetextmap[active_node]->property_fill_color().set_value("darkblue");
306 nodetextmap[active_node]->raise_to_top();
308 // mapwin.updateNode(active_node);
309 propertyUpdate(active_node);
322 bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e)
326 case GDK_BUTTON_PRESS:
327 //in edge creation right button has special meaning
328 if(e->button.button!=3)
330 //there is not yet selected node
331 if(active_node==INVALID)
333 //we mark the location of the event to be able to calculate parameters of dragging
335 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
337 active_item=(get_item_at(clicked_x, clicked_y));
339 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
341 if(nodesmap[i]==active_item)
346 //the clicked item is really a node
347 if(active_node!=INVALID)
349 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
352 //clicked item was not a node. It could be e.g. edge.
358 //we only have to do sg. if the mouse button
359 // is pressed already once AND the click was
360 // on a node that was found in the set of
361 //nodes, and now we only search for the second
365 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
366 target_item=(get_item_at(clicked_x, clicked_y));
367 Node target_node=INVALID;
368 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
370 if(nodesmap[i]==target_item)
375 //the clicked item is a node, the edge can be drawn
376 if(target_node!=INVALID)
378 (mytab.mapstorage).modified = true;
380 *(nodesmap[target_node]) <<
381 Gnome::Canvas::Properties::fill_color("red");
384 active_edge=(mytab.mapstorage).graph.addEdge(active_node,
388 for (std::map<std::string,
389 Graph::EdgeMap<double>*>::const_iterator it =
390 (mytab.mapstorage).edgemap_storage.begin(); it !=
391 (mytab.mapstorage).edgemap_storage.end(); ++it)
393 (*(it->second))[active_edge] =
394 (mytab.mapstorage).edgemap_default[it->first];
396 // increment the id map's default value
397 (mytab.mapstorage).edgemap_default["label"] += 1.0;
399 if(target_node!=active_node)
401 // set the coordinates of the arrow on the new edge
402 MapStorage& ms = mytab.mapstorage;
403 ms.arrow_pos.set(active_edge,
404 (ms.coords[ms.graph.source(active_edge)] +
405 ms.coords[ms.graph.target(active_edge)])/ 2.0);
408 edgesmap[active_edge]=new BrokenEdge(displayed_graph, active_edge,
413 // set the coordinates of the arrow on the new edge
414 MapStorage& ms = mytab.mapstorage;
415 ms.arrow_pos.set(active_edge,
416 (ms.coords[ms.graph.source(active_edge)] +
420 edgesmap[active_edge]=new LoopEdge(displayed_graph, active_edge,
424 //initializing edge-text as well, to empty string
425 XY text_pos=mytab.mapstorage.arrow_pos[active_edge];
426 text_pos+=(XY(10,10));
428 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,
429 text_pos.x, text_pos.y, "");
430 edgetextmap[active_edge]->property_fill_color().set_value(
432 edgetextmap[active_edge]->raise_to_top();
434 propertyUpdate(active_edge);
436 //clicked item was not a node. it could be an e.g. edge. we do not
437 //deal with it furthermore.
445 case GDK_BUTTON_RELEASE:
447 //we clear settings in two cases
448 //1: the edge is ready (target_item has valid value)
449 //2: the edge creation is cancelled with right button
450 if((target_item)||(e->button.button==3))
454 *active_item << Gnome::Canvas::Properties::fill_color("blue");
459 *target_item << Gnome::Canvas::Properties::fill_color("blue");
472 bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e)
476 case GDK_BUTTON_PRESS:
477 //finding the clicked items
478 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
479 active_item=(get_item_at(clicked_x, clicked_y));
483 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
485 if(nodesmap[i]==active_item)
491 if(active_node==INVALID)
493 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
495 if(edgesmap[i]->getLine()==active_item)
502 // return if the clicked object is neither an edge nor a node
503 if (active_edge == INVALID) return false;
505 //recolor activated item
508 *active_item << Gnome::Canvas::Properties::fill_color("red");
512 case GDK_BUTTON_RELEASE:
513 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
516 //the cursor was not moved since pressing it
517 if( active_item == ( get_item_at (clicked_x, clicked_y) ) )
520 if(active_node!=INVALID)
522 (mytab.mapstorage).modified = true;
524 std::set<Graph::Edge> edges_to_delete;
526 for(OutEdgeIt e((mytab.mapstorage).graph,active_node);e!=INVALID;++e)
528 edges_to_delete.insert(e);
531 for(InEdgeIt e((mytab.mapstorage).graph,active_node);e!=INVALID;++e)
533 edges_to_delete.insert(e);
536 //deleting collected edges
537 for(std::set<Graph::Edge>::iterator
538 edge_set_it=edges_to_delete.begin();
539 edge_set_it!=edges_to_delete.end();
542 deleteItem(*edge_set_it);
544 deleteItem(active_node);
546 //a simple edge was chosen
547 else if (active_edge != INVALID)
549 deleteItem(active_edge);
552 //pointer was moved, deletion is cancelled
555 if(active_node!=INVALID)
557 *active_item << Gnome::Canvas::Properties::fill_color("blue");
559 else if (active_edge != INVALID)
561 *active_item << Gnome::Canvas::Properties::fill_color("green");
571 case GDK_MOTION_NOTIFY:
580 bool GraphDisplayerCanvas::mapEditEventHandler(GdkEvent* e)
582 if(actual_tool==MAP_EDIT)
586 case GDK_BUTTON_PRESS:
588 //for determine, whether it was an edge
589 Edge clicked_edge=INVALID;
590 //for determine, whether it was a node
591 Node clicked_node=INVALID;
593 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
594 active_item=(get_item_at(clicked_x, clicked_y));
596 //find the activated item between text of nodes
597 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
599 //at the same time only one can be active
600 if(nodetextmap[i]==active_item)
606 //if there was not, search for it between nodes
607 if(clicked_node==INVALID)
609 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
611 //at the same time only one can be active
612 if(nodesmap[i]==active_item)
619 if(clicked_node==INVALID)
621 //find the activated item between texts
622 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
624 //at the same time only one can be active
625 if(edgetextmap[i]==active_item)
631 //if it was not between texts, search for it between edges
632 if(clicked_edge==INVALID)
634 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
636 //at the same time only one can be active
637 if((edgesmap[i]->getLine())==active_item)
645 //if it was really a node...
646 if(clicked_node!=INVALID)
648 // the id map is not editable
649 if (nodemap_to_edit == "label") return 0;
651 //and there is activated map
652 if(nodetextmap[clicked_node]->property_text().get_value()!="")
654 //activate the general variable for it
655 active_node=clicked_node;
658 Gtk::Dialog dialog("Edit value", true);
659 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
660 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
661 Gtk::VBox* vbox = dialog.get_vbox();
662 Gtk::SpinButton spin(0.0, 4);
663 spin.set_increments(1.0, 10.0);
664 spin.set_range(-1000000.0, 1000000.0);
665 spin.set_numeric(true);
666 spin.set_value(atof(nodetextmap[active_node]->property_text().get_value().c_str()));
669 switch (dialog.run())
671 case Gtk::RESPONSE_NONE:
672 case Gtk::RESPONSE_CANCEL:
674 case Gtk::RESPONSE_ACCEPT:
675 double new_value = spin.get_value();
676 (*(mytab.mapstorage).nodemap_storage[nodemap_to_edit])[active_node] =
678 std::ostringstream ostr;
680 nodetextmap[active_node]->property_text().set_value(ostr.str());
681 //mapwin.updateNode(active_node);
682 //mapwin.updateNode(Node(INVALID));
683 propertyUpdate(Node(INVALID));
688 //if it was really an edge...
689 if(clicked_edge!=INVALID)
691 // the id map is not editable
692 if (edgemap_to_edit == "label") return 0;
694 //and there is activated map
695 if(edgetextmap[clicked_edge]->property_text().get_value()!="")
697 //activate the general variable for it
698 active_edge=clicked_edge;
701 Gtk::Dialog dialog("Edit value", true);
702 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
703 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
704 Gtk::VBox* vbox = dialog.get_vbox();
705 Gtk::SpinButton spin(0.0, 4);
706 spin.set_increments(1.0, 10.0);
707 spin.set_range(-1000000.0, 1000000.0);
708 spin.set_numeric(true);
709 spin.set_value(atof(edgetextmap[active_edge]->property_text().get_value().c_str()));
712 switch (dialog.run())
714 case Gtk::RESPONSE_NONE:
715 case Gtk::RESPONSE_CANCEL:
717 case Gtk::RESPONSE_ACCEPT:
718 double new_value = spin.get_value();
719 (*(mytab.mapstorage).edgemap_storage[edgemap_to_edit])[active_edge] =
721 std::ostringstream ostr;
723 edgetextmap[active_edge]->property_text().set_value(
725 //mapwin.updateEdge(active_edge);
726 // mapwin.updateEdge(Edge(INVALID));
727 propertyUpdate(Edge(INVALID));
740 void GraphDisplayerCanvas::deleteItem(Node node_to_delete)
742 delete(nodetextmap[node_to_delete]);
743 delete(nodesmap[node_to_delete]);
744 (mytab.mapstorage).graph.erase(node_to_delete);
747 void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete)
749 delete(edgetextmap[edge_to_delete]);
750 delete(edgesmap[edge_to_delete]);
751 (mytab.mapstorage).graph.erase(edge_to_delete);
754 void GraphDisplayerCanvas::textReposition(XY new_place)
756 new_place+=(XY(10,10));
757 edgetextmap[forming_edge]->property_x().set_value(new_place.x);
758 edgetextmap[forming_edge]->property_y().set_value(new_place.y);
761 void GraphDisplayerCanvas::toggleEdgeActivity(EdgeBase* active_bre, bool on)
765 if(forming_edge!=INVALID)
767 std::cerr << "ERROR!!!! Valid edge found!" << std::endl;
771 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
773 if(edgesmap[i]==active_bre)
782 if(forming_edge!=INVALID)
784 forming_edge=INVALID;
788 std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
793 void GraphDisplayerCanvas::moveNode(double dx, double dy, Gnome::Canvas::Item * item, Node node)
795 Gnome::Canvas::Item * moved_item=item;
796 Node moved_node=node;
798 if(item==NULL && node==INVALID)
800 moved_item=active_item;
801 moved_node=active_node;
808 //repositioning node and its text
809 moved_item->move(dx, dy);
810 nodetextmap[moved_node]->move(dx, dy);
812 // the new coordinates of the centre of the node
813 double coord_x = dx + (mytab.mapstorage).coords[moved_node].x;
814 double coord_y = dy + (mytab.mapstorage).coords[moved_node].y;
816 // write back the new coordinates to the coords map
817 (mytab.mapstorage).coords.set(moved_node, XY(coord_x, coord_y));
819 //all the edges connected to the moved point has to be redrawn
820 for(OutEdgeIt ei((mytab.mapstorage).graph,moved_node);ei!=INVALID;++ei)
824 if (mytab.mapstorage.graph.source(ei) == mytab.mapstorage.graph.target(ei))
826 arrow_pos = mytab.mapstorage.arrow_pos[ei] + XY(dx, dy);
830 XY moved_node_1(coord_x - dx, coord_y - dy);
831 XY moved_node_2(coord_x, coord_y);
832 Node target = mytab.mapstorage.graph.target(ei);
833 XY fix_node(mytab.mapstorage.coords[target].x,
834 mytab.mapstorage.coords[target].y);
835 XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
837 arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton);
840 mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
841 edgesmap[ei]->draw();
843 //reposition of edgetext
844 XY text_pos=mytab.mapstorage.arrow_pos[ei];
845 text_pos+=(XY(10,10));
846 edgetextmap[ei]->property_x().set_value(text_pos.x);
847 edgetextmap[ei]->property_y().set_value(text_pos.y);
850 for(InEdgeIt ei((mytab.mapstorage).graph,moved_node);ei!=INVALID;++ei)
852 if (mytab.mapstorage.graph.source(ei) != mytab.mapstorage.graph.target(ei))
854 XY moved_node_1(coord_x - dx, coord_y - dy);
855 XY moved_node_2(coord_x, coord_y);
856 Node source = mytab.mapstorage.graph.source(ei);
857 XY fix_node(mytab.mapstorage.coords[source].x,
858 mytab.mapstorage.coords[source].y);
859 XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
862 arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton);
864 mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
865 edgesmap[ei]->draw();
867 //reposition of edgetext
868 XY text_pos=mytab.mapstorage.arrow_pos[ei];
869 text_pos+=(XY(10,10));
870 edgetextmap[ei]->property_x().set_value(text_pos.x);
871 edgetextmap[ei]->property_y().set_value(text_pos.y);