Removed this extra widget thing, because it is now developed in my private branch.
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;
269 active_node=(mytab.mapstorage).graph.addNode();
271 //initiating values corresponding to new node in maps
273 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
275 // update coordinates
276 (mytab.mapstorage).coords.set(active_node, XY(clicked_x, clicked_y));
278 // update all other maps
279 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
280 (mytab.mapstorage).nodemap_storage.begin(); it !=
281 (mytab.mapstorage).nodemap_storage.end(); ++it)
283 if ((it->first != "coordinates_x") &&
284 (it->first != "coordinates_y"))
286 (*(it->second))[active_node] =
287 (mytab.mapstorage).nodemap_default[it->first];
290 // increment the id map's default value
291 (mytab.mapstorage).nodemap_default["label"] += 1.0;
293 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph,
294 clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
295 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
296 *(nodesmap[active_node]) <<
297 Gnome::Canvas::Properties::fill_color("blue");
298 *(nodesmap[active_node]) <<
299 Gnome::Canvas::Properties::outline_color("black");
300 active_item->raise_to_top();
302 (nodesmap[active_node])->show();
304 nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph,
305 clicked_x+node_property_defaults[N_RADIUS]+5,
306 clicked_y+node_property_defaults[N_RADIUS]+5, "");
307 nodetextmap[active_node]->property_fill_color().set_value("darkblue");
308 nodetextmap[active_node]->raise_to_top();
310 // mapwin.updateNode(active_node);
311 propertyUpdate(active_node);
324 bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e)
328 case GDK_BUTTON_PRESS:
329 //in edge creation right button has special meaning
330 if(e->button.button!=3)
332 //there is not yet selected node
333 if(active_node==INVALID)
335 //we mark the location of the event to be able to calculate parameters of dragging
337 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
339 active_item=(get_item_at(clicked_x, clicked_y));
341 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
343 if(nodesmap[i]==active_item)
348 //the clicked item is really a node
349 if(active_node!=INVALID)
351 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
354 //clicked item was not a node. It could be e.g. edge.
360 //we only have to do sg. if the mouse button
361 // is pressed already once AND the click was
362 // on a node that was found in the set of
363 //nodes, and now we only search for the second
367 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
368 target_item=(get_item_at(clicked_x, clicked_y));
369 Node target_node=INVALID;
370 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
372 if(nodesmap[i]==target_item)
377 //the clicked item is a node, the edge can be drawn
378 if(target_node!=INVALID)
380 (mytab.mapstorage).modified = true;
382 *(nodesmap[target_node]) <<
383 Gnome::Canvas::Properties::fill_color("red");
386 active_edge=(mytab.mapstorage).graph.addEdge(active_node,
390 for (std::map<std::string,
391 Graph::EdgeMap<double>*>::const_iterator it =
392 (mytab.mapstorage).edgemap_storage.begin(); it !=
393 (mytab.mapstorage).edgemap_storage.end(); ++it)
395 (*(it->second))[active_edge] =
396 (mytab.mapstorage).edgemap_default[it->first];
398 // increment the id map's default value
399 (mytab.mapstorage).edgemap_default["label"] += 1.0;
401 if(target_node!=active_node)
403 // set the coordinates of the arrow on the new edge
404 MapStorage& ms = mytab.mapstorage;
405 ms.arrow_pos.set(active_edge,
406 (ms.coords[ms.graph.source(active_edge)] +
407 ms.coords[ms.graph.target(active_edge)])/ 2.0);
410 edgesmap[active_edge]=new BrokenEdge(displayed_graph, active_edge,
415 // set the coordinates of the arrow on the new edge
416 MapStorage& ms = mytab.mapstorage;
417 ms.arrow_pos.set(active_edge,
418 (ms.coords[ms.graph.source(active_edge)] +
422 edgesmap[active_edge]=new LoopEdge(displayed_graph, active_edge,
426 //initializing edge-text as well, to empty string
427 XY text_pos=mytab.mapstorage.arrow_pos[active_edge];
428 text_pos+=(XY(10,10));
430 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,
431 text_pos.x, text_pos.y, "");
432 edgetextmap[active_edge]->property_fill_color().set_value(
434 edgetextmap[active_edge]->raise_to_top();
436 propertyUpdate(active_edge);
438 //clicked item was not a node. it could be an e.g. edge. we do not
439 //deal with it furthermore.
447 case GDK_BUTTON_RELEASE:
449 //we clear settings in two cases
450 //1: the edge is ready (target_item has valid value)
451 //2: the edge creation is cancelled with right button
452 if((target_item)||(e->button.button==3))
456 propertyUpdate(active_node,N_COLOR);
461 propertyUpdate((mytab.mapstorage).graph.target(active_edge),N_COLOR);
474 bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e)
478 case GDK_BUTTON_PRESS:
479 //finding the clicked items
480 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
481 active_item=(get_item_at(clicked_x, clicked_y));
485 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
487 if(nodesmap[i]==active_item)
493 if(active_node==INVALID)
495 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
497 if(edgesmap[i]->getLine()==active_item)
504 // return if the clicked object is neither an edge nor a node
505 if (active_edge == INVALID) return false;
507 //recolor activated item
510 *active_item << Gnome::Canvas::Properties::fill_color("red");
514 case GDK_BUTTON_RELEASE:
515 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
518 //the cursor was not moved since pressing it
519 if( active_item == ( get_item_at (clicked_x, clicked_y) ) )
522 if(active_node!=INVALID)
524 (mytab.mapstorage).modified = true;
526 std::set<Graph::Edge> edges_to_delete;
528 for(OutEdgeIt e((mytab.mapstorage).graph,active_node);e!=INVALID;++e)
530 edges_to_delete.insert(e);
533 for(InEdgeIt e((mytab.mapstorage).graph,active_node);e!=INVALID;++e)
535 edges_to_delete.insert(e);
538 //deleting collected edges
539 for(std::set<Graph::Edge>::iterator
540 edge_set_it=edges_to_delete.begin();
541 edge_set_it!=edges_to_delete.end();
544 deleteItem(*edge_set_it);
546 deleteItem(active_node);
548 //a simple edge was chosen
549 else if (active_edge != INVALID)
551 deleteItem(active_edge);
554 //pointer was moved, deletion is cancelled
557 if(active_node!=INVALID)
559 *active_item << Gnome::Canvas::Properties::fill_color("blue");
561 else if (active_edge != INVALID)
563 *active_item << Gnome::Canvas::Properties::fill_color("green");
573 case GDK_MOTION_NOTIFY:
582 bool GraphDisplayerCanvas::mapEditEventHandler(GdkEvent* e)
584 if(actual_tool==MAP_EDIT)
588 case GDK_BUTTON_PRESS:
590 //for determine, whether it was an edge
591 Edge clicked_edge=INVALID;
592 //for determine, whether it was a node
593 Node clicked_node=INVALID;
595 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
596 active_item=(get_item_at(clicked_x, clicked_y));
598 //find the activated item between text of nodes
599 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
601 //at the same time only one can be active
602 if(nodetextmap[i]==active_item)
608 //if there was not, search for it between nodes
609 if(clicked_node==INVALID)
611 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
613 //at the same time only one can be active
614 if(nodesmap[i]==active_item)
621 if(clicked_node==INVALID)
623 //find the activated item between texts
624 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
626 //at the same time only one can be active
627 if(edgetextmap[i]==active_item)
633 //if it was not between texts, search for it between edges
634 if(clicked_edge==INVALID)
636 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
638 //at the same time only one can be active
639 if((edgesmap[i]->getLine())==active_item)
647 //if it was really a node...
648 if(clicked_node!=INVALID)
650 // the id map is not editable
651 if (nodemap_to_edit == "label") return 0;
653 //and there is activated map
654 if(nodetextmap[clicked_node]->property_text().get_value()!="")
656 //activate the general variable for it
657 active_node=clicked_node;
660 Gtk::Dialog dialog("Edit value", true);
661 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
662 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
663 Gtk::VBox* vbox = dialog.get_vbox();
664 Gtk::SpinButton spin(0.0, 4);
665 spin.set_increments(1.0, 10.0);
666 spin.set_range(-1000000.0, 1000000.0);
667 spin.set_numeric(true);
668 spin.set_value(atof(nodetextmap[active_node]->property_text().get_value().c_str()));
671 switch (dialog.run())
673 case Gtk::RESPONSE_NONE:
674 case Gtk::RESPONSE_CANCEL:
676 case Gtk::RESPONSE_ACCEPT:
677 double new_value = spin.get_value();
678 (*(mytab.mapstorage).nodemap_storage[nodemap_to_edit])[active_node] =
680 std::ostringstream ostr;
682 nodetextmap[active_node]->property_text().set_value(ostr.str());
683 //mapwin.updateNode(active_node);
684 //mapwin.updateNode(Node(INVALID));
685 propertyUpdate(Node(INVALID));
690 //if it was really an edge...
691 if(clicked_edge!=INVALID)
693 // the id map is not editable
694 if (edgemap_to_edit == "label") return 0;
696 //and there is activated map
697 if(edgetextmap[clicked_edge]->property_text().get_value()!="")
699 //activate the general variable for it
700 active_edge=clicked_edge;
703 Gtk::Dialog dialog("Edit value", true);
704 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
705 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
706 Gtk::VBox* vbox = dialog.get_vbox();
707 Gtk::SpinButton spin(0.0, 4);
708 spin.set_increments(1.0, 10.0);
709 spin.set_range(-1000000.0, 1000000.0);
710 spin.set_numeric(true);
711 spin.set_value(atof(edgetextmap[active_edge]->property_text().get_value().c_str()));
714 switch (dialog.run())
716 case Gtk::RESPONSE_NONE:
717 case Gtk::RESPONSE_CANCEL:
719 case Gtk::RESPONSE_ACCEPT:
720 double new_value = spin.get_value();
721 (*(mytab.mapstorage).edgemap_storage[edgemap_to_edit])[active_edge] =
723 std::ostringstream ostr;
725 edgetextmap[active_edge]->property_text().set_value(
727 //mapwin.updateEdge(active_edge);
728 // mapwin.updateEdge(Edge(INVALID));
729 propertyUpdate(Edge(INVALID));
742 void GraphDisplayerCanvas::deleteItem(Node node_to_delete)
744 delete(nodetextmap[node_to_delete]);
745 delete(nodesmap[node_to_delete]);
746 (mytab.mapstorage).graph.erase(node_to_delete);
749 void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete)
751 delete(edgetextmap[edge_to_delete]);
752 delete(edgesmap[edge_to_delete]);
753 (mytab.mapstorage).graph.erase(edge_to_delete);
756 void GraphDisplayerCanvas::textReposition(XY new_place)
758 new_place+=(XY(10,10));
759 edgetextmap[forming_edge]->property_x().set_value(new_place.x);
760 edgetextmap[forming_edge]->property_y().set_value(new_place.y);
763 void GraphDisplayerCanvas::toggleEdgeActivity(EdgeBase* active_bre, bool on)
767 if(forming_edge!=INVALID)
769 std::cerr << "ERROR!!!! Valid edge found!" << std::endl;
773 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
775 if(edgesmap[i]==active_bre)
784 if(forming_edge!=INVALID)
786 forming_edge=INVALID;
790 std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
795 void GraphDisplayerCanvas::moveNode(double dx, double dy, Gnome::Canvas::Item * item, Node node)
797 Gnome::Canvas::Item * moved_item=item;
798 Node moved_node=node;
800 if(item==NULL && node==INVALID)
802 moved_item=active_item;
803 moved_node=active_node;
810 //repositioning node and its text
811 moved_item->move(dx, dy);
812 nodetextmap[moved_node]->move(dx, dy);
814 // the new coordinates of the centre of the node
815 double coord_x = dx + (mytab.mapstorage).coords[moved_node].x;
816 double coord_y = dy + (mytab.mapstorage).coords[moved_node].y;
818 // write back the new coordinates to the coords map
819 (mytab.mapstorage).coords.set(moved_node, XY(coord_x, coord_y));
821 //all the edges connected to the moved point has to be redrawn
822 for(OutEdgeIt ei((mytab.mapstorage).graph,moved_node);ei!=INVALID;++ei)
826 if (mytab.mapstorage.graph.source(ei) == mytab.mapstorage.graph.target(ei))
828 arrow_pos = mytab.mapstorage.arrow_pos[ei] + XY(dx, dy);
832 XY moved_node_1(coord_x - dx, coord_y - dy);
833 XY moved_node_2(coord_x, coord_y);
834 Node target = mytab.mapstorage.graph.target(ei);
835 XY fix_node(mytab.mapstorage.coords[target].x,
836 mytab.mapstorage.coords[target].y);
837 XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
839 arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton);
842 mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
843 edgesmap[ei]->draw();
845 //reposition of edgetext
846 XY text_pos=mytab.mapstorage.arrow_pos[ei];
847 text_pos+=(XY(10,10));
848 edgetextmap[ei]->property_x().set_value(text_pos.x);
849 edgetextmap[ei]->property_y().set_value(text_pos.y);
852 for(InEdgeIt ei((mytab.mapstorage).graph,moved_node);ei!=INVALID;++ei)
854 if (mytab.mapstorage.graph.source(ei) != mytab.mapstorage.graph.target(ei))
856 XY moved_node_1(coord_x - dx, coord_y - dy);
857 XY moved_node_2(coord_x, coord_y);
858 Node source = mytab.mapstorage.graph.source(ei);
859 XY fix_node(mytab.mapstorage.coords[source].x,
860 mytab.mapstorage.coords[source].y);
861 XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
864 arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton);
866 mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
867 edgesmap[ei]->draw();
869 //reposition of edgetext
870 XY text_pos=mytab.mapstorage.arrow_pos[ei];
871 text_pos+=(XY(10,10));
872 edgetextmap[ei]->property_x().set_value(text_pos.x);
873 edgetextmap[ei]->property_y().set_value(text_pos.y);
878 Gdk::Color GraphDisplayerCanvas::rainbowColorCounter(double min, double max, double w)
882 double pos=(w-min)/(max-min);
885 //rainbow transitions contain 6 phase
886 //in each phase only one color is changed
887 //first we determine the phase, in which
888 //the actual value belongs to
889 for (int i=0;i<=5;i++)
891 if(((double)i/6<pos)&&(pos<=(double(i+1)/6)))
898 //within its 1/6 long phase the relativ position
899 //determines the power of the color changed in
901 //we normalize that to one, to be able to give percentage
902 //value for the function
903 double rel_pos=(pos-(phase/6))*6;
908 color.set_rgb_p (1, 0, 1-rel_pos);
911 color.set_rgb_p (1, rel_pos, 0);
914 color.set_rgb_p (1-rel_pos, 1, 0);
917 color.set_rgb_p (0, 1, rel_pos);
920 color.set_rgb_p (0, 1-rel_pos, 1);
923 color.set_rgb_p ((rel_pos/3), 0, 1);
926 std::cout << "Wrong phase: " << phase << " " << pos << std::endl;
931 std::cout << "Wrong phase: " << phase << " " << pos << std::endl;