1 #include "graph_displayer_canvas.h"
5 bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
7 Gnome::Canvas::CanvasAA::on_expose_event(event);
13 void GraphDisplayerCanvas::changeEditorialTool(int newtool)
15 if(actual_tool!=newtool)
18 actual_handler.disconnect();
24 GdkEvent * generated=new GdkEvent();
25 generated->type=GDK_BUTTON_RELEASE;
26 generated->button.button=3;
27 createEdgeEventHandler(generated);
49 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
53 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false);
57 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false);
61 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false);
66 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
75 int GraphDisplayerCanvas::getActualTool()
80 bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e)
82 static Gnome::Canvas::Text *coord_text = 0;
85 case GDK_BUTTON_PRESS:
86 //we mark the location of the event to be able to calculate parameters of dragging
87 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
89 active_item=(get_item_at(clicked_x, clicked_y));
91 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
93 if(nodesmap[i]==active_item)
98 isbutton=e->button.button;
100 case GDK_BUTTON_RELEASE:
110 case GDK_MOTION_NOTIFY:
111 //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
112 if(active_node!=INVALID)
114 (mytab.mapstorage).modified = true;
116 //new coordinates will be the old values,
117 //because the item will be moved to the
118 //new coordinate therefore the new movement
119 //has to be calculated from here
123 window_to_world (e->motion.x, e->motion.y, new_x, new_y);
125 double dx=new_x-clicked_x;
126 double dy=new_y-clicked_y;
133 // reposition the coordinates text
134 std::ostringstream ostr;
136 (mytab.mapstorage).coords[active_node].x << ", " <<
137 (mytab.mapstorage).coords[active_node].y << ")";
139 (nodesmap[active_node]->property_x2().get_value() -
140 nodesmap[active_node]->property_x1().get_value()) / 2.0;
143 coord_text->property_text().set_value(ostr.str());
144 coord_text->property_x().set_value((mytab.mapstorage).coords[active_node].x +
146 coord_text->property_y().set_value((mytab.mapstorage).coords[active_node].y -
151 coord_text = new Gnome::Canvas::Text(
153 (mytab.mapstorage).coords[active_node].x + radius,
154 (mytab.mapstorage).coords[active_node].y - radius,
156 coord_text->property_fill_color().set_value("black");
157 coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST);
168 XY GraphDisplayerCanvas::calcArrowPos(XY moved_node_1, XY moved_node_2, XY fix_node, XY old_arrow_pos, int move_code)
173 return XY((moved_node_2.x + fix_node.x) / 2.0, (moved_node_2.y + fix_node.y) / 2.0);
176 return old_arrow_pos;
180 //////////////////////////////////////////////////////////////////////////////////////////////////////
181 /////////// keeps shape-with scalar multiplication - version 2.
182 //////////////////////////////////////////////////////////////////////////////////////////////////////
184 //old vector from one to the other node - a
185 XY a_v(moved_node_1.x-fix_node.x,moved_node_1.y-fix_node.y);
186 //new vector from one to the other node - b
187 XY b_v(moved_node_2.x-fix_node.x,moved_node_2.y-fix_node.y);
189 double absa=sqrt(a_v.normSquare());
190 double absb=sqrt(b_v.normSquare());
192 if ((absa == 0.0) || (absb == 0.0))
194 return old_arrow_pos;
198 //old vector from one node to the breakpoint - c
199 XY c_v(old_arrow_pos.x-fix_node.x,old_arrow_pos.y-fix_node.y);
201 //unit vector with the same direction to a_v
202 XY a_v_u(a_v.x/absa,a_v.y/absa);
204 //normal vector of unit vector with the same direction to a_v
205 XY a_v_u_n(((-1)*a_v_u.y),a_v_u.x);
207 //unit vector with the same direction to b_v
208 XY b_v_u(b_v.x/absb,b_v.y/absb);
210 //normal vector of unit vector with the same direction to b_v
211 XY b_v_u_n(((-1)*b_v_u.y),b_v_u.x);
213 //vector c in a_v_u and a_v_u_n co-ordinate system
214 XY c_a(c_v*a_v_u,c_v*a_v_u_n);
216 //new vector from one node to the breakpoint - d - we have to calculate this one
217 XY d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n);
219 return XY(d_v.x+fix_node.x,d_v.y+fix_node.y);
229 bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
234 case GDK_MOTION_NOTIFY:
236 GdkEvent * generated=new GdkEvent();
237 generated->motion.x=e->motion.x;
238 generated->motion.y=e->motion.y;
239 generated->type=GDK_MOTION_NOTIFY;
240 moveEventHandler(generated);
244 case GDK_BUTTON_RELEASE:
245 (mytab.mapstorage).modified = true;
249 active_node=(mytab.mapstorage).graph.addNode();
251 //initiating values corresponding to new node in maps
253 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
255 // update coordinates
256 (mytab.mapstorage).coords.set(active_node, XY(clicked_x, clicked_y));
258 // update all other maps
259 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
260 (mytab.mapstorage).nodemap_storage.begin(); it !=
261 (mytab.mapstorage).nodemap_storage.end(); ++it)
263 if ((it->first != "coordinates_x") &&
264 (it->first != "coordinates_y"))
266 (*(it->second))[active_node] =
267 (mytab.mapstorage).nodemap_default[it->first];
270 // increment the id map's default value
271 (mytab.mapstorage).nodemap_default["label"] += 1.0;
273 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph,
274 clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
275 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
276 *(nodesmap[active_node]) <<
277 Gnome::Canvas::Properties::fill_color("blue");
278 *(nodesmap[active_node]) <<
279 Gnome::Canvas::Properties::outline_color("black");
280 active_item->raise_to_top();
282 (nodesmap[active_node])->show();
284 nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph,
285 clicked_x+node_property_defaults[N_RADIUS]+5,
286 clicked_y+node_property_defaults[N_RADIUS]+5, "");
287 nodetextmap[active_node]->property_fill_color().set_value("darkblue");
288 nodetextmap[active_node]->raise_to_top();
290 // mapwin.updateNode(active_node);
291 propertyUpdate(active_node);
304 bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e)
308 case GDK_BUTTON_PRESS:
309 //in edge creation right button has special meaning
310 if(e->button.button!=3)
312 //there is not yet selected node
313 if(active_node==INVALID)
315 //we mark the location of the event to be able to calculate parameters of dragging
317 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
319 active_item=(get_item_at(clicked_x, clicked_y));
321 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
323 if(nodesmap[i]==active_item)
328 //the clicked item is really a node
329 if(active_node!=INVALID)
331 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
334 //clicked item was not a node. It could be e.g. edge.
340 //we only have to do sg. if the mouse button
341 // is pressed already once AND the click was
342 // on a node that was found in the set of
343 //nodes, and now we only search for the second
347 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
348 target_item=(get_item_at(clicked_x, clicked_y));
349 Node target_node=INVALID;
350 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
352 if(nodesmap[i]==target_item)
357 //the clicked item is a node, the edge can be drawn
358 if(target_node!=INVALID)
360 (mytab.mapstorage).modified = true;
362 *(nodesmap[target_node]) <<
363 Gnome::Canvas::Properties::fill_color("red");
366 active_edge=(mytab.mapstorage).graph.addEdge(active_node,
370 for (std::map<std::string,
371 Graph::EdgeMap<double>*>::const_iterator it =
372 (mytab.mapstorage).edgemap_storage.begin(); it !=
373 (mytab.mapstorage).edgemap_storage.end(); ++it)
375 (*(it->second))[active_edge] =
376 (mytab.mapstorage).edgemap_default[it->first];
378 // increment the id map's default value
379 (mytab.mapstorage).edgemap_default["label"] += 1.0;
381 if(target_node!=active_node)
383 // set the coordinates of the arrow on the new edge
384 MapStorage& ms = mytab.mapstorage;
385 ms.arrow_pos.set(active_edge,
386 (ms.coords[ms.graph.source(active_edge)] +
387 ms.coords[ms.graph.target(active_edge)])/ 2.0);
390 edgesmap[active_edge]=new BrokenEdge(displayed_graph, active_edge,
395 // set the coordinates of the arrow on the new edge
396 MapStorage& ms = mytab.mapstorage;
397 ms.arrow_pos.set(active_edge,
398 (ms.coords[ms.graph.source(active_edge)] +
402 edgesmap[active_edge]=new LoopEdge(displayed_graph, active_edge,
406 //initializing edge-text as well, to empty string
407 XY text_pos=mytab.mapstorage.arrow_pos[active_edge];
408 text_pos+=(XY(10,10));
410 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,
411 text_pos.x, text_pos.y, "");
412 edgetextmap[active_edge]->property_fill_color().set_value(
414 edgetextmap[active_edge]->raise_to_top();
416 propertyUpdate(active_edge);
418 //clicked item was not a node. it could be an e.g. edge. we do not
419 //deal with it furthermore.
427 case GDK_BUTTON_RELEASE:
429 //we clear settings in two cases
430 //1: the edge is ready (target_item has valid value)
431 //2: the edge creation is cancelled with right button
432 if((target_item)||(e->button.button==3))
436 *active_item << Gnome::Canvas::Properties::fill_color("blue");
441 *target_item << Gnome::Canvas::Properties::fill_color("blue");
454 bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e)
458 case GDK_BUTTON_PRESS:
459 //finding the clicked items
460 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
461 active_item=(get_item_at(clicked_x, clicked_y));
465 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
467 if(nodesmap[i]==active_item)
473 if(active_node==INVALID)
475 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
477 if(edgesmap[i]->getLine()==active_item)
484 // return if the clicked object is neither an edge nor a node
485 if (active_edge == INVALID) return false;
487 //recolor activated item
490 *active_item << Gnome::Canvas::Properties::fill_color("red");
494 case GDK_BUTTON_RELEASE:
495 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
498 //the cursor was not moved since pressing it
499 if( active_item == ( get_item_at (clicked_x, clicked_y) ) )
502 if(active_node!=INVALID)
504 (mytab.mapstorage).modified = true;
506 std::set<Graph::Edge> edges_to_delete;
508 for(OutEdgeIt e((mytab.mapstorage).graph,active_node);e!=INVALID;++e)
510 edges_to_delete.insert(e);
513 for(InEdgeIt e((mytab.mapstorage).graph,active_node);e!=INVALID;++e)
515 edges_to_delete.insert(e);
518 //deleting collected edges
519 for(std::set<Graph::Edge>::iterator
520 edge_set_it=edges_to_delete.begin();
521 edge_set_it!=edges_to_delete.end();
524 deleteItem(*edge_set_it);
526 deleteItem(active_node);
528 //a simple edge was chosen
529 else if (active_edge != INVALID)
531 deleteItem(active_edge);
534 //pointer was moved, deletion is cancelled
537 if(active_node!=INVALID)
539 *active_item << Gnome::Canvas::Properties::fill_color("blue");
541 else if (active_edge != INVALID)
543 *active_item << Gnome::Canvas::Properties::fill_color("green");
553 case GDK_MOTION_NOTIFY:
562 bool GraphDisplayerCanvas::mapEditEventHandler(GdkEvent* e)
564 if(actual_tool==MAP_EDIT)
568 case GDK_BUTTON_PRESS:
570 //for determine, whether it was an edge
571 Edge clicked_edge=INVALID;
572 //for determine, whether it was a node
573 Node clicked_node=INVALID;
575 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
576 active_item=(get_item_at(clicked_x, clicked_y));
578 //find the activated item between text of nodes
579 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
581 //at the same time only one can be active
582 if(nodetextmap[i]==active_item)
588 //if there was not, search for it between nodes
589 if(clicked_node==INVALID)
591 for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
593 //at the same time only one can be active
594 if(nodesmap[i]==active_item)
601 if(clicked_node==INVALID)
603 //find the activated item between texts
604 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
606 //at the same time only one can be active
607 if(edgetextmap[i]==active_item)
613 //if it was not between texts, search for it between edges
614 if(clicked_edge==INVALID)
616 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
618 //at the same time only one can be active
619 if((edgesmap[i]->getLine())==active_item)
627 //if it was really a node...
628 if(clicked_node!=INVALID)
630 // the id map is not editable
631 if (nodemap_to_edit == "label") return 0;
633 //and there is activated map
634 if(nodetextmap[clicked_node]->property_text().get_value()!="")
636 //activate the general variable for it
637 active_node=clicked_node;
640 Gtk::Dialog dialog("Edit value", true);
641 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
642 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
643 Gtk::VBox* vbox = dialog.get_vbox();
644 Gtk::SpinButton spin(0.0, 4);
645 spin.set_increments(1.0, 10.0);
646 spin.set_range(-1000000.0, 1000000.0);
647 spin.set_numeric(true);
648 spin.set_value(atof(nodetextmap[active_node]->property_text().get_value().c_str()));
651 switch (dialog.run())
653 case Gtk::RESPONSE_NONE:
654 case Gtk::RESPONSE_CANCEL:
656 case Gtk::RESPONSE_ACCEPT:
657 double new_value = spin.get_value();
658 (*(mytab.mapstorage).nodemap_storage[nodemap_to_edit])[active_node] =
660 std::ostringstream ostr;
662 nodetextmap[active_node]->property_text().set_value(ostr.str());
663 //mapwin.updateNode(active_node);
664 //mapwin.updateNode(Node(INVALID));
665 propertyUpdate(Node(INVALID));
670 //if it was really an edge...
671 if(clicked_edge!=INVALID)
673 // the id map is not editable
674 if (edgemap_to_edit == "label") return 0;
676 //and there is activated map
677 if(edgetextmap[clicked_edge]->property_text().get_value()!="")
679 //activate the general variable for it
680 active_edge=clicked_edge;
683 Gtk::Dialog dialog("Edit value", true);
684 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
685 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
686 Gtk::VBox* vbox = dialog.get_vbox();
687 Gtk::SpinButton spin(0.0, 4);
688 spin.set_increments(1.0, 10.0);
689 spin.set_range(-1000000.0, 1000000.0);
690 spin.set_numeric(true);
691 spin.set_value(atof(edgetextmap[active_edge]->property_text().get_value().c_str()));
694 switch (dialog.run())
696 case Gtk::RESPONSE_NONE:
697 case Gtk::RESPONSE_CANCEL:
699 case Gtk::RESPONSE_ACCEPT:
700 double new_value = spin.get_value();
701 (*(mytab.mapstorage).edgemap_storage[edgemap_to_edit])[active_edge] =
703 std::ostringstream ostr;
705 edgetextmap[active_edge]->property_text().set_value(
707 //mapwin.updateEdge(active_edge);
708 // mapwin.updateEdge(Edge(INVALID));
709 propertyUpdate(Edge(INVALID));
722 void GraphDisplayerCanvas::deleteItem(Node node_to_delete)
724 delete(nodetextmap[node_to_delete]);
725 delete(nodesmap[node_to_delete]);
726 (mytab.mapstorage).graph.erase(node_to_delete);
729 void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete)
731 delete(edgetextmap[edge_to_delete]);
732 delete(edgesmap[edge_to_delete]);
733 (mytab.mapstorage).graph.erase(edge_to_delete);
736 void GraphDisplayerCanvas::textReposition(XY new_place)
738 new_place+=(XY(10,10));
739 edgetextmap[forming_edge]->property_x().set_value(new_place.x);
740 edgetextmap[forming_edge]->property_y().set_value(new_place.y);
743 void GraphDisplayerCanvas::toggleEdgeActivity(EdgeBase* active_bre, bool on)
747 if(forming_edge!=INVALID)
749 std::cerr << "ERROR!!!! Valid edge found!" << std::endl;
753 for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
755 if(edgesmap[i]==active_bre)
764 if(forming_edge!=INVALID)
766 forming_edge=INVALID;
770 std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
775 void GraphDisplayerCanvas::moveNode(double dx, double dy, Gnome::Canvas::Item * item, Node node)
777 Gnome::Canvas::Item * moved_item=item;
778 Node moved_node=node;
780 if(item==NULL && node==INVALID)
782 moved_item=active_item;
783 moved_node=active_node;
790 //repositioning node and its text
791 moved_item->move(dx, dy);
792 nodetextmap[moved_node]->move(dx, dy);
794 // the new coordinates of the centre of the node
795 double coord_x = dx + (mytab.mapstorage).coords[moved_node].x;
796 double coord_y = dy + (mytab.mapstorage).coords[moved_node].y;
798 // write back the new coordinates to the coords map
799 (mytab.mapstorage).coords.set(moved_node, XY(coord_x, coord_y));
801 //all the edges connected to the moved point has to be redrawn
802 for(OutEdgeIt ei((mytab.mapstorage).graph,moved_node);ei!=INVALID;++ei)
806 if (mytab.mapstorage.graph.source(ei) == mytab.mapstorage.graph.target(ei))
808 arrow_pos = mytab.mapstorage.arrow_pos[ei] + XY(dx, dy);
812 XY moved_node_1(coord_x - dx, coord_y - dy);
813 XY moved_node_2(coord_x, coord_y);
814 Node target = mytab.mapstorage.graph.target(ei);
815 XY fix_node(mytab.mapstorage.coords[target].x,
816 mytab.mapstorage.coords[target].y);
817 XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
819 arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton);
822 mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
823 edgesmap[ei]->draw();
825 //reposition of edgetext
826 XY text_pos=mytab.mapstorage.arrow_pos[ei];
827 text_pos+=(XY(10,10));
828 edgetextmap[ei]->property_x().set_value(text_pos.x);
829 edgetextmap[ei]->property_y().set_value(text_pos.y);
832 for(InEdgeIt ei((mytab.mapstorage).graph,moved_node);ei!=INVALID;++ei)
834 if (mytab.mapstorage.graph.source(ei) != mytab.mapstorage.graph.target(ei))
836 XY moved_node_1(coord_x - dx, coord_y - dy);
837 XY moved_node_2(coord_x, coord_y);
838 Node source = mytab.mapstorage.graph.source(ei);
839 XY fix_node(mytab.mapstorage.coords[source].x,
840 mytab.mapstorage.coords[source].y);
841 XY old_arrow_pos(mytab.mapstorage.arrow_pos[ei]);
844 arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton);
846 mytab.mapstorage.arrow_pos.set(ei, arrow_pos);
847 edgesmap[ei]->draw();
849 //reposition of edgetext
850 XY text_pos=mytab.mapstorage.arrow_pos[ei];
851 text_pos+=(XY(10,10));
852 edgetextmap[ei]->property_x().set_value(text_pos.x);
853 edgetextmap[ei]->property_y().set_value(text_pos.y);