diff -r d1fc1bf7decc -r 1f3baf3bd1f2 src/work/peter/graph-displayer.cc --- a/src/work/peter/graph-displayer.cc Mon Apr 04 16:19:29 2005 +0000 +++ b/src/work/peter/graph-displayer.cc Mon Apr 04 19:22:04 2005 +0000 @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -36,29 +37,20 @@ private: ///Event handler function that handles dragging nodes of displayed_graph - bool event_handler(GdkEvent* e, int b); + bool event_handler(GdkEvent* e, Node n); - ///Event handler function that handles dragging displayed_graph - bool tri_mover(GdkEvent* e); - - ///Coordinates of Weight Point of tirangle - Gnome::Art::Point * wp; - ///Array of nodes of planefigure - Gnome::Canvas::Ellipse ** nodes; - ///Sides of planefigure - Gnome::Canvas::Polygon * sides; + ///The graph, on which we work + Graph g; + ///Map of nodes of planefigure + Graph::NodeMap nodesmap; + ///Map of edges of planefigure + Graph::EdgeMap edgesmap; ///Group of graphical elements of displayed_graph Gnome::Canvas::Group displayed_graph; ///Indicates whether the button of mouse is pressed or not bool isbutton; - ///Number Of Elements - the number of nodes - int noe; - - ///Array of coordinates - double * coordinates; - ///At this location was the mousebutton pressed. ///It helps to calculate the distance of dragging. double clicked_x, clicked_y; @@ -72,53 +64,11 @@ }; -///When we click on the weight point we can drag the whole planefigure. This function resolves it. -bool GraphDisplayerCanvas::tri_mover(GdkEvent* e) -{ - switch(e->type) - { - case GDK_BUTTON_PRESS: - clicked_x=e->button.x; - clicked_y=e->button.y; - isbutton=true; - break; - case GDK_BUTTON_RELEASE: - isbutton=false; - active_item=NULL; - break; - case GDK_MOTION_NOTIFY: - if(isbutton) - { - double dx=e->motion.x-clicked_x; - double dy=e->motion.y-clicked_y; - - Gnome::Canvas::Points coos; - - for(int i=0;i<=noe;i++) - { - nodes[i]->move(dx,dy); - - double x=(coordinates[2*i]+=dx); - double y=(coordinates[2*i+1]+=dy); - - if(i!=noe)coos.push_back(Gnome::Art::Point(x,y)); - - } - - clicked_x=e->motion.x; - clicked_y=e->motion.y; - - sides->property_points().set_value(coos); - } - default: break; - } - return true; -} ///This function moves only one node of displayed_graph, ///but recalculate the location of weight point, ///and also redraw the sides of the planefigure. -bool GraphDisplayerCanvas::event_handler(GdkEvent* e, int b) +bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n) { switch(e->type) { @@ -135,77 +85,98 @@ case GDK_MOTION_NOTIFY: if(isbutton) { - //double x1, y1, x2, y2; - //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2); - //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2); - //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y); double dx=e->motion.x-clicked_x; double dy=e->motion.y-clicked_y; active_item->move(dx, dy); - - coordinates[2*b]+=dx; - coordinates[2*b+1]+=dy; - - Gnome::Canvas::Points coos; - - double x_wp=0; - double y_wp=0; - - for(int i=0;iproperty_points().set_value(coos); - - x_wp/=noe; - y_wp/=noe; - - dx=x_wp-coordinates[noe*2]; - dy=y_wp-coordinates[noe*2+1]; - nodes[noe]->move(dx, dy); - - coordinates[noe*2]+=dx; - coordinates[noe*2+1]+=dy; - clicked_x=e->motion.x; clicked_y=e->motion.y; + + EdgeIt e; + + g.firstOut(e,n); + for(;e!=INVALID;g.nextOut(e)) + { + Gnome::Canvas::Points coos; + double x1, x2, y1, y2; + + nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); + + nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); + + edgesmap[e]->property_points().set_value(coos); + } + + g.firstIn(e,n); + for(;e!=INVALID;g.nextIn(e)) + { + Gnome::Canvas::Points coos; + double x1, x2, y1, y2; + + nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2); + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); + + nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2); + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); + + edgesmap[e]->property_points().set_value(coos); + } } default: break; } return true; } -GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & g, CoordinatesMap & cm):displayed_graph(*(root()), 0, 0),isbutton(false),active_item(NULL) +GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm):g(gr),nodesmap(g),edgesmap(g),displayed_graph(*(root()), 0, 0),isbutton(false),active_item(NULL) { - nodes=new Gnome::Canvas::Ellipse* [countNodes(g)]; - int sorszam=0; - + for (EdgeIt i(g); i!=INVALID; ++i) + { + Gnome::Canvas::Points coos; + coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y)); + coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y)); + edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos); + *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green"); + edgesmap[i]->property_width_pixels().set_value(10); + } for (NodeIt i(g); i!=INVALID; ++i) { - nodes[sorszam]= new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20); - *(nodes[sorszam]) << Gnome::Canvas::Properties::fill_color("blue"); - *(nodes[sorszam]) << Gnome::Canvas::Properties::outline_color("black"); - (nodes[sorszam])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),sorszam)); - sorszam++; + nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20); + *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue"); + *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black"); + (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i)); } - for (EdgeIt i(g); i!=INVALID; ++i) - { - } - - } GraphDisplayerCanvas::~GraphDisplayerCanvas() { + Graph::NodeMap id(g); + Graph::NodeMap xc(g); + Graph::NodeMap yc(g); + + int j=1; + + for (NodeIt i(g); i!=INVALID; ++i) + { + double x1,y1,x2,y2; + nodesmap[i]->get_bounds(x1, y1, x2, y2); + + id[i]=j++; + xc[i]=(x1+x2)/2; + yc[i]=(y1+y2)/2; + } + + GraphWriter writer(std::cout,g); + + writer.addNodeMap("id", id); + writer.addNodeMap("coordinates_x", xc); + writer.addNodeMap("coordinates_y", yc); + writer.run(); } + //MainWin: - class MainWin : public Gtk::Window { public: @@ -290,6 +261,7 @@ Graph g; CoordinatesMap cm(g); + Graph::EdgeMap cap(g); //we create one object to read x coordinates //and one to read y coordinate of nodes and write them to cm NodeMap. @@ -303,10 +275,6 @@ reader.addNodeMap("coordinates_y", yreader); reader.run(); - for (NodeIt i(g); i!=INVALID; ++i) - std::cout << " " << g.id(i) << " " << cm[i]; - std::cout << std::endl; - Gnome::Canvas::init(); Gtk::Main app(argc, argv);