Put an end to every gcc-4.0 warning.
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 //draw the new node in red at the clicked place
220 case GDK_2BUTTON_PRESS:
221 //std::cout << "double click" << std::endl;
223 case GDK_BUTTON_PRESS:
224 mapstorage.modified = true;
228 active_node=mapstorage.graph.addNode();
230 //initiating values corresponding to new node in maps
232 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
234 mapstorage.coords.set(active_node, xy<double>(clicked_x, clicked_y));
235 (*mapstorage.nodemap_storage["id"])[active_node] =
236 mapstorage.graph.id(active_node);
238 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph,
239 clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
240 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
241 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
242 *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
243 (nodesmap[active_node])->show();
245 nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph,
246 clicked_x+node_property_defaults[N_RADIUS]+5,
247 clicked_y+node_property_defaults[N_RADIUS]+5, "");
248 nodetextmap[active_node]->property_fill_color().set_value("darkblue");
250 mapwin.updateNode(active_node);
255 case GDK_MOTION_NOTIFY:
257 GdkEvent * generated=new GdkEvent();
258 generated->motion.x=e->motion.x;
259 generated->motion.y=e->motion.y;
260 generated->type=GDK_MOTION_NOTIFY;
261 moveEventHandler(generated);
265 //finalize the new node
266 case GDK_BUTTON_RELEASE:
267 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
269 active_item->lower_to_bottom();
272 target_item=get_item_at(clicked_x, clicked_y);
274 active_item->raise_to_top();
277 if(target_item==active_item)
279 //Its appropriate color is given by update.
280 *active_item << Gnome::Canvas::Properties::fill_color("blue");
284 //In this case the given color has to be overwritten, because the noe covers an other item.
285 *active_item << Gnome::Canvas::Properties::fill_color("lightblue");
297 bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e)
301 case GDK_BUTTON_PRESS:
302 //in edge creation right button has special meaning
303 if(e->button.button!=3)
305 //there is not yet selected node
306 if(active_node==INVALID)
308 //we mark the location of the event to be able to calculate parameters of dragging
310 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
312 active_item=(get_item_at(clicked_x, clicked_y));
314 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
316 if(nodesmap[i]==active_item)
321 //the clicked item is really a node
322 if(active_node!=INVALID)
324 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
327 //clicked item was not a node. It could be e.g. edge.
333 //we only have to do sg. if the mouse button
334 // is pressed already once AND the click was
335 // on a node that was found in the set of
336 //nodes, and now we only search for the second
340 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
341 target_item=(get_item_at(clicked_x, clicked_y));
342 Node target_node=INVALID;
343 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
345 if(nodesmap[i]==target_item)
350 //the clicked item is a node, the edge can be drawn
351 if(target_node!=INVALID)
353 if(target_node!=active_node)
355 mapstorage.modified = true;
357 *(nodesmap[target_node]) <<
358 Gnome::Canvas::Properties::fill_color("red");
361 active_edge=mapstorage.graph.addEdge(active_node,
364 //initiating values corresponding to new edge in maps
365 mapstorage.initMapsForEdge(active_edge);
366 (*mapstorage.edgemap_storage["id"])[active_edge] =
367 mapstorage.graph.id(active_edge);
369 //calculating coordinates of new edge
370 Gnome::Canvas::Points coos;
371 double x1, x2, y1, y2;
373 active_item->get_bounds(x1, y1, x2, y2);
374 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
376 target_item->get_bounds(x1, y1, x2, y2);
377 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
380 edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this);
381 *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
382 edgesmap[active_edge]->property_width_pixels().set_value(10);
384 //redraw nodes to blank terminations of the new edge
385 target_item->raise_to_top();
386 active_item->raise_to_top();
388 //initializing edge-text as well, to empty string
389 xy<double> text_pos=edgesmap[active_edge]->getArrowPos();
390 text_pos+=(xy<double>(10,10));
392 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
393 edgetextmap[active_edge]->property_fill_color().set_value("darkgreen");
395 //updating its properties
396 mapwin.updateEdge(active_edge);
401 std::cerr << "Loop edge is not yet implemented!" << std::endl;
404 //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
412 case GDK_BUTTON_RELEASE:
414 //we clear settings in two cases
415 //1: the edge is ready (target_item has valid value)
416 //2: the edge creation is cancelled with right button
417 if((target_item)||(e->button.button==3))
421 *active_item << Gnome::Canvas::Properties::fill_color("blue");
426 *target_item << Gnome::Canvas::Properties::fill_color("blue");
439 bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e)
443 case GDK_BUTTON_PRESS:
444 //finding the clicked items
445 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
446 active_item=(get_item_at(clicked_x, clicked_y));
450 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
452 if(nodesmap[i]==active_item)
458 if(active_node==INVALID)
460 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
462 if(edgesmap[i]==active_item)
469 //recolor activated item
472 *active_item << Gnome::Canvas::Properties::fill_color("red");
476 case GDK_BUTTON_RELEASE:
477 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
480 //the cursor was not moved since pressing it
481 if( active_item == ( get_item_at (clicked_x, clicked_y) ) )
484 if(active_node!=INVALID)
486 mapstorage.modified = true;
488 //collecting edges to delete
490 std::set<Graph::Edge> edges_to_delete;
492 mapstorage.graph.firstOut(e,active_node);
493 for(;e!=INVALID;mapstorage.graph.nextOut(e))
495 edges_to_delete.insert(e);
498 mapstorage.graph.firstIn(e,active_node);
499 for(;e!=INVALID;mapstorage.graph.nextIn(e))
501 edges_to_delete.insert(e);
504 //deleting collected edges
505 for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
507 deleteItem(*edge_set_it);
509 deleteItem(active_node);
511 //a simple edge was chosen
514 deleteItem(active_edge);
517 //pointer was moved, deletion is cancelled
520 if(active_node!=INVALID)
522 *active_item << Gnome::Canvas::Properties::fill_color("blue");
526 *active_item << Gnome::Canvas::Properties::fill_color("green");
536 case GDK_MOTION_NOTIFY:
545 bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e)
547 if(actual_tool==EDGE_MAP_EDIT)
552 //for Escape or Enter hide the displayed widget
554 nodeMapEditEventHandler(e);
557 case GDK_BUTTON_PRESS:
558 //If the click happened on an edge we place the entrywidget there and fill in the value of the activated map at that edge.
560 //for determine, whether it was an edge
561 Edge clicked_edge=INVALID;
563 //find the activated item between texts
564 active_item=(get_item_at(e->button.x, e->button.y));
565 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
567 if(edgetextmap[i]==active_item)
573 //if it was not between texts, search for it between edges
574 if(clicked_edge==INVALID)
576 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
577 active_item=(get_item_at(clicked_x, clicked_y));
579 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
581 //at the same time only one can be active
582 if((edgesmap[i]==active_item)||(edgetextmap[i]==active_item))
588 //if it was really an edge...
589 if(clicked_edge!=INVALID)
591 //If there is already edited edge, it has to be saved first
592 if(entrywidget.is_visible())
594 GdkEvent * generated=new GdkEvent();
595 generated->type=GDK_KEY_PRESS;
596 ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
597 entryWidgetChangeHandler(generated);
599 //If the previous value could be saved, we can go further, otherwise not
600 if(!entrywidget.is_visible())
602 //and there is activated map
603 if(edgetextmap[clicked_edge]->property_text().get_value()!="")
605 //activate the general variable for it
606 active_edge=clicked_edge;
607 //delete visible widget if there is
608 if(canvasentrywidget)
610 delete(canvasentrywidget);
613 //initialize the entry
616 //fill in the correct value
617 entrywidget.set_text(edgetextmap[active_edge]->property_text().get_value());
619 //replace and resize the entry to the activated edge and put it in a Canvas::Widget to be able to display it on gdc
620 xy<double> entry_coos;
621 entry_coos.x=(edgetextmap[active_edge])->property_x().get_value();
622 entry_coos.x-=edgetextmap[active_edge]->property_text_width().get_value()/2;
623 entry_coos.y=(edgetextmap[active_edge])->property_y().get_value();
624 entry_coos.y-=edgetextmap[active_edge]->property_text_height().get_value()*1.5/2;
625 canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget);
626 canvasentrywidget->property_width().set_value(edgetextmap[active_edge]->property_text_width().get_value()*4);
627 canvasentrywidget->property_height().set_value(edgetextmap[active_edge]->property_text_height().get_value()*1.5);
629 //setting the focus to newly created widget
630 parentwin->set_focus(entrywidget);
631 parentwin->activate_focus();
635 //if it was not an edge...
638 //In this case the click did not happen on an edge
639 //if there is visible entry we save the value in it
640 //we pretend like an Enter was presse din the Entry widget
641 GdkEvent * generated=new GdkEvent();
642 generated->type=GDK_KEY_PRESS;
643 ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
644 entryWidgetChangeHandler(generated);
655 bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e)
657 if(actual_tool==NODE_MAP_EDIT)
662 //for Escape or Enter hide the displayed widget
664 switch(((GdkEventKey*)e)->keyval)
679 case GDK_BUTTON_PRESS:
680 //If the click happened on an edge we place the entrywidget there and fill in the value of the activated map at that edge.
682 //for determine, whether it was a node
683 Node clicked_node=INVALID;
685 //find the activated item between texts
686 active_item=(get_item_at(e->button.x, e->button.y));
687 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
689 //at the same time only one can be active
690 if(nodetextmap[i]==active_item)
696 //if there was not, search for it between nodes
697 if(clicked_node==INVALID)
699 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
700 active_item=(get_item_at(clicked_x, clicked_y));
702 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
704 //at the same time only one can be active
705 if(nodesmap[i]==active_item)
711 //if it was really an edge...
712 if(clicked_node!=INVALID)
714 //If there is already edited edge, it has to be saved first
715 if(entrywidget.is_visible())
717 GdkEvent * generated=new GdkEvent();
718 generated->type=GDK_KEY_PRESS;
719 ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
720 entryWidgetChangeHandler(generated);
722 //If the previous value could be saved, we can go further, otherwise not
723 if(!entrywidget.is_visible())
725 //and there is activated map
726 if(nodetextmap[clicked_node]->property_text().get_value()!="")
728 //activate the general variable for it
729 active_node=clicked_node;
730 //delete visible widget if there is
731 if(canvasentrywidget)
733 delete(canvasentrywidget);
736 //initialize the entry
739 //fill in the correct value
740 entrywidget.set_text(nodetextmap[active_node]->property_text().get_value());
742 //replace and resize the entry to the activated node and put it in a Canvas::Widget to be able to display it on gdc
743 xy<double> entry_coos;
744 entry_coos.x=(nodetextmap[active_node])->property_x().get_value();
745 entry_coos.x-=nodetextmap[active_node]->property_text_width().get_value()/2;
746 entry_coos.y=(nodetextmap[active_node])->property_y().get_value();
747 entry_coos.y-=nodetextmap[active_node]->property_text_height().get_value()*1.5/2;
748 canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget);
749 canvasentrywidget->property_width().set_value(nodetextmap[active_node]->property_text_width().get_value()*4);
750 canvasentrywidget->property_height().set_value(nodetextmap[active_node]->property_text_height().get_value()*1.5);
754 //if it was not an edge...
757 //In this case the click did not happen on an edge
758 //if there is visible entry we save the value in it
759 //we pretend like an Enter was presse din the Entry widget
760 GdkEvent * generated=new GdkEvent();
761 generated->type=GDK_KEY_PRESS;
762 ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
763 entryWidgetChangeHandler(generated);
774 bool GraphDisplayerCanvas::entryWidgetChangeHandler(GdkEvent* e)
776 if(entrywidget.is_visible())
778 if(e->type==GDK_KEY_PRESS)
780 switch(((GdkEventKey*)e)->keyval)
788 //these variables check whether the text in the entry is valid
789 bool valid_double=true;
792 //getting the value from the entry and converting it to double
793 Glib::ustring mapvalue_str = entrywidget.get_text();
795 char * mapvalue_ch=new char [mapvalue_str.length()];
796 for(int i=0;i<(int)(mapvalue_str.length());i++)
798 if(((mapvalue_str[i]<'0')||(mapvalue_str[i]>'9'))&&(mapvalue_str[i]!='.'))
804 if(mapvalue_str[i]=='.')
809 mapvalue_ch[i]=mapvalue_str[i];
812 //if the text in the entry was correct
813 if((point_num<=1)&&(valid_double))
815 double mapvalue_d=atof(mapvalue_ch);
817 //reconvert the double to string for the correct format
818 std::ostringstream ostr;
821 //save the value to the correct place
825 edgetextmap[active_edge]->property_text().set_value(ostr.str());
826 (*(mapstorage.edgemap_storage)[edgemap_to_edit])[active_edge]=mapvalue_d;
827 mapwin.updateEdge(active_edge);
830 nodetextmap[active_node]->property_text().set_value(ostr.str());
831 (*(mapstorage.nodemap_storage)[nodemap_to_edit])[active_node]=mapvalue_d;
832 mapwin.updateNode(active_node);
839 //the text in the entry was not correct for a double
842 std::cerr << "ERROR: only handling of double values is implemented yet!" << std::endl;
855 void GraphDisplayerCanvas::deleteItem(Node node_to_delete)
857 delete(nodetextmap[node_to_delete]);
858 delete(nodesmap[node_to_delete]);
859 mapstorage.graph.erase(node_to_delete);
862 void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete)
864 delete(edgetextmap[edge_to_delete]);
865 delete(edgesmap[edge_to_delete]);
866 mapstorage.graph.erase(edge_to_delete);
869 void GraphDisplayerCanvas::textReposition(xy<double> new_place)
871 new_place+=(xy<double>(10,10));
872 edgetextmap[forming_edge]->property_x().set_value(new_place.x);
873 edgetextmap[forming_edge]->property_y().set_value(new_place.y);
876 void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on)
880 if(forming_edge!=INVALID)
882 std::cerr << "ERROR!!!! Valid edge found!" << std::endl;
886 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
888 if(edgesmap[i]==active_bre)
897 if(forming_edge!=INVALID)
899 forming_edge=INVALID;
903 std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
909 int GraphDisplayerCanvas::addNewEdgeMap(double default_value, std::string mapname)
912 Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (mapstorage.graph, default_value);
914 //if addition was not successful addEdgeMap returns one.
915 //cause can be that there is already a map named like the new one
916 if(mapstorage.addEdgeMap(mapname,emptr))
922 //add it to the list of the displayable maps
923 mapwin.registerNewEdgeMap(mapname);
926 changeEdgeText(mapname);
931 int GraphDisplayerCanvas::addNewNodeMap(double default_value, std::string mapname)
934 Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (mapstorage.graph,default_value);
936 //if addition was not successful addNodeMap returns one.
937 //cause can be that there is already a map named like the new one
938 if(mapstorage.addNodeMap(mapname,emptr))
943 //add it to the list of the displayable maps
944 mapwin.registerNewNodeMap(mapname);
947 changeNodeText(mapname);