Graph displayer displays graphs now.
1.1 --- a/src/work/peter/graph-displayer.cc Mon Apr 04 16:19:29 2005 +0000
1.2 +++ b/src/work/peter/graph-displayer.cc Mon Apr 04 19:22:04 2005 +0000
1.3 @@ -11,6 +11,7 @@
1.4
1.5 #include <lemon/list_graph.h>
1.6 #include <lemon/graph_reader.h>
1.7 +#include <lemon/graph_writer.h>
1.8 #include <lemon/graph_utils.h>
1.9 #include <lemon/maps.h>
1.10 #include <lemon/error.h>
1.11 @@ -36,29 +37,20 @@
1.12 private:
1.13
1.14 ///Event handler function that handles dragging nodes of displayed_graph
1.15 - bool event_handler(GdkEvent* e, int b);
1.16 + bool event_handler(GdkEvent* e, Node n);
1.17
1.18 - ///Event handler function that handles dragging displayed_graph
1.19 - bool tri_mover(GdkEvent* e);
1.20 -
1.21 - ///Coordinates of Weight Point of tirangle
1.22 - Gnome::Art::Point * wp;
1.23 - ///Array of nodes of planefigure
1.24 - Gnome::Canvas::Ellipse ** nodes;
1.25 - ///Sides of planefigure
1.26 - Gnome::Canvas::Polygon * sides;
1.27 + ///The graph, on which we work
1.28 + Graph g;
1.29 + ///Map of nodes of planefigure
1.30 + Graph::NodeMap<Gnome::Canvas::Ellipse *> nodesmap;
1.31 + ///Map of edges of planefigure
1.32 + Graph::EdgeMap<Gnome::Canvas::Line *> edgesmap;
1.33 ///Group of graphical elements of displayed_graph
1.34 Gnome::Canvas::Group displayed_graph;
1.35
1.36 ///Indicates whether the button of mouse is pressed or not
1.37 bool isbutton;
1.38
1.39 - ///Number Of Elements - the number of nodes
1.40 - int noe;
1.41 -
1.42 - ///Array of coordinates
1.43 - double * coordinates;
1.44 -
1.45 ///At this location was the mousebutton pressed.
1.46 ///It helps to calculate the distance of dragging.
1.47 double clicked_x, clicked_y;
1.48 @@ -72,53 +64,11 @@
1.49
1.50 };
1.51
1.52 -///When we click on the weight point we can drag the whole planefigure. This function resolves it.
1.53 -bool GraphDisplayerCanvas::tri_mover(GdkEvent* e)
1.54 -{
1.55 - switch(e->type)
1.56 - {
1.57 - case GDK_BUTTON_PRESS:
1.58 - clicked_x=e->button.x;
1.59 - clicked_y=e->button.y;
1.60 - isbutton=true;
1.61 - break;
1.62 - case GDK_BUTTON_RELEASE:
1.63 - isbutton=false;
1.64 - active_item=NULL;
1.65 - break;
1.66 - case GDK_MOTION_NOTIFY:
1.67 - if(isbutton)
1.68 - {
1.69 - double dx=e->motion.x-clicked_x;
1.70 - double dy=e->motion.y-clicked_y;
1.71 -
1.72 - Gnome::Canvas::Points coos;
1.73 -
1.74 - for(int i=0;i<=noe;i++)
1.75 - {
1.76 - nodes[i]->move(dx,dy);
1.77 -
1.78 - double x=(coordinates[2*i]+=dx);
1.79 - double y=(coordinates[2*i+1]+=dy);
1.80 -
1.81 - if(i!=noe)coos.push_back(Gnome::Art::Point(x,y));
1.82 -
1.83 - }
1.84 -
1.85 - clicked_x=e->motion.x;
1.86 - clicked_y=e->motion.y;
1.87 -
1.88 - sides->property_points().set_value(coos);
1.89 - }
1.90 - default: break;
1.91 - }
1.92 - return true;
1.93 -}
1.94
1.95 ///This function moves only one node of displayed_graph,
1.96 ///but recalculate the location of weight point,
1.97 ///and also redraw the sides of the planefigure.
1.98 -bool GraphDisplayerCanvas::event_handler(GdkEvent* e, int b)
1.99 +bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
1.100 {
1.101 switch(e->type)
1.102 {
1.103 @@ -135,77 +85,98 @@
1.104 case GDK_MOTION_NOTIFY:
1.105 if(isbutton)
1.106 {
1.107 - //double x1, y1, x2, y2;
1.108 - //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
1.109 - //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
1.110 - //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
1.111 double dx=e->motion.x-clicked_x;
1.112 double dy=e->motion.y-clicked_y;
1.113 active_item->move(dx, dy);
1.114 -
1.115 - coordinates[2*b]+=dx;
1.116 - coordinates[2*b+1]+=dy;
1.117 -
1.118 - Gnome::Canvas::Points coos;
1.119 -
1.120 - double x_wp=0;
1.121 - double y_wp=0;
1.122 -
1.123 - for(int i=0;i<noe;i++)
1.124 - {
1.125 - coos.push_back(Gnome::Art::Point(coordinates[2*i], coordinates[2*i+1]));
1.126 -
1.127 - x_wp+=coordinates[2*i];
1.128 - y_wp+=coordinates[2*i+1];
1.129 - }
1.130 -
1.131 - sides->property_points().set_value(coos);
1.132 -
1.133 - x_wp/=noe;
1.134 - y_wp/=noe;
1.135 -
1.136 - dx=x_wp-coordinates[noe*2];
1.137 - dy=y_wp-coordinates[noe*2+1];
1.138 - nodes[noe]->move(dx, dy);
1.139 -
1.140 - coordinates[noe*2]+=dx;
1.141 - coordinates[noe*2+1]+=dy;
1.142 -
1.143 clicked_x=e->motion.x;
1.144 clicked_y=e->motion.y;
1.145 +
1.146 + EdgeIt e;
1.147 +
1.148 + g.firstOut(e,n);
1.149 + for(;e!=INVALID;g.nextOut(e))
1.150 + {
1.151 + Gnome::Canvas::Points coos;
1.152 + double x1, x2, y1, y2;
1.153 +
1.154 + nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
1.155 + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
1.156 +
1.157 + nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
1.158 + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
1.159 +
1.160 + edgesmap[e]->property_points().set_value(coos);
1.161 + }
1.162 +
1.163 + g.firstIn(e,n);
1.164 + for(;e!=INVALID;g.nextIn(e))
1.165 + {
1.166 + Gnome::Canvas::Points coos;
1.167 + double x1, x2, y1, y2;
1.168 +
1.169 + nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
1.170 + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
1.171 +
1.172 + nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
1.173 + coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
1.174 +
1.175 + edgesmap[e]->property_points().set_value(coos);
1.176 + }
1.177 }
1.178 default: break;
1.179 }
1.180 return true;
1.181 }
1.182
1.183 -GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & g, CoordinatesMap & cm):displayed_graph(*(root()), 0, 0),isbutton(false),active_item(NULL)
1.184 +GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm):g(gr),nodesmap(g),edgesmap(g),displayed_graph(*(root()), 0, 0),isbutton(false),active_item(NULL)
1.185 {
1.186 - nodes=new Gnome::Canvas::Ellipse* [countNodes(g)];
1.187 - int sorszam=0;
1.188 -
1.189 + for (EdgeIt i(g); i!=INVALID; ++i)
1.190 + {
1.191 + Gnome::Canvas::Points coos;
1.192 + coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
1.193 + coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
1.194 + edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
1.195 + *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
1.196 + edgesmap[i]->property_width_pixels().set_value(10);
1.197 + }
1.198 for (NodeIt i(g); i!=INVALID; ++i)
1.199 {
1.200 - nodes[sorszam]= new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
1.201 - *(nodes[sorszam]) << Gnome::Canvas::Properties::fill_color("blue");
1.202 - *(nodes[sorszam]) << Gnome::Canvas::Properties::outline_color("black");
1.203 - (nodes[sorszam])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),sorszam));
1.204 - sorszam++;
1.205 + nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
1.206 + *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
1.207 + *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
1.208 + (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
1.209 }
1.210
1.211 - for (EdgeIt i(g); i!=INVALID; ++i)
1.212 - {
1.213 - }
1.214 -
1.215 -
1.216 }
1.217
1.218 GraphDisplayerCanvas::~GraphDisplayerCanvas()
1.219 {
1.220 + Graph::NodeMap <int> id(g);
1.221 + Graph::NodeMap <double> xc(g);
1.222 + Graph::NodeMap <double> yc(g);
1.223 +
1.224 + int j=1;
1.225 +
1.226 + for (NodeIt i(g); i!=INVALID; ++i)
1.227 + {
1.228 + double x1,y1,x2,y2;
1.229 + nodesmap[i]->get_bounds(x1, y1, x2, y2);
1.230 +
1.231 + id[i]=j++;
1.232 + xc[i]=(x1+x2)/2;
1.233 + yc[i]=(y1+y2)/2;
1.234 + }
1.235 +
1.236 + GraphWriter<Graph> writer(std::cout,g);
1.237 +
1.238 + writer.addNodeMap("id", id);
1.239 + writer.addNodeMap("coordinates_x", xc);
1.240 + writer.addNodeMap("coordinates_y", yc);
1.241 + writer.run();
1.242 }
1.243
1.244 +
1.245 //MainWin:
1.246 -
1.247 class MainWin : public Gtk::Window
1.248 {
1.249 public:
1.250 @@ -290,6 +261,7 @@
1.251 Graph g;
1.252
1.253 CoordinatesMap cm(g);
1.254 + Graph::EdgeMap<double> cap(g);
1.255
1.256 //we create one object to read x coordinates
1.257 //and one to read y coordinate of nodes and write them to cm NodeMap.
1.258 @@ -303,10 +275,6 @@
1.259 reader.addNodeMap("coordinates_y", yreader);
1.260 reader.run();
1.261
1.262 - for (NodeIt i(g); i!=INVALID; ++i)
1.263 - std::cout << " " << g.id(i) << " " << cm[i];
1.264 - std::cout << std::endl;
1.265 -
1.266 Gnome::Canvas::init();
1.267 Gtk::Main app(argc, argv);
1.268
2.1 --- a/src/work/peter/graphocska.lgf Mon Apr 04 16:19:29 2005 +0000
2.2 +++ b/src/work/peter/graphocska.lgf Mon Apr 04 19:22:04 2005 +0000
2.3 @@ -1,11 +1,39 @@
2.4 @nodeset
2.5 -id coordinates_x coordinates_y
2.6 -1 300 300
2.7 -2 -300 -300
2.8 -3 300 -300
2.9 -4 -300 300
2.10 -5 100 100
2.11 -6 -100 -100
2.12 -7 -100 100
2.13 -8 100 -100
2.14 -@end
2.15 +id coordinates_x coordinates_y
2.16 +1 230 -80
2.17 +2 230 100
2.18 +3 120 -80
2.19 +4 120 100
2.20 +5 20 100
2.21 +6 20 -80
2.22 +7 -40 10
2.23 +8 -100 100
2.24 +9 -100 10
2.25 +10 -100 -80
2.26 +11 -200 -80
2.27 +12 -200 10
2.28 +13 -200 100
2.29 +14 -300 100
2.30 +15 -300 -80
2.31 +
2.32 +@edgeset
2.33 + cap
2.34 +15 14 1
2.35 +14 13 2
2.36 +13 12 3
2.37 +13 8 4
2.38 +12 11 5
2.39 +12 9 6
2.40 +11 10 7
2.41 +10 9 8
2.42 +10 7 9
2.43 +9 8 10
2.44 +7 6 11
2.45 +6 5 12
2.46 +6 3 13
2.47 +5 4 14
2.48 +4 3 15
2.49 +3 2 16
2.50 +2 1 17
2.51 +
2.52 +@end
2.53 \ No newline at end of file