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);
31 //has to do the same thing as in the case of NODE_MAP_EDIT
51 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
55 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false);
59 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false);
63 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false);
68 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false);
72 actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false);
81 int GraphDisplayerCanvas::getActualTool()
86 bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e)
88 static Gnome::Canvas::Text *coord_text = 0;
91 case GDK_BUTTON_PRESS:
92 //we mark the location of the event to be able to calculate parameters of dragging
93 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
95 active_item=(get_item_at(clicked_x, clicked_y));
97 for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i)
99 if(nodesmap[i]==active_item)
104 switch(e->button.button)
114 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 (mainwin.mapstorage).modified = true;
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);
146 // the new coordinates of the centre of the node
147 double coord_x = new_x - (clicked_x - (mainwin.mapstorage).coords[active_node].x);
148 double coord_y = new_y - (clicked_y - (mainwin.mapstorage).coords[active_node].y);
153 // write back the new coordinates to the coords map
154 (mainwin.mapstorage).coords.set(active_node, xy<double>(coord_x, coord_y));
156 // reposition the coordinates text
157 std::ostringstream ostr;
159 (mainwin.mapstorage).coords[active_node].x << ", " <<
160 (mainwin.mapstorage).coords[active_node].y << ")";
162 (nodesmap[active_node]->property_x2().get_value() -
163 nodesmap[active_node]->property_x1().get_value()) / 2.0;
166 coord_text->property_text().set_value(ostr.str());
167 coord_text->property_x().set_value((mainwin.mapstorage).coords[active_node].x +
169 coord_text->property_y().set_value((mainwin.mapstorage).coords[active_node].y -
174 coord_text = new Gnome::Canvas::Text(
176 (mainwin.mapstorage).coords[active_node].x + radius,
177 (mainwin.mapstorage).coords[active_node].y - radius,
179 coord_text->property_fill_color().set_value("black");
180 coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST);
183 //all the edges connected to the moved point has to be redrawn
184 for(OutEdgeIt ei((mainwin.mapstorage).graph,active_node);ei!=INVALID;++ei)
186 Gnome::Canvas::Points coos;
187 double x1, x2, y1, y2;
189 nodesmap[(mainwin.mapstorage).graph.source(ei)]->get_bounds(x1, y1, x2, y2);
190 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
192 nodesmap[(mainwin.mapstorage).graph.target(ei)]->get_bounds(x1, y1, x2, y2);
193 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
197 edgesmap[ei]->setPoints(coos);
201 edgesmap[ei]->setPoints(coos,true);
204 //reposition of edgetext
205 xy<double> text_pos=edgesmap[ei]->getArrowPos();
206 text_pos+=(xy<double>(10,10));
207 edgetextmap[ei]->property_x().set_value(text_pos.x);
208 edgetextmap[ei]->property_y().set_value(text_pos.y);
211 for(InEdgeIt ei((mainwin.mapstorage).graph,active_node);ei!=INVALID;++ei)
213 Gnome::Canvas::Points coos;
214 double x1, x2, y1, y2;
216 nodesmap[(mainwin.mapstorage).graph.source(ei)]->get_bounds(x1, y1, x2, y2);
217 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
219 nodesmap[(mainwin.mapstorage).graph.target(ei)]->get_bounds(x1, y1, x2, y2);
220 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
224 edgesmap[ei]->setPoints(coos);
228 edgesmap[ei]->setPoints(coos,true);
231 xy<double> text_pos=edgesmap[ei]->getArrowPos();
232 text_pos+=(xy<double>(10,10));
233 edgetextmap[ei]->property_x().set_value(text_pos.x);
234 edgetextmap[ei]->property_y().set_value(text_pos.y);
243 bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
248 case GDK_MOTION_NOTIFY:
250 GdkEvent * generated=new GdkEvent();
251 generated->motion.x=e->motion.x;
252 generated->motion.y=e->motion.y;
253 generated->type=GDK_MOTION_NOTIFY;
254 moveEventHandler(generated);
258 case GDK_BUTTON_RELEASE:
259 (mainwin.mapstorage).modified = true;
263 active_node=(mainwin.mapstorage).graph.addNode();
265 //initiating values corresponding to new node in maps
267 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
269 // update coordinates
270 (mainwin.mapstorage).coords.set(active_node, xy<double>(clicked_x, clicked_y));
272 // update all other maps
273 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
274 (mainwin.mapstorage).nodemap_storage.begin(); it !=
275 (mainwin.mapstorage).nodemap_storage.end(); ++it)
277 if ((it->first != "coordinates_x") &&
278 (it->first != "coordinates_y"))
280 (*(it->second))[active_node] =
281 (mainwin.mapstorage).nodemap_default[it->first];
284 // increment the id map's default value
285 (mainwin.mapstorage).nodemap_default["id"] += 1.0;
287 nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph,
288 clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
289 active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
290 *(nodesmap[active_node]) <<
291 Gnome::Canvas::Properties::fill_color("blue");
292 *(nodesmap[active_node]) <<
293 Gnome::Canvas::Properties::outline_color("black");
294 active_item->raise_to_top();
296 (nodesmap[active_node])->show();
298 nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph,
299 clicked_x+node_property_defaults[N_RADIUS]+5,
300 clicked_y+node_property_defaults[N_RADIUS]+5, "");
301 nodetextmap[active_node]->property_fill_color().set_value("darkblue");
302 nodetextmap[active_node]->raise_to_top();
304 // mapwin.updateNode(active_node);
305 propertyUpdate(active_node);
318 bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e)
322 case GDK_BUTTON_PRESS:
323 //in edge creation right button has special meaning
324 if(e->button.button!=3)
326 //there is not yet selected node
327 if(active_node==INVALID)
329 //we mark the location of the event to be able to calculate parameters of dragging
331 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
333 active_item=(get_item_at(clicked_x, clicked_y));
335 for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i)
337 if(nodesmap[i]==active_item)
342 //the clicked item is really a node
343 if(active_node!=INVALID)
345 *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
348 //clicked item was not a node. It could be e.g. edge.
354 //we only have to do sg. if the mouse button
355 // is pressed already once AND the click was
356 // on a node that was found in the set of
357 //nodes, and now we only search for the second
361 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
362 target_item=(get_item_at(clicked_x, clicked_y));
363 Node target_node=INVALID;
364 for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i)
366 if(nodesmap[i]==target_item)
371 //the clicked item is a node, the edge can be drawn
372 if(target_node!=INVALID)
374 if(target_node!=active_node)
376 (mainwin.mapstorage).modified = true;
378 *(nodesmap[target_node]) <<
379 Gnome::Canvas::Properties::fill_color("red");
382 active_edge=(mainwin.mapstorage).graph.addEdge(active_node,
386 for (std::map<std::string,
387 Graph::EdgeMap<double>*>::const_iterator it =
388 (mainwin.mapstorage).edgemap_storage.begin(); it !=
389 (mainwin.mapstorage).edgemap_storage.end(); ++it)
391 (*(it->second))[active_edge] =
392 (mainwin.mapstorage).edgemap_default[it->first];
394 // increment the id map's default value
395 (mainwin.mapstorage).edgemap_default["id"] += 1.0;
397 //calculating coordinates of new edge
398 Gnome::Canvas::Points coos;
399 double x1, x2, y1, y2;
401 active_item->get_bounds(x1, y1, x2, y2);
402 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
404 target_item->get_bounds(x1, y1, x2, y2);
405 coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
408 edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos,
410 *(edgesmap[active_edge]) <<
411 Gnome::Canvas::Properties::fill_color("green");
412 edgesmap[active_edge]->property_width_pixels().set_value(10);
414 edgesmap[active_edge]->lower_to_bottom();
416 //initializing edge-text as well, to empty string
417 xy<double> text_pos=edgesmap[active_edge]->getArrowPos();
418 text_pos+=(xy<double>(10,10));
420 edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,
421 text_pos.x, text_pos.y, "");
422 edgetextmap[active_edge]->property_fill_color().set_value(
424 edgetextmap[active_edge]->raise_to_top();
426 //updating its properties
427 // mapwin.updateEdge(active_edge);
428 propertyUpdate(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((mainwin.mapstorage).graph); i!=INVALID; ++i)
485 if(nodesmap[i]==active_item)
491 if(active_node==INVALID)
493 for (EdgeIt i((mainwin.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 (mainwin.mapstorage).modified = true;
521 std::set<Graph::Edge> edges_to_delete;
523 for(OutEdgeIt e((mainwin.mapstorage).graph,active_node);e!=INVALID;++e)
525 edges_to_delete.insert(e);
528 for(InEdgeIt e((mainwin.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((mainwin.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((mainwin.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", mainwin, 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();
631 Gtk::SpinButton spin(0.0, 4);
632 spin.set_increments(1.0, 10.0);
633 spin.set_range(-1000000.0, 1000000.0);
634 spin.set_numeric(true);
637 switch (dialog.run())
639 case Gtk::RESPONSE_NONE:
640 case Gtk::RESPONSE_CANCEL:
642 case Gtk::RESPONSE_ACCEPT:
643 double new_value = spin.get_value();
644 (*(mainwin.mapstorage).edgemap_storage[edgemap_to_edit])[active_edge] =
646 std::ostringstream ostr;
648 edgetextmap[active_edge]->property_text().set_value(
650 //mapwin.updateEdge(active_edge);
651 // mapwin.updateEdge(Edge(INVALID));
652 propertyUpdate(Edge(INVALID));
665 bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e)
667 if(actual_tool==NODE_MAP_EDIT)
671 case GDK_BUTTON_PRESS:
673 //for determine, whether it was a node
674 Node clicked_node=INVALID;
676 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
677 active_item=(get_item_at(clicked_x, clicked_y));
679 //find the activated item between texts
680 for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i)
682 //at the same time only one can be active
683 if(nodetextmap[i]==active_item)
689 //if there was not, search for it between nodes
690 if(clicked_node==INVALID)
692 for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i)
694 //at the same time only one can be active
695 if(nodesmap[i]==active_item)
702 //if it was really a node...
703 if(clicked_node!=INVALID)
705 // the id map is not editable
706 if (nodemap_to_edit == "id") return 0;
708 //and there is activated map
709 if(nodetextmap[clicked_node]->property_text().get_value()!="")
711 //activate the general variable for it
712 active_node=clicked_node;
715 Gtk::Dialog dialog("Edit value", mainwin, true);
716 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
717 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
718 Gtk::VBox* vbox = dialog.get_vbox();
719 Gtk::SpinButton spin(0.0, 4);
720 spin.set_increments(1.0, 10.0);
721 spin.set_range(-1000000.0, 1000000.0);
722 spin.set_numeric(true);
725 switch (dialog.run())
727 case Gtk::RESPONSE_NONE:
728 case Gtk::RESPONSE_CANCEL:
730 case Gtk::RESPONSE_ACCEPT:
731 double new_value = spin.get_value();
732 (*(mainwin.mapstorage).nodemap_storage[nodemap_to_edit])[active_node] =
734 std::ostringstream ostr;
736 nodetextmap[active_node]->property_text().set_value(
738 //mapwin.updateNode(active_node);
739 // mapwin.updateNode(Node(INVALID));
740 propertyUpdate(Node(INVALID));
753 void GraphDisplayerCanvas::deleteItem(Node node_to_delete)
755 delete(nodetextmap[node_to_delete]);
756 delete(nodesmap[node_to_delete]);
757 (mainwin.mapstorage).graph.erase(node_to_delete);
760 void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete)
762 delete(edgetextmap[edge_to_delete]);
763 delete(edgesmap[edge_to_delete]);
764 (mainwin.mapstorage).graph.erase(edge_to_delete);
767 void GraphDisplayerCanvas::textReposition(xy<double> new_place)
769 new_place+=(xy<double>(10,10));
770 edgetextmap[forming_edge]->property_x().set_value(new_place.x);
771 edgetextmap[forming_edge]->property_y().set_value(new_place.y);
774 void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on)
778 if(forming_edge!=INVALID)
780 std::cerr << "ERROR!!!! Valid edge found!" << std::endl;
784 for (EdgeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i)
786 if(edgesmap[i]==active_bre)
795 if(forming_edge!=INVALID)
797 forming_edge=INVALID;
801 std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
807 int GraphDisplayerCanvas::addNewEdgeMap(double default_value, std::string mapname)
810 Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> ((mainwin.mapstorage).graph, default_value);
812 //if addition was not successful addEdgeMap returns one.
813 //cause can be that there is already a map named like the new one
814 if((mainwin.mapstorage).addEdgeMap(mapname,emptr, default_value))
820 //add it to the list of the displayable maps
821 mainwin.registerNewEdgeMap(mapname);
824 changeEdgeText(mapname);
829 int GraphDisplayerCanvas::addNewNodeMap(double default_value, std::string mapname)
832 Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> ((mainwin.mapstorage).graph,default_value);
834 //if addition was not successful addNodeMap returns one.
835 //cause can be that there is already a map named like the new one
836 if((mainwin.mapstorage).addNodeMap(mapname,emptr, default_value))
841 //add it to the list of the displayable maps
842 mainwin.registerNewNodeMap(mapname);
845 changeNodeText(mapname);