ladanyi@1442
|
1 |
#include <graph_displayer_canvas.h>
|
ladanyi@1442
|
2 |
#include <math.h>
|
ladanyi@1442
|
3 |
|
hegyi@1474
|
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),target_item(NULL)
|
ladanyi@1442
|
5 |
{
|
hegyi@1468
|
6 |
|
hegyi@1474
|
7 |
actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
|
hegyi@1468
|
8 |
|
ladanyi@1442
|
9 |
//set_center_scroll_region(true);
|
ladanyi@1442
|
10 |
|
ladanyi@1442
|
11 |
//first edges are drawn, to hide joining with nodes later
|
ladanyi@1442
|
12 |
|
ladanyi@1442
|
13 |
for (EdgeIt i(g); i!=INVALID; ++i)
|
ladanyi@1442
|
14 |
{
|
ladanyi@1442
|
15 |
|
ladanyi@1442
|
16 |
//drawing green lines, coordinates are from cm
|
ladanyi@1442
|
17 |
|
ladanyi@1442
|
18 |
Gnome::Canvas::Points coos;
|
ladanyi@1442
|
19 |
coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
|
ladanyi@1442
|
20 |
coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
|
ladanyi@1442
|
21 |
|
ladanyi@1442
|
22 |
edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
|
ladanyi@1442
|
23 |
*(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
|
ladanyi@1442
|
24 |
edgesmap[i]->property_width_pixels().set_value(10);
|
ladanyi@1442
|
25 |
|
ladanyi@1442
|
26 |
//initializing edge-text as well, to empty string
|
ladanyi@1442
|
27 |
|
ladanyi@1442
|
28 |
double x1, x2, y1, y2;
|
ladanyi@1442
|
29 |
edgesmap[i]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1442
|
30 |
|
ladanyi@1442
|
31 |
edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
|
ladanyi@1442
|
32 |
edgetextmap[i]->property_fill_color().set_value("black");
|
ladanyi@1442
|
33 |
}
|
ladanyi@1442
|
34 |
|
ladanyi@1442
|
35 |
//afterwards nodes come to be drawn
|
ladanyi@1442
|
36 |
|
ladanyi@1442
|
37 |
NodeIt i(g);
|
ladanyi@1442
|
38 |
int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
|
ladanyi@1442
|
39 |
|
ladanyi@1442
|
40 |
for (; i!=INVALID; ++i)
|
ladanyi@1442
|
41 |
{
|
ladanyi@1442
|
42 |
//minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
|
ladanyi@1442
|
43 |
|
ladanyi@1442
|
44 |
if(cm[i].x>maxx)maxx=(int)cm[i].x;
|
ladanyi@1442
|
45 |
if(cm[i].y>maxy)maxy=(int)cm[i].y;
|
ladanyi@1442
|
46 |
if(cm[i].x<minx)minx=(int)cm[i].x;
|
ladanyi@1442
|
47 |
if(cm[i].y<miny)miny=(int)cm[i].y;
|
ladanyi@1442
|
48 |
|
ladanyi@1442
|
49 |
//drawing bule nodes, with black line around them
|
ladanyi@1442
|
50 |
|
ladanyi@1442
|
51 |
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
|
52 |
*(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
|
ladanyi@1442
|
53 |
*(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
|
hegyi@1468
|
54 |
//!!!!!!! (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
|
ladanyi@1442
|
55 |
}
|
ladanyi@1442
|
56 |
|
ladanyi@1442
|
57 |
updateScrollRegion();
|
ladanyi@1442
|
58 |
}
|
ladanyi@1442
|
59 |
|
ladanyi@1442
|
60 |
GraphDisplayerCanvas::~GraphDisplayerCanvas()
|
ladanyi@1442
|
61 |
{
|
ladanyi@1442
|
62 |
|
ladanyi@1442
|
63 |
//writing out the end state of the graph
|
ladanyi@1442
|
64 |
//\todo all the maps has to be write out!
|
ladanyi@1442
|
65 |
|
ladanyi@1442
|
66 |
Graph::NodeMap <int> id(g);
|
ladanyi@1442
|
67 |
Graph::NodeMap <double> xc(g);
|
ladanyi@1442
|
68 |
Graph::NodeMap <double> yc(g);
|
ladanyi@1442
|
69 |
|
ladanyi@1442
|
70 |
int j=1;
|
ladanyi@1442
|
71 |
|
ladanyi@1442
|
72 |
for (NodeIt i(g); i!=INVALID; ++i)
|
ladanyi@1442
|
73 |
{
|
ladanyi@1442
|
74 |
double x1,y1,x2,y2;
|
ladanyi@1442
|
75 |
nodesmap[i]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1442
|
76 |
|
ladanyi@1442
|
77 |
id[i]=j++;
|
ladanyi@1442
|
78 |
xc[i]=(x1+x2)/2;
|
ladanyi@1442
|
79 |
yc[i]=(y1+y2)/2;
|
ladanyi@1442
|
80 |
}
|
ladanyi@1442
|
81 |
|
ladanyi@1442
|
82 |
GraphWriter<Graph> writer(std::cout,g);
|
ladanyi@1442
|
83 |
|
ladanyi@1442
|
84 |
writer.writeNodeMap("id", id);
|
ladanyi@1442
|
85 |
writer.writeNodeMap("coordinates_x", xc);
|
ladanyi@1442
|
86 |
writer.writeNodeMap("coordinates_y", yc);
|
ladanyi@1442
|
87 |
writer.run();
|
ladanyi@1442
|
88 |
}
|
ladanyi@1442
|
89 |
|
ladanyi@1442
|
90 |
int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
|
ladanyi@1442
|
91 |
{
|
ladanyi@1442
|
92 |
for (EdgeIt i(g); i!=INVALID; ++i)
|
ladanyi@1442
|
93 |
{
|
ladanyi@1442
|
94 |
int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
|
ladanyi@1442
|
95 |
edgesmap[i]->property_width_pixels().set_value(w);
|
ladanyi@1442
|
96 |
}
|
ladanyi@1442
|
97 |
return 0;
|
ladanyi@1442
|
98 |
};
|
ladanyi@1442
|
99 |
|
ladanyi@1442
|
100 |
int GraphDisplayerCanvas::changeColor (std::string mapname)
|
ladanyi@1442
|
101 |
{
|
ladanyi@1442
|
102 |
|
ladanyi@1442
|
103 |
//function maps the range of the maximum and
|
ladanyi@1442
|
104 |
//the minimum of the nodemap to the range of
|
ladanyi@1442
|
105 |
//green in RGB
|
ladanyi@1442
|
106 |
|
ladanyi@1442
|
107 |
for (EdgeIt i(g); i!=INVALID; ++i)
|
ladanyi@1442
|
108 |
{
|
ladanyi@1442
|
109 |
double w=(*(mapstorage.edgemap_storage)[mapname])[i];
|
ladanyi@1442
|
110 |
double max=mapstorage.maxOfEdgeMap(mapname);
|
ladanyi@1442
|
111 |
double min=mapstorage.minOfEdgeMap(mapname);
|
ladanyi@1442
|
112 |
|
ladanyi@1442
|
113 |
//std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
|
ladanyi@1442
|
114 |
Gdk::Color color;
|
ladanyi@1442
|
115 |
if(max!=min)
|
ladanyi@1442
|
116 |
{
|
ladanyi@1442
|
117 |
color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
|
ladanyi@1442
|
118 |
}
|
ladanyi@1442
|
119 |
else
|
ladanyi@1442
|
120 |
{
|
ladanyi@1442
|
121 |
color.set_rgb_p (0, 100, 0);
|
ladanyi@1442
|
122 |
}
|
ladanyi@1442
|
123 |
|
ladanyi@1442
|
124 |
edgesmap[i]->property_fill_color_gdk().set_value(color);
|
ladanyi@1442
|
125 |
}
|
ladanyi@1442
|
126 |
return 0;
|
ladanyi@1442
|
127 |
};
|
ladanyi@1442
|
128 |
|
ladanyi@1442
|
129 |
int GraphDisplayerCanvas::changeText (std::string mapname)
|
ladanyi@1442
|
130 |
{
|
ladanyi@1442
|
131 |
|
ladanyi@1442
|
132 |
//the number in the map will be written on the edge
|
ladanyi@1442
|
133 |
//EXCEPT when the name of the map is Text, because
|
ladanyi@1442
|
134 |
//in that case empty string will be written, because
|
ladanyi@1442
|
135 |
//that is the deleter map
|
ladanyi@1442
|
136 |
//\todo isn't it a bit woodcutter?
|
ladanyi@1442
|
137 |
|
ladanyi@1442
|
138 |
for (EdgeIt i(g); i!=INVALID; ++i)
|
ladanyi@1442
|
139 |
{
|
ladanyi@1442
|
140 |
if(mapname!="Text")
|
ladanyi@1442
|
141 |
{
|
ladanyi@1442
|
142 |
double number=(*(mapstorage.edgemap_storage)[mapname])[i];
|
ladanyi@1442
|
143 |
int length=(int)(floor(log(number)/log(10)))+1;
|
ladanyi@1442
|
144 |
int maxpos=(int)(pow(10,length-1));
|
ladanyi@1442
|
145 |
int strl=length+1+RANGE;
|
ladanyi@1442
|
146 |
char * str=new char[strl];
|
ladanyi@1442
|
147 |
str[length]='.';
|
ladanyi@1442
|
148 |
str[strl]='\0';
|
ladanyi@1442
|
149 |
|
ladanyi@1442
|
150 |
for(int j=0;j<strl;j++)
|
ladanyi@1442
|
151 |
{
|
ladanyi@1442
|
152 |
if(j!=length)
|
ladanyi@1442
|
153 |
{
|
ladanyi@1442
|
154 |
int digit=(int)(number/maxpos);
|
ladanyi@1442
|
155 |
str[j]=(digit+'0');
|
ladanyi@1442
|
156 |
number-=digit*maxpos;
|
ladanyi@1442
|
157 |
number*=10;
|
ladanyi@1442
|
158 |
}
|
ladanyi@1442
|
159 |
}
|
ladanyi@1442
|
160 |
|
ladanyi@1442
|
161 |
edgetextmap[i]->property_text().set_value(str);
|
ladanyi@1442
|
162 |
}
|
ladanyi@1442
|
163 |
else
|
ladanyi@1442
|
164 |
{
|
ladanyi@1442
|
165 |
edgetextmap[i]->property_text().set_value("");
|
ladanyi@1442
|
166 |
}
|
ladanyi@1442
|
167 |
}
|
ladanyi@1442
|
168 |
return 0;
|
ladanyi@1442
|
169 |
};
|
ladanyi@1442
|
170 |
|
ladanyi@1442
|
171 |
bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
|
ladanyi@1442
|
172 |
{
|
ladanyi@1442
|
173 |
switch(e->type)
|
ladanyi@1442
|
174 |
{
|
ladanyi@1442
|
175 |
case GDK_BUTTON_PRESS:
|
ladanyi@1442
|
176 |
//we mark the location of the event to be able to calculate parameters of dragging
|
ladanyi@1442
|
177 |
clicked_x=e->button.x;
|
ladanyi@1442
|
178 |
clicked_y=e->button.y;
|
ladanyi@1442
|
179 |
active_item=(get_item_at(e->button.x, e->button.y));
|
ladanyi@1442
|
180 |
isbutton=true;
|
ladanyi@1442
|
181 |
break;
|
ladanyi@1442
|
182 |
case GDK_BUTTON_RELEASE:
|
ladanyi@1442
|
183 |
isbutton=false;
|
ladanyi@1442
|
184 |
active_item=NULL;
|
ladanyi@1444
|
185 |
updateScrollRegion();
|
ladanyi@1442
|
186 |
break;
|
ladanyi@1442
|
187 |
case GDK_MOTION_NOTIFY:
|
ladanyi@1442
|
188 |
//we only have to do sg. if the mouse button is pressed
|
ladanyi@1442
|
189 |
if(isbutton)
|
ladanyi@1442
|
190 |
{
|
ladanyi@1442
|
191 |
//new coordinates will be the old values,
|
ladanyi@1442
|
192 |
//because the item will be moved to the
|
ladanyi@1442
|
193 |
//new coordinate therefore the new movement
|
ladanyi@1442
|
194 |
//has to be calculated from here
|
ladanyi@1442
|
195 |
|
ladanyi@1442
|
196 |
double dx=e->motion.x-clicked_x;
|
ladanyi@1442
|
197 |
double dy=e->motion.y-clicked_y;
|
ladanyi@1442
|
198 |
active_item->move(dx, dy);
|
ladanyi@1442
|
199 |
clicked_x=e->motion.x;
|
ladanyi@1442
|
200 |
clicked_y=e->motion.y;
|
ladanyi@1442
|
201 |
|
ladanyi@1442
|
202 |
//all the edges connected to the moved point has to be redrawn
|
ladanyi@1442
|
203 |
|
ladanyi@1442
|
204 |
EdgeIt e;
|
ladanyi@1442
|
205 |
g.firstOut(e,n);
|
ladanyi@1442
|
206 |
for(;e!=INVALID;g.nextOut(e))
|
ladanyi@1442
|
207 |
{
|
ladanyi@1442
|
208 |
Gnome::Canvas::Points coos;
|
ladanyi@1442
|
209 |
double x1, x2, y1, y2;
|
ladanyi@1442
|
210 |
|
ladanyi@1442
|
211 |
nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1442
|
212 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
ladanyi@1442
|
213 |
|
ladanyi@1442
|
214 |
nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1442
|
215 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
ladanyi@1442
|
216 |
|
ladanyi@1442
|
217 |
edgesmap[e]->property_points().set_value(coos);
|
ladanyi@1442
|
218 |
|
ladanyi@1442
|
219 |
edgesmap[e]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1442
|
220 |
|
ladanyi@1442
|
221 |
edgetextmap[e]->property_x().set_value((x1+x2)/2);
|
ladanyi@1442
|
222 |
edgetextmap[e]->property_y().set_value((y1+y2)/2);
|
ladanyi@1442
|
223 |
}
|
ladanyi@1442
|
224 |
|
ladanyi@1442
|
225 |
g.firstIn(e,n);
|
ladanyi@1442
|
226 |
for(;e!=INVALID;g.nextIn(e))
|
ladanyi@1442
|
227 |
{
|
ladanyi@1442
|
228 |
Gnome::Canvas::Points coos;
|
ladanyi@1442
|
229 |
double x1, x2, y1, y2;
|
ladanyi@1442
|
230 |
|
ladanyi@1442
|
231 |
nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1442
|
232 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
ladanyi@1442
|
233 |
|
ladanyi@1442
|
234 |
nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1442
|
235 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
ladanyi@1442
|
236 |
|
ladanyi@1442
|
237 |
edgesmap[e]->property_points().set_value(coos);
|
ladanyi@1442
|
238 |
|
ladanyi@1442
|
239 |
edgesmap[e]->get_bounds(x1, y1, x2, y2);
|
ladanyi@1442
|
240 |
|
ladanyi@1442
|
241 |
edgetextmap[e]->property_x().set_value((x1+x2)/2);
|
ladanyi@1442
|
242 |
edgetextmap[e]->property_y().set_value((y1+y2)/2);
|
ladanyi@1442
|
243 |
}
|
ladanyi@1442
|
244 |
}
|
ladanyi@1442
|
245 |
default: break;
|
ladanyi@1442
|
246 |
}
|
ladanyi@1442
|
247 |
return true;
|
ladanyi@1442
|
248 |
}
|
ladanyi@1442
|
249 |
|
ladanyi@1442
|
250 |
bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
|
ladanyi@1442
|
251 |
{
|
ladanyi@1442
|
252 |
Gnome::Canvas::CanvasAA::on_expose_event(event);
|
ladanyi@1442
|
253 |
//usleep(10000);
|
ladanyi@1442
|
254 |
//rezoom();
|
ladanyi@1442
|
255 |
return true;
|
ladanyi@1442
|
256 |
}
|
ladanyi@1442
|
257 |
|
ladanyi@1442
|
258 |
void GraphDisplayerCanvas::zoomIn()
|
ladanyi@1442
|
259 |
{
|
ladanyi@1442
|
260 |
set_pixels_per_unit(
|
ladanyi@1442
|
261 |
(1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
|
ladanyi@1442
|
262 |
}
|
ladanyi@1442
|
263 |
|
ladanyi@1442
|
264 |
void GraphDisplayerCanvas::zoomOut()
|
ladanyi@1442
|
265 |
{
|
ladanyi@1442
|
266 |
set_pixels_per_unit(
|
ladanyi@1442
|
267 |
(1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
|
ladanyi@1442
|
268 |
}
|
ladanyi@1442
|
269 |
|
ladanyi@1442
|
270 |
void GraphDisplayerCanvas::zoomFit()
|
ladanyi@1442
|
271 |
{
|
ladanyi@1442
|
272 |
// get the height and width of the canvas
|
ladanyi@1442
|
273 |
Gtk::Allocation a = get_allocation();
|
ladanyi@1442
|
274 |
int aw = a.get_width();
|
ladanyi@1442
|
275 |
int ah = a.get_height();
|
ladanyi@1442
|
276 |
// add some space
|
ladanyi@1442
|
277 |
aw -= 5; if (aw < 0) aw = 0;
|
ladanyi@1442
|
278 |
ah -= 5; if (ah < 0) ah = 0;
|
ladanyi@1442
|
279 |
|
ladanyi@1442
|
280 |
// get the bounding box of the graph
|
ladanyi@1442
|
281 |
double wx1, wy1, wx2, wy2;
|
ladanyi@1442
|
282 |
Gnome::Canvas::Item* pCanvasItem = root();
|
ladanyi@1442
|
283 |
pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
|
ladanyi@1442
|
284 |
|
ladanyi@1442
|
285 |
// fit the graph to the window
|
ladanyi@1444
|
286 |
double ppu1 = (double) aw / fabs(wx2 - wx1);
|
ladanyi@1444
|
287 |
double ppu2 = (double) ah / fabs(wy2 - wy1);
|
ladanyi@1444
|
288 |
set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2);
|
ladanyi@1442
|
289 |
}
|
ladanyi@1442
|
290 |
|
ladanyi@1442
|
291 |
void GraphDisplayerCanvas::zoom100()
|
ladanyi@1442
|
292 |
{
|
ladanyi@1442
|
293 |
set_pixels_per_unit(1.0);
|
ladanyi@1442
|
294 |
}
|
ladanyi@1442
|
295 |
|
ladanyi@1442
|
296 |
void GraphDisplayerCanvas::updateScrollRegion()
|
ladanyi@1442
|
297 |
{
|
ladanyi@1442
|
298 |
double wx1, wy1, wx2, wy2;
|
ladanyi@1442
|
299 |
Gnome::Canvas::Item* pCanvasItem = root();
|
ladanyi@1442
|
300 |
pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
|
ladanyi@1444
|
301 |
set_scroll_region(wx1, wy1, wx2, wy2);
|
ladanyi@1442
|
302 |
}
|
hegyi@1468
|
303 |
|
hegyi@1468
|
304 |
void GraphDisplayerCanvas::changeEditorialTool(int newtool)
|
hegyi@1468
|
305 |
{
|
hegyi@1468
|
306 |
actual_handler.disconnect();
|
hegyi@1468
|
307 |
|
hegyi@1468
|
308 |
switch(newtool)
|
hegyi@1468
|
309 |
{
|
hegyi@1468
|
310 |
case MOVE:
|
hegyi@1468
|
311 |
actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false);
|
hegyi@1468
|
312 |
break;
|
hegyi@1468
|
313 |
case CREATE_NODE:
|
hegyi@1468
|
314 |
actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
|
hegyi@1468
|
315 |
break;
|
hegyi@1468
|
316 |
case CREATE_EDGE:
|
hegyi@1468
|
317 |
actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
|
hegyi@1468
|
318 |
break;
|
hegyi@1468
|
319 |
default:
|
hegyi@1468
|
320 |
break;
|
hegyi@1468
|
321 |
}
|
hegyi@1468
|
322 |
}
|
hegyi@1468
|
323 |
|
hegyi@1468
|
324 |
bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e)
|
hegyi@1468
|
325 |
{
|
hegyi@1468
|
326 |
switch(e->type)
|
hegyi@1468
|
327 |
{
|
hegyi@1468
|
328 |
case GDK_BUTTON_PRESS:
|
hegyi@1468
|
329 |
//we mark the location of the event to be able to calculate parameters of dragging
|
hegyi@1468
|
330 |
clicked_x=e->button.x;
|
hegyi@1468
|
331 |
clicked_y=e->button.y;
|
hegyi@1468
|
332 |
active_item=(get_item_at(e->button.x, e->button.y));
|
hegyi@1468
|
333 |
active_node=INVALID;
|
hegyi@1468
|
334 |
for (NodeIt i(g); i!=INVALID; ++i)
|
hegyi@1468
|
335 |
{
|
hegyi@1468
|
336 |
if(nodesmap[i]==active_item)
|
hegyi@1468
|
337 |
{
|
hegyi@1468
|
338 |
active_node=i;
|
hegyi@1468
|
339 |
}
|
hegyi@1468
|
340 |
}
|
hegyi@1468
|
341 |
isbutton=true;
|
hegyi@1468
|
342 |
break;
|
hegyi@1468
|
343 |
case GDK_BUTTON_RELEASE:
|
hegyi@1468
|
344 |
isbutton=false;
|
hegyi@1468
|
345 |
active_item=NULL;
|
hegyi@1468
|
346 |
updateScrollRegion();
|
hegyi@1468
|
347 |
break;
|
hegyi@1468
|
348 |
case GDK_MOTION_NOTIFY:
|
hegyi@1468
|
349 |
//we only have to do sg. if the mouse button is pressed
|
hegyi@1468
|
350 |
if(isbutton)
|
hegyi@1468
|
351 |
{
|
hegyi@1468
|
352 |
//new coordinates will be the old values,
|
hegyi@1468
|
353 |
//because the item will be moved to the
|
hegyi@1468
|
354 |
//new coordinate therefore the new movement
|
hegyi@1468
|
355 |
//has to be calculated from here
|
hegyi@1468
|
356 |
|
hegyi@1468
|
357 |
double dx=e->motion.x-clicked_x;
|
hegyi@1468
|
358 |
double dy=e->motion.y-clicked_y;
|
hegyi@1468
|
359 |
|
hegyi@1468
|
360 |
active_item->move(dx, dy);
|
hegyi@1468
|
361 |
|
hegyi@1468
|
362 |
clicked_x=e->motion.x;
|
hegyi@1468
|
363 |
clicked_y=e->motion.y;
|
hegyi@1468
|
364 |
|
hegyi@1468
|
365 |
//all the edges connected to the moved point has to be redrawn
|
hegyi@1468
|
366 |
EdgeIt e;
|
hegyi@1468
|
367 |
|
hegyi@1468
|
368 |
g.firstOut(e,active_node);
|
hegyi@1468
|
369 |
|
hegyi@1468
|
370 |
for(;e!=INVALID;g.nextOut(e))
|
hegyi@1468
|
371 |
{
|
hegyi@1468
|
372 |
Gnome::Canvas::Points coos;
|
hegyi@1468
|
373 |
double x1, x2, y1, y2;
|
hegyi@1468
|
374 |
|
hegyi@1468
|
375 |
nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
|
hegyi@1468
|
376 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
hegyi@1468
|
377 |
|
hegyi@1468
|
378 |
nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
|
hegyi@1468
|
379 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
hegyi@1468
|
380 |
|
hegyi@1468
|
381 |
edgesmap[e]->property_points().set_value(coos);
|
hegyi@1468
|
382 |
|
hegyi@1468
|
383 |
edgesmap[e]->get_bounds(x1, y1, x2, y2);
|
hegyi@1468
|
384 |
|
hegyi@1468
|
385 |
edgetextmap[e]->property_x().set_value((x1+x2)/2);
|
hegyi@1468
|
386 |
edgetextmap[e]->property_y().set_value((y1+y2)/2);
|
hegyi@1468
|
387 |
}
|
hegyi@1468
|
388 |
|
hegyi@1468
|
389 |
g.firstIn(e,active_node);
|
hegyi@1468
|
390 |
for(;e!=INVALID;g.nextIn(e))
|
hegyi@1468
|
391 |
{
|
hegyi@1468
|
392 |
Gnome::Canvas::Points coos;
|
hegyi@1468
|
393 |
double x1, x2, y1, y2;
|
hegyi@1468
|
394 |
|
hegyi@1468
|
395 |
nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
|
hegyi@1468
|
396 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
hegyi@1468
|
397 |
|
hegyi@1468
|
398 |
nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
|
hegyi@1468
|
399 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
hegyi@1468
|
400 |
|
hegyi@1468
|
401 |
edgesmap[e]->property_points().set_value(coos);
|
hegyi@1468
|
402 |
|
hegyi@1468
|
403 |
edgesmap[e]->get_bounds(x1, y1, x2, y2);
|
hegyi@1468
|
404 |
|
hegyi@1468
|
405 |
edgetextmap[e]->property_x().set_value((x1+x2)/2);
|
hegyi@1468
|
406 |
edgetextmap[e]->property_y().set_value((y1+y2)/2);
|
hegyi@1468
|
407 |
}
|
hegyi@1468
|
408 |
}
|
hegyi@1468
|
409 |
default: break;
|
hegyi@1468
|
410 |
}
|
hegyi@1468
|
411 |
|
hegyi@1468
|
412 |
return true;
|
hegyi@1468
|
413 |
}
|
hegyi@1468
|
414 |
|
hegyi@1468
|
415 |
bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e)
|
hegyi@1468
|
416 |
{
|
hegyi@1468
|
417 |
switch(e->type)
|
hegyi@1468
|
418 |
{
|
hegyi@1468
|
419 |
case GDK_BUTTON_PRESS:
|
hegyi@1468
|
420 |
isbutton=true;
|
hegyi@1468
|
421 |
|
hegyi@1468
|
422 |
active_node=NodeIt(g,g.addNode());
|
hegyi@1468
|
423 |
|
hegyi@1468
|
424 |
window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
|
hegyi@1468
|
425 |
|
hegyi@1468
|
426 |
nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
|
hegyi@1468
|
427 |
active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
|
hegyi@1468
|
428 |
*(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
|
hegyi@1468
|
429 |
*(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
|
hegyi@1468
|
430 |
(nodesmap[active_node])->show();
|
hegyi@1468
|
431 |
break;
|
hegyi@1468
|
432 |
case GDK_MOTION_NOTIFY:
|
hegyi@1468
|
433 |
{
|
hegyi@1468
|
434 |
double world_motion_x, world_motion_y;
|
hegyi@1468
|
435 |
GdkEvent * generated=new GdkEvent();
|
hegyi@1468
|
436 |
window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y);
|
hegyi@1468
|
437 |
generated->motion.x=world_motion_x;
|
hegyi@1468
|
438 |
generated->motion.y=world_motion_y;
|
hegyi@1468
|
439 |
generated->type=GDK_MOTION_NOTIFY;
|
hegyi@1468
|
440 |
move_event_handler(generated);
|
hegyi@1468
|
441 |
break;
|
hegyi@1468
|
442 |
}
|
hegyi@1468
|
443 |
case GDK_BUTTON_RELEASE:
|
hegyi@1468
|
444 |
isbutton=false;
|
hegyi@1468
|
445 |
*active_item << Gnome::Canvas::Properties::fill_color("blue");
|
hegyi@1468
|
446 |
active_item=NULL;
|
hegyi@1468
|
447 |
updateScrollRegion();
|
hegyi@1468
|
448 |
break;
|
hegyi@1468
|
449 |
default:
|
hegyi@1468
|
450 |
break;
|
hegyi@1468
|
451 |
}
|
hegyi@1468
|
452 |
return false;
|
hegyi@1468
|
453 |
}
|
hegyi@1468
|
454 |
|
hegyi@1468
|
455 |
bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e)
|
hegyi@1468
|
456 |
{
|
hegyi@1474
|
457 |
switch(e->type)
|
hegyi@1474
|
458 |
{
|
hegyi@1474
|
459 |
case GDK_BUTTON_PRESS:
|
hegyi@1474
|
460 |
if(!active_item)
|
hegyi@1474
|
461 |
{
|
hegyi@1474
|
462 |
//we mark the location of the event to be able to calculate parameters of dragging
|
hegyi@1474
|
463 |
clicked_x=e->button.x;
|
hegyi@1474
|
464 |
clicked_y=e->button.y;
|
hegyi@1474
|
465 |
active_item=(get_item_at(e->button.x, e->button.y));
|
hegyi@1474
|
466 |
active_node=INVALID;
|
hegyi@1474
|
467 |
for (NodeIt i(g); i!=INVALID; ++i)
|
hegyi@1474
|
468 |
{
|
hegyi@1474
|
469 |
if(nodesmap[i]==active_item)
|
hegyi@1474
|
470 |
{
|
hegyi@1474
|
471 |
active_node=i;
|
hegyi@1474
|
472 |
}
|
hegyi@1474
|
473 |
}
|
hegyi@1474
|
474 |
*(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
|
hegyi@1474
|
475 |
isbutton=true;
|
hegyi@1474
|
476 |
}
|
hegyi@1474
|
477 |
else
|
hegyi@1474
|
478 |
{
|
hegyi@1474
|
479 |
target_item=(get_item_at(e->button.x, e->button.y));
|
hegyi@1474
|
480 |
Graph::NodeIt target_node=INVALID;
|
hegyi@1474
|
481 |
for (NodeIt i(g); i!=INVALID; ++i)
|
hegyi@1474
|
482 |
{
|
hegyi@1474
|
483 |
if(nodesmap[i]==target_item)
|
hegyi@1474
|
484 |
{
|
hegyi@1474
|
485 |
target_node=i;
|
hegyi@1474
|
486 |
}
|
hegyi@1474
|
487 |
}
|
hegyi@1474
|
488 |
*(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
|
hegyi@1474
|
489 |
|
hegyi@1474
|
490 |
//creating new edge
|
hegyi@1474
|
491 |
// Graph::Edge new_edge=g.addEdge(active_node, target_node);
|
hegyi@1474
|
492 |
active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
|
hegyi@1474
|
493 |
|
hegyi@1474
|
494 |
//calculating coordinates of new edge
|
hegyi@1474
|
495 |
Gnome::Canvas::Points coos;
|
hegyi@1474
|
496 |
double x1, x2, y1, y2;
|
hegyi@1474
|
497 |
|
hegyi@1474
|
498 |
active_item->get_bounds(x1, y1, x2, y2);
|
hegyi@1474
|
499 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
hegyi@1474
|
500 |
|
hegyi@1474
|
501 |
target_item->get_bounds(x1, y1, x2, y2);
|
hegyi@1474
|
502 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
hegyi@1474
|
503 |
|
hegyi@1474
|
504 |
//drawing new edge
|
hegyi@1474
|
505 |
edgesmap[active_edge]=new Gnome::Canvas::Line(displayed_graph, coos);
|
hegyi@1474
|
506 |
*(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
|
hegyi@1474
|
507 |
edgesmap[active_edge]->property_width_pixels().set_value(10);
|
hegyi@1474
|
508 |
|
hegyi@1474
|
509 |
//redraw nodes to blank terminations of the new edge
|
hegyi@1474
|
510 |
target_item->raise_to_top();
|
hegyi@1474
|
511 |
active_item->raise_to_top();
|
hegyi@1474
|
512 |
|
hegyi@1474
|
513 |
//initializing edge-text as well, to empty string
|
hegyi@1474
|
514 |
edgesmap[active_edge]->get_bounds(x1, y1, x2, y2);
|
hegyi@1474
|
515 |
edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
|
hegyi@1474
|
516 |
edgetextmap[active_edge]->property_fill_color().set_value("black");
|
hegyi@1474
|
517 |
}
|
hegyi@1474
|
518 |
break;
|
hegyi@1474
|
519 |
case GDK_BUTTON_RELEASE:
|
hegyi@1474
|
520 |
isbutton=false;
|
hegyi@1474
|
521 |
if(target_item)
|
hegyi@1474
|
522 |
{
|
hegyi@1474
|
523 |
*active_item << Gnome::Canvas::Properties::fill_color("blue");
|
hegyi@1474
|
524 |
*target_item << Gnome::Canvas::Properties::fill_color("blue");
|
hegyi@1474
|
525 |
active_item=NULL;
|
hegyi@1474
|
526 |
target_item=NULL;
|
hegyi@1474
|
527 |
}
|
hegyi@1474
|
528 |
break;
|
hegyi@1474
|
529 |
default:
|
hegyi@1474
|
530 |
break;
|
hegyi@1474
|
531 |
}
|
hegyi@1468
|
532 |
return false;
|
hegyi@1468
|
533 |
}
|
hegyi@1468
|
534 |
|