1 #include "graph_displayer_canvas.h"
2 #include "broken_edge.h"
6 bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
8 Gnome::Canvas::CanvasAA::on_expose_event(event);
14 void GraphDisplayerCanvas::changeEditorialTool(int newtool)
16 if(actual_tool!=newtool)
19 actual_handler.disconnect();
25 GdkEvent * generated=new GdkEvent();
26 generated->type=GDK_BUTTON_RELEASE;
27 generated->button.button=3;
28 createEdgeEventHandler(generated);
32 //has to do the same thing as in the case of NODE_MAP_EDIT
35 GdkEvent * generated=new GdkEvent();
36 generated->type=GDK_KEY_PRESS;
37 ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
38 entryWidgetChangeHandler(generated);
57 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
61 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false);
65 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false);
69 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false);
74 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false);
78 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false);
87 int GraphDisplayerCanvas::getActualTool()
92 bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e)
96 case GDK_BUTTON_PRESS:
97 //we mark the location of the event to be able to calculate parameters of dragging
98 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
100 active_item=(get_item_at(clicked_x, clicked_y));
102 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
104 if(nodesmap[i]==active_item)
109 switch(e->button.button)
119 case GDK_BUTTON_RELEASE:
124 case GDK_MOTION_NOTIFY:
125 //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
126 if(active_node!=INVALID)
128 mapstorage.modified = true;
129 mapstorage.coords.set(active_node, xy<double>(clicked_x, clicked_y));
130 //new coordinates will be the old values,
131 //because the item will be moved to the
132 //new coordinate therefore the new movement
133 //has to be calculated from here
137 window_to_world (e->motion.x, e->motion.y, new_x, new_y);
139 double dx=new_x-clicked_x;
140 double dy=new_y-clicked_y;
142 //repositioning node and its text
143 active_item->move(dx, dy);
144 nodetextmap[active_node]->move(dx, dy);
149 //all the edges connected to the moved point has to be redrawn
152 mapstorage.graph.firstOut(ei,active_node);
154 for(;ei!=INVALID;mapstorage.graph.nextOut(ei))
156 Gnome::Canvas::Points coos;
157 double x1, x2, y1, y2;
159 nodesmap[mapstorage.graph.source(ei)]->get_bounds(x1, y1, x2, y2);
160 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
162 nodesmap[mapstorage.graph.target(ei)]->get_bounds(x1, y1, x2, y2);
163 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
167 edgesmap[ei]->setPoints(coos);
171 edgesmap[ei]->setPoints(coos,true);
174 //reposition of edgetext
175 xy<double> text_pos=edgesmap[ei]->getArrowPos();
176 text_pos+=(xy<double>(10,10));
177 edgetextmap[ei]->property_x().set_value(text_pos.x);
178 edgetextmap[ei]->property_y().set_value(text_pos.y);
181 mapstorage.graph.firstIn(ei,active_node);
182 for(;ei!=INVALID;mapstorage.graph.nextIn(ei))
184 Gnome::Canvas::Points coos;
185 double x1, x2, y1, y2;
187 nodesmap[mapstorage.graph.source(ei)]->get_bounds(x1, y1, x2, y2);
188 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
190 nodesmap[mapstorage.graph.target(ei)]->get_bounds(x1, y1, x2, y2);
191 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
195 edgesmap[ei]->setPoints(coos);
199 edgesmap[ei]->setPoints(coos,true);
202 xy<double> text_pos=edgesmap[ei]->getArrowPos();
203 text_pos+=(xy<double>(10,10));
204 edgetextmap[ei]->property_x().set_value(text_pos.x);
205 edgetextmap[ei]->property_y().set_value(text_pos.y);
214 bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
219 case GDK_MOTION_NOTIFY:
221 GdkEvent * generated=new GdkEvent();
222 generated->motion.x=e->motion.x;
223 generated->motion.y=e->motion.y;
224 generated->type=GDK_MOTION_NOTIFY;
225 moveEventHandler(generated);
229 case GDK_BUTTON_RELEASE:
230 mapstorage.modified = true;
234 active_node=mapstorage.graph.addNode();
236 //initiating values corresponding to new node in maps
238 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
240 // update coordinates
241 mapstorage.coords.set(active_node, xy<double>(clicked_x, clicked_y));
243 // update all other maps
244 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
245 mapstorage.nodemap_storage.begin(); it !=
246 mapstorage.nodemap_storage.end(); ++it)
248 if ((it->first != "coordinates_x") &&
249 (it->first != "coordinates_y"))
251 (*(it->second))[active_node] =
252 mapstorage.nodemap_default[it->first];
255 // increment the id map's default value
256 mapstorage.nodemap_default["id"] += 1.0;
258 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph,
259 clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
260 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
261 *(nodesmap[active_node]) <<
262 Gnome::Canvas::Properties::fill_color("blue");
263 *(nodesmap[active_node]) <<
264 Gnome::Canvas::Properties::outline_color("black");
265 active_item->raise_to_top();
267 (nodesmap[active_node])->show();
269 nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph,
270 clicked_x+node_property_defaults[N_RADIUS]+5,
271 clicked_y+node_property_defaults[N_RADIUS]+5, "");
272 nodetextmap[active_node]->property_fill_color().set_value("darkblue");
273 nodetextmap[active_node]->raise_to_top();
275 mapwin.updateNode(active_node);
289 bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e)
293 case GDK_BUTTON_PRESS:
294 //in edge creation right button has special meaning
295 if(e->button.button!=3)
297 //there is not yet selected node
298 if(active_node==INVALID)
300 //we mark the location of the event to be able to calculate parameters of dragging
302 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
304 active_item=(get_item_at(clicked_x, clicked_y));
306 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
308 if(nodesmap[i]==active_item)
313 //the clicked item is really a node
314 if(active_node!=INVALID)
316 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
319 //clicked item was not a node. It could be e.g. edge.
325 //we only have to do sg. if the mouse button
326 // is pressed already once AND the click was
327 // on a node that was found in the set of
328 //nodes, and now we only search for the second
332 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
333 target_item=(get_item_at(clicked_x, clicked_y));
334 Node target_node=INVALID;
335 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
337 if(nodesmap[i]==target_item)
342 //the clicked item is a node, the edge can be drawn
343 if(target_node!=INVALID)
345 if(target_node!=active_node)
347 mapstorage.modified = true;
349 *(nodesmap[target_node]) <<
350 Gnome::Canvas::Properties::fill_color("red");
353 active_edge=mapstorage.graph.addEdge(active_node,
357 for (std::map<std::string,
358 Graph::EdgeMap<double>*>::const_iterator it =
359 mapstorage.edgemap_storage.begin(); it !=
360 mapstorage.edgemap_storage.end(); ++it)
362 (*(it->second))[active_edge] =
363 mapstorage.edgemap_default[it->first];
365 // increment the id map's default value
366 mapstorage.edgemap_default["id"] += 1.0;
368 //calculating coordinates of new edge
369 Gnome::Canvas::Points coos;
370 double x1, x2, y1, y2;
372 active_item->get_bounds(x1, y1, x2, y2);
373 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
375 target_item->get_bounds(x1, y1, x2, y2);
376 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
379 edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos,
381 *(edgesmap[active_edge]) <<
382 Gnome::Canvas::Properties::fill_color("green");
383 edgesmap[active_edge]->property_width_pixels().set_value(10);
385 edgesmap[active_edge]->lower_to_bottom();
387 //initializing edge-text as well, to empty string
388 xy<double> text_pos=edgesmap[active_edge]->getArrowPos();
389 text_pos+=(xy<double>(10,10));
391 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,
392 text_pos.x, text_pos.y, "");
393 edgetextmap[active_edge]->property_fill_color().set_value(
395 edgetextmap[active_edge]->raise_to_top();
397 //updating its properties
398 mapwin.updateEdge(active_edge);
403 std::cerr << "Loop edge is not yet implemented!" << std::endl;
406 //clicked item was not a node. it could be an e.g. edge. we do not
407 //deal with it furthermore.
415 case GDK_BUTTON_RELEASE:
417 //we clear settings in two cases
418 //1: the edge is ready (target_item has valid value)
419 //2: the edge creation is cancelled with right button
420 if((target_item)||(e->button.button==3))
424 *active_item << Gnome::Canvas::Properties::fill_color("blue");
429 *target_item << Gnome::Canvas::Properties::fill_color("blue");
442 bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e)
446 case GDK_BUTTON_PRESS:
447 //finding the clicked items
448 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
449 active_item=(get_item_at(clicked_x, clicked_y));
453 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
455 if(nodesmap[i]==active_item)
461 if(active_node==INVALID)
463 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
465 if(edgesmap[i]==active_item)
472 //recolor activated item
475 *active_item << Gnome::Canvas::Properties::fill_color("red");
479 case GDK_BUTTON_RELEASE:
480 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
483 //the cursor was not moved since pressing it
484 if( active_item == ( get_item_at (clicked_x, clicked_y) ) )
487 if(active_node!=INVALID)
489 mapstorage.modified = true;
491 //collecting edges to delete
493 std::set<Graph::Edge> edges_to_delete;
495 mapstorage.graph.firstOut(e,active_node);
496 for(;e!=INVALID;mapstorage.graph.nextOut(e))
498 edges_to_delete.insert(e);
501 mapstorage.graph.firstIn(e,active_node);
502 for(;e!=INVALID;mapstorage.graph.nextIn(e))
504 edges_to_delete.insert(e);
507 //deleting collected edges
508 for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
510 deleteItem(*edge_set_it);
512 deleteItem(active_node);
514 //a simple edge was chosen
517 deleteItem(active_edge);
520 //pointer was moved, deletion is cancelled
523 if(active_node!=INVALID)
525 *active_item << Gnome::Canvas::Properties::fill_color("blue");
529 *active_item << Gnome::Canvas::Properties::fill_color("green");
539 case GDK_MOTION_NOTIFY:
548 bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e)
550 if(actual_tool==EDGE_MAP_EDIT)
555 //for Escape or Enter hide the displayed widget
557 nodeMapEditEventHandler(e);
560 case GDK_BUTTON_PRESS:
561 //If the click happened on an edge we place the entrywidget there and fill in the value of the activated map at that edge.
563 //for determine, whether it was an edge
564 Edge clicked_edge=INVALID;
566 //find the activated item between texts
567 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
568 active_item=(get_item_at(clicked_x, clicked_y));
569 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
571 if(edgetextmap[i]==active_item)
577 //if it was not between texts, search for it between edges
578 if(clicked_edge==INVALID)
580 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
582 //at the same time only one can be active
583 if((edgesmap[i]==active_item)||(edgetextmap[i]==active_item))
589 //if it was really an edge...
590 if(clicked_edge!=INVALID)
592 // the id map is not editable
593 if (edgemap_to_edit == "id") return 0;
595 //If there is already edited edge, it has to be saved first
596 if(entrywidget.is_visible())
598 GdkEvent * generated=new GdkEvent();
599 generated->type=GDK_KEY_PRESS;
600 ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
601 entryWidgetChangeHandler(generated);
603 //If the previous value could be saved, we can go further, otherwise not
604 if(!entrywidget.is_visible())
606 //and there is activated map
607 if(edgetextmap[clicked_edge]->property_text().get_value()!="")
609 //activate the general variable for it
610 active_edge=clicked_edge;
611 //delete visible widget if there is
612 if(canvasentrywidget)
614 delete(canvasentrywidget);
617 //initialize the entry
620 //fill in the correct value
621 entrywidget.set_text(edgetextmap[active_edge]->property_text().get_value());
623 //replace and resize the entry to the activated edge and put it in a Canvas::Widget to be able to display it on gdc
624 xy<double> entry_coos;
625 entry_coos.x=(edgetextmap[active_edge])->property_x().get_value();
626 entry_coos.x-=edgetextmap[active_edge]->property_text_width().get_value()/2;
627 entry_coos.y=(edgetextmap[active_edge])->property_y().get_value();
628 entry_coos.y-=edgetextmap[active_edge]->property_text_height().get_value()*1.5/2;
629 canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget);
630 canvasentrywidget->property_width().set_value(edgetextmap[active_edge]->property_text_width().get_value()*4);
631 canvasentrywidget->property_height().set_value(edgetextmap[active_edge]->property_text_height().get_value()*1.5);
633 //setting the focus to newly created widget
634 parentwin->set_focus(entrywidget);
635 parentwin->activate_focus();
639 //if it was not an edge...
642 //In this case the click did not happen on an edge
643 //if there is visible entry we save the value in it
644 //we pretend like an Enter was presse din the Entry widget
645 GdkEvent * generated=new GdkEvent();
646 generated->type=GDK_KEY_PRESS;
647 ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
648 entryWidgetChangeHandler(generated);
659 bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e)
661 if(actual_tool==NODE_MAP_EDIT)
666 //for Escape or Enter hide the displayed widget
668 switch(((GdkEventKey*)e)->keyval)
683 case GDK_BUTTON_PRESS:
684 //If the click happened on an edge we place the entrywidget there and fill in the value of the activated map at that edge.
686 //for determine, whether it was a node
687 Node clicked_node=INVALID;
689 //find the activated item between texts
690 active_item=(get_item_at(e->button.x, e->button.y));
691 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
693 //at the same time only one can be active
694 if(nodetextmap[i]==active_item)
700 //if there was not, search for it between nodes
701 if(clicked_node==INVALID)
703 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
704 active_item=(get_item_at(clicked_x, clicked_y));
706 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
708 //at the same time only one can be active
709 if(nodesmap[i]==active_item)
715 //if it was really an edge...
716 if(clicked_node!=INVALID)
718 // the id map is not editable
719 if (nodemap_to_edit == "id") return 0;
720 //If there is already edited edge, it has to be saved first
721 if(entrywidget.is_visible())
723 GdkEvent * generated=new GdkEvent();
724 generated->type=GDK_KEY_PRESS;
725 ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
726 entryWidgetChangeHandler(generated);
728 //If the previous value could be saved, we can go further, otherwise not
729 if(!entrywidget.is_visible())
731 //and there is activated map
732 if(nodetextmap[clicked_node]->property_text().get_value()!="")
734 //activate the general variable for it
735 active_node=clicked_node;
736 //delete visible widget if there is
737 if(canvasentrywidget)
739 delete(canvasentrywidget);
742 //initialize the entry
745 //fill in the correct value
746 entrywidget.set_text(nodetextmap[active_node]->property_text().get_value());
748 //replace and resize the entry to the activated node and put it in a Canvas::Widget to be able to display it on gdc
749 xy<double> entry_coos;
750 entry_coos.x=(nodetextmap[active_node])->property_x().get_value();
751 entry_coos.x-=nodetextmap[active_node]->property_text_width().get_value()/2;
752 entry_coos.y=(nodetextmap[active_node])->property_y().get_value();
753 entry_coos.y-=nodetextmap[active_node]->property_text_height().get_value()*1.5/2;
754 canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget);
755 canvasentrywidget->property_width().set_value(nodetextmap[active_node]->property_text_width().get_value()*4);
756 canvasentrywidget->property_height().set_value(nodetextmap[active_node]->property_text_height().get_value()*1.5);
760 //if it was not an edge...
763 //In this case the click did not happen on an edge
764 //if there is visible entry we save the value in it
765 //we pretend like an Enter was presse din the Entry widget
766 GdkEvent * generated=new GdkEvent();
767 generated->type=GDK_KEY_PRESS;
768 ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
769 entryWidgetChangeHandler(generated);
780 bool GraphDisplayerCanvas::entryWidgetChangeHandler(GdkEvent* e)
782 if(entrywidget.is_visible())
784 if(e->type==GDK_KEY_PRESS)
786 switch(((GdkEventKey*)e)->keyval)
794 //these variables check whether the text in the entry is valid
795 bool valid_double=true;
798 //getting the value from the entry and converting it to double
799 Glib::ustring mapvalue_str = entrywidget.get_text();
801 char * mapvalue_ch=new char [mapvalue_str.length()];
802 for(int i=0;i<(int)(mapvalue_str.length());i++)
804 if(((mapvalue_str[i]<'0')||(mapvalue_str[i]>'9'))&&(mapvalue_str[i]!='.'))
810 if(mapvalue_str[i]=='.')
815 mapvalue_ch[i]=mapvalue_str[i];
818 //if the text in the entry was correct
819 if((point_num<=1)&&(valid_double))
821 double mapvalue_d=atof(mapvalue_ch);
823 //reconvert the double to string for the correct format
824 std::ostringstream ostr;
827 //save the value to the correct place
831 edgetextmap[active_edge]->property_text().set_value(ostr.str());
832 (*(mapstorage.edgemap_storage)[edgemap_to_edit])[active_edge]=mapvalue_d;
833 mapwin.updateEdge(active_edge);
836 nodetextmap[active_node]->property_text().set_value(ostr.str());
837 (*(mapstorage.nodemap_storage)[nodemap_to_edit])[active_node]=mapvalue_d;
838 mapwin.updateNode(active_node);
845 //the text in the entry was not correct for a double
848 std::cerr << "ERROR: only handling of double values is implemented yet!" << std::endl;
861 void GraphDisplayerCanvas::deleteItem(Node node_to_delete)
863 delete(nodetextmap[node_to_delete]);
864 delete(nodesmap[node_to_delete]);
865 mapstorage.graph.erase(node_to_delete);
868 void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete)
870 delete(edgetextmap[edge_to_delete]);
871 delete(edgesmap[edge_to_delete]);
872 mapstorage.graph.erase(edge_to_delete);
875 void GraphDisplayerCanvas::textReposition(xy<double> new_place)
877 new_place+=(xy<double>(10,10));
878 edgetextmap[forming_edge]->property_x().set_value(new_place.x);
879 edgetextmap[forming_edge]->property_y().set_value(new_place.y);
882 void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on)
886 if(forming_edge!=INVALID)
888 std::cerr << "ERROR!!!! Valid edge found!" << std::endl;
892 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
894 if(edgesmap[i]==active_bre)
903 if(forming_edge!=INVALID)
905 forming_edge=INVALID;
909 std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
915 int GraphDisplayerCanvas::addNewEdgeMap(double default_value, std::string mapname)
918 Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (mapstorage.graph, default_value);
920 //if addition was not successful addEdgeMap returns one.
921 //cause can be that there is already a map named like the new one
922 if(mapstorage.addEdgeMap(mapname,emptr, default_value))
928 //add it to the list of the displayable maps
929 mapwin.registerNewEdgeMap(mapname);
932 changeEdgeText(mapname);
937 int GraphDisplayerCanvas::addNewNodeMap(double default_value, std::string mapname)
940 Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (mapstorage.graph,default_value);
942 //if addition was not successful addNodeMap returns one.
943 //cause can be that there is already a map named like the new one
944 if(mapstorage.addNodeMap(mapname,emptr, default_value))
949 //add it to the list of the displayable maps
950 mapwin.registerNewNodeMap(mapname);
953 changeNodeText(mapname);