Graph displayer is now displaying nodes. Edges remain still undisplayed yet.
authorhegyi
Fri, 01 Apr 2005 09:43:52 +0000
changeset 12886cc7b573b7b5
parent 1287 984723507b86
child 1289 142633fc5014
Graph displayer is now displaying nodes. Edges remain still undisplayed yet.
src/work/peter/canvas-test.cc
src/work/peter/magic_plane_figure.cc
     1.1 --- a/src/work/peter/canvas-test.cc	Fri Apr 01 08:47:40 2005 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,269 +0,0 @@
     1.4 -// This example was started by Guillaume Laurent.
     1.5 -// It has become a place to dump code that tests parts of the
     1.6 -// gnomemm canvas code. Little thought has been given to the
     1.7 -// actual on-screen output.
     1.8 -
     1.9 -#include <libgnomecanvasmm.h>
    1.10 -#include <libgnomecanvasmm/polygon.h>
    1.11 -#include <iostream>
    1.12 -#include <lemon/list_graph.h>
    1.13 -
    1.14 -class CanvasExample : public Gnome::Canvas::CanvasAA
    1.15 -{
    1.16 -	typedef Gnome::Canvas::CanvasAA Parent;
    1.17 -
    1.18 -public:
    1.19 -	CanvasExample(double *, int);
    1.20 -	virtual ~CanvasExample();
    1.21 -
    1.22 -private:
    1.23 -
    1.24 -	///Event handler function that handles dragging nodes of triangle
    1.25 -	bool event_handler(GdkEvent* e, int b);
    1.26 -
    1.27 -	///Event handler function that handles dragging triangle
    1.28 -	bool tri_mover(GdkEvent* e);
    1.29 -
    1.30 -	///Coordinates of Weight Point of tirangle
    1.31 -	Gnome::Art::Point * wp;
    1.32 -	///Array of nodes of planefigure
    1.33 -	Gnome::Canvas::Ellipse ** nodes;
    1.34 -	///Sides of planefigure
    1.35 -	Gnome::Canvas::Polygon * sides;
    1.36 -	///Group of graphical elements of triangle
    1.37 -	Gnome::Canvas::Group triangle;
    1.38 -
    1.39 -	///Indicates whether the button of mouse is pressed or not
    1.40 -	bool isbutton;
    1.41 -
    1.42 -	///Number Of Elements - the number of nodes
    1.43 -	int noe;
    1.44 -
    1.45 -	///Array of coordinates
    1.46 -	double * coordinates;
    1.47 -
    1.48 -	///At this location was the mousebutton pressed.
    1.49 -	///It helps to calculate the distance of dragging.
    1.50 -	double clicked_x, clicked_y;
    1.51 -
    1.52 -	///Remembers which Gnome::Canvas::Item was pressed.
    1.53 -	///this variable is needed, because
    1.54 -	///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
    1.55 -	///2. we would like to handle only ony item per movement, therefore quering it is not a working solution
    1.56 -	Gnome::Canvas::Item * active_item;
    1.57 -
    1.58 -
    1.59 -};
    1.60 -
    1.61 -///When we click on the weight point we can drag the whole triangle. This function resolves it.
    1.62 -bool CanvasExample::tri_mover(GdkEvent* e)
    1.63 -{
    1.64 -	switch(e->type)
    1.65 -	{
    1.66 -		case GDK_BUTTON_PRESS:
    1.67 -			clicked_x=e->button.x;
    1.68 -			clicked_y=e->button.y;
    1.69 -			isbutton=true;
    1.70 -			break;
    1.71 -		case GDK_BUTTON_RELEASE:
    1.72 -			isbutton=false;
    1.73 -			active_item=NULL;
    1.74 -			break;
    1.75 -		case GDK_MOTION_NOTIFY:
    1.76 -			if(isbutton)
    1.77 -			{
    1.78 -				double dx=e->motion.x-clicked_x;
    1.79 -				double dy=e->motion.y-clicked_y;
    1.80 -
    1.81 -				Gnome::Canvas::Points coos;
    1.82 -
    1.83 -				for(int i=0;i<=noe;i++)
    1.84 -				{
    1.85 -					nodes[i]->move(dx,dy);
    1.86 -
    1.87 -					double x=(coordinates[2*i]+=dx);
    1.88 -					double y=(coordinates[2*i+1]+=dy);
    1.89 -
    1.90 -					if(i!=noe)coos.push_back(Gnome::Art::Point(x,y));
    1.91 -
    1.92 -				}
    1.93 -
    1.94 -				clicked_x=e->motion.x;
    1.95 -				clicked_y=e->motion.y;
    1.96 -
    1.97 -				sides->property_points().set_value(coos);
    1.98 -			}
    1.99 -		default: break;
   1.100 -	}
   1.101 -	return true;
   1.102 -}
   1.103 -
   1.104 -///This function moves only one node of triangle,
   1.105 -///but recalculate the location of wight point,
   1.106 -///and also redraw the sides of the planefigure.
   1.107 -bool CanvasExample::event_handler(GdkEvent* e, int b)
   1.108 -{
   1.109 -	switch(e->type)
   1.110 -	{
   1.111 -		case GDK_BUTTON_PRESS:
   1.112 -			clicked_x=e->button.x;
   1.113 -			clicked_y=e->button.y;
   1.114 -			active_item=(get_item_at(e->button.x, e->button.y));
   1.115 -			isbutton=true;
   1.116 -			break;
   1.117 -		case GDK_BUTTON_RELEASE:
   1.118 -			isbutton=false;
   1.119 -			active_item=NULL;
   1.120 -			break;
   1.121 -		case GDK_MOTION_NOTIFY:
   1.122 -			if(isbutton)
   1.123 -			{
   1.124 -				//double x1, y1, x2, y2;
   1.125 -				//(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
   1.126 -				//printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
   1.127 -				//printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
   1.128 -				double dx=e->motion.x-clicked_x;
   1.129 -				double dy=e->motion.y-clicked_y;
   1.130 -				active_item->move(dx, dy);
   1.131 -
   1.132 -				coordinates[2*b]+=dx;
   1.133 -				coordinates[2*b+1]+=dy;
   1.134 -
   1.135 -				Gnome::Canvas::Points coos;
   1.136 -
   1.137 -				double x_wp=0;
   1.138 -				double y_wp=0;
   1.139 -
   1.140 -				for(int i=0;i<noe;i++)
   1.141 -				{
   1.142 -					coos.push_back(Gnome::Art::Point(coordinates[2*i], coordinates[2*i+1]));
   1.143 -
   1.144 -					x_wp+=coordinates[2*i];
   1.145 -					y_wp+=coordinates[2*i+1];
   1.146 -				}
   1.147 -
   1.148 -				sides->property_points().set_value(coos);
   1.149 -
   1.150 -				x_wp/=noe;
   1.151 -				y_wp/=noe;
   1.152 -
   1.153 -				dx=x_wp-coordinates[noe*2];
   1.154 -				dy=y_wp-coordinates[noe*2+1];
   1.155 -				nodes[noe]->move(dx, dy);
   1.156 -
   1.157 -				coordinates[noe*2]+=dx;
   1.158 -				coordinates[noe*2+1]+=dy;
   1.159 -
   1.160 -				clicked_x=e->motion.x;
   1.161 -				clicked_y=e->motion.y;
   1.162 -			}
   1.163 -		default: break;
   1.164 -	}
   1.165 -	return true;
   1.166 -}
   1.167 -
   1.168 -CanvasExample::CanvasExample(double * coosarray, int numofcoos):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
   1.169 -{
   1.170 -	noe=numofcoos/2;
   1.171 -
   1.172 -	coordinates=new double [numofcoos+2];
   1.173 -
   1.174 -	double x_wp=0;
   1.175 -	double y_wp=0;
   1.176 -
   1.177 -	Gnome::Canvas::Points coos;
   1.178 -	for(int i=0;i<numofcoos;i+=2)
   1.179 -	{
   1.180 -		coordinates[i]=coosarray[i];
   1.181 -		coordinates[i+1]=coosarray[i+1];
   1.182 -		coos.push_back(Gnome::Art::Point(coordinates[i],
   1.183 -						 coordinates[i+1]));
   1.184 -
   1.185 -		x_wp+=coordinates[i];
   1.186 -		y_wp+=coordinates[i+1];
   1.187 -
   1.188 -	}
   1.189 -
   1.190 -	sides=new Gnome::Canvas::Polygon(triangle, coos);
   1.191 -	*sides << Gnome::Canvas::Properties::outline_color("green");
   1.192 -	sides->property_width_pixels().set_value(10);
   1.193 -
   1.194 -	nodes=new Gnome::Canvas::Ellipse* [noe+1];
   1.195 -
   1.196 -	for(int i=0; i<noe;i++)
   1.197 -	{
   1.198 -		nodes[i]= new Gnome::Canvas::Ellipse(triangle, coos[i].get_x()-20, coos[i].get_y()-20, coos[i].get_x()+20, coos[i].get_y()+20);
   1.199 -		*(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
   1.200 -		*(nodes[i]) << Gnome::Canvas::Properties::outline_color("black");
   1.201 -		(nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),i));
   1.202 -	}
   1.203 -
   1.204 -	coordinates[numofcoos]=x_wp/noe;
   1.205 -	coordinates[numofcoos+1]=y_wp/noe;
   1.206 -
   1.207 -	wp=new Gnome::Art::Point(coordinates[numofcoos],coordinates[numofcoos+1]);
   1.208 -
   1.209 -	nodes[noe]= new Gnome::Canvas::Ellipse
   1.210 -	(
   1.211 -		triangle,
   1.212 -		coordinates[numofcoos]-20,
   1.213 -		coordinates[numofcoos+1]-20,
   1.214 -		coordinates[numofcoos]+20,
   1.215 -		coordinates[numofcoos+1]+20
   1.216 -	);
   1.217 -	*(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue");
   1.218 -	*(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black");
   1.219 -	(nodes[noe])->signal_event().connect(sigc::mem_fun(*this, &CanvasExample::tri_mover));
   1.220 -
   1.221 -
   1.222 -
   1.223 -}
   1.224 -
   1.225 -CanvasExample::~CanvasExample()
   1.226 -{
   1.227 -}
   1.228 -
   1.229 -//MainWin:
   1.230 -
   1.231 -class MainWin : public Gtk::Window
   1.232 -{
   1.233 -public:
   1.234 -	MainWin(const std::string& title, double *, int);
   1.235 -
   1.236 -protected:
   1.237 -	//Member widgets:
   1.238 -	CanvasExample m_canvas;
   1.239 -};
   1.240 -
   1.241 -MainWin::MainWin(const std::string& title, double * coosarray, int noc):m_canvas(coosarray, noc)
   1.242 -{
   1.243 -	set_title (title);
   1.244 -	add(m_canvas);
   1.245 -	set_default_size(900,600);
   1.246 -
   1.247 -	show_all();
   1.248 -}
   1.249 -
   1.250 -//main():
   1.251 -
   1.252 -int main(int argc, char *argv[])
   1.253 -{
   1.254 -	if((argc>=7)&& (argc%2) )
   1.255 -	{
   1.256 -		double * coosarray=new double[argc];
   1.257 -
   1.258 -		for(int i=1;i<argc;i++)
   1.259 -		{
   1.260 -			coosarray[i-1]=atof(argv[i]);
   1.261 -			printf("%g%c",coosarray[i-1],i%2?' ':'\n');
   1.262 -		}
   1.263 -
   1.264 -		Gnome::Canvas::init();
   1.265 -		Gtk::Main app(argc, argv);
   1.266 -
   1.267 -		MainWin mainwin("Magic Triangle",coosarray,argc-1);
   1.268 -		app.run(mainwin);
   1.269 -	}
   1.270 -
   1.271 -	return 0;
   1.272 -}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/work/peter/magic_plane_figure.cc	Fri Apr 01 09:43:52 2005 +0000
     2.3 @@ -0,0 +1,269 @@
     2.4 +// This example was started by Guillaume Laurent.
     2.5 +// It has become a place to dump code that tests parts of the
     2.6 +// gnomemm canvas code. Little thought has been given to the
     2.7 +// actual on-screen output.
     2.8 +
     2.9 +#include <libgnomecanvasmm.h>
    2.10 +#include <libgnomecanvasmm/polygon.h>
    2.11 +#include <iostream>
    2.12 +#include <lemon/list_graph.h>
    2.13 +
    2.14 +class CanvasExample : public Gnome::Canvas::CanvasAA
    2.15 +{
    2.16 +	typedef Gnome::Canvas::CanvasAA Parent;
    2.17 +
    2.18 +public:
    2.19 +	CanvasExample(double *, int);
    2.20 +	virtual ~CanvasExample();
    2.21 +
    2.22 +private:
    2.23 +
    2.24 +	///Event handler function that handles dragging nodes of triangle
    2.25 +	bool event_handler(GdkEvent* e, int b);
    2.26 +
    2.27 +	///Event handler function that handles dragging triangle
    2.28 +	bool tri_mover(GdkEvent* e);
    2.29 +
    2.30 +	///Coordinates of Weight Point of tirangle
    2.31 +	Gnome::Art::Point * wp;
    2.32 +	///Array of nodes of planefigure
    2.33 +	Gnome::Canvas::Ellipse ** nodes;
    2.34 +	///Sides of planefigure
    2.35 +	Gnome::Canvas::Polygon * sides;
    2.36 +	///Group of graphical elements of triangle
    2.37 +	Gnome::Canvas::Group triangle;
    2.38 +
    2.39 +	///Indicates whether the button of mouse is pressed or not
    2.40 +	bool isbutton;
    2.41 +
    2.42 +	///Number Of Elements - the number of nodes
    2.43 +	int noe;
    2.44 +
    2.45 +	///Array of coordinates
    2.46 +	double * coordinates;
    2.47 +
    2.48 +	///At this location was the mousebutton pressed.
    2.49 +	///It helps to calculate the distance of dragging.
    2.50 +	double clicked_x, clicked_y;
    2.51 +
    2.52 +	///Remembers which Gnome::Canvas::Item was pressed.
    2.53 +	///this variable is needed, because
    2.54 +	///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
    2.55 +	///2. we would like to handle only ony item per movement, therefore quering it is not a working solution
    2.56 +	Gnome::Canvas::Item * active_item;
    2.57 +
    2.58 +
    2.59 +};
    2.60 +
    2.61 +///When we click on the weight point we can drag the whole triangle. This function resolves it.
    2.62 +bool CanvasExample::tri_mover(GdkEvent* e)
    2.63 +{
    2.64 +	switch(e->type)
    2.65 +	{
    2.66 +		case GDK_BUTTON_PRESS:
    2.67 +			clicked_x=e->button.x;
    2.68 +			clicked_y=e->button.y;
    2.69 +			isbutton=true;
    2.70 +			break;
    2.71 +		case GDK_BUTTON_RELEASE:
    2.72 +			isbutton=false;
    2.73 +			active_item=NULL;
    2.74 +			break;
    2.75 +		case GDK_MOTION_NOTIFY:
    2.76 +			if(isbutton)
    2.77 +			{
    2.78 +				double dx=e->motion.x-clicked_x;
    2.79 +				double dy=e->motion.y-clicked_y;
    2.80 +
    2.81 +				Gnome::Canvas::Points coos;
    2.82 +
    2.83 +				for(int i=0;i<=noe;i++)
    2.84 +				{
    2.85 +					nodes[i]->move(dx,dy);
    2.86 +
    2.87 +					double x=(coordinates[2*i]+=dx);
    2.88 +					double y=(coordinates[2*i+1]+=dy);
    2.89 +
    2.90 +					if(i!=noe)coos.push_back(Gnome::Art::Point(x,y));
    2.91 +
    2.92 +				}
    2.93 +
    2.94 +				clicked_x=e->motion.x;
    2.95 +				clicked_y=e->motion.y;
    2.96 +
    2.97 +				sides->property_points().set_value(coos);
    2.98 +			}
    2.99 +		default: break;
   2.100 +	}
   2.101 +	return true;
   2.102 +}
   2.103 +
   2.104 +///This function moves only one node of triangle,
   2.105 +///but recalculate the location of wight point,
   2.106 +///and also redraw the sides of the planefigure.
   2.107 +bool CanvasExample::event_handler(GdkEvent* e, int b)
   2.108 +{
   2.109 +	switch(e->type)
   2.110 +	{
   2.111 +		case GDK_BUTTON_PRESS:
   2.112 +			clicked_x=e->button.x;
   2.113 +			clicked_y=e->button.y;
   2.114 +			active_item=(get_item_at(e->button.x, e->button.y));
   2.115 +			isbutton=true;
   2.116 +			break;
   2.117 +		case GDK_BUTTON_RELEASE:
   2.118 +			isbutton=false;
   2.119 +			active_item=NULL;
   2.120 +			break;
   2.121 +		case GDK_MOTION_NOTIFY:
   2.122 +			if(isbutton)
   2.123 +			{
   2.124 +				//double x1, y1, x2, y2;
   2.125 +				//(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
   2.126 +				//printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
   2.127 +				//printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
   2.128 +				double dx=e->motion.x-clicked_x;
   2.129 +				double dy=e->motion.y-clicked_y;
   2.130 +				active_item->move(dx, dy);
   2.131 +
   2.132 +				coordinates[2*b]+=dx;
   2.133 +				coordinates[2*b+1]+=dy;
   2.134 +
   2.135 +				Gnome::Canvas::Points coos;
   2.136 +
   2.137 +				double x_wp=0;
   2.138 +				double y_wp=0;
   2.139 +
   2.140 +				for(int i=0;i<noe;i++)
   2.141 +				{
   2.142 +					coos.push_back(Gnome::Art::Point(coordinates[2*i], coordinates[2*i+1]));
   2.143 +
   2.144 +					x_wp+=coordinates[2*i];
   2.145 +					y_wp+=coordinates[2*i+1];
   2.146 +				}
   2.147 +
   2.148 +				sides->property_points().set_value(coos);
   2.149 +
   2.150 +				x_wp/=noe;
   2.151 +				y_wp/=noe;
   2.152 +
   2.153 +				dx=x_wp-coordinates[noe*2];
   2.154 +				dy=y_wp-coordinates[noe*2+1];
   2.155 +				nodes[noe]->move(dx, dy);
   2.156 +
   2.157 +				coordinates[noe*2]+=dx;
   2.158 +				coordinates[noe*2+1]+=dy;
   2.159 +
   2.160 +				clicked_x=e->motion.x;
   2.161 +				clicked_y=e->motion.y;
   2.162 +			}
   2.163 +		default: break;
   2.164 +	}
   2.165 +	return true;
   2.166 +}
   2.167 +
   2.168 +CanvasExample::CanvasExample(double * coosarray, int numofcoos):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
   2.169 +{
   2.170 +	noe=numofcoos/2;
   2.171 +
   2.172 +	coordinates=new double [numofcoos+2];
   2.173 +
   2.174 +	double x_wp=0;
   2.175 +	double y_wp=0;
   2.176 +
   2.177 +	Gnome::Canvas::Points coos;
   2.178 +	for(int i=0;i<numofcoos;i+=2)
   2.179 +	{
   2.180 +		coordinates[i]=coosarray[i];
   2.181 +		coordinates[i+1]=coosarray[i+1];
   2.182 +		coos.push_back(Gnome::Art::Point(coordinates[i],
   2.183 +						 coordinates[i+1]));
   2.184 +
   2.185 +		x_wp+=coordinates[i];
   2.186 +		y_wp+=coordinates[i+1];
   2.187 +
   2.188 +	}
   2.189 +
   2.190 +	sides=new Gnome::Canvas::Polygon(triangle, coos);
   2.191 +	*sides << Gnome::Canvas::Properties::outline_color("green");
   2.192 +	sides->property_width_pixels().set_value(10);
   2.193 +
   2.194 +	nodes=new Gnome::Canvas::Ellipse* [noe+1];
   2.195 +
   2.196 +	for(int i=0; i<noe;i++)
   2.197 +	{
   2.198 +		nodes[i]= new Gnome::Canvas::Ellipse(triangle, coos[i].get_x()-20, coos[i].get_y()-20, coos[i].get_x()+20, coos[i].get_y()+20);
   2.199 +		*(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
   2.200 +		*(nodes[i]) << Gnome::Canvas::Properties::outline_color("black");
   2.201 +		(nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),i));
   2.202 +	}
   2.203 +
   2.204 +	coordinates[numofcoos]=x_wp/noe;
   2.205 +	coordinates[numofcoos+1]=y_wp/noe;
   2.206 +
   2.207 +	wp=new Gnome::Art::Point(coordinates[numofcoos],coordinates[numofcoos+1]);
   2.208 +
   2.209 +	nodes[noe]= new Gnome::Canvas::Ellipse
   2.210 +	(
   2.211 +		triangle,
   2.212 +		coordinates[numofcoos]-20,
   2.213 +		coordinates[numofcoos+1]-20,
   2.214 +		coordinates[numofcoos]+20,
   2.215 +		coordinates[numofcoos+1]+20
   2.216 +	);
   2.217 +	*(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue");
   2.218 +	*(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black");
   2.219 +	(nodes[noe])->signal_event().connect(sigc::mem_fun(*this, &CanvasExample::tri_mover));
   2.220 +
   2.221 +
   2.222 +
   2.223 +}
   2.224 +
   2.225 +CanvasExample::~CanvasExample()
   2.226 +{
   2.227 +}
   2.228 +
   2.229 +//MainWin:
   2.230 +
   2.231 +class MainWin : public Gtk::Window
   2.232 +{
   2.233 +public:
   2.234 +	MainWin(const std::string& title, double *, int);
   2.235 +
   2.236 +protected:
   2.237 +	//Member widgets:
   2.238 +	CanvasExample m_canvas;
   2.239 +};
   2.240 +
   2.241 +MainWin::MainWin(const std::string& title, double * coosarray, int noc):m_canvas(coosarray, noc)
   2.242 +{
   2.243 +	set_title (title);
   2.244 +	add(m_canvas);
   2.245 +	set_default_size(900,600);
   2.246 +
   2.247 +	show_all();
   2.248 +}
   2.249 +
   2.250 +//main():
   2.251 +
   2.252 +int main(int argc, char *argv[])
   2.253 +{
   2.254 +	if((argc>=7)&& (argc%2) )
   2.255 +	{
   2.256 +		double * coosarray=new double[argc];
   2.257 +
   2.258 +		for(int i=1;i<argc;i++)
   2.259 +		{
   2.260 +			coosarray[i-1]=atof(argv[i]);
   2.261 +			printf("%g%c",coosarray[i-1],i%2?' ':'\n');
   2.262 +		}
   2.263 +
   2.264 +		Gnome::Canvas::init();
   2.265 +		Gtk::Main app(argc, argv);
   2.266 +
   2.267 +		MainWin mainwin("Magic Triangle",coosarray,argc-1);
   2.268 +		app.run(mainwin);
   2.269 +	}
   2.270 +
   2.271 +	return 0;
   2.272 +}