[Lemon-commits] [lemon_svn] hegyi: r1736 - hugo/trunk/src/work/peter

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:47:15 CET 2006


Author: hegyi
Date: Mon Apr  4 21:22:04 2005
New Revision: 1736

Modified:
   hugo/trunk/src/work/peter/graph-displayer.cc
   hugo/trunk/src/work/peter/graphocska.lgf

Log:
Graph displayer displays graphs now.

Modified: hugo/trunk/src/work/peter/graph-displayer.cc
==============================================================================
--- hugo/trunk/src/work/peter/graph-displayer.cc	(original)
+++ hugo/trunk/src/work/peter/graph-displayer.cc	Mon Apr  4 21:22:04 2005
@@ -11,6 +11,7 @@
 
 #include <lemon/list_graph.h>
 #include <lemon/graph_reader.h>
+#include <lemon/graph_writer.h>
 #include <lemon/graph_utils.h>
 #include <lemon/maps.h>
 #include <lemon/error.h>
@@ -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<Gnome::Canvas::Ellipse *> nodesmap;  
+  ///Map of edges of planefigure
+  Graph::EdgeMap<Gnome::Canvas::Line *> 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);
+        clicked_x=e->motion.x;
+        clicked_y=e->motion.y;
 
-        coordinates[2*b]+=dx;
-        coordinates[2*b+1]+=dy;
-
-        Gnome::Canvas::Points coos;
+				EdgeIt e;
 
-        double x_wp=0;
-        double y_wp=0;
+				g.firstOut(e,n);
+				for(;e!=INVALID;g.nextOut(e))
+				{
+						Gnome::Canvas::Points coos;
+						double x1, x2, y1, y2;
 
-        for(int i=0;i<noe;i++)
-        {
-          coos.push_back(Gnome::Art::Point(coordinates[2*i], coordinates[2*i+1]));
+						nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
+						coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
 
-          x_wp+=coordinates[2*i];
-          y_wp+=coordinates[2*i+1];
-        }
+						nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
+						coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
 
-        sides->property_points().set_value(coos);
+						edgesmap[e]->property_points().set_value(coos);
+				}
 
-        x_wp/=noe;
-        y_wp/=noe;
+				g.firstIn(e,n);
+				for(;e!=INVALID;g.nextIn(e))
+				{
+						Gnome::Canvas::Points coos;
+						double x1, x2, y1, y2;
 
-        dx=x_wp-coordinates[noe*2];
-        dy=y_wp-coordinates[noe*2+1];
-        nodes[noe]->move(dx, dy);
+						nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
+						coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
 
-        coordinates[noe*2]+=dx;
-        coordinates[noe*2+1]+=dy;
+						nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
+						coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
 
-        clicked_x=e->motion.x;
-        clicked_y=e->motion.y;
+						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 (NodeIt i(g); i!=INVALID; ++i)
+		for (EdgeIt 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++;
+				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 (EdgeIt i(g); i!=INVALID; ++i)
+    for (NodeIt i(g); i!=INVALID; ++i)
     {
+				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));
     }
 
-
 }
 
 GraphDisplayerCanvas::~GraphDisplayerCanvas()
 {
+		Graph::NodeMap <int> id(g);
+		Graph::NodeMap <double> xc(g);
+		Graph::NodeMap <double> 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<Graph> writer(std::cout,g);
+
+		writer.addNodeMap("id", id);
+		writer.addNodeMap("coordinates_x", xc);
+		writer.addNodeMap("coordinates_y", yc);
+		writer.run();
 }
 
-//MainWin:
 
+//MainWin:
 class MainWin : public Gtk::Window
 {
 public:
@@ -290,6 +261,7 @@
   Graph g;
 
   CoordinatesMap cm(g);
+  Graph::EdgeMap<double> 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);
 

Modified: hugo/trunk/src/work/peter/graphocska.lgf
==============================================================================
--- hugo/trunk/src/work/peter/graphocska.lgf	(original)
+++ hugo/trunk/src/work/peter/graphocska.lgf	Mon Apr  4 21:22:04 2005
@@ -1,11 +1,39 @@
 @nodeset
-id   coordinates_x    coordinates_y
-1    300	      300
-2    -300	      -300
-3    300	      -300
-4    -300	      300
-5    100	      100
-6    -100	      -100
-7    -100	      100
-8    100	      -100
- at end
+id	coordinates_x	coordinates_y	
+1	230	-80	
+2	230	100	
+3	120	-80	
+4	120	100	
+5	20	100	
+6	20	-80	
+7	-40	10	
+8	-100	100	
+9	-100	10	
+10	-100	-80	
+11	-200	-80	
+12	-200	10	
+13	-200	100	
+14	-300	100	
+15	-300	-80	
+
+ at edgeset
+        cap
+15 14 1
+14 13 2
+13 12 3
+13 8 4
+12 11 5 
+12 9 6
+11 10 7
+10 9 8
+10 7 9
+9 8 10
+7 6 11
+6 5 12
+6 3 13
+5 4 14
+4 3 15
+3 2 16
+2 1 17
+
+ at end
\ No newline at end of file



More information about the Lemon-commits mailing list