Sorry for the previous commit, it was not ready yet, but that damned up arrow... So in this new revision string-double and double-string conversion is corrected to a more C++ way.
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(g); 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 //new coordinates will be the old values,
129 //because the item will be moved to the
130 //new coordinate therefore the new movement
131 //has to be calculated from here
135 window_to_world (e->motion.x, e->motion.y, new_x, new_y);
137 double dx=new_x-clicked_x;
138 double dy=new_y-clicked_y;
140 //repositioning node and its text
141 active_item->move(dx, dy);
142 nodetextmap[active_node]->move(dx, dy);
147 //all the edges connected to the moved point has to be redrawn
150 g.firstOut(ei,active_node);
152 for(;ei!=INVALID;g.nextOut(ei))
154 Gnome::Canvas::Points coos;
155 double x1, x2, y1, y2;
157 nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
158 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
160 nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
161 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
165 edgesmap[ei]->setPoints(coos);
169 edgesmap[ei]->setPoints(coos,true);
172 //reposition of edgetext
173 xy<double> text_pos=edgesmap[ei]->getArrowPos();
174 text_pos+=(xy<double>(10,10));
175 edgetextmap[ei]->property_x().set_value(text_pos.x);
176 edgetextmap[ei]->property_y().set_value(text_pos.y);
179 g.firstIn(ei,active_node);
180 for(;ei!=INVALID;g.nextIn(ei))
182 Gnome::Canvas::Points coos;
183 double x1, x2, y1, y2;
185 nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
186 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
188 nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
189 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
193 edgesmap[ei]->setPoints(coos);
197 edgesmap[ei]->setPoints(coos,true);
200 xy<double> text_pos=edgesmap[ei]->getArrowPos();
201 text_pos+=(xy<double>(10,10));
202 edgetextmap[ei]->property_x().set_value(text_pos.x);
203 edgetextmap[ei]->property_y().set_value(text_pos.y);
212 bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
217 //draw the new node in red at the clicked place
218 case GDK_2BUTTON_PRESS:
219 std::cout << "double click" << std::endl;
221 case GDK_BUTTON_PRESS:
224 active_node=NodeIt(g,g.addNode());
226 //initiating values corresponding to new node in maps
228 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
230 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
231 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
232 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
233 *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
234 (nodesmap[active_node])->show();
236 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, "");
237 nodetextmap[active_node]->property_fill_color().set_value("darkblue");
239 mapwin->updateNode(active_node);
244 case GDK_MOTION_NOTIFY:
246 GdkEvent * generated=new GdkEvent();
247 generated->motion.x=e->motion.x;
248 generated->motion.y=e->motion.y;
249 generated->type=GDK_MOTION_NOTIFY;
250 moveEventHandler(generated);
254 //finalize the new node
255 case GDK_BUTTON_RELEASE:
256 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
258 active_item->lower_to_bottom();
261 target_item=get_item_at(clicked_x, clicked_y);
263 active_item->raise_to_top();
266 if(target_item==active_item)
268 //Its appropriate color is given by update.
269 *active_item << Gnome::Canvas::Properties::fill_color("blue");
273 //In this case the given color has to be overwritten, because the noe covers an other item.
274 *active_item << Gnome::Canvas::Properties::fill_color("lightblue");
286 bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e)
290 case GDK_BUTTON_PRESS:
291 //in edge creation right button has special meaning
292 if(e->button.button!=3)
294 //there is not yet selected node
295 if(active_node==INVALID)
297 //we mark the location of the event to be able to calculate parameters of dragging
299 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
301 active_item=(get_item_at(clicked_x, clicked_y));
303 for (NodeIt i(g); i!=INVALID; ++i)
305 if(nodesmap[i]==active_item)
310 //the clicked item is really a node
311 if(active_node!=INVALID)
313 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
316 //clicked item was not a node. It could be e.g. edge.
322 //we only have to do sg. if the mouse button
323 // is pressed already once AND the click was
324 // on a node that was found in the set of
325 //nodes, and now we only search for the second
329 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
330 target_item=(get_item_at(clicked_x, clicked_y));
331 Graph::NodeIt target_node=INVALID;
332 for (NodeIt i(g); i!=INVALID; ++i)
334 if(nodesmap[i]==target_item)
339 //the clicked item is a node, the edge can be drawn
340 if(target_node!=INVALID)
342 if(target_node!=active_node)
344 *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
347 active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
349 //initiating values corresponding to new edge in maps
350 mapstorage.initMapsForEdge(active_edge);
352 //calculating coordinates of new edge
353 Gnome::Canvas::Points coos;
354 double x1, x2, y1, y2;
356 active_item->get_bounds(x1, y1, x2, y2);
357 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
359 target_item->get_bounds(x1, y1, x2, y2);
360 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
363 edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this);
364 *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
365 edgesmap[active_edge]->property_width_pixels().set_value(10);
367 //redraw nodes to blank terminations of the new edge
368 target_item->raise_to_top();
369 active_item->raise_to_top();
371 //initializing edge-text as well, to empty string
372 xy<double> text_pos=edgesmap[active_edge]->getArrowPos();
373 text_pos+=(xy<double>(10,10));
375 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
376 edgetextmap[active_edge]->property_fill_color().set_value("darkgreen");
378 //updating its properties
379 mapwin->updateEdge(active_edge);
384 std::cout << "Loop edge is not yet implemented!" << std::endl;
387 //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
395 case GDK_BUTTON_RELEASE:
397 //we clear settings in two cases
398 //1: the edge is ready (target_item has valid value)
399 //2: the edge creation is cancelled with right button
400 if((target_item)||(e->button.button==3))
404 *active_item << Gnome::Canvas::Properties::fill_color("blue");
409 *target_item << Gnome::Canvas::Properties::fill_color("blue");
422 bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e)
426 case GDK_BUTTON_PRESS:
427 //finding the clicked items
428 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
429 active_item=(get_item_at(clicked_x, clicked_y));
434 for (NodeIt i(g); i!=INVALID; ++i)
436 if(nodesmap[i]==active_item)
442 if(active_node==INVALID)
444 for (EdgeIt i(g); i!=INVALID; ++i)
446 if(edgesmap[i]==active_item)
453 //recolor activated item
456 *active_item << Gnome::Canvas::Properties::fill_color("red");
460 case GDK_BUTTON_RELEASE:
461 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
464 //the cursor was not moved since pressing it
465 if( active_item == ( get_item_at (clicked_x, clicked_y) ) )
468 if(active_node!=INVALID)
471 //collecting edges to delete
473 std::set<Graph::Edge> edges_to_delete;
475 g.firstOut(e,active_node);
476 for(;e!=INVALID;g.nextOut(e))
478 edges_to_delete.insert(e);
481 g.firstIn(e,active_node);
482 for(;e!=INVALID;g.nextIn(e))
484 edges_to_delete.insert(e);
487 //deleting collected edges
488 for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
490 deleteItem(*edge_set_it);
492 deleteItem(active_node);
494 //a simple edge was chosen
497 deleteItem(active_edge);
500 //pointer was moved, deletion is cancelled
503 if(active_node!=INVALID)
505 *active_item << Gnome::Canvas::Properties::fill_color("blue");
509 *active_item << Gnome::Canvas::Properties::fill_color("green");
519 case GDK_MOTION_NOTIFY:
528 bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e)
533 //for Escape or Enter hide the displayed widget
535 nodeMapEditEventHandler(e);
538 case GDK_BUTTON_PRESS:
539 //If the click happened on an edge we place the entrywidget there and fill in the value of the activated map at that edge.
541 //find the activated item
542 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
543 active_item=(get_item_at(clicked_x, clicked_y));
545 //determine, whether it was an edge
546 Graph::EdgeIt clicked_edge=INVALID;
547 for (EdgeIt i(g); i!=INVALID; ++i)
549 if(edgesmap[i]==active_item)
554 //if it was really an edge...
555 if(clicked_edge!=INVALID)
557 //If there is already edited edge, it has to be saved first
558 if(entrywidget.is_visible())
560 GdkEvent * generated=new GdkEvent();
561 generated->type=GDK_KEY_PRESS;
562 ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
563 entryWidgetChangeHandler(generated);
565 //If the previous value could be saved, we can go further, otherwise not
566 if(!entrywidget.is_visible())
568 //and there is activated map
569 if(edgetextmap[clicked_edge]->property_text().get_value()!="")
571 //activate the general variable for it
572 active_edge=clicked_edge;
573 //delete visible widget if there is
574 if(canvasentrywidget)
576 delete(canvasentrywidget);
579 //initialize the entry
582 //fill in the correct value
583 entrywidget.set_text(edgetextmap[active_edge]->property_text().get_value());
585 //replace and resize the entry to the activated edge and put it in a Canvas::Widget to be able to display it on gdc
586 xy<double> entry_coos;
587 entry_coos.x=(edgetextmap[active_edge])->property_x().get_value();
588 entry_coos.x-=edgetextmap[active_edge]->property_text_width().get_value()/2;
589 entry_coos.y=(edgetextmap[active_edge])->property_y().get_value();
590 entry_coos.y-=edgetextmap[active_edge]->property_text_height().get_value()*1.5/2;
591 canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget);
592 canvasentrywidget->property_width().set_value(edgetextmap[active_edge]->property_text_width().get_value()*1.5);
593 canvasentrywidget->property_height().set_value(edgetextmap[active_edge]->property_text_height().get_value()*1.5);
597 //if it was not an edge...
600 //In this case the click did not happen on an edge
601 //if there is visible entry we save the value in it
602 //we pretend like an Enter was presse din the Entry widget
603 GdkEvent * generated=new GdkEvent();
604 generated->type=GDK_KEY_PRESS;
605 ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
606 entryWidgetChangeHandler(generated);
616 bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e)
621 //for Escape or Enter hide the displayed widget
623 switch(((GdkEventKey*)e)->keyval)
638 case GDK_BUTTON_PRESS:
639 //If the click happened on an edge we place the entrywidget there and fill in the value of the activated map at that edge.
641 //find the activated item
642 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
643 active_item=(get_item_at(clicked_x, clicked_y));
645 //determine, whether it was a node
646 Graph::NodeIt clicked_node=INVALID;
647 for (NodeIt i(g); i!=INVALID; ++i)
649 if(nodesmap[i]==active_item)
655 //if it was really an edge...
656 if(clicked_node!=INVALID)
658 //If there is already edited edge, it has to be saved first
659 if(entrywidget.is_visible())
661 GdkEvent * generated=new GdkEvent();
662 generated->type=GDK_KEY_PRESS;
663 ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
664 entryWidgetChangeHandler(generated);
666 //If the previous value could be saved, we can go further, otherwise not
667 if(!entrywidget.is_visible())
669 //and there is activated map
670 if(nodetextmap[clicked_node]->property_text().get_value()!="")
672 //activate the general variable for it
673 active_node=clicked_node;
674 //delete visible widget if there is
675 if(canvasentrywidget)
677 delete(canvasentrywidget);
680 //initialize the entry
683 //fill in the correct value
684 entrywidget.set_text(nodetextmap[active_node]->property_text().get_value());
686 //replace and resize the entry to the activated node and put it in a Canvas::Widget to be able to display it on gdc
687 xy<double> entry_coos;
688 entry_coos.x=(nodetextmap[active_node])->property_x().get_value();
689 entry_coos.x-=nodetextmap[active_node]->property_text_width().get_value()/2;
690 entry_coos.y=(nodetextmap[active_node])->property_y().get_value();
691 entry_coos.y-=nodetextmap[active_node]->property_text_height().get_value()*1.5/2;
692 canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget);
693 canvasentrywidget->property_width().set_value(nodetextmap[active_node]->property_text_width().get_value()*1.5);
694 canvasentrywidget->property_height().set_value(nodetextmap[active_node]->property_text_height().get_value()*1.5);
698 //if it was not an edge...
701 //In this case the click did not happen on an edge
702 //if there is visible entry we save the value in it
703 //we pretend like an Enter was presse din the Entry widget
704 GdkEvent * generated=new GdkEvent();
705 generated->type=GDK_KEY_PRESS;
706 ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
707 entryWidgetChangeHandler(generated);
717 bool GraphDisplayerCanvas::entryWidgetChangeHandler(GdkEvent* e)
719 if(entrywidget.is_visible())
721 if(e->type==GDK_KEY_PRESS)
723 switch(((GdkEventKey*)e)->keyval)
731 //these variables check whether the text in the entry is valid
732 bool valid_double=true;
735 //getting the value from the entry and converting it to double
736 Glib::ustring mapvalue_str = entrywidget.get_text();
738 char * mapvalue_ch=new char [mapvalue_str.length()];
739 for(int i=0;i<(int)(mapvalue_str.length());i++)
741 if(((mapvalue_str[i]<'0')||(mapvalue_str[i]>'9'))&&(mapvalue_str[i]!='.'))
747 if(mapvalue_str[i]=='.')
752 mapvalue_ch[i]=mapvalue_str[i];
755 //if the text in the entry was correct
756 if((point_num<=1)&&(valid_double))
758 double mapvalue_d=atof(mapvalue_ch);
760 //reconvert the double to string for the correct format
761 std::ostringstream ostr;
764 //save the value to the correct place
768 edgetextmap[active_edge]->property_text().set_value(ostr.str());
769 (*(mapstorage.edgemap_storage)[edgemap_to_edit])[active_edge]=mapvalue_d;
772 nodetextmap[active_node]->property_text().set_value(ostr.str());
773 (*(mapstorage.nodemap_storage)[nodemap_to_edit])[active_node]=mapvalue_d;
780 //the text in the entry was not correct for a double
783 std::cout << "ERROR: only handling of double values is implemented yet!" << std::endl;
796 void GraphDisplayerCanvas::deleteItem(NodeIt node_to_delete)
798 delete(nodetextmap[node_to_delete]);
799 delete(nodesmap[node_to_delete]);
800 g.erase(node_to_delete);
803 void GraphDisplayerCanvas::deleteItem(EdgeIt edge_to_delete)
805 delete(edgetextmap[edge_to_delete]);
806 delete(edgesmap[edge_to_delete]);
807 g.erase(edge_to_delete);
810 void GraphDisplayerCanvas::deleteItem(Graph::Edge edge_to_delete)
812 delete(edgetextmap[edge_to_delete]);
813 delete(edgesmap[edge_to_delete]);
814 g.erase(edge_to_delete);
817 void GraphDisplayerCanvas::textReposition(xy<double> new_place)
819 new_place+=(xy<double>(10,10));
820 edgetextmap[forming_edge]->property_x().set_value(new_place.x);
821 edgetextmap[forming_edge]->property_y().set_value(new_place.y);
824 void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on)
828 if(forming_edge!=INVALID)
830 std::cout << "ERROR!!!! Valid edge found!" << std::endl;
834 for (EdgeIt i(g); i!=INVALID; ++i)
836 if(edgesmap[i]==active_bre)
845 if(forming_edge!=INVALID)
847 forming_edge=INVALID;
851 std::cout << "ERROR!!!! Invalid edge found!" << std::endl;
857 void GraphDisplayerCanvas::addNewEdgeMap(double default_value, std::string mapname)
860 Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (g,default_value);
861 mapstorage.addEdgeMap(mapname,emptr);
863 //add it to the list of the displayable maps
864 mapwin->registerNewEdgeMap(mapname);
867 changeEdgeText(mapname);
870 void GraphDisplayerCanvas::addNewNodeMap(double default_value, std::string mapname)
873 Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (g,default_value);
874 mapstorage.addNodeMap(mapname,emptr);
876 //add it to the list of the displayable maps
877 mapwin->registerNewNodeMap(mapname);
880 changeNodeText(mapname);