ladanyi@1
|
1 |
#include <graph_displayer_canvas.h>
|
hegyi@4
|
2 |
#include <math.h>
|
ladanyi@1
|
3 |
|
ladanyi@1
|
4 |
GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm, MapStorage & ms):g(gr),nodesmap(g),edgesmap(g),edgetextmap(g),displayed_graph(*(root()), 0, 0),mapstorage(ms),isbutton(false),active_item(NULL)
|
ladanyi@1
|
5 |
{
|
ladanyi@1
|
6 |
|
hegyi@4
|
7 |
//first edges are drawn, to hide joining with nodes later
|
hegyi@4
|
8 |
|
ladanyi@1
|
9 |
for (EdgeIt i(g); i!=INVALID; ++i)
|
ladanyi@1
|
10 |
{
|
hegyi@4
|
11 |
|
hegyi@4
|
12 |
//drawing green lines, coordinates are from cm
|
hegyi@4
|
13 |
|
ladanyi@1
|
14 |
Gnome::Canvas::Points coos;
|
ladanyi@1
|
15 |
coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
|
ladanyi@1
|
16 |
coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
|
ladanyi@1
|
17 |
|
ladanyi@1
|
18 |
edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
|
ladanyi@1
|
19 |
*(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
|
hegyi@4
|
20 |
edgesmap[i]->property_width_pixels().set_value(10);
|
ladanyi@1
|
21 |
|
hegyi@4
|
22 |
//initializing edge-text as well, to empty string
|
hegyi@4
|
23 |
|
ladanyi@1
|
24 |
double x1, x2, y1, y2;
|
ladanyi@1
|
25 |
edgesmap[i]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1
|
26 |
|
ladanyi@1
|
27 |
edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
|
ladanyi@1
|
28 |
edgetextmap[i]->property_fill_color().set_value("black");
|
ladanyi@1
|
29 |
}
|
ladanyi@1
|
30 |
|
hegyi@4
|
31 |
//afterwards nodes come to be drawn
|
hegyi@4
|
32 |
|
ladanyi@1
|
33 |
NodeIt i(g);
|
ladanyi@1
|
34 |
int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
|
ladanyi@1
|
35 |
|
ladanyi@1
|
36 |
for (; i!=INVALID; ++i)
|
ladanyi@1
|
37 |
{
|
hegyi@4
|
38 |
//minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
|
hegyi@4
|
39 |
|
ladanyi@1
|
40 |
if(cm[i].x>maxx)maxx=(int)cm[i].x;
|
ladanyi@1
|
41 |
if(cm[i].y>maxy)maxy=(int)cm[i].y;
|
ladanyi@1
|
42 |
if(cm[i].x<minx)minx=(int)cm[i].x;
|
ladanyi@1
|
43 |
if(cm[i].y<miny)miny=(int)cm[i].y;
|
ladanyi@1
|
44 |
|
hegyi@4
|
45 |
//drawing bule nodes, with black line around them
|
hegyi@4
|
46 |
|
ladanyi@1
|
47 |
nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
|
ladanyi@1
|
48 |
*(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
|
ladanyi@1
|
49 |
*(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
|
ladanyi@1
|
50 |
(nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
|
ladanyi@1
|
51 |
}
|
ladanyi@1
|
52 |
|
hegyi@4
|
53 |
//setting zoom to be able to see the whole graph on the canvas
|
hegyi@4
|
54 |
|
ladanyi@1
|
55 |
double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
|
ladanyi@1
|
56 |
double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
|
ladanyi@1
|
57 |
|
ladanyi@1
|
58 |
set_pixels_per_unit((biggest_x>biggest_y)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
|
ladanyi@1
|
59 |
std::cout<<abs(maxx)<<" "<<abs(minx)<<" big x "<<biggest_x<<" "<<abs(maxy)<<" "<<abs(miny)<<" big y "<<biggest_y<<std::endl;
|
ladanyi@1
|
60 |
std::cout<<maxx<<" "<<minx<<" big x "<<biggest_x<<" "<<maxy<<" "<<miny<<" big y "<<biggest_y<<std::endl;
|
ladanyi@1
|
61 |
std::cout<<"dx "<<(maxx-minx)<<" dy "<<(maxy-miny)<<" xrate "<<((maxx-minx)/WIN_WIDTH)<<" yrate "<<((maxy-miny)/WIN_HEIGHT)<<std::endl;
|
ladanyi@1
|
62 |
|
ladanyi@1
|
63 |
}
|
ladanyi@1
|
64 |
|
ladanyi@1
|
65 |
GraphDisplayerCanvas::~GraphDisplayerCanvas()
|
ladanyi@1
|
66 |
{
|
ladanyi@1
|
67 |
|
hegyi@4
|
68 |
//writing out the end state of the graph
|
hegyi@4
|
69 |
//\todo all the maps has to be write out!
|
ladanyi@1
|
70 |
|
hegyi@4
|
71 |
Graph::NodeMap <int> id(g);
|
hegyi@4
|
72 |
Graph::NodeMap <double> xc(g);
|
hegyi@4
|
73 |
Graph::NodeMap <double> yc(g);
|
hegyi@4
|
74 |
|
hegyi@4
|
75 |
int j=1;
|
hegyi@4
|
76 |
|
hegyi@4
|
77 |
for (NodeIt i(g); i!=INVALID; ++i)
|
hegyi@4
|
78 |
{
|
hegyi@4
|
79 |
double x1,y1,x2,y2;
|
hegyi@4
|
80 |
nodesmap[i]->get_bounds(x1, y1, x2, y2);
|
hegyi@4
|
81 |
|
hegyi@4
|
82 |
id[i]=j++;
|
hegyi@4
|
83 |
xc[i]=(x1+x2)/2;
|
hegyi@4
|
84 |
yc[i]=(y1+y2)/2;
|
hegyi@4
|
85 |
}
|
ladanyi@1
|
86 |
|
hegyi@4
|
87 |
GraphWriter<Graph> writer(std::cout,g);
|
hegyi@4
|
88 |
|
hegyi@4
|
89 |
writer.writeNodeMap("id", id);
|
hegyi@4
|
90 |
writer.writeNodeMap("coordinates_x", xc);
|
hegyi@4
|
91 |
writer.writeNodeMap("coordinates_y", yc);
|
hegyi@4
|
92 |
writer.run();
|
ladanyi@1
|
93 |
}
|
ladanyi@1
|
94 |
|
ladanyi@1
|
95 |
int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
|
ladanyi@1
|
96 |
{
|
ladanyi@1
|
97 |
for (EdgeIt i(g); i!=INVALID; ++i)
|
ladanyi@1
|
98 |
{
|
ladanyi@1
|
99 |
int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
|
ladanyi@1
|
100 |
edgesmap[i]->property_width_pixels().set_value(w);
|
ladanyi@1
|
101 |
}
|
ladanyi@1
|
102 |
return 0;
|
ladanyi@1
|
103 |
};
|
ladanyi@1
|
104 |
|
ladanyi@1
|
105 |
int GraphDisplayerCanvas::changeColor (std::string mapname)
|
ladanyi@1
|
106 |
{
|
hegyi@4
|
107 |
|
hegyi@4
|
108 |
//function maps the range of the maximum and
|
hegyi@4
|
109 |
//the minimum of the nodemap to the range of
|
hegyi@4
|
110 |
//green in RGB
|
hegyi@4
|
111 |
|
ladanyi@1
|
112 |
for (EdgeIt i(g); i!=INVALID; ++i)
|
ladanyi@1
|
113 |
{
|
ladanyi@1
|
114 |
double w=(*(mapstorage.edgemap_storage)[mapname])[i];
|
ladanyi@1
|
115 |
double max=mapstorage.maxOfEdgeMap(mapname);
|
ladanyi@1
|
116 |
double min=mapstorage.minOfEdgeMap(mapname);
|
ladanyi@1
|
117 |
|
ladanyi@1
|
118 |
//std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
|
ladanyi@1
|
119 |
Gdk::Color color;
|
ladanyi@1
|
120 |
if(max!=min)
|
ladanyi@1
|
121 |
{
|
ladanyi@1
|
122 |
color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
|
ladanyi@1
|
123 |
}
|
ladanyi@1
|
124 |
else
|
ladanyi@1
|
125 |
{
|
ladanyi@1
|
126 |
color.set_rgb_p (0, 100, 0);
|
ladanyi@1
|
127 |
}
|
ladanyi@1
|
128 |
|
ladanyi@1
|
129 |
edgesmap[i]->property_fill_color_gdk().set_value(color);
|
ladanyi@1
|
130 |
}
|
ladanyi@1
|
131 |
return 0;
|
ladanyi@1
|
132 |
};
|
ladanyi@1
|
133 |
|
ladanyi@1
|
134 |
int GraphDisplayerCanvas::changeText (std::string mapname)
|
ladanyi@1
|
135 |
{
|
hegyi@4
|
136 |
|
hegyi@4
|
137 |
//the number in the map will be written on the edge
|
hegyi@4
|
138 |
//EXCEPT when the name of the map is Text, because
|
hegyi@4
|
139 |
//in that case empty string will be written, because
|
hegyi@4
|
140 |
//that is the deleter map
|
hegyi@4
|
141 |
//\todo isn't it a bit woodcutter?
|
hegyi@4
|
142 |
|
ladanyi@1
|
143 |
for (EdgeIt i(g); i!=INVALID; ++i)
|
ladanyi@1
|
144 |
{
|
ladanyi@1
|
145 |
if(mapname!="Text")
|
ladanyi@1
|
146 |
{
|
ladanyi@1
|
147 |
double number=(*(mapstorage.edgemap_storage)[mapname])[i];
|
ladanyi@1
|
148 |
int length=(int)(floor(log(number)/log(10)))+1;
|
ladanyi@1
|
149 |
int maxpos=(int)(pow(10,length-1));
|
ladanyi@1
|
150 |
int strl=length+1+RANGE;
|
ladanyi@1
|
151 |
char * str=new char[strl];
|
ladanyi@1
|
152 |
str[length]='.';
|
ladanyi@1
|
153 |
str[strl]='\0';
|
ladanyi@1
|
154 |
|
ladanyi@1
|
155 |
for(int j=0;j<strl;j++)
|
ladanyi@1
|
156 |
{
|
ladanyi@1
|
157 |
if(j!=length)
|
ladanyi@1
|
158 |
{
|
ladanyi@1
|
159 |
int digit=(int)(number/maxpos);
|
ladanyi@1
|
160 |
str[j]=(digit+'0');
|
ladanyi@1
|
161 |
number-=digit*maxpos;
|
ladanyi@1
|
162 |
number*=10;
|
ladanyi@1
|
163 |
}
|
ladanyi@1
|
164 |
}
|
ladanyi@1
|
165 |
|
ladanyi@1
|
166 |
edgetextmap[i]->property_text().set_value(str);
|
ladanyi@1
|
167 |
}
|
ladanyi@1
|
168 |
else
|
ladanyi@1
|
169 |
{
|
ladanyi@1
|
170 |
edgetextmap[i]->property_text().set_value("");
|
ladanyi@1
|
171 |
}
|
ladanyi@1
|
172 |
}
|
ladanyi@1
|
173 |
return 0;
|
ladanyi@1
|
174 |
};
|
ladanyi@1
|
175 |
|
ladanyi@1
|
176 |
|
ladanyi@1
|
177 |
int GraphDisplayerCanvas::rezoom ()
|
ladanyi@1
|
178 |
{
|
hegyi@4
|
179 |
|
hegyi@4
|
180 |
//searches for the minimum and the maximum
|
hegyi@4
|
181 |
//value of the coordinates of the nodes to
|
hegyi@4
|
182 |
//set the pixel rpo unit to a value to be
|
hegyi@4
|
183 |
//able to see the whole graph in the canvas
|
hegyi@4
|
184 |
//\todo does not work properly
|
hegyi@4
|
185 |
|
ladanyi@1
|
186 |
double x1, x2, y1, y2;
|
ladanyi@1
|
187 |
int x,y;
|
ladanyi@1
|
188 |
|
ladanyi@1
|
189 |
NodeIt i(g);
|
ladanyi@1
|
190 |
nodesmap[i]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1
|
191 |
|
ladanyi@1
|
192 |
x=(int)((x1+x2)/2);
|
ladanyi@1
|
193 |
y=(int)((y1+y2)/2);
|
ladanyi@1
|
194 |
|
ladanyi@1
|
195 |
int maxx=0, maxy=0, minx=(int)x, miny=(int)y;
|
ladanyi@1
|
196 |
|
ladanyi@1
|
197 |
for (; i!=INVALID; ++i)
|
ladanyi@1
|
198 |
{
|
ladanyi@1
|
199 |
nodesmap[i]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1
|
200 |
|
ladanyi@1
|
201 |
x=(int)((x1+x2)/2);
|
ladanyi@1
|
202 |
y=(int)((y1+y2)/2);
|
ladanyi@1
|
203 |
|
ladanyi@1
|
204 |
if(x>maxx)maxx=x;
|
ladanyi@1
|
205 |
if(y>maxy)maxy=y;
|
ladanyi@1
|
206 |
if(x<minx)minx=x;
|
ladanyi@1
|
207 |
if(y<miny)miny=y;
|
ladanyi@1
|
208 |
}
|
ladanyi@1
|
209 |
|
ladanyi@1
|
210 |
double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
|
ladanyi@1
|
211 |
double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
|
ladanyi@1
|
212 |
|
ladanyi@1
|
213 |
set_pixels_per_unit((biggest_x-WIN_WIDTH>biggest_y-WIN_HEIGHT)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
|
ladanyi@1
|
214 |
return 0;
|
ladanyi@1
|
215 |
};
|
ladanyi@1
|
216 |
|
ladanyi@1
|
217 |
|
ladanyi@1
|
218 |
bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
|
ladanyi@1
|
219 |
{
|
ladanyi@1
|
220 |
switch(e->type)
|
ladanyi@1
|
221 |
{
|
ladanyi@1
|
222 |
case GDK_BUTTON_PRESS:
|
hegyi@4
|
223 |
//we mark the location of the event to be able to calculate parameters of dragging
|
ladanyi@1
|
224 |
clicked_x=e->button.x;
|
ladanyi@1
|
225 |
clicked_y=e->button.y;
|
ladanyi@1
|
226 |
active_item=(get_item_at(e->button.x, e->button.y));
|
ladanyi@1
|
227 |
isbutton=true;
|
ladanyi@1
|
228 |
break;
|
ladanyi@1
|
229 |
case GDK_BUTTON_RELEASE:
|
ladanyi@1
|
230 |
isbutton=false;
|
ladanyi@1
|
231 |
active_item=NULL;
|
ladanyi@1
|
232 |
break;
|
ladanyi@1
|
233 |
case GDK_MOTION_NOTIFY:
|
hegyi@4
|
234 |
//we only have to do sg. if the mouse button is pressed
|
ladanyi@1
|
235 |
if(isbutton)
|
ladanyi@1
|
236 |
{
|
hegyi@4
|
237 |
//new coordinates will be the old values,
|
hegyi@4
|
238 |
//because the item will be moved to the
|
hegyi@4
|
239 |
//new coordinate therefore the new movement
|
hegyi@4
|
240 |
//has to be calculated from here
|
hegyi@4
|
241 |
|
ladanyi@1
|
242 |
double dx=e->motion.x-clicked_x;
|
ladanyi@1
|
243 |
double dy=e->motion.y-clicked_y;
|
ladanyi@1
|
244 |
active_item->move(dx, dy);
|
ladanyi@1
|
245 |
clicked_x=e->motion.x;
|
ladanyi@1
|
246 |
clicked_y=e->motion.y;
|
ladanyi@1
|
247 |
|
hegyi@4
|
248 |
//all the edges connected to the moved point has to be redrawn
|
hegyi@4
|
249 |
|
ladanyi@1
|
250 |
EdgeIt e;
|
ladanyi@1
|
251 |
g.firstOut(e,n);
|
ladanyi@1
|
252 |
for(;e!=INVALID;g.nextOut(e))
|
ladanyi@1
|
253 |
{
|
ladanyi@1
|
254 |
Gnome::Canvas::Points coos;
|
ladanyi@1
|
255 |
double x1, x2, y1, y2;
|
ladanyi@1
|
256 |
|
ladanyi@1
|
257 |
nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1
|
258 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
ladanyi@1
|
259 |
|
ladanyi@1
|
260 |
nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1
|
261 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
ladanyi@1
|
262 |
|
ladanyi@1
|
263 |
edgesmap[e]->property_points().set_value(coos);
|
ladanyi@1
|
264 |
|
ladanyi@1
|
265 |
edgesmap[e]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1
|
266 |
|
ladanyi@1
|
267 |
edgetextmap[e]->property_x().set_value((x1+x2)/2);
|
ladanyi@1
|
268 |
edgetextmap[e]->property_y().set_value((y1+y2)/2);
|
ladanyi@1
|
269 |
}
|
ladanyi@1
|
270 |
|
ladanyi@1
|
271 |
g.firstIn(e,n);
|
ladanyi@1
|
272 |
for(;e!=INVALID;g.nextIn(e))
|
ladanyi@1
|
273 |
{
|
ladanyi@1
|
274 |
Gnome::Canvas::Points coos;
|
ladanyi@1
|
275 |
double x1, x2, y1, y2;
|
ladanyi@1
|
276 |
|
ladanyi@1
|
277 |
nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1
|
278 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
ladanyi@1
|
279 |
|
ladanyi@1
|
280 |
nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1
|
281 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
ladanyi@1
|
282 |
|
ladanyi@1
|
283 |
edgesmap[e]->property_points().set_value(coos);
|
ladanyi@1
|
284 |
|
ladanyi@1
|
285 |
edgesmap[e]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1
|
286 |
|
ladanyi@1
|
287 |
edgetextmap[e]->property_x().set_value((x1+x2)/2);
|
ladanyi@1
|
288 |
edgetextmap[e]->property_y().set_value((y1+y2)/2);
|
ladanyi@1
|
289 |
}
|
ladanyi@1
|
290 |
}
|
ladanyi@1
|
291 |
default: break;
|
ladanyi@1
|
292 |
}
|
ladanyi@1
|
293 |
return true;
|
ladanyi@1
|
294 |
}
|
ladanyi@1
|
295 |
|
ladanyi@1
|
296 |
bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
|
ladanyi@1
|
297 |
{
|
ladanyi@1
|
298 |
Gnome::Canvas::CanvasAA::on_expose_event(event);
|
ladanyi@1
|
299 |
//usleep(10000);
|
ladanyi@1
|
300 |
//rezoom();
|
ladanyi@1
|
301 |
return true;
|
ladanyi@1
|
302 |
}
|