- Timer can be stop()ed and (re)start()ed.
- Obsolete \bug removed
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);
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(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(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 mapstorage.modified = true;
378 *(nodesmap[target_node]) <<
379 Gnome::Canvas::Properties::fill_color("red");
382 active_edge=mapstorage.graph.addEdge(active_node,
386 for (std::map<std::string,
387 Graph::EdgeMap<double>*>::const_iterator it =
388 mapstorage.edgemap_storage.begin(); it !=
389 mapstorage.edgemap_storage.end(); ++it)
391 (*(it->second))[active_edge] =
392 mapstorage.edgemap_default[it->first];
394 // increment the id map's default value
395 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);
432 std::cerr << "Loop edge is not yet implemented!" << std::endl;
435 //clicked item was not a node. it could be an e.g. edge. we do not
436 //deal with it furthermore.
444 case GDK_BUTTON_RELEASE:
446 //we clear settings in two cases
447 //1: the edge is ready (target_item has valid value)
448 //2: the edge creation is cancelled with right button
449 if((target_item)||(e->button.button==3))
453 *active_item << Gnome::Canvas::Properties::fill_color("blue");
458 *target_item << Gnome::Canvas::Properties::fill_color("blue");
471 bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e)
475 case GDK_BUTTON_PRESS:
476 //finding the clicked items
477 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
478 active_item=(get_item_at(clicked_x, clicked_y));
482 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
484 if(nodesmap[i]==active_item)
490 if(active_node==INVALID)
492 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
494 if(edgesmap[i]==active_item)
501 //recolor activated item
504 *active_item << Gnome::Canvas::Properties::fill_color("red");
508 case GDK_BUTTON_RELEASE:
509 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
512 //the cursor was not moved since pressing it
513 if( active_item == ( get_item_at (clicked_x, clicked_y) ) )
516 if(active_node!=INVALID)
518 mapstorage.modified = true;
520 std::set<Graph::Edge> edges_to_delete;
522 for(OutEdgeIt e(mapstorage.graph,active_node);e!=INVALID;++e)
524 edges_to_delete.insert(e);
527 for(InEdgeIt e(mapstorage.graph,active_node);e!=INVALID;++e)
529 edges_to_delete.insert(e);
532 //deleting collected edges
533 for(std::set<Graph::Edge>::iterator
534 edge_set_it=edges_to_delete.begin();
535 edge_set_it!=edges_to_delete.end();
538 deleteItem(*edge_set_it);
540 deleteItem(active_node);
542 //a simple edge was chosen
545 deleteItem(active_edge);
548 //pointer was moved, deletion is cancelled
551 if(active_node!=INVALID)
553 *active_item << Gnome::Canvas::Properties::fill_color("blue");
557 *active_item << Gnome::Canvas::Properties::fill_color("green");
567 case GDK_MOTION_NOTIFY:
576 bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e)
578 if(actual_tool==EDGE_MAP_EDIT)
582 case GDK_BUTTON_PRESS:
584 //for determine, whether it was an edge
585 Edge clicked_edge=INVALID;
587 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
588 active_item=(get_item_at(clicked_x, clicked_y));
590 //find the activated item between texts
591 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
593 //at the same time only one can be active
594 if(edgetextmap[i]==active_item)
600 //if it was not between texts, search for it between edges
601 if(clicked_edge==INVALID)
603 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
605 //at the same time only one can be active
606 if((edgesmap[i]==active_item)||(edgetextmap[i]==active_item))
613 //if it was really an edge...
614 if(clicked_edge!=INVALID)
616 // the id map is not editable
617 if (edgemap_to_edit == "id") return 0;
619 //and there is activated map
620 if(edgetextmap[clicked_edge]->property_text().get_value()!="")
622 //activate the general variable for it
623 active_edge=clicked_edge;
626 Gtk::Dialog dialog("Edit value", *parentwin, true);
627 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
628 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
629 Gtk::VBox* vbox = dialog.get_vbox();
631 (*mapstorage.edgemap_storage[edgemap_to_edit])[active_edge],
635 //TODO: find out why doesn't it work with
636 //numeric_limits<double>::min/max
637 Gtk::SpinButton spin(adj);
638 spin.set_numeric(true);
642 switch (dialog.run())
644 case Gtk::RESPONSE_NONE:
645 case Gtk::RESPONSE_CANCEL:
647 case Gtk::RESPONSE_ACCEPT:
648 double new_value = spin.get_value();
649 (*mapstorage.edgemap_storage[edgemap_to_edit])[active_edge] =
651 std::ostringstream ostr;
653 edgetextmap[active_edge]->property_text().set_value(
655 //mapwin.updateEdge(active_edge);
656 mapwin.updateEdge(Edge(INVALID));
669 bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e)
671 if(actual_tool==NODE_MAP_EDIT)
675 case GDK_BUTTON_PRESS:
677 //for determine, whether it was a node
678 Node clicked_node=INVALID;
680 window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
681 active_item=(get_item_at(clicked_x, clicked_y));
683 //find the activated item between texts
684 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
686 //at the same time only one can be active
687 if(nodetextmap[i]==active_item)
693 //if there was not, search for it between nodes
694 if(clicked_node==INVALID)
696 for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
698 //at the same time only one can be active
699 if(nodesmap[i]==active_item)
706 //if it was really a node...
707 if(clicked_node!=INVALID)
709 // the id map is not editable
710 if (nodemap_to_edit == "id") return 0;
712 //and there is activated map
713 if(nodetextmap[clicked_node]->property_text().get_value()!="")
715 //activate the general variable for it
716 active_node=clicked_node;
719 Gtk::Dialog dialog("Edit value", *parentwin, true);
720 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
721 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
722 Gtk::VBox* vbox = dialog.get_vbox();
724 (*mapstorage.nodemap_storage[nodemap_to_edit])[active_node],
728 //TODO: find out why doesn't it work with
729 //numeric_limits<double>::min/max
730 Gtk::SpinButton spin(adj);
731 spin.set_numeric(true);
735 switch (dialog.run())
737 case Gtk::RESPONSE_NONE:
738 case Gtk::RESPONSE_CANCEL:
740 case Gtk::RESPONSE_ACCEPT:
741 double new_value = spin.get_value();
742 (*mapstorage.nodemap_storage[nodemap_to_edit])[active_node] =
744 std::ostringstream ostr;
746 nodetextmap[active_node]->property_text().set_value(
748 //mapwin.updateNode(active_node);
749 mapwin.updateNode(Node(INVALID));
762 void GraphDisplayerCanvas::deleteItem(Node node_to_delete)
764 delete(nodetextmap[node_to_delete]);
765 delete(nodesmap[node_to_delete]);
766 mapstorage.graph.erase(node_to_delete);
769 void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete)
771 delete(edgetextmap[edge_to_delete]);
772 delete(edgesmap[edge_to_delete]);
773 mapstorage.graph.erase(edge_to_delete);
776 void GraphDisplayerCanvas::textReposition(xy<double> new_place)
778 new_place+=(xy<double>(10,10));
779 edgetextmap[forming_edge]->property_x().set_value(new_place.x);
780 edgetextmap[forming_edge]->property_y().set_value(new_place.y);
783 void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on)
787 if(forming_edge!=INVALID)
789 std::cerr << "ERROR!!!! Valid edge found!" << std::endl;
793 for (EdgeIt i(mapstorage.graph); i!=INVALID; ++i)
795 if(edgesmap[i]==active_bre)
804 if(forming_edge!=INVALID)
806 forming_edge=INVALID;
810 std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
816 int GraphDisplayerCanvas::addNewEdgeMap(double default_value, std::string mapname)
819 Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (mapstorage.graph, default_value);
821 //if addition was not successful addEdgeMap returns one.
822 //cause can be that there is already a map named like the new one
823 if(mapstorage.addEdgeMap(mapname,emptr, default_value))
829 //add it to the list of the displayable maps
830 mapwin.registerNewEdgeMap(mapname);
833 changeEdgeText(mapname);
838 int GraphDisplayerCanvas::addNewNodeMap(double default_value, std::string mapname)
841 Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (mapstorage.graph,default_value);
843 //if addition was not successful addNodeMap returns one.
844 //cause can be that there is already a map named like the new one
845 if(mapstorage.addNodeMap(mapname,emptr, default_value))
850 //add it to the list of the displayable maps
851 mapwin.registerNewNodeMap(mapname);
854 changeNodeText(mapname);