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
52 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
56 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false);
60 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false);
64 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false);
69 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false);
73 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false);
82 int GraphDisplayerCanvas::getActualTool()
87 bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e)
89 static Gnome::Canvas::Text *coord_text = 0;
92 case GDK_BUTTON_PRESS:
93 //we mark the location of the event to be able to calculate parameters of dragging
94 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
96 active_item=(get_item_at(clicked_x, clicked_y));
98 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
100 if(nodesmap[i]==active_item)
105 switch(e->button.button)
115 case GDK_BUTTON_RELEASE:
125 case GDK_MOTION_NOTIFY:
126 //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
127 if(active_node!=INVALID)
129 mapstorage.modified = true;
131 //new coordinates will be the old values,
132 //because the item will be moved to the
133 //new coordinate therefore the new movement
134 //has to be calculated from here
138 window_to_world (e->motion.x, e->motion.y, new_x, new_y);
140 double dx=new_x-clicked_x;
141 double dy=new_y-clicked_y;
143 //repositioning node and its text
144 active_item->move(dx, dy);
145 nodetextmap[active_node]->move(dx, dy);
147 // the new coordinates of the centre of the node
148 double coord_x = new_x - (clicked_x - mapstorage.coords[active_node].x);
149 double coord_y = new_y - (clicked_y - mapstorage.coords[active_node].y);
154 // write back the new coordinates to the coords map
155 mapstorage.coords.set(active_node, xy<double>(coord_x, coord_y));
157 // reposition the coordinates text
158 std::ostringstream ostr;
160 mapstorage.coords[active_node].x << ", " <<
161 mapstorage.coords[active_node].y << ")";
163 (nodesmap[active_node]->property_x2().get_value() -
164 nodesmap[active_node]->property_x1().get_value()) / 2.0;
167 coord_text->property_text().set_value(ostr.str());
168 coord_text->property_x().set_value(mapstorage.coords[active_node].x +
170 coord_text->property_y().set_value(mapstorage.coords[active_node].y -
175 coord_text = new Gnome::Canvas::Text(
177 mapstorage.coords[active_node].x + radius,
178 mapstorage.coords[active_node].y - radius,
180 coord_text->property_fill_color().set_value("black");
181 coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST);
184 //all the edges connected to the moved point has to be redrawn
185 for(OutEdgeIt ei(mapstorage.graph,active_node);ei!=INVALID;++ei)
187 Gnome::Canvas::Points coos;
188 double x1, x2, y1, y2;
190 nodesmap[mapstorage.graph.source(ei)]->get_bounds(x1, y1, x2, y2);
191 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
193 nodesmap[mapstorage.graph.target(ei)]->get_bounds(x1, y1, x2, y2);
194 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
198 edgesmap[ei]->setPoints(coos);
202 edgesmap[ei]->setPoints(coos,true);
205 //reposition of edgetext
206 xy<double> text_pos=edgesmap[ei]->getArrowPos();
207 text_pos+=(xy<double>(10,10));
208 edgetextmap[ei]->property_x().set_value(text_pos.x);
209 edgetextmap[ei]->property_y().set_value(text_pos.y);
212 for(InEdgeIt ei(mapstorage.graph,active_node);ei!=INVALID;++ei)
214 Gnome::Canvas::Points coos;
215 double x1, x2, y1, y2;
217 nodesmap[mapstorage.graph.source(ei)]->get_bounds(x1, y1, x2, y2);
218 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
220 nodesmap[mapstorage.graph.target(ei)]->get_bounds(x1, y1, x2, y2);
221 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
225 edgesmap[ei]->setPoints(coos);
229 edgesmap[ei]->setPoints(coos,true);
232 xy<double> text_pos=edgesmap[ei]->getArrowPos();
233 text_pos+=(xy<double>(10,10));
234 edgetextmap[ei]->property_x().set_value(text_pos.x);
235 edgetextmap[ei]->property_y().set_value(text_pos.y);
244 bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
249 case GDK_MOTION_NOTIFY:
251 GdkEvent * generated=new GdkEvent();
252 generated->motion.x=e->motion.x;
253 generated->motion.y=e->motion.y;
254 generated->type=GDK_MOTION_NOTIFY;
255 moveEventHandler(generated);
259 case GDK_BUTTON_RELEASE:
260 mapstorage.modified = true;
264 active_node=mapstorage.graph.addNode();
266 //initiating values corresponding to new node in maps
268 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
270 // update coordinates
271 mapstorage.coords.set(active_node, xy<double>(clicked_x, clicked_y));
273 // update all other maps
274 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
275 mapstorage.nodemap_storage.begin(); it !=
276 mapstorage.nodemap_storage.end(); ++it)
278 if ((it->first != "coordinates_x") &&
279 (it->first != "coordinates_y"))
281 (*(it->second))[active_node] =
282 mapstorage.nodemap_default[it->first];
285 // increment the id map's default value
286 mapstorage.nodemap_default["id"] += 1.0;
288 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph,
289 clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
290 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
291 *(nodesmap[active_node]) <<
292 Gnome::Canvas::Properties::fill_color("blue");
293 *(nodesmap[active_node]) <<
294 Gnome::Canvas::Properties::outline_color("black");
295 active_item->raise_to_top();
297 (nodesmap[active_node])->show();
299 nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph,
300 clicked_x+node_property_defaults[N_RADIUS]+5,
301 clicked_y+node_property_defaults[N_RADIUS]+5, "");
302 nodetextmap[active_node]->property_fill_color().set_value("darkblue");
303 nodetextmap[active_node]->raise_to_top();
305 mapwin.updateNode(active_node);
319 bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e)
323 case GDK_BUTTON_PRESS:
324 //in edge creation right button has special meaning
325 if(e->button.button!=3)
327 //there is not yet selected node
328 if(active_node==INVALID)
330 //we mark the location of the event to be able to calculate parameters of dragging
332 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
334 active_item=(get_item_at(clicked_x, clicked_y));
336 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
338 if(nodesmap[i]==active_item)
343 //the clicked item is really a node
344 if(active_node!=INVALID)
346 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
349 //clicked item was not a node. It could be e.g. edge.
355 //we only have to do sg. if the mouse button
356 // is pressed already once AND the click was
357 // on a node that was found in the set of
358 //nodes, and now we only search for the second
362 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
363 target_item=(get_item_at(clicked_x, clicked_y));
364 Node target_node=INVALID;
365 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
367 if(nodesmap[i]==target_item)
372 //the clicked item is a node, the edge can be drawn
373 if(target_node!=INVALID)
375 if(target_node!=active_node)
377 mapstorage.modified = true;
379 *(nodesmap[target_node]) <<
380 Gnome::Canvas::Properties::fill_color("red");
383 active_edge=mapstorage.graph.addEdge(active_node,
387 for (std::map<std::string,
388 Graph::EdgeMap<double>*>::const_iterator it =
389 mapstorage.edgemap_storage.begin(); it !=
390 mapstorage.edgemap_storage.end(); ++it)
392 (*(it->second))[active_edge] =
393 mapstorage.edgemap_default[it->first];
395 // increment the id map's default value
396 mapstorage.edgemap_default["id"] += 1.0;
398 //calculating coordinates of new edge
399 Gnome::Canvas::Points coos;
400 double x1, x2, y1, y2;
402 active_item->get_bounds(x1, y1, x2, y2);
403 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
405 target_item->get_bounds(x1, y1, x2, y2);
406 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
409 edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos,
411 *(edgesmap[active_edge]) <<
412 Gnome::Canvas::Properties::fill_color("green");
413 edgesmap[active_edge]->property_width_pixels().set_value(10);
415 edgesmap[active_edge]->lower_to_bottom();
417 //initializing edge-text as well, to empty string
418 xy<double> text_pos=edgesmap[active_edge]->getArrowPos();
419 text_pos+=(xy<double>(10,10));
421 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,
422 text_pos.x, text_pos.y, "");
423 edgetextmap[active_edge]->property_fill_color().set_value(
425 edgetextmap[active_edge]->raise_to_top();
427 //updating its properties
428 mapwin.updateEdge(active_edge);
433 std::cerr << "Loop edge is not yet implemented!" << std::endl;
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(mapstorage.graph); i!=INVALID; ++i)
485 if(nodesmap[i]==active_item)
491 if(active_node==INVALID)
493 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
495 if(edgesmap[i]==active_item)
502 //recolor activated item
505 *active_item << Gnome::Canvas::Properties::fill_color("red");
509 case GDK_BUTTON_RELEASE:
510 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
513 //the cursor was not moved since pressing it
514 if( active_item == ( get_item_at (clicked_x, clicked_y) ) )
517 if(active_node!=INVALID)
519 mapstorage.modified = true;
521 std::set<Graph::Edge> edges_to_delete;
523 for(OutEdgeIt e(mapstorage.graph,active_node);e!=INVALID;++e)
525 edges_to_delete.insert(e);
528 for(InEdgeIt e(mapstorage.graph,active_node);e!=INVALID;++e)
530 edges_to_delete.insert(e);
533 //deleting collected edges
534 for(std::set<Graph::Edge>::iterator
535 edge_set_it=edges_to_delete.begin();
536 edge_set_it!=edges_to_delete.end();
539 deleteItem(*edge_set_it);
541 deleteItem(active_node);
543 //a simple edge was chosen
546 deleteItem(active_edge);
549 //pointer was moved, deletion is cancelled
552 if(active_node!=INVALID)
554 *active_item << Gnome::Canvas::Properties::fill_color("blue");
558 *active_item << Gnome::Canvas::Properties::fill_color("green");
568 case GDK_MOTION_NOTIFY:
577 bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e)
579 if(actual_tool==EDGE_MAP_EDIT)
583 case GDK_BUTTON_PRESS:
585 //for determine, whether it was an edge
586 Edge clicked_edge=INVALID;
588 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
589 active_item=(get_item_at(clicked_x, clicked_y));
591 //find the activated item between texts
592 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
594 //at the same time only one can be active
595 if(edgetextmap[i]==active_item)
601 //if it was not between texts, search for it between edges
602 if(clicked_edge==INVALID)
604 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
606 //at the same time only one can be active
607 if((edgesmap[i]==active_item)||(edgetextmap[i]==active_item))
614 //if it was really an edge...
615 if(clicked_edge!=INVALID)
617 // the id map is not editable
618 if (edgemap_to_edit == "id") return 0;
620 //and there is activated map
621 if(edgetextmap[clicked_edge]->property_text().get_value()!="")
623 //activate the general variable for it
624 active_edge=clicked_edge;
627 Gtk::Dialog dialog("Edit value", *parentwin, true);
628 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
629 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
630 Gtk::VBox* vbox = dialog.get_vbox();
632 (*mapstorage.edgemap_storage[edgemap_to_edit])[active_edge],
636 //TODO: find out why doesn't it work with
637 //numeric_limits<double>::min/max
638 Gtk::SpinButton spin(adj);
639 spin.set_numeric(true);
643 switch (dialog.run())
645 case Gtk::RESPONSE_NONE:
646 case Gtk::RESPONSE_CANCEL:
648 case Gtk::RESPONSE_ACCEPT:
649 double new_value = spin.get_value();
650 (*mapstorage.edgemap_storage[edgemap_to_edit])[active_edge] =
652 std::ostringstream ostr;
654 edgetextmap[active_edge]->property_text().set_value(
656 //mapwin.updateEdge(active_edge);
657 mapwin.updateEdge(Edge(INVALID));
670 bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e)
672 if(actual_tool==NODE_MAP_EDIT)
676 case GDK_BUTTON_PRESS:
678 //for determine, whether it was a node
679 Node clicked_node=INVALID;
681 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
682 active_item=(get_item_at(clicked_x, clicked_y));
684 //find the activated item between texts
685 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
687 //at the same time only one can be active
688 if(nodetextmap[i]==active_item)
694 //if there was not, search for it between nodes
695 if(clicked_node==INVALID)
697 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
699 //at the same time only one can be active
700 if(nodesmap[i]==active_item)
707 //if it was really a node...
708 if(clicked_node!=INVALID)
710 // the id map is not editable
711 if (nodemap_to_edit == "id") return 0;
713 //and there is activated map
714 if(nodetextmap[clicked_node]->property_text().get_value()!="")
716 //activate the general variable for it
717 active_node=clicked_node;
720 Gtk::Dialog dialog("Edit value", *parentwin, true);
721 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
722 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
723 Gtk::VBox* vbox = dialog.get_vbox();
725 (*mapstorage.nodemap_storage[nodemap_to_edit])[active_node],
729 //TODO: find out why doesn't it work with
730 //numeric_limits<double>::min/max
731 Gtk::SpinButton spin(adj);
732 spin.set_numeric(true);
736 switch (dialog.run())
738 case Gtk::RESPONSE_NONE:
739 case Gtk::RESPONSE_CANCEL:
741 case Gtk::RESPONSE_ACCEPT:
742 double new_value = spin.get_value();
743 (*mapstorage.nodemap_storage[nodemap_to_edit])[active_node] =
745 std::ostringstream ostr;
747 nodetextmap[active_node]->property_text().set_value(
749 //mapwin.updateNode(active_node);
750 mapwin.updateNode(Node(INVALID));
763 void GraphDisplayerCanvas::deleteItem(Node node_to_delete)
765 delete(nodetextmap[node_to_delete]);
766 delete(nodesmap[node_to_delete]);
767 mapstorage.graph.erase(node_to_delete);
770 void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete)
772 delete(edgetextmap[edge_to_delete]);
773 delete(edgesmap[edge_to_delete]);
774 mapstorage.graph.erase(edge_to_delete);
777 void GraphDisplayerCanvas::textReposition(xy<double> new_place)
779 new_place+=(xy<double>(10,10));
780 edgetextmap[forming_edge]->property_x().set_value(new_place.x);
781 edgetextmap[forming_edge]->property_y().set_value(new_place.y);
784 void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on)
788 if(forming_edge!=INVALID)
790 std::cerr << "ERROR!!!! Valid edge found!" << std::endl;
794 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
796 if(edgesmap[i]==active_bre)
805 if(forming_edge!=INVALID)
807 forming_edge=INVALID;
811 std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
817 int GraphDisplayerCanvas::addNewEdgeMap(double default_value, std::string mapname)
820 Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (mapstorage.graph, default_value);
822 //if addition was not successful addEdgeMap returns one.
823 //cause can be that there is already a map named like the new one
824 if(mapstorage.addEdgeMap(mapname,emptr, default_value))
830 //add it to the list of the displayable maps
831 mapwin.registerNewEdgeMap(mapname);
834 changeEdgeText(mapname);
839 int GraphDisplayerCanvas::addNewNodeMap(double default_value, std::string mapname)
842 Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (mapstorage.graph,default_value);
844 //if addition was not successful addNodeMap returns one.
845 //cause can be that there is already a map named like the new one
846 if(mapstorage.addNodeMap(mapname,emptr, default_value))
851 //add it to the list of the displayable maps
852 mapwin.registerNewNodeMap(mapname);
855 changeNodeText(mapname);