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