ladanyi@6
|
1 |
#include <graph_displayer_canvas.h>
|
ladanyi@6
|
2 |
#include <math.h>
|
ladanyi@6
|
3 |
|
hegyi@11
|
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@6
|
5 |
{
|
hegyi@9
|
6 |
|
hegyi@11
|
7 |
actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
|
hegyi@9
|
8 |
|
hegyi@12
|
9 |
active_node=INVALID;
|
hegyi@12
|
10 |
active_edge=INVALID;
|
hegyi@12
|
11 |
|
ladanyi@6
|
12 |
//set_center_scroll_region(true);
|
ladanyi@6
|
13 |
|
ladanyi@6
|
14 |
//first edges are drawn, to hide joining with nodes later
|
ladanyi@6
|
15 |
|
ladanyi@6
|
16 |
for (EdgeIt i(g); i!=INVALID; ++i)
|
ladanyi@6
|
17 |
{
|
ladanyi@6
|
18 |
|
ladanyi@6
|
19 |
//drawing green lines, coordinates are from cm
|
ladanyi@6
|
20 |
|
ladanyi@6
|
21 |
Gnome::Canvas::Points coos;
|
ladanyi@6
|
22 |
coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
|
ladanyi@6
|
23 |
coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
|
ladanyi@6
|
24 |
|
ladanyi@6
|
25 |
edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
|
ladanyi@6
|
26 |
*(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
|
ladanyi@6
|
27 |
edgesmap[i]->property_width_pixels().set_value(10);
|
ladanyi@6
|
28 |
|
ladanyi@6
|
29 |
//initializing edge-text as well, to empty string
|
ladanyi@6
|
30 |
|
ladanyi@6
|
31 |
double x1, x2, y1, y2;
|
ladanyi@6
|
32 |
edgesmap[i]->get_bounds(x1, y1, x2, y2);
|
ladanyi@6
|
33 |
|
ladanyi@6
|
34 |
edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
|
ladanyi@6
|
35 |
edgetextmap[i]->property_fill_color().set_value("black");
|
ladanyi@6
|
36 |
}
|
ladanyi@6
|
37 |
|
ladanyi@6
|
38 |
//afterwards nodes come to be drawn
|
ladanyi@6
|
39 |
|
ladanyi@6
|
40 |
NodeIt i(g);
|
ladanyi@6
|
41 |
int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
|
ladanyi@6
|
42 |
|
ladanyi@6
|
43 |
for (; i!=INVALID; ++i)
|
ladanyi@6
|
44 |
{
|
ladanyi@6
|
45 |
//minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
|
ladanyi@6
|
46 |
|
ladanyi@6
|
47 |
if(cm[i].x>maxx)maxx=(int)cm[i].x;
|
ladanyi@6
|
48 |
if(cm[i].y>maxy)maxy=(int)cm[i].y;
|
ladanyi@6
|
49 |
if(cm[i].x<minx)minx=(int)cm[i].x;
|
ladanyi@6
|
50 |
if(cm[i].y<miny)miny=(int)cm[i].y;
|
ladanyi@6
|
51 |
|
ladanyi@6
|
52 |
//drawing bule nodes, with black line around them
|
ladanyi@6
|
53 |
|
ladanyi@6
|
54 |
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@6
|
55 |
*(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
|
ladanyi@6
|
56 |
*(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
|
hegyi@9
|
57 |
//!!!!!!! (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
|
ladanyi@6
|
58 |
}
|
ladanyi@6
|
59 |
|
ladanyi@6
|
60 |
updateScrollRegion();
|
ladanyi@6
|
61 |
}
|
ladanyi@6
|
62 |
|
ladanyi@6
|
63 |
GraphDisplayerCanvas::~GraphDisplayerCanvas()
|
ladanyi@6
|
64 |
{
|
ladanyi@6
|
65 |
|
ladanyi@6
|
66 |
//writing out the end state of the graph
|
ladanyi@6
|
67 |
//\todo all the maps has to be write out!
|
ladanyi@6
|
68 |
|
ladanyi@6
|
69 |
Graph::NodeMap <int> id(g);
|
ladanyi@6
|
70 |
Graph::NodeMap <double> xc(g);
|
ladanyi@6
|
71 |
Graph::NodeMap <double> yc(g);
|
ladanyi@6
|
72 |
|
ladanyi@6
|
73 |
int j=1;
|
ladanyi@6
|
74 |
|
ladanyi@6
|
75 |
for (NodeIt i(g); i!=INVALID; ++i)
|
ladanyi@6
|
76 |
{
|
ladanyi@6
|
77 |
double x1,y1,x2,y2;
|
ladanyi@6
|
78 |
nodesmap[i]->get_bounds(x1, y1, x2, y2);
|
ladanyi@6
|
79 |
|
ladanyi@6
|
80 |
id[i]=j++;
|
ladanyi@6
|
81 |
xc[i]=(x1+x2)/2;
|
ladanyi@6
|
82 |
yc[i]=(y1+y2)/2;
|
ladanyi@6
|
83 |
}
|
ladanyi@6
|
84 |
|
ladanyi@6
|
85 |
GraphWriter<Graph> writer(std::cout,g);
|
ladanyi@6
|
86 |
|
ladanyi@6
|
87 |
writer.writeNodeMap("id", id);
|
ladanyi@6
|
88 |
writer.writeNodeMap("coordinates_x", xc);
|
ladanyi@6
|
89 |
writer.writeNodeMap("coordinates_y", yc);
|
ladanyi@6
|
90 |
writer.run();
|
ladanyi@6
|
91 |
}
|
ladanyi@6
|
92 |
|
ladanyi@6
|
93 |
int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
|
ladanyi@6
|
94 |
{
|
ladanyi@6
|
95 |
for (EdgeIt i(g); i!=INVALID; ++i)
|
ladanyi@6
|
96 |
{
|
ladanyi@6
|
97 |
int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
|
ladanyi@6
|
98 |
edgesmap[i]->property_width_pixels().set_value(w);
|
ladanyi@6
|
99 |
}
|
ladanyi@6
|
100 |
return 0;
|
ladanyi@6
|
101 |
};
|
ladanyi@6
|
102 |
|
ladanyi@6
|
103 |
int GraphDisplayerCanvas::changeColor (std::string mapname)
|
ladanyi@6
|
104 |
{
|
ladanyi@6
|
105 |
|
ladanyi@6
|
106 |
//function maps the range of the maximum and
|
ladanyi@6
|
107 |
//the minimum of the nodemap to the range of
|
ladanyi@6
|
108 |
//green in RGB
|
ladanyi@6
|
109 |
|
ladanyi@6
|
110 |
for (EdgeIt i(g); i!=INVALID; ++i)
|
ladanyi@6
|
111 |
{
|
ladanyi@6
|
112 |
double w=(*(mapstorage.edgemap_storage)[mapname])[i];
|
ladanyi@6
|
113 |
double max=mapstorage.maxOfEdgeMap(mapname);
|
ladanyi@6
|
114 |
double min=mapstorage.minOfEdgeMap(mapname);
|
ladanyi@6
|
115 |
|
ladanyi@6
|
116 |
//std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
|
ladanyi@6
|
117 |
Gdk::Color color;
|
ladanyi@6
|
118 |
if(max!=min)
|
ladanyi@6
|
119 |
{
|
ladanyi@6
|
120 |
color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
|
ladanyi@6
|
121 |
}
|
ladanyi@6
|
122 |
else
|
ladanyi@6
|
123 |
{
|
ladanyi@6
|
124 |
color.set_rgb_p (0, 100, 0);
|
ladanyi@6
|
125 |
}
|
ladanyi@6
|
126 |
|
ladanyi@6
|
127 |
edgesmap[i]->property_fill_color_gdk().set_value(color);
|
ladanyi@6
|
128 |
}
|
ladanyi@6
|
129 |
return 0;
|
ladanyi@6
|
130 |
};
|
ladanyi@6
|
131 |
|
ladanyi@6
|
132 |
int GraphDisplayerCanvas::changeText (std::string mapname)
|
ladanyi@6
|
133 |
{
|
ladanyi@6
|
134 |
|
ladanyi@6
|
135 |
//the number in the map will be written on the edge
|
ladanyi@6
|
136 |
//EXCEPT when the name of the map is Text, because
|
ladanyi@6
|
137 |
//in that case empty string will be written, because
|
ladanyi@6
|
138 |
//that is the deleter map
|
ladanyi@6
|
139 |
//\todo isn't it a bit woodcutter?
|
ladanyi@6
|
140 |
|
ladanyi@6
|
141 |
for (EdgeIt i(g); i!=INVALID; ++i)
|
ladanyi@6
|
142 |
{
|
ladanyi@6
|
143 |
if(mapname!="Text")
|
ladanyi@6
|
144 |
{
|
ladanyi@6
|
145 |
double number=(*(mapstorage.edgemap_storage)[mapname])[i];
|
ladanyi@6
|
146 |
int length=(int)(floor(log(number)/log(10)))+1;
|
ladanyi@6
|
147 |
int maxpos=(int)(pow(10,length-1));
|
ladanyi@6
|
148 |
int strl=length+1+RANGE;
|
ladanyi@6
|
149 |
char * str=new char[strl];
|
ladanyi@6
|
150 |
str[length]='.';
|
ladanyi@6
|
151 |
str[strl]='\0';
|
ladanyi@6
|
152 |
|
ladanyi@6
|
153 |
for(int j=0;j<strl;j++)
|
ladanyi@6
|
154 |
{
|
ladanyi@6
|
155 |
if(j!=length)
|
ladanyi@6
|
156 |
{
|
ladanyi@6
|
157 |
int digit=(int)(number/maxpos);
|
ladanyi@6
|
158 |
str[j]=(digit+'0');
|
ladanyi@6
|
159 |
number-=digit*maxpos;
|
ladanyi@6
|
160 |
number*=10;
|
ladanyi@6
|
161 |
}
|
ladanyi@6
|
162 |
}
|
ladanyi@6
|
163 |
|
ladanyi@6
|
164 |
edgetextmap[i]->property_text().set_value(str);
|
ladanyi@6
|
165 |
}
|
ladanyi@6
|
166 |
else
|
ladanyi@6
|
167 |
{
|
ladanyi@6
|
168 |
edgetextmap[i]->property_text().set_value("");
|
ladanyi@6
|
169 |
}
|
ladanyi@6
|
170 |
}
|
ladanyi@6
|
171 |
return 0;
|
ladanyi@6
|
172 |
};
|
ladanyi@6
|
173 |
|
ladanyi@6
|
174 |
bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
|
ladanyi@6
|
175 |
{
|
ladanyi@6
|
176 |
switch(e->type)
|
ladanyi@6
|
177 |
{
|
ladanyi@6
|
178 |
case GDK_BUTTON_PRESS:
|
ladanyi@6
|
179 |
//we mark the location of the event to be able to calculate parameters of dragging
|
ladanyi@6
|
180 |
clicked_x=e->button.x;
|
ladanyi@6
|
181 |
clicked_y=e->button.y;
|
ladanyi@6
|
182 |
active_item=(get_item_at(e->button.x, e->button.y));
|
ladanyi@6
|
183 |
isbutton=true;
|
ladanyi@6
|
184 |
break;
|
ladanyi@6
|
185 |
case GDK_BUTTON_RELEASE:
|
ladanyi@6
|
186 |
isbutton=false;
|
ladanyi@6
|
187 |
active_item=NULL;
|
ladanyi@7
|
188 |
updateScrollRegion();
|
ladanyi@6
|
189 |
break;
|
ladanyi@6
|
190 |
case GDK_MOTION_NOTIFY:
|
ladanyi@6
|
191 |
//we only have to do sg. if the mouse button is pressed
|
ladanyi@6
|
192 |
if(isbutton)
|
ladanyi@6
|
193 |
{
|
ladanyi@6
|
194 |
//new coordinates will be the old values,
|
ladanyi@6
|
195 |
//because the item will be moved to the
|
ladanyi@6
|
196 |
//new coordinate therefore the new movement
|
ladanyi@6
|
197 |
//has to be calculated from here
|
ladanyi@6
|
198 |
|
ladanyi@6
|
199 |
double dx=e->motion.x-clicked_x;
|
ladanyi@6
|
200 |
double dy=e->motion.y-clicked_y;
|
ladanyi@6
|
201 |
active_item->move(dx, dy);
|
ladanyi@6
|
202 |
clicked_x=e->motion.x;
|
ladanyi@6
|
203 |
clicked_y=e->motion.y;
|
ladanyi@6
|
204 |
|
ladanyi@6
|
205 |
//all the edges connected to the moved point has to be redrawn
|
ladanyi@6
|
206 |
|
ladanyi@6
|
207 |
EdgeIt e;
|
ladanyi@6
|
208 |
g.firstOut(e,n);
|
ladanyi@6
|
209 |
for(;e!=INVALID;g.nextOut(e))
|
ladanyi@6
|
210 |
{
|
ladanyi@6
|
211 |
Gnome::Canvas::Points coos;
|
ladanyi@6
|
212 |
double x1, x2, y1, y2;
|
ladanyi@6
|
213 |
|
ladanyi@6
|
214 |
nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
|
ladanyi@6
|
215 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
ladanyi@6
|
216 |
|
ladanyi@6
|
217 |
nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
|
ladanyi@6
|
218 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
ladanyi@6
|
219 |
|
ladanyi@6
|
220 |
edgesmap[e]->property_points().set_value(coos);
|
ladanyi@6
|
221 |
|
ladanyi@6
|
222 |
edgesmap[e]->get_bounds(x1, y1, x2, y2);
|
ladanyi@6
|
223 |
|
ladanyi@6
|
224 |
edgetextmap[e]->property_x().set_value((x1+x2)/2);
|
ladanyi@6
|
225 |
edgetextmap[e]->property_y().set_value((y1+y2)/2);
|
ladanyi@6
|
226 |
}
|
ladanyi@6
|
227 |
|
ladanyi@6
|
228 |
g.firstIn(e,n);
|
ladanyi@6
|
229 |
for(;e!=INVALID;g.nextIn(e))
|
ladanyi@6
|
230 |
{
|
ladanyi@6
|
231 |
Gnome::Canvas::Points coos;
|
ladanyi@6
|
232 |
double x1, x2, y1, y2;
|
ladanyi@6
|
233 |
|
ladanyi@6
|
234 |
nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
|
ladanyi@6
|
235 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
ladanyi@6
|
236 |
|
ladanyi@6
|
237 |
nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
|
ladanyi@6
|
238 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
ladanyi@6
|
239 |
|
ladanyi@6
|
240 |
edgesmap[e]->property_points().set_value(coos);
|
ladanyi@6
|
241 |
|
ladanyi@6
|
242 |
edgesmap[e]->get_bounds(x1, y1, x2, y2);
|
ladanyi@6
|
243 |
|
ladanyi@6
|
244 |
edgetextmap[e]->property_x().set_value((x1+x2)/2);
|
ladanyi@6
|
245 |
edgetextmap[e]->property_y().set_value((y1+y2)/2);
|
ladanyi@6
|
246 |
}
|
ladanyi@6
|
247 |
}
|
ladanyi@6
|
248 |
default: break;
|
ladanyi@6
|
249 |
}
|
ladanyi@6
|
250 |
return true;
|
ladanyi@6
|
251 |
}
|
ladanyi@6
|
252 |
|
ladanyi@6
|
253 |
bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
|
ladanyi@6
|
254 |
{
|
ladanyi@6
|
255 |
Gnome::Canvas::CanvasAA::on_expose_event(event);
|
ladanyi@6
|
256 |
//usleep(10000);
|
ladanyi@6
|
257 |
//rezoom();
|
ladanyi@6
|
258 |
return true;
|
ladanyi@6
|
259 |
}
|
ladanyi@6
|
260 |
|
ladanyi@6
|
261 |
void GraphDisplayerCanvas::zoomIn()
|
ladanyi@6
|
262 |
{
|
ladanyi@6
|
263 |
set_pixels_per_unit(
|
ladanyi@6
|
264 |
(1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
|
ladanyi@6
|
265 |
}
|
ladanyi@6
|
266 |
|
ladanyi@6
|
267 |
void GraphDisplayerCanvas::zoomOut()
|
ladanyi@6
|
268 |
{
|
ladanyi@6
|
269 |
set_pixels_per_unit(
|
ladanyi@6
|
270 |
(1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
|
ladanyi@6
|
271 |
}
|
ladanyi@6
|
272 |
|
ladanyi@6
|
273 |
void GraphDisplayerCanvas::zoomFit()
|
ladanyi@6
|
274 |
{
|
ladanyi@6
|
275 |
// get the height and width of the canvas
|
ladanyi@6
|
276 |
Gtk::Allocation a = get_allocation();
|
ladanyi@6
|
277 |
int aw = a.get_width();
|
ladanyi@6
|
278 |
int ah = a.get_height();
|
ladanyi@6
|
279 |
// add some space
|
ladanyi@6
|
280 |
aw -= 5; if (aw < 0) aw = 0;
|
ladanyi@6
|
281 |
ah -= 5; if (ah < 0) ah = 0;
|
ladanyi@6
|
282 |
|
ladanyi@6
|
283 |
// get the bounding box of the graph
|
ladanyi@6
|
284 |
double wx1, wy1, wx2, wy2;
|
ladanyi@6
|
285 |
Gnome::Canvas::Item* pCanvasItem = root();
|
ladanyi@6
|
286 |
pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
|
ladanyi@6
|
287 |
|
ladanyi@6
|
288 |
// fit the graph to the window
|
ladanyi@7
|
289 |
double ppu1 = (double) aw / fabs(wx2 - wx1);
|
ladanyi@7
|
290 |
double ppu2 = (double) ah / fabs(wy2 - wy1);
|
ladanyi@7
|
291 |
set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2);
|
ladanyi@6
|
292 |
}
|
ladanyi@6
|
293 |
|
ladanyi@6
|
294 |
void GraphDisplayerCanvas::zoom100()
|
ladanyi@6
|
295 |
{
|
ladanyi@6
|
296 |
set_pixels_per_unit(1.0);
|
ladanyi@6
|
297 |
}
|
ladanyi@6
|
298 |
|
ladanyi@6
|
299 |
void GraphDisplayerCanvas::updateScrollRegion()
|
ladanyi@6
|
300 |
{
|
ladanyi@6
|
301 |
double wx1, wy1, wx2, wy2;
|
ladanyi@6
|
302 |
Gnome::Canvas::Item* pCanvasItem = root();
|
ladanyi@6
|
303 |
pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
|
ladanyi@7
|
304 |
set_scroll_region(wx1, wy1, wx2, wy2);
|
ladanyi@6
|
305 |
}
|
hegyi@9
|
306 |
|
hegyi@9
|
307 |
void GraphDisplayerCanvas::changeEditorialTool(int newtool)
|
hegyi@9
|
308 |
{
|
hegyi@9
|
309 |
actual_handler.disconnect();
|
hegyi@9
|
310 |
|
hegyi@9
|
311 |
switch(newtool)
|
hegyi@9
|
312 |
{
|
hegyi@9
|
313 |
case MOVE:
|
hegyi@9
|
314 |
actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false);
|
hegyi@9
|
315 |
break;
|
hegyi@12
|
316 |
|
hegyi@12
|
317 |
//it has to assigned to canvas, because all the canvas has to be monitored, not only the elements of the already drawn group
|
hegyi@9
|
318 |
case CREATE_NODE:
|
hegyi@9
|
319 |
actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
|
hegyi@9
|
320 |
break;
|
hegyi@12
|
321 |
|
hegyi@9
|
322 |
case CREATE_EDGE:
|
hegyi@9
|
323 |
actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
|
hegyi@9
|
324 |
break;
|
hegyi@13
|
325 |
|
hegyi@13
|
326 |
case ERASER:
|
hegyi@13
|
327 |
actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraser_event_handler), false);
|
hegyi@13
|
328 |
break;
|
hegyi@13
|
329 |
|
hegyi@9
|
330 |
default:
|
hegyi@9
|
331 |
break;
|
hegyi@9
|
332 |
}
|
hegyi@9
|
333 |
}
|
hegyi@9
|
334 |
|
hegyi@9
|
335 |
bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e)
|
hegyi@9
|
336 |
{
|
hegyi@9
|
337 |
switch(e->type)
|
hegyi@9
|
338 |
{
|
hegyi@9
|
339 |
case GDK_BUTTON_PRESS:
|
hegyi@9
|
340 |
//we mark the location of the event to be able to calculate parameters of dragging
|
hegyi@9
|
341 |
clicked_x=e->button.x;
|
hegyi@9
|
342 |
clicked_y=e->button.y;
|
hegyi@9
|
343 |
active_item=(get_item_at(e->button.x, e->button.y));
|
hegyi@9
|
344 |
active_node=INVALID;
|
hegyi@9
|
345 |
for (NodeIt i(g); i!=INVALID; ++i)
|
hegyi@9
|
346 |
{
|
hegyi@9
|
347 |
if(nodesmap[i]==active_item)
|
hegyi@9
|
348 |
{
|
hegyi@9
|
349 |
active_node=i;
|
hegyi@9
|
350 |
}
|
hegyi@9
|
351 |
}
|
hegyi@9
|
352 |
isbutton=true;
|
hegyi@9
|
353 |
break;
|
hegyi@9
|
354 |
case GDK_BUTTON_RELEASE:
|
hegyi@9
|
355 |
isbutton=false;
|
hegyi@9
|
356 |
active_item=NULL;
|
hegyi@12
|
357 |
active_node=INVALID;
|
hegyi@9
|
358 |
updateScrollRegion();
|
hegyi@9
|
359 |
break;
|
hegyi@9
|
360 |
case GDK_MOTION_NOTIFY:
|
hegyi@12
|
361 |
//we only have to do sg. if the mouse button is pressed AND the click was on a node that was found in the set of nodes
|
hegyi@12
|
362 |
if(active_node!=INVALID)
|
hegyi@9
|
363 |
{
|
hegyi@9
|
364 |
//new coordinates will be the old values,
|
hegyi@9
|
365 |
//because the item will be moved to the
|
hegyi@9
|
366 |
//new coordinate therefore the new movement
|
hegyi@9
|
367 |
//has to be calculated from here
|
hegyi@9
|
368 |
|
hegyi@9
|
369 |
double dx=e->motion.x-clicked_x;
|
hegyi@9
|
370 |
double dy=e->motion.y-clicked_y;
|
hegyi@9
|
371 |
|
hegyi@9
|
372 |
active_item->move(dx, dy);
|
hegyi@9
|
373 |
|
hegyi@9
|
374 |
clicked_x=e->motion.x;
|
hegyi@9
|
375 |
clicked_y=e->motion.y;
|
hegyi@9
|
376 |
|
hegyi@9
|
377 |
//all the edges connected to the moved point has to be redrawn
|
hegyi@9
|
378 |
EdgeIt e;
|
hegyi@9
|
379 |
|
hegyi@9
|
380 |
g.firstOut(e,active_node);
|
hegyi@9
|
381 |
|
hegyi@9
|
382 |
for(;e!=INVALID;g.nextOut(e))
|
hegyi@9
|
383 |
{
|
hegyi@9
|
384 |
Gnome::Canvas::Points coos;
|
hegyi@9
|
385 |
double x1, x2, y1, y2;
|
hegyi@9
|
386 |
|
hegyi@9
|
387 |
nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
|
hegyi@9
|
388 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
hegyi@9
|
389 |
|
hegyi@9
|
390 |
nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
|
hegyi@9
|
391 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
hegyi@9
|
392 |
|
hegyi@9
|
393 |
edgesmap[e]->property_points().set_value(coos);
|
hegyi@9
|
394 |
|
hegyi@9
|
395 |
edgesmap[e]->get_bounds(x1, y1, x2, y2);
|
hegyi@9
|
396 |
|
hegyi@9
|
397 |
edgetextmap[e]->property_x().set_value((x1+x2)/2);
|
hegyi@9
|
398 |
edgetextmap[e]->property_y().set_value((y1+y2)/2);
|
hegyi@9
|
399 |
}
|
hegyi@9
|
400 |
|
hegyi@9
|
401 |
g.firstIn(e,active_node);
|
hegyi@9
|
402 |
for(;e!=INVALID;g.nextIn(e))
|
hegyi@9
|
403 |
{
|
hegyi@9
|
404 |
Gnome::Canvas::Points coos;
|
hegyi@9
|
405 |
double x1, x2, y1, y2;
|
hegyi@9
|
406 |
|
hegyi@9
|
407 |
nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
|
hegyi@9
|
408 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
hegyi@9
|
409 |
|
hegyi@9
|
410 |
nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
|
hegyi@9
|
411 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
hegyi@9
|
412 |
|
hegyi@9
|
413 |
edgesmap[e]->property_points().set_value(coos);
|
hegyi@9
|
414 |
|
hegyi@9
|
415 |
edgesmap[e]->get_bounds(x1, y1, x2, y2);
|
hegyi@9
|
416 |
|
hegyi@9
|
417 |
edgetextmap[e]->property_x().set_value((x1+x2)/2);
|
hegyi@9
|
418 |
edgetextmap[e]->property_y().set_value((y1+y2)/2);
|
hegyi@9
|
419 |
}
|
hegyi@9
|
420 |
}
|
hegyi@9
|
421 |
default: break;
|
hegyi@9
|
422 |
}
|
hegyi@9
|
423 |
|
hegyi@9
|
424 |
return true;
|
hegyi@9
|
425 |
}
|
hegyi@9
|
426 |
|
hegyi@9
|
427 |
bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e)
|
hegyi@9
|
428 |
{
|
hegyi@9
|
429 |
switch(e->type)
|
hegyi@9
|
430 |
{
|
hegyi@12
|
431 |
|
hegyi@12
|
432 |
//draw the new node in red at the clicked place
|
hegyi@9
|
433 |
case GDK_BUTTON_PRESS:
|
hegyi@9
|
434 |
isbutton=true;
|
hegyi@9
|
435 |
|
hegyi@9
|
436 |
active_node=NodeIt(g,g.addNode());
|
hegyi@9
|
437 |
|
hegyi@9
|
438 |
window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
|
hegyi@9
|
439 |
|
hegyi@9
|
440 |
nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
|
hegyi@9
|
441 |
active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
|
hegyi@9
|
442 |
*(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
|
hegyi@9
|
443 |
*(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
|
hegyi@9
|
444 |
(nodesmap[active_node])->show();
|
hegyi@9
|
445 |
break;
|
hegyi@12
|
446 |
|
hegyi@12
|
447 |
//move the new node
|
hegyi@9
|
448 |
case GDK_MOTION_NOTIFY:
|
hegyi@9
|
449 |
{
|
hegyi@9
|
450 |
double world_motion_x, world_motion_y;
|
hegyi@9
|
451 |
GdkEvent * generated=new GdkEvent();
|
hegyi@9
|
452 |
window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y);
|
hegyi@9
|
453 |
generated->motion.x=world_motion_x;
|
hegyi@9
|
454 |
generated->motion.y=world_motion_y;
|
hegyi@9
|
455 |
generated->type=GDK_MOTION_NOTIFY;
|
hegyi@9
|
456 |
move_event_handler(generated);
|
hegyi@9
|
457 |
break;
|
hegyi@9
|
458 |
}
|
hegyi@12
|
459 |
|
hegyi@12
|
460 |
//finalize the new node
|
hegyi@9
|
461 |
case GDK_BUTTON_RELEASE:
|
hegyi@9
|
462 |
isbutton=false;
|
hegyi@9
|
463 |
*active_item << Gnome::Canvas::Properties::fill_color("blue");
|
hegyi@9
|
464 |
active_item=NULL;
|
hegyi@12
|
465 |
active_node=INVALID;
|
hegyi@9
|
466 |
updateScrollRegion();
|
hegyi@9
|
467 |
break;
|
hegyi@9
|
468 |
default:
|
hegyi@9
|
469 |
break;
|
hegyi@9
|
470 |
}
|
hegyi@9
|
471 |
return false;
|
hegyi@9
|
472 |
}
|
hegyi@9
|
473 |
|
hegyi@9
|
474 |
bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e)
|
hegyi@9
|
475 |
{
|
hegyi@11
|
476 |
switch(e->type)
|
hegyi@11
|
477 |
{
|
hegyi@11
|
478 |
case GDK_BUTTON_PRESS:
|
hegyi@12
|
479 |
//in edge creatino right button has special meaning
|
hegyi@12
|
480 |
if(e->button.button!=3)
|
hegyi@11
|
481 |
{
|
hegyi@12
|
482 |
//there is not yet selected node
|
hegyi@12
|
483 |
if(active_node==INVALID)
|
hegyi@11
|
484 |
{
|
hegyi@12
|
485 |
//we mark the location of the event to be able to calculate parameters of dragging
|
hegyi@12
|
486 |
clicked_x=e->button.x;
|
hegyi@12
|
487 |
clicked_y=e->button.y;
|
hegyi@12
|
488 |
active_item=(get_item_at(e->button.x, e->button.y));
|
hegyi@12
|
489 |
active_node=INVALID;
|
hegyi@12
|
490 |
for (NodeIt i(g); i!=INVALID; ++i)
|
hegyi@11
|
491 |
{
|
hegyi@12
|
492 |
if(nodesmap[i]==active_item)
|
hegyi@12
|
493 |
{
|
hegyi@12
|
494 |
active_node=i;
|
hegyi@12
|
495 |
}
|
hegyi@12
|
496 |
}
|
hegyi@12
|
497 |
//the clicked item is really a node
|
hegyi@12
|
498 |
if(active_node!=INVALID)
|
hegyi@12
|
499 |
{
|
hegyi@12
|
500 |
*(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
|
hegyi@12
|
501 |
isbutton=true;
|
hegyi@12
|
502 |
}
|
hegyi@12
|
503 |
//clicked item was not a node. It could be e.g. edge.
|
hegyi@12
|
504 |
else
|
hegyi@12
|
505 |
{
|
hegyi@12
|
506 |
active_item=NULL;
|
hegyi@11
|
507 |
}
|
hegyi@11
|
508 |
}
|
hegyi@12
|
509 |
//we only have to do sg. if the mouse button
|
hegyi@12
|
510 |
// is pressed already once AND the click was
|
hegyi@12
|
511 |
// on a node that was found in the set of
|
hegyi@12
|
512 |
//nodes, and now we only search for the second
|
hegyi@12
|
513 |
//node
|
hegyi@12
|
514 |
else
|
hegyi@11
|
515 |
{
|
hegyi@12
|
516 |
target_item=(get_item_at(e->button.x, e->button.y));
|
hegyi@12
|
517 |
Graph::NodeIt target_node=INVALID;
|
hegyi@12
|
518 |
for (NodeIt i(g); i!=INVALID; ++i)
|
hegyi@11
|
519 |
{
|
hegyi@12
|
520 |
if(nodesmap[i]==target_item)
|
hegyi@12
|
521 |
{
|
hegyi@12
|
522 |
target_node=i;
|
hegyi@12
|
523 |
}
|
hegyi@12
|
524 |
}
|
hegyi@12
|
525 |
//the clicked item is a node, the edge can be drawn
|
hegyi@12
|
526 |
if(target_node!=INVALID)
|
hegyi@12
|
527 |
{
|
hegyi@12
|
528 |
*(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
|
hegyi@12
|
529 |
|
hegyi@12
|
530 |
//creating new edge
|
hegyi@12
|
531 |
active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
|
hegyi@12
|
532 |
|
hegyi@12
|
533 |
//calculating coordinates of new edge
|
hegyi@12
|
534 |
Gnome::Canvas::Points coos;
|
hegyi@12
|
535 |
double x1, x2, y1, y2;
|
hegyi@12
|
536 |
|
hegyi@12
|
537 |
active_item->get_bounds(x1, y1, x2, y2);
|
hegyi@12
|
538 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
hegyi@12
|
539 |
|
hegyi@12
|
540 |
target_item->get_bounds(x1, y1, x2, y2);
|
hegyi@12
|
541 |
coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
|
hegyi@12
|
542 |
|
hegyi@12
|
543 |
//drawing new edge
|
hegyi@12
|
544 |
edgesmap[active_edge]=new Gnome::Canvas::Line(displayed_graph, coos);
|
hegyi@12
|
545 |
*(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
|
hegyi@12
|
546 |
edgesmap[active_edge]->property_width_pixels().set_value(10);
|
hegyi@12
|
547 |
|
hegyi@12
|
548 |
//redraw nodes to blank terminations of the new edge
|
hegyi@12
|
549 |
target_item->raise_to_top();
|
hegyi@12
|
550 |
active_item->raise_to_top();
|
hegyi@12
|
551 |
|
hegyi@12
|
552 |
//initializing edge-text as well, to empty string
|
hegyi@12
|
553 |
edgesmap[active_edge]->get_bounds(x1, y1, x2, y2);
|
hegyi@12
|
554 |
edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
|
hegyi@12
|
555 |
edgetextmap[active_edge]->property_fill_color().set_value("black");
|
hegyi@12
|
556 |
}
|
hegyi@12
|
557 |
//clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
|
hegyi@12
|
558 |
else
|
hegyi@12
|
559 |
{
|
hegyi@12
|
560 |
target_item=NULL;
|
hegyi@11
|
561 |
}
|
hegyi@11
|
562 |
}
|
hegyi@11
|
563 |
}
|
hegyi@11
|
564 |
break;
|
hegyi@11
|
565 |
case GDK_BUTTON_RELEASE:
|
hegyi@11
|
566 |
isbutton=false;
|
hegyi@12
|
567 |
//we clear settings in two cases
|
hegyi@12
|
568 |
//1: the edge is ready (target_item has valid value)
|
hegyi@12
|
569 |
//2: the edge creation is cancelled with right button
|
hegyi@12
|
570 |
if((target_item)||(e->button.button==3))
|
hegyi@11
|
571 |
{
|
hegyi@12
|
572 |
if(active_item)
|
hegyi@12
|
573 |
{
|
hegyi@12
|
574 |
*active_item << Gnome::Canvas::Properties::fill_color("blue");
|
hegyi@12
|
575 |
active_item=NULL;
|
hegyi@12
|
576 |
}
|
hegyi@12
|
577 |
if(target_item)
|
hegyi@12
|
578 |
{
|
hegyi@12
|
579 |
*target_item << Gnome::Canvas::Properties::fill_color("blue");
|
hegyi@12
|
580 |
target_item=NULL;
|
hegyi@12
|
581 |
}
|
hegyi@12
|
582 |
active_node=INVALID;
|
hegyi@12
|
583 |
active_edge=INVALID;
|
hegyi@11
|
584 |
}
|
hegyi@11
|
585 |
break;
|
hegyi@11
|
586 |
default:
|
hegyi@11
|
587 |
break;
|
hegyi@11
|
588 |
}
|
hegyi@9
|
589 |
return false;
|
hegyi@9
|
590 |
}
|
hegyi@9
|
591 |
|
hegyi@13
|
592 |
bool GraphDisplayerCanvas::eraser_event_handler(GdkEvent* e)
|
hegyi@13
|
593 |
{
|
hegyi@13
|
594 |
switch(e->type)
|
hegyi@13
|
595 |
{
|
hegyi@13
|
596 |
case GDK_BUTTON_PRESS:
|
hegyi@13
|
597 |
active_item=(get_item_at(e->button.x, e->button.y));
|
hegyi@13
|
598 |
active_node=INVALID;
|
hegyi@13
|
599 |
active_edge=INVALID;
|
hegyi@13
|
600 |
for (NodeIt i(g); i!=INVALID; ++i)
|
hegyi@13
|
601 |
{
|
hegyi@13
|
602 |
if(nodesmap[i]==active_item)
|
hegyi@13
|
603 |
{
|
hegyi@13
|
604 |
active_node=i;
|
hegyi@13
|
605 |
}
|
hegyi@13
|
606 |
}
|
hegyi@13
|
607 |
if(active_node==INVALID)
|
hegyi@13
|
608 |
{
|
hegyi@13
|
609 |
for (EdgeIt i(g); i!=INVALID; ++i)
|
hegyi@13
|
610 |
{
|
hegyi@13
|
611 |
if(edgesmap[i]==active_item)
|
hegyi@13
|
612 |
{
|
hegyi@13
|
613 |
active_edge=i;
|
hegyi@13
|
614 |
}
|
hegyi@13
|
615 |
}
|
hegyi@13
|
616 |
}
|
hegyi@13
|
617 |
*active_item << Gnome::Canvas::Properties::fill_color("red");
|
hegyi@13
|
618 |
break;
|
hegyi@13
|
619 |
|
hegyi@13
|
620 |
case GDK_BUTTON_RELEASE:
|
hegyi@13
|
621 |
if(active_item==(get_item_at(e->button.x, e->button.y)))
|
hegyi@13
|
622 |
{
|
hegyi@13
|
623 |
if(active_node!=INVALID)
|
hegyi@13
|
624 |
{
|
hegyi@14
|
625 |
|
hegyi@16
|
626 |
//collecting edges to delete
|
hegyi@16
|
627 |
EdgeIt e;
|
hegyi@16
|
628 |
std::set<Graph::Edge> edges_to_delete;
|
hegyi@16
|
629 |
|
hegyi@16
|
630 |
g.firstOut(e,active_node);
|
hegyi@16
|
631 |
for(;e!=INVALID;g.nextOut(e))
|
hegyi@13
|
632 |
{
|
hegyi@16
|
633 |
edges_to_delete.insert(e);
|
hegyi@16
|
634 |
}
|
hegyi@16
|
635 |
|
hegyi@16
|
636 |
g.firstIn(e,active_node);
|
hegyi@16
|
637 |
for(;e!=INVALID;g.nextIn(e))
|
hegyi@16
|
638 |
{
|
hegyi@16
|
639 |
edges_to_delete.insert(e);
|
hegyi@16
|
640 |
}
|
hegyi@16
|
641 |
|
hegyi@16
|
642 |
//deleting collected edges
|
hegyi@16
|
643 |
for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
|
hegyi@16
|
644 |
{
|
hegyi@16
|
645 |
delete_item(*edge_set_it);
|
hegyi@13
|
646 |
}
|
hegyi@14
|
647 |
delete_item(active_node);
|
hegyi@14
|
648 |
}
|
hegyi@16
|
649 |
//a simple edge was chosen
|
hegyi@14
|
650 |
else
|
hegyi@14
|
651 |
{
|
hegyi@14
|
652 |
delete_item(active_edge);
|
hegyi@14
|
653 |
}
|
hegyi@13
|
654 |
|
hegyi@13
|
655 |
|
hegyi@13
|
656 |
}
|
hegyi@16
|
657 |
//pointer was moved, deletion is cancelled
|
hegyi@13
|
658 |
else
|
hegyi@13
|
659 |
{
|
hegyi@13
|
660 |
if(active_node!=INVALID)
|
hegyi@13
|
661 |
{
|
hegyi@13
|
662 |
*active_item << Gnome::Canvas::Properties::fill_color("blue");
|
hegyi@13
|
663 |
}
|
hegyi@13
|
664 |
else
|
hegyi@13
|
665 |
{
|
hegyi@13
|
666 |
*active_item << Gnome::Canvas::Properties::fill_color("green");
|
hegyi@13
|
667 |
}
|
hegyi@13
|
668 |
}
|
hegyi@16
|
669 |
//reseting datas
|
hegyi@13
|
670 |
active_item=NULL;
|
hegyi@13
|
671 |
active_edge=INVALID;
|
hegyi@13
|
672 |
active_node=INVALID;
|
hegyi@13
|
673 |
break;
|
hegyi@13
|
674 |
|
hegyi@13
|
675 |
case GDK_MOTION_NOTIFY:
|
hegyi@13
|
676 |
break;
|
hegyi@13
|
677 |
|
hegyi@13
|
678 |
default:
|
hegyi@13
|
679 |
break;
|
hegyi@13
|
680 |
}
|
hegyi@13
|
681 |
return true;
|
hegyi@13
|
682 |
}
|
hegyi@13
|
683 |
|
hegyi@14
|
684 |
void GraphDisplayerCanvas::delete_item(NodeIt node_to_delete)
|
hegyi@13
|
685 |
{
|
hegyi@14
|
686 |
delete(nodesmap[node_to_delete]);
|
hegyi@14
|
687 |
g.erase(node_to_delete);
|
hegyi@13
|
688 |
}
|
hegyi@13
|
689 |
|
hegyi@14
|
690 |
void GraphDisplayerCanvas::delete_item(EdgeIt edge_to_delete)
|
hegyi@14
|
691 |
{
|
hegyi@14
|
692 |
delete(edgesmap[edge_to_delete]);
|
hegyi@14
|
693 |
g.erase(edge_to_delete);
|
hegyi@14
|
694 |
}
|
hegyi@14
|
695 |
|
hegyi@16
|
696 |
void GraphDisplayerCanvas::delete_item(Graph::Edge edge_to_delete)
|
hegyi@16
|
697 |
{
|
hegyi@16
|
698 |
delete(edgesmap[edge_to_delete]);
|
hegyi@16
|
699 |
g.erase(edge_to_delete);
|
hegyi@16
|
700 |
}
|
hegyi@16
|
701 |
|